on c19392f help sign in
elk/src/Parse/Repl.hs
101 lines 2.0 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.ByteString.UTF8 as Utf8
11import qualified Data.Text as Text
12import Data.Text (Text)
14import Parse.Primitives
15import qualified Parse.Primitives.Keyword as Keyword
16import qualified Parse.Primitives.Symbol as Symbol
17import qualified Parse.Primitives.Variable as Var
18import qualified Parse.Pattern as Pattern
20import qualified AST.Pattern as P
21import qualified Reporting.Annotation as A
25-- ENTRY
28data Entry
29 = Import String Text
30 | Type String Text
31 | Def (Maybe String) Text
32 | Other Text
33 | Annotation
34 | Port
38-- PARSE
41parseEntry :: String -> Entry
42parseEntry rawEntry =
43 let
44 source =
45 Text.pack rawEntry
46 in
47 case run (entryParser source) (Utf8.fromString rawEntry) of
48 Right entry ->
49 entry
51 Left _ ->
52 Other source
55entryParser :: Text -> Parser Entry
56entryParser source =
57 oneOf
58 [ do Keyword.import_
59 spaces
60 name <- Var.moduleName
61 return (Import (Text.unpack name) source)
63 , do Keyword.port_
64 return Port
66 , do Keyword.type_
67 spaces
68 oneOf
69 [ do Keyword.alias_
70 spaces
71 , return ()
72 ]
73 name <- Var.upper
74 return (Type (Text.unpack name) source)
76 , do root <- Pattern.term
77 spaces
78 case A.drop root of
79 P.RVar name ->
80 oneOf
81 [ do Symbol.hasType
82 return Annotation
83 , do chompArgs
84 return (Def (Just (Text.unpack name)) source)
85 ]
87 _ ->
88 do chompArgs
89 return (Def Nothing source)
90 ]
93chompArgs :: Parser ()
94chompArgs =
95 oneOf
96 [ do Pattern.term
97 spaces
98 chompArgs
99 , do Symbol.equals
100 return ()
101 ]