on e00a3f3 help sign in
elk/src/Parse/Repl.hs
102 lines 1.9 kB history raw
1{-# OPTIONS_GHC -Wall -fno-warn-unused-do-bind #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Parse.Repl
4 ( Entry(..)
5 , parseEntry
6 )
7 where
10import qualified Data.Text as Text
11import Data.Text (Text)
13import Parse.Helpers
14import qualified Parse.Expression as Expr
15import qualified Parse.Pattern as Pattern
17import qualified AST.Pattern as P
18import qualified Reporting.Annotation as A
22-- ENTRY
25data Entry
26 = Import String Text
27 | Type String Text
28 | Def (Maybe String) Text
29 | Other Text
30 | Annotation
31 | Port
32 | Infix
36-- PARSE
39parseEntry :: String -> Entry
40parseEntry rawEntry =
41 let
42 source =
43 Text.pack rawEntry
44 in
45 case run (entryParser source) source of
46 Right entry ->
47 entry
49 Left _ ->
50 Other source
53entryParser :: Text -> Parser Entry
54entryParser source =
55 oneOf
56 [ do keyword "import"
57 spaces
58 name <- qualifiedCapVar
59 return (Import (Text.unpack name) source)
61 , do oneOf (map keyword ["infix","infixl","infixr"])
62 return Infix
64 , do keyword "port"
65 return Port
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)
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 ]
88 _ ->
89 do chompArgs
90 return (Def Nothing source)
91 ]
94chompArgs :: Parser ()
95chompArgs =
96 oneOf
97 [ do Pattern.term
98 spaces
99 chompArgs
100 , do equals
101 return ()
102 ]