-
Notifications
You must be signed in to change notification settings - Fork 4
/
Parser.fs
751 lines (627 loc) · 23 KB
/
Parser.fs
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
/// <summary>
/// Parser for the Starling language.
/// </summary>
module Starling.Lang.Parser
open System
open FParsec
open Chessie.ErrorHandling
open Starling
open Starling.Collections
open Starling.Core.Symbolic
open Starling.Core.TypeSystem
open Starling.Core.Var
open Starling.Lang.AST
// Manually re-overload some FParsec operators Chessie overloaded.
let (>>=) = FParsec.Primitives.(>>=)
// General TODOs:
// - TODO(CaptainHayashi): remove leftwards uses of ws
// - TODO(CaptainHayashi): create and parse proper syntax definition
// - TODO(CaptainHayashi): make more idiomatic!
// Whitespace.
// TODO(CaptainHayashi): is some of this redundant?
/// Parser for skipping line comments.
let lcom : Parser<unit, unit> = skipString "//" .>> skipRestOfLine true
/// Parser for skipping block comments.
let bcom, bcomImpl = createParserForwardedToRef()
do bcomImpl := skipString "/*" .>> skipManyTill (bcom <|> skipAnyChar) (skipString "*/")
/// Parser for skipping comments.
let com : Parser<unit, unit> = (lcom <|> bcom) <?> "comment"
/// Parser for skipping zero or more whitespace characters.
let ws : Parser<unit, unit> = skipMany (com <|> spaces1)
/// Parser accepting whitespace followed by a semicolon.
let wsSemi : Parser<unit, unit> = ws .>> pstring ";"
/// As pipe2, but with automatic whitespace parsing after each parser.
let pipe2ws x y f = pipe2 (x .>> ws) (y .>> ws) f
/// As pipe3, but with automatic whitespace parsing after each parser.
let pipe3ws x y z f = pipe3 (x .>> ws) (y .>> ws) (z .>> ws) f
/// Parses an identifier.
let parseIdentifier =
(many1Chars2
(pchar '_' <|> asciiLetter)
(pchar '_' <|> asciiLetter <|> digit)) <?> "identifier"
// Bracket parsers.
/// <summary>
/// Parser for items in a pair of matching brackets.
/// Automatically parses any whitespace after `bra`, and before `ket`.
/// </summary>
let inBrackets bra ket = between (pstring bra .>> ws) (ws >>. pstring ket)
/// <summary>
/// Parser for items in <c>(parentheses)</c>.
/// </summary>
let inParens p = inBrackets "(" ")" p
/// <summary>
/// Parser for items in <c>[brackets]</c>.
/// </summary>
let inSquareBrackets p = inBrackets "[" "]" p
/// <summary>
/// Parser for items in <c>{braces}</c>.
/// </summary>
let inBraces p = inBrackets "{" "}" p
/// <summary>
/// Parser for items in <c>{|view braces|}</c>.
/// </summary>
let inViewBraces p = inBrackets "{|" "|}" p
/// <summary>
/// Parser for items in <c>[|interpolate braces|]</c>.
/// </summary>
let inInterpBraces p = inBrackets "[|" "|]" p
/// <summary>
/// Parser for items in <c><|atomic braces|></c>.
/// </summary>
let inAtomicBraces p = inBrackets "<|" "|>" p
(*
* Forwards.
*)
// These parsers are recursively defined, so we must set up
// forwarding stubs to them before we can proceed with the
// definitions.
/// Parser for `raw` views (not surrounded in {braces}).
let parseView, parseViewRef =
createParserForwardedToRef<View, unit> ()
/// Parser for view definitions.
let parseViewSignature, parseViewSignatureRef =
createParserForwardedToRef<ViewSignature, unit> ()
/// Parser for atomic sets.
let parseAtomic, parseAtomicRef =
createParserForwardedToRef<Atomic, unit> ()
/// Parser for commands.
let parseCommand, parseCommandRef =
createParserForwardedToRef<Command, unit> ()
/// Parser for blocks.
let parseBlock, parseBlockRef
= createParserForwardedToRef<Command list, unit> ()
/// Parser for expressions.
/// The expression parser is split into several chains as per
/// preference rank.
let parseExpression, parseExpressionRef =
createParserForwardedToRef<Expression, unit> ()
// From here on out, everything should line up more or less with the
// BNF, except in reverse, bottom-up order.
(*
* Parameters and lists.
*)
/// Parses a comma-delimited parameter list.
/// Each parameter is parsed by argp.
let parseParams argp =
sepBy argp (pstring "," .>> ws)
// ^- {empty}
// | <identifier>
// | <identifier> , <params>
/// Parses a non-empty comma-delimited parameter list.
let parseDefs argp =
sepBy1 argp (pstring "," .>> ws)
/// Parses a comma-delimited, parenthesised parameter list.
let parseParamList argp =
// TODO(CaptainHayashi):
// Make this generic in the first argument to sepBy, and also
// make said first argument more robust -- currently this parses all
// whitespace before the ,!
inParens (parseParams argp)
// ^- ()
// | ( <params> )
(*
* Expressions.
*)
/// Takes a Parser<'a, 'u> and gives back an annotated AST Node parser
/// Parser<Node<'a>, 'u> which will annotate with extra information from
/// the stream.
let nodify v =
getPosition
>>= fun p ->
v |>> fun x -> { Position = { StreamName = p.StreamName; Line = p.Line; Column = p.Column; }
Node = x }
/// <summary>
/// Parser for symbolic sentences.
///
/// <para>
/// Symbolic sentences mix free strings and #-delimited parameter
/// references.
/// </para>
/// </summary>
let parseSymbolicSentence =
many
(choice
[ inInterpBraces parseExpression |>> SymArg <?> "interpolated expression"
many1Chars (noneOf "}[") |>> SymString <?> "symbol body"
// TODO(MattWindsor91): this is a bit crap.
(pchar '[' .>>. noneOf "|"
|>> fun (x, y) -> SymString (System.String.Concat [| x; y |])) ] )
/// <summary>
/// Parser for symbolic expressions.
///
/// <para>
/// Symbolic expressions are of the form
/// <c>%{sentence}(expr1, expr2, ..., exprN)</c>.
/// </para>
/// </summary>
let parseSymbolic =
pstring "%" >>. inBraces (parseSymbolicSentence) <?> "symbolic"
/// Parser for primary expressions.
let parsePrimaryExpression =
let expressions =
(inParens parseExpression) :: List.map nodify [
pstring "true" >>% True
pstring "false" >>% False
pint64 |>> Num
parseIdentifier |>> Identifier
parseSymbolic |>> Symbolic
]
choice expressions .>> ws
/// Generic parser for tiers of binary expressions.
/// Accepts the next precedence level parser, and a list of pairs of operator and AST representation.
/// This generates a LEFT-associative precedence level.
let parseBinaryExpressionLevel nextLevel expList =
let parseBopExpr (ops, op) =
// TODO(CaptainHayashi): can this be solved without backtracking?
nodify (pstring ops)
.>>? ws
(* A binary operator cannot ever be followed by + or -.
This check removes ambiguity between + and ++, and - and --. *)
.>>? notFollowedBy (anyOf "+-")
|>> fun x -> fun a b -> { Node = BopExpr(op, a, b); Position = x.Position }
chainl1 (nextLevel .>> ws)
(choice
(List.map parseBopExpr expList)
)
/// Parser for postfix expressions.
let parsePostfixExpression, parsePostfixExpressionRef = createParserForwardedToRef<Expression, unit> ()
do parsePostfixExpressionRef :=
let parseArraySubscript pex
= nodify ((inSquareBrackets parseExpression <?> "array subscript")
>>= fun a -> preturn (ArraySubscript (pex, a)))
<|> preturn pex
parsePrimaryExpression .>> ws >>= parseArraySubscript
/// Parser for unary expressions
/// TODO(CaptainHayashi): this is a bit hacky, could unify postfix / unary expr?
let parseUnaryExpression =
parsePostfixExpression
<|>
nodify (skipString "!" >>. ws >>. parsePostfixExpression |>> (fun x -> UopExpr (Neg, x )))
/// Parser for multiplicative expressions.
let parseMultiplicativeExpression =
parseBinaryExpressionLevel parseUnaryExpression
[ ("*", Mul)
("/", Div)
("%", Mod) ]
/// Parser for additive expressions.
let parseAdditiveExpression =
parseBinaryExpressionLevel parseMultiplicativeExpression
[ ("+", Add)
("-", Sub) ]
/// Parser for relational expressions.
let parseRelationalExpression =
parseBinaryExpressionLevel parseAdditiveExpression
[ (">=", Ge)
("<=", Le)
(">" , Gt)
("<" , Lt) ]
/// Parser for equality expressions.
let parseEqualityExpression =
parseBinaryExpressionLevel parseRelationalExpression
[ ("==", Eq)
("!=", Neq) ]
/// Parser for logical IMPL expressions.
let parseImplExpression =
parseBinaryExpressionLevel parseEqualityExpression
[ ("=>", Imp) ]
/// Parser for logical AND expressions.
let parseAndExpression =
parseBinaryExpressionLevel parseImplExpression
[ ("&&", And) ]
/// Parser for logical OR expressions.
let parseOrExpression =
parseBinaryExpressionLevel parseAndExpression
[ ("||", Or) ]
do parseExpressionRef := parseOrExpression <?> "expression"
(*
* Atomic actions.
*)
/// Parser for compare-and-swaps.
/// This parser DOES NOT parse whitespace afterwards.
let parseCAS =
pstring "CAS"
>>. inParens (pipe3ws (parseExpression .>> ws .>> pstring ",")
(parseExpression .>> ws .>> pstring ",")
parseExpression
(curry3 CompareAndSwap))
/// Parser for fetch sigils.
let parseFetchSigil =
choice [ pstring "++" >>% Increment
pstring "--" >>% Decrement ] <|>% Direct
/// Parser for fetch right-hand-sides.
let parseFetch fetcher =
pipe2ws parseExpression
parseFetchSigil
(fun fetchee sigil -> Fetch (fetcher, fetchee, sigil))
/// Parser for fetch actions.
let parseFetchOrPostfix =
parseExpression
.>> ws
.>>. parseFetchSigil
>>= function
| (x, Direct) -> ws >>. pstring "=" >>. ws >>. parseFetch x
| p -> Postfix p |> preturn
/// Parser for assume actions.
let parseAssume =
pstring "assume" >>. ws >>. inParens parseExpression |>> Assume
/// Parser for local assignments.
let parseAssign =
pipe2ws parseExpression
// ^- <lvalue> ...
(pstring "=" >>. ws >>. parseExpression)
// ... = <expression> ;
mkPair
/// Parser for havoc actions.
let parseHavoc =
skipString "havoc" >>. ws >>. parseIdentifier
/// Parser for if (expr) block else block.
let parseIfLike pLeg ctor =
pipe3ws (pstring "if" >>. ws >>. inParens parseExpression)
(inBraces pLeg)
(opt (pstring "else" >>. ws >>. inBraces pLeg))
ctor
/// Parser for atomic actions.
do parseAtomicRef :=
choice [ (stringReturn "id" Id)
// These two need to fire before parseFetchOrPostfix due to
// ambiguity.
parseIfLike (many1 (parseAtomic .>> ws)) (curry3 ACond)
parseSymbolic .>> wsSemi |>> SymAtomic
parseHavoc .>> wsSemi |>> Havoc
parseAssume .>> wsSemi
parseCAS .>> wsSemi
parseFetchOrPostfix .>> wsSemi ]
|> nodify
/// Parser for a collection of atomic actions.
let parseAtomicSet =
inAtomicBraces (many1 (parseAtomic .>> ws))
/// Parses a Func given the argument parser argp.
let parseFunc argp =
pipe2ws parseIdentifier (parseParamList argp) (fun f xs -> {Name = f; Params = xs})
(*
* View-likes (views and view definitions).
*)
/// Parses a view-like thing, with the given basic parser and
/// joining constructor.
let parseViewLike basic join =
chainl1 basic
// ^- <basic-view>
// | <basic-view> ...
(stringReturn "*" (curry join) .>> ws)
// ... * <view>
(*
* Types.
*)
/// Parses a builtin primitive type.
let parseBuiltinPrimType : Parser<TypeLiteral, unit> =
choice [
stringReturn "int" TInt
stringReturn "bool" TBool
]
/// Parses a type identifier.
let parseType : Parser<TypeLiteral, unit> =
let parsePrimType =
parseBuiltinPrimType <|> (parseIdentifier |>> TUser)
let parseArray = inSquareBrackets pint32 |>> curry TArray
let parseSuffixes = many parseArray
let rec applySuffixes ty suffixes =
match suffixes with
| [] -> ty
| x::xs -> applySuffixes (x ty) xs
pipe2ws parsePrimType parseSuffixes applySuffixes
/// Parses a parameter.
let parseParam : Parser<Param, unit> =
let buildParam ty id = { ParamType = ty; ParamName = id }
pipe2ws parseType parseIdentifier buildParam
// ^ <type> <identifier>
(*
* Views.
*)
/// Parses a conditional view.
let parseIfView =
// TODO: use parseIflike.
pipe3ws (pstring "if" >>. ws >>. parseExpression)
// ^- if <view-exprn> ...
(pstring "then" >>. ws >>. parseView)
// ^- ... then <view> ...
(pstring "else" >>. ws >>. parseView)
// ^- ... else <view>
(curry3 View.If)
/// Parses a functional view.
let parseFuncView = parseFunc parseExpression |>> View.Func
/// Parses the unit view (`emp`, for our purposes).
let parseUnit = stringReturn "emp" Unit
/// Parses a `basic` view (unit, if, named, or bracketed).
let parseBasicView =
choice [ parseUnit
// ^- `emp'
parseIfView
// ^- if <view-exprn> then <view> else <view>
parseFuncView
// ^- <identifier>
// | <identifier> <arg-list>
inParens parseView ]
// ( <view> )
do parseViewRef := parseViewLike parseBasicView Join
/// Parser for view expressions.
let parseViewExpr =
inViewBraces
((stringReturn "?" Unknown .>> ws)
<|> pipe2ws
parseView
(opt (stringReturn "?" ()))
(fun v qm ->
match qm with
| Some () -> Questioned v
| None -> Unmarked v))
// ^- {| <view> |}
// | {| <view> ? |}
// | {| ? |}
(*
* View definitions.
*)
/// Parses a functional view definition.
let parseStrFuncView = parseFunc parseIdentifier |>> ViewSignature.Func
/// Parses the unit view definition.
let parseDUnit = stringReturn "emp" ViewSignature.Unit
/// Parses a view iterator definition.
let parseIteratorDef = inSquareBrackets parseIdentifier
/// Parses an iterated item, feeding the two results to another function.
let parseIteratedContainer
(parseInner : Parser<'Inner, unit>)
(comb : string -> 'Inner -> 'Outer)
: Parser<'Outer, unit> =
pipe2ws
(pstring "iter" >>. ws >>. parseIteratorDef)
(parseInner)
comb
/// Parses an iterated view definition.
let parseDIterated =
parseIteratedContainer
(parseFunc parseIdentifier)
(fun e f -> ViewSignature.Iterated(f, e))
/// Parses a `basic` view definition (unit, if, named, or bracketed).
let parseBasicViewSignature =
choice [ parseDUnit
// ^- `emp'
parseStrFuncView
// ^- <identifier>
// | <identifier> <arg-list>
inParens parseViewSignature ]
// ( <view> )
do parseViewSignatureRef := parseViewLike parseBasicViewSignature ViewSignature.Join
(*
* View prototypes.
*)
/// Parses a view prototype (a LHS followed optionally by an iterator).
let parseViewProto =
// TODO (CaptainHayashi): so much backtracking...
(pstring "iter" >>. ws >>.
(parseFunc parseParam
|>> (fun lhs -> WithIterator lhs)))
<|>
(parseFunc parseParam
|>> (fun lhs -> NoIterator (lhs, false)))
/// Parses a set of one or more view prototypes.
let parseViewProtoSet =
pstring "view" >>. ws >>. parseDefs parseViewProto .>> wsSemi
(*
* Commands.
*)
/// Parser for blocks.
let parseParSet =
(sepBy1 (parseBlock .>> ws) (pstring "||" .>> ws)) |>> Blocks
// ^- <block>
// | <block> || <par-set>
/// Parser for the 'while (expr)' leg of while and do-while commands.
let parseWhileLeg =
pstring "while" >>. ws >>. inParens parseExpression
/// Parser for while (expr) block.
let parseWhile =
parseWhileLeg .>> ws .>>. parseBlock |>> While
/// Parser for do (expr) while block.
let parseDoWhile =
pstring "do" >>. ws
>>. parseBlock
.>>. (ws >>. parseWhileLeg .>> wsSemi)
|>> DoWhile
/// Parser for lists of semicolon-terminated commands.
let parseCommands = many (parseCommand .>> ws)
/// Parser for if (expr) block else block.
let parseIf = parseIfLike parseCommands (curry3 If)
/// Parser for prim compositions.
let parsePrimSet =
(* Possible configurations:
1) At least one non-atomic followed by, optionally, an atomic and
zero or more non-atomics;
2) An atomic, followed by zero or more non-atomics.
2 is easier to spot, so we try it first. *)
let parseAtomicFirstPrimSet =
pipe2
(parseAtomicSet .>> ws)
(many (attempt (parseAssign .>> wsSemi .>> ws)))
(fun atom rassigns ->
Prim { PreAssigns = []; Atomics = atom; PostAssigns = rassigns } )
let parseNonAtomicFirstPrimSet =
pipe2
(many1 (parseAssign .>> wsSemi .>> ws))
(opt
(parseAtomicSet .>> ws
.>>. many (parseAssign .>> wsSemi .>> ws)))
(fun lassigns tail ->
let (atom, rassigns) = withDefault ([], []) tail
Prim
( { PreAssigns = lassigns
Atomics = atom
PostAssigns = rassigns } ))
parseAtomicFirstPrimSet <|> parseNonAtomicFirstPrimSet
/// Parser for `skip` commands.
/// Skip is inserted when we're in command position, but see a semicolon.
let parseSkip
= stringReturn ";" (Prim { PreAssigns = []
Atomics = []
PostAssigns = [] })
// ^- ;
/// Parser for simple commands (atomics, skips, and bracketed commands).
do parseCommandRef :=
nodify <|
(choice [parseSkip
// ^- ;
parseViewExpr |>> ViewExpr
// ^ {| ... |}
parseIf
// ^- if ( <expression> ) <block> <block>
parseDoWhile
// ^- do <block> while ( <expression> )
parseWhile
// ^- while ( <expression> ) <block>
parseParSet
// ^- <par-set>
parsePrimSet ])
// ^- <prim-set>
(*
* Blocks.
*)
do parseBlockRef := inBraces parseCommands
(*
* Top-level definitions.
*)
/// Parses a constraint right-hand side.
let parseConstraintRhs : Parser<Expression option, unit> =
choice [
(stringReturn "?" None)
(parseExpression |>> Some) ]
// ^ ?
// ^ %{ <symbol> %}
// ^ <expression>
/// Parses a constraint.
let parseConstraint : Parser<ViewSignature * Expression option, unit> =
pstring "constraint" >>. ws
// ^- constraint ..
>>. pipe3ws
(parseDIterated <|> parseViewSignature)
// ^- <view> ...
(pstring "->")
parseConstraintRhs
(fun d _ v -> (d, v))
.>> pstring ";"
/// parse an exclusivity constraint
let parseExclusive : Parser<List<StrFunc>, unit> =
pstring "exclusive" >>. ws
// ^- exclusive ..
>>. parseDefs (parseFunc parseIdentifier)
.>> wsSemi
/// parse a disjointness constraint
let parseDisjoint : Parser<List<StrFunc>, unit> =
pstring "disjoint" >>. ws
// ^- exclusive ..
>>. parseDefs (parseFunc parseIdentifier)
.>> wsSemi
/// Parses a single method, excluding leading or trailing whitespace.
let parseMethod =
pstring "method" >>. ws >>.
// ^- method ...
pipe2ws (parseFunc parseParam)
// ^- <identifier> <arg-list> ...
parseBlock
// ^- ... <block>
(fun s b -> {Signature = s ; Body = b} )
/// Parses a variable declaration with the given initial keyword and AST type.
let parseVarDecl kw (atype : VarDecl -> ScriptItem') =
let parseList = parseParams parseIdentifier .>> wsSemi
let buildVarDecl t vs = atype { VarType = t; VarNames = vs }
pstring kw >>. ws >>. pipe2ws parseType parseList buildVarDecl
/// Parses a search directive.
let parseSearch =
pstring "search" >>. ws
// ^- search
>>. pint32
// ^- ... <depth>
.>> wsSemi
/// Parses a typedef.
let parseTypedef =
// TODO(CaptainHayashi): forbid 'typedef bool int'.
// TODO(CaptainHayashi): maybe one day permit 'typedef int[] heap'.
// TODO(CaptainHayashi): maybe one day permit 'typedef typedef1 typedef2'.
skipString "typedef"
>>. ws >>.
pipe2ws
parseBuiltinPrimType
parseIdentifier
(fun ty id -> Typedef (ty, id))
.>> wsSemi
/// Parses a pragma.
let parsePragma =
skipString "pragma"
>>. ws >>.
pipe2ws
parseIdentifier
(inBraces (manyChars (noneOf "}")))
(fun k v -> { Key = k; Value = v })
.>> wsSemi
/// Parses a script of zero or more methods, including leading and trailing whitespace.
let parseScript =
// TODO(CaptainHayashi): parse things that aren't methods:
// axioms definitions, etc
ws >>. manyTill (choice (List.map nodify
[parsePragma |>> Pragma
// ^- pragma <identifier> { ... };
parseMethod |>> Method
// ^- method <identifier> <arg-list> <block>
parseConstraint |>> Constraint
// ^- constraint <view> -> <expression> ;
parseExclusive |>> Exclusive
// ^- exclusive <view>, <view>, ... ;
parseDisjoint |>> Disjoint
// ^- disjoint <view>, <view>, ... ;
parseViewProtoSet |>> ViewProtos
// ^- view <identifier> ;
// | view <identifier> <view-proto-param-list> ;
parseSearch |>> Search
// ^- search 0;
parseTypedef
// ^- typedef int Node;
parseVarDecl "shared" SharedVars
// ^- shared <type> <identifier> ;
parseVarDecl "thread" ThreadVars]) .>> ws ) eof
// ^- thread <type> <identifier> ;
(*
* Frontend
*)
/// Opens the file with the given name, parses it, and returns the AST.
/// The AST is given inside a Chessie result.
let parseFile name =
try
// If - or no name was given, parse from the console.
let stream, streamName =
match name with
| None ->
eprintfn "note: no input filename given, reading from stdin"
(Console.OpenStandardInput (), "(stdin)")
| Some("-") -> (Console.OpenStandardInput (), "(stdin)")
| Some(nam) -> (IO.File.OpenRead(nam) :> IO.Stream, nam)
runParserOnStream parseScript () streamName stream Text.Encoding.UTF8
|> function | Success (result, _, _) -> ok result
| Failure (errorMsg, _, _) -> fail errorMsg
with
| :? System.IO.FileNotFoundException -> fail ("File not found: " + Option.get name)