@@ -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+ ]