on 00a3069 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 qualified AST.Source as Src
15import qualified Elm.Name as N
16import Parse.Primitives
17import qualified Parse.Primitives.Keyword as Keyword
18import qualified Parse.Primitives.Symbol as Symbol
19import qualified Parse.Primitives.Variable as Var
20import qualified Parse.Pattern as Pattern
21import qualified Reporting.Annotation as A
25-- ENTRY
28data Entry
29 = Import N.Name Text
30 | Type N.Name Text
31 | Def (Maybe N.Name) 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 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 name source)
76 , do root <- Pattern.term
77 spaces
78 case A.toValue root of
79 Src.PVar name ->
80 oneOf
81 [ do Symbol.hasType
82 return Annotation
83 , do chompArgs
84 return (Def (Just name) source)
85 ]
87 _ ->
88 do Symbol.equals
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 ]