forked from slindley/effect-handlers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParseHandlers.hs
187 lines (155 loc) · 3.77 KB
/
ParseHandlers.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
module ParseHandlers where
import Text.ParserCombinators.Parsec
import Data.Char (isSpace)
import ParseCode
-- we could use the applicative interface...
--
-- I'm not convinced that it makes things clearer though
--
--import Control.Applicative ((<*>), (<*), (*>), (<$>), (<$))
{- Handles constraints -}
type HandlesConstraint = (String, [(String, [String])])
data QuantifierKind = Forall | Exists
deriving Show
type OpDef = ([String], String, [String], String)
type HandlerDef = (Maybe (String, [(String, [String])], Maybe String),
String,
[String],
[(String, [String])],
String,
String)
parseHandlesConstraint :: String -> HandlesConstraint
parseHandlesConstraint s =
case parse handlesconstraint "" s of
Right r -> r
handlesconstraint :: GenParser Char a HandlesConstraint
handlesconstraint =
do
spaces
h <- lowerId
spaces
sig <- opSigs
return (h, sig)
{- Handler definitions -}
parseHandlerDef :: String -> HandlerDef
parseHandlerDef s =
case parse handlerdef "" s of
Right r -> r
opSigs :: GenParser Char a [(String, [String])]
opSigs =
do
char '{'
sig <- opSig `sepBy` char ','
char '}'
return sig
handlerSig :: GenParser Char a [(String, [String])]
handlerSig =
do
sig <-
(do
string "handles"
spaces
sig <- opSigs
spaces
return sig)
return sig
opSig :: GenParser Char a (String, [String])
opSig =
do
spaces
name <- upperId
spaces
ts <- typeArgs
spaces
return (name, ts)
handlerdef :: GenParser Char a HandlerDef
handlerdef =
do
spaces
h <- optionMaybe forward
name <- upperId
spaces
ts <- tyVars
string "::"
spaces
r <- result
spaces
sig <- handlerSig
spaces
string "where"
many isSpaceNoNewline
many newline
cs <- clauses
return (h, name, ts, sig, r, cs)
forward =
do
string "forward"
spaces
h <- lowerId
spaces
sig <- option [] handlerSig
char '.'
spaces
c <- optionMaybe handlerConstraint
return (h, sig, c)
handlerConstraint :: GenParser Char a String
handlerConstraint =
do
c <- paren
spaces
string "=>"
spaces
return c
isSpaceNoNewline = satisfy (\c -> isSpace c && c /= '\n' && c /= '\r')
result = manyTill anyChar (try (lookAhead
(do {many1 space; string "handles" <|>
string "where"})))
--result = manyTill anyChar (try (do {spaces; string "where"; many isSpaceNoNewline; many newline}))
clauses = many anyChar
{- Operation definitions -}
parseOpDef :: String -> OpDef
parseOpDef s =
case parse opdef "" s of
Right r -> r
opdef :: GenParser Char a OpDef
opdef =
do
spaces
us <- option [] forall
name <- upperId
spaces
xs <- tyVars
string "::"
spaces
sig <- many anyChar
return (us, name, xs, sig)
forall =
do
string "forall"
spaces
us <- lowerId `sepBy` spaces
spaces
char '.'
spaces
return us
{- Utilities -}
typeArgs :: GenParser Char a [String]
typeArgs = typeArg `sepBy` spaces
typeArg :: GenParser Char a String
typeArg = tyVar <|> paren
tyVars :: GenParser Char a [String]
tyVars = many (do {x <- tyVar; spaces; return x})
tyVar :: GenParser Char a String
tyVar = lowerId
lowerId :: GenParser Char a String
lowerId =
do
c <- lower
cs <- many (alphaNum <|> char '\'')
return (c : cs)
upperId :: GenParser Char a String
upperId =
do
c <- upper
cs <- many (alphaNum <|> char '\'')
return (c : cs)