on master help sign in
Expose Elm.Utils.parseEntry for more precise REPL identification
commit3c71d93104c9a331fd9a5a50dcc082585f9639f4 authorEvan Czaplicki <evancz@users.noreply.github.com> dateFri 14 Apr 2017, 16:21 (10 years ago) parent374b9cf
4 files changed+113−26
elm-compiler.cabal+1 src/Elm/Utils.hs+9−25 src/Parse/Expression.hs+1−1 src/Parse/Repl.hs+102
elm-compiler.cabal+1
@@ -114,6 +114,7 @@ Library
114114 Parse.Parse,
115115 Parse.Pattern,
116116 Parse.Primitives,
117+ Parse.Repl,
117118 Parse.Type,
118119 Reporting.Annotation,
119120 Reporting.Bag,
src/Elm/Utils.hs+9−25
@@ -1,17 +1,14 @@
11{-# OPTIONS_GHC -Wall #-}
22module Elm.Utils
3− ( (|>), (<|)
4− , nearbyNames
5− , isDeclaration
6− ) where
7−
8−import Data.Text (Text)
9−
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
3+ ( (|>), (<|)
4+ , nearbyNames
5+ , Entry(..)
6+ , parseEntry
7+ )
8+ where
9+
10+
11+import Parse.Repl (Entry(..), parseEntry)
1512import Reporting.Helpers (nearbyNames)
1613
1714
@@ -36,16 +33,3 @@ f <| x = f x
3633infixr 0 <|
3734infixl 0 |>
3835
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 @@
11{-# OPTIONS_GHC -Wall -fno-warn-unused-do-bind #-}
22{-# LANGUAGE OverloadedStrings #-}
3−module Parse.Expression (term, expression, definition) where
3+module Parse.Expression (term, expression, definition, rootPattern) where
44
55import Control.Monad (guard)
66import 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+ ]