on master help sign in
Expose Elm.Utils.parseEntry for more precise REPL identification
3c71d93Evan Czaplicki
elm-compiler.cabal+1
@@ -114,6 +114,7 @@ Library
114 Parse.Parse,
114 Parse.Parse,
115 Parse.Pattern,
115 Parse.Pattern,
116 Parse.Primitives,
116 Parse.Primitives,
117+ Parse.Repl,
117 Parse.Type,
118 Parse.Type,
118 Reporting.Annotation,
119 Reporting.Annotation,
119 Reporting.Bag,
120 Reporting.Bag,
src/Elm/Utils.hs+9−25
@@ -1,17 +1,14 @@
1{-# OPTIONS_GHC -Wall #-}
1{-# OPTIONS_GHC -Wall #-}
2module Elm.Utils
2module Elm.Utils
3− ( (|>), (<|)
3+ ( (|>), (<|)
4− , nearbyNames
4+ , nearbyNames
5− , isDeclaration
5+ , Entry(..)
6+ , parseEntry
7+ )
6− ) where
8+ where
7−
9+
8−import Data.Text (Text)
10+
9−
11+import Parse.Repl (Entry(..), parseEntry)
10−import qualified AST.Expression.Source as Source
11−import qualified AST.Pattern as Pattern
12−import qualified Parse.Helpers as Parse
13−import qualified Parse.Expression as Parse
14−import qualified Reporting.Annotation as A
15import Reporting.Helpers (nearbyNames)
12import Reporting.Helpers (nearbyNames)
16
13
17
14
@@ -36,16 +33,3 @@ f <| x = f x
36infixr 0 <|
33infixr 0 <|
37infixl 0 |>
34infixl 0 |>
38
35
39−
40−
41−-- DECL CHECKER
42−
43−
44−isDeclaration :: Text -> Maybe [Text]
45−isDeclaration source =
46− case Parse.run Parse.definition source of
47− Right (A.A _ (Source.Definition pattern _), _, _) ->
48− Just (Pattern.boundVarList pattern)
49−
50− _ ->
51− Nothing
src/Parse/Expression.hs+1−1
@@ -1,6 +1,6 @@
1{-# OPTIONS_GHC -Wall -fno-warn-unused-do-bind #-}
1{-# OPTIONS_GHC -Wall -fno-warn-unused-do-bind #-}
2{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE OverloadedStrings #-}
3−module Parse.Expression (term, expression, definition) where
3+module Parse.Expression (term, expression, definition, rootPattern) where
4
4
5import Control.Monad (guard)
5import Control.Monad (guard)
6import qualified Data.List as List
6import qualified Data.List as List
src/Parse/Repl.hsnew file+102
@@ -0,0 +1,102 @@
1+{-# OPTIONS_GHC -Wall -fno-warn-unused-do-bind #-}
2+{-# LANGUAGE OverloadedStrings #-}
3+module Parse.Repl
4+ ( Entry(..)
5+ , parseEntry
6+ )
7+ where
8+
9+
10+import qualified Data.Text as Text
11+import Data.Text (Text)
12+
13+import Parse.Helpers
14+import qualified Parse.Expression as Expr
15+import qualified Parse.Pattern as Pattern
16+
17+import qualified AST.Pattern as P
18+import qualified Reporting.Annotation as A
19+
20+
21+
22+-- ENTRY
23+
24+
25+data Entry
26+ = Import String Text
27+ | Type String Text
28+ | Def (Maybe String) Text
29+ | Other Text
30+ | Annotation
31+ | Port
32+ | Infix
33+
34+
35+
36+-- PARSE
37+
38+
39+parseEntry :: String -> Entry
40+parseEntry rawEntry =
41+ let
42+ source =
43+ Text.pack rawEntry
44+ in
45+ case run (entryParser source) source of
46+ Right entry ->
47+ entry
48+
49+ Left _ ->
50+ Other source
51+
52+
53+entryParser :: Text -> Parser Entry
54+entryParser source =
55+ oneOf
56+ [ do keyword "import"
57+ spaces
58+ name <- qualifiedCapVar
59+ return (Import (Text.unpack name) source)
60+
61+ , do oneOf (map keyword ["infix","infixl","infixr"])
62+ return Infix
63+
64+ , do keyword "port"
65+ return Port
66+
67+ , do keyword "type"
68+ spaces
69+ oneOf
70+ [ do keyword "alias"
71+ spaces
72+ , return ()
73+ ]
74+ name <- capVar
75+ return (Type (Text.unpack name) source)
76+
77+ , do root <- Expr.rootPattern =<< getPosition
78+ spaces
79+ case A.drop root of
80+ P.Var name ->
81+ oneOf
82+ [ do hasType
83+ return Annotation
84+ , do chompArgs
85+ return (Def (Just (Text.unpack name)) source)
86+ ]
87+
88+ _ ->
89+ do chompArgs
90+ return (Def Nothing source)
91+ ]
92+
93+
94+chompArgs :: Parser ()
95+chompArgs =
96+ oneOf
97+ [ do Pattern.term
98+ spaces
99+ chompArgs
100+ , do equals
101+ return ()
102+ ]