-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathParser.hs
453 lines (407 loc) · 14.1 KB
/
Parser.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
module Parser where
import Text.Parsec
import Math
import Control.Arrow
import Data.Char
import Head
ignore = many thingsToIgnore
thingsToIgnore = theorem <|> moduleInstance <|> extends <|> divisionLine <|> do{oneOf " \n"; return()}
divisionLine = do try $ do {string "--"; many (char '-'); char '\n'; ignore; return()}
variablesDeclaration = do try $ do string "VARIABLE"
optional (char 'S')
ws
vs <- identifier `sepBy` try comma
ignore
return vs
theorem = do try $ do {string "THEOREM"; ws; manyTill anyChar (char '\n'); ignore; return()}
moduleInstance = do try $ do {string "INSTANCE"; ws; identifier; ignore; return()}
extends = do try $ do {string "EXTENDS"; ws; identifier `sepBy` try comma; ignore; return()}
identifier = many1 (oneOf (['a'..'z'] ++ ['A'..'Z'] ++ ['_'] ++ ['0'..'9']))
constant = do try $ do c <- oneOf ['A'..'Z']
cs <- many (oneOf (['a'..'z'] ++ ['A'..'Z'] ++ ['_'] ++ ['0'..'9']))
return (c:cs)
reserved = ",;.(){}\"/\\[]"
comma = do {ws; char ','; ws; return ()}
infOr = do {ws; string "\\/"; ws; return ()}
infAnd = do {ws; string "/\\"; ws; return ()}
parseTla :: FilePath -> IO (Either String (Module, [Definition]))
parseTla a = do f <- readFile a
let e = parse specification "Error:" f
return (left show e)
parseState s = parse action ("Error parsing " ++ s ++ ":") s
parseValue s = parse value ("Error parsing " ++ s ++ ":") s
parseTrace t = fmap resolveReferences (parse trace ("Error parsing " ++ t ++ ":") t)
resolveReferences :: [TraceState] -> [Action]
resolveReferences ts = resolveReferences' ts ts
resolveReferences' :: [TraceState] -> [TraceState] -> [Action]
resolveReferences' _ [] = []
resolveReferences' ts (Act a:ss) = a : resolveReferences' ts ss
resolveReferences' ts (StepRef i:ss) = let a = case ts !! i of
(Act a') -> a'
_ -> error("Reference to reference")
in a: resolveReferences' ts ss
data TraceState = Act Action | StepRef Int
-- trace :: Either ParseError TraceState
trace = do many1 $ traceStep
traceStep = do try $ do string "State "
_ <- many digit
string ": <"
_ <- manyTill anyChar (try (char '>'))
ignore
a <- action
ignore
return (Act a)
<|> do string "Back to state "
digits <- many1 digit
let n = foldl (\x d -> 10 * x + (digitToInt d)) 0 digits
string ": <"
_ <- manyTill anyChar (try (char '>'))
ignore
return (StepRef (n-1))
specification = do (n, d) <- moduleHeader
ws
ds <- many definition
ws
many $ char '='
ignore
many comment
eof
return (Module n d, ds)
moduleHeader = do many (char '-')
string " MODULE "
name <- identifier
ws
divisionLine
documentation <- many comment
ignore
return (name, documentation)
comment = do try $ do string "(*"
c <- anyChar `manyTill` (do try $ string "*)")
ignore
return c
<|>
do try $ do string "\\* "
c <- anyChar `manyTill` char '\n'
ignore
return c
declaration = do try $ do string "CONSTANT"
optional (char 'S')
ws
cs <- constant `sepBy` try comma
ignore
return cs
parameters = identifier `sepBy` comma
call = do try $ do i <- identifier
char '('
ws
ps <- value `sepBy` comma
char ')'
ignore
return (i, ps)
<|>
do try $ do i <- identifier
ws
return (i, [])
defSignature = do try $ do i <- identifier
char '('
ws
ps <- parameters
char ')'
ignore
return (i, ps)
<|>
do try $ do i <- identifier
ws
return (i, [])
definition = do {Comment <$> comment;}
<|>
do try $ do {Constants <$> declaration;}
<|>
do try $ do {Variables <$> variablesDeclaration;}
<|>
do try $ do (i, ps) <- defSignature
ws
string "=="
ws
ValueDefinition i ps <$> value
<|>
do try $ do (i, ps) <- defSignature
string "=="
ws
doc <- many comment
a <- action
ignore
return (ActionDefinition i ps doc a)
orAction = do string "\\/"
ws
a <- action
ws
return a
andAction = do string "/\\"
ws
a <- action
ws
return a
action = do try $ do string "IF"
ws
p <- predicate
string "THEN"
ws
at <- action
string "ELSE"
ws
af <- action
return (ActionIf p at af)
<|>
do {Condition <$> predicate;}
<|>
do {p <- primed; char '='; ws; Primed p <$> value;}
<|>
do string "UNCHANGED"
ws
string "<<"
vs <- identifier `sepBy` comma
string ">>"
ws
return (Unchanged vs)
<|>
do {(i, ps) <- call; return (ActionCall i ps)}
<|>
do try $ do {ws; as <- many1 andAction; return (ActionAnd as)}
<|>
do try $ do {ws; as <- many1 orAction; return (ActionOr as)}
<|>
do try $ do string "\\E"
ws
i <- identifier
ws
string "\\in"
ws
v <- value
char ':'
ws
as <- action `sepBy` infOr
return (Exists i v (ActionOr as))
<|>
do try $ do string "\\A"
ws
i <- identifier
ws
string "\\in"
ws
v <- value
char ':'
ws
a <- action
return (ForAll i v a)
predicate = do try $ do char '('
ws
ps <- atomPredicate `sepBy` infAnd
char ')'
ws
return (And ps)
<|>
do try $ do char '('
ws
ps <- atomPredicate `sepBy` infOr
char ')'
ws
return (Or ps)
<|> atomPredicate
atomPredicate = do try $ do v1 <- value
char '='
ws
Equality v1 <$> value
<|>
do try $ do v1 <- value
char '#'
ws
Inequality v1 <$> value
<|>
do try $ do v1 <- value
char '>'
ws
Gt v1 <$> value
<|>
do try $ do v1 <- value
char '<'
ws
Lt v1 <$> value
<|>
do try $ do v1 <- value
string ">="
ws
Gte v1 <$> value
<|>
do try $ do v1 <- value
string "<="
ws
Lte v1 <$> value
<|>
do try $ do v1 <- value
string "\\in"
ws
RecordBelonging v1 <$> value
<|>
do try $ do v1 <- value
string "\\notin"
ws
v2 <- value
RecordNotBelonging v1 <$> value
<|>
do try $ do string "\\A"
ws
(i, v, p) <- inFilter
return (PForAll i v p)
predicateConditionCall = do try $ do i <- identifier
return (ConditionCall i [])
inFilter = do i <- identifier
ws
string "\\in"
ws
v <- value
char ':'
ws
p <- predicate
return (i, v, p)
mapping = do try $ do ws
i <- identifier
ws
string "\\in"
ws
a <- value
ws
string "|->"
ws
v <- value
ws
return (All i a, v)
<|>
do ws
i <- identifier
ws
string "|->"
ws
v <- value
ws
return (Key (Str i), v)
primed = do try $ do i <- identifier
char '\''
ws
return i
value = do try $ do string "Cardinality("
ws
s <- set
char ')'
ws
return (Cardinality s)
<|>
do {l <- lit; return (Lit l)}
<|>
do {caseStatement;}
<|>
do try $ do {n1 <- Math.number; string ".."; n2 <- Math.number; ws; return (Range n1 n2)}
<|>
do {record;}
<|>
do try $ do {i <- identifier; char '['; k <- identifier; char ']'; ws; return (Index (Ref i) (Ref k))}
<|>
do try $ do {i <- identifier; char '['; k <- literal; char ']'; ws; return (Index (Ref i) k)}
<|>
do {set;}
<|>
do try $ do {n1 <- Math.number; string ".."; n2 <- Math.number; ws; return (Range n1 n2)}
<|>
do try $ do {e <- arithmeticExpression; ws; return (e)}
set = do try $ do {s1 <- atomSet; string "\\cup"; ws; s2 <- set; ws; return (Union s1 s2)}
<|>
atomSet
atomSet = do try $ do char '{'
ws
vs <- value `sepBy` try comma
char '}'
ws
return (Set vs)
<|>
do try $ do char '{'
(i, v, p) <- inFilter
char '}'
ws
return (Filtered i v p)
<|>
do {e <- arithmeticExpression; ws; return (e)}
caseMatch = do try $ do string "OTHER"
ws
string "->"
ws
v <- value
ws
return (DefaultMatch v)
<|>
do p <- try $ predicate <|> predicateConditionCall
ws
string "->"
ws
v <- value
ws
return (Match p v)
caseStatement = do try $ do string "CASE"
ws
cs <- caseMatch `sepBy` try (do {ws; string "[]"; ws})
ws
return (Case cs)
record = do try $ do char '['
ms <- mapping `sepBy` try comma
char ']'
ws
return (Record ms)
<|>
do try $ do string "SetAsFun({"
ws
ts <- tuple `sepBy` try comma
ws
string "})"
ws
return (Record (map (\(Tuple [k, v]) -> (Key k, Lit v)) ts))
<|>
do try $ do char '('
ms <- recEntry `sepBy` try (string "@@ ")
char ')'
ws
return (Record ms)
<|>
do char '['
i <- identifier
ws
string "EXCEPT"
ws
string "!["
k <- identifier
char ']'
ws
char '='
ws
v <- value
char ']'
ws
return (Except i [(Ref k, v)])
recEntry = do try $ do l <- lit
ws
string ":>"
ws
v <- value
ws
return (Key l, v)
tuple = do try $ do {string "<<"; vs <- lit `sepBy` try comma; string ">>"; ws; return (Tuple vs)}
lit = do try $ do stringLiteral
<|>
do try $ do numberLiteral
<|>
do try $ do {string "TRUE"; return (Boolean True)}
<|>
do try $ do {string "FALSE"; return (Boolean False)}
<|>
tuple
stringLiteral = do try $ do {char '\"'; cs <- many1 (noneOf reserved); char '\"'; ws; return (Str cs)}
literal = do try $ do {s <- stringLiteral; return (Lit s)}
arithmeticExpression = Math.build