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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
|
/*-----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation. All Rights Reserved.
//
//-----------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------
// Dafny
// Rustan Leino, first created 25 January 2008
//--------------------------------------------------------------------------*/
using System.Collections.Generic;
using System.Numerics;
using Microsoft.Boogie;
using System.IO;
using System.Text;
COMPILER Dafny
/*--------------------------------------------------------------------------*/
static List<ModuleDecl!>! theModules = new List<ModuleDecl!>();
static Expression! dummyExpr = new LiteralExpr(Token.NoToken);
static FrameExpression! dummyFrameExpr = new FrameExpression(dummyExpr, null);
static Statement! dummyStmt = new ReturnStmt(Token.NoToken);
static Attributes.Argument! dummyAttrArg = new Attributes.Argument("dummyAttrArg");
static Scope<string>! parseVarScope = new Scope<string>();
static int anonymousIds = 0;
struct MemberModifiers {
public bool IsGhost;
public bool IsStatic;
public bool IsUnlimited;
}
// helper routine for parsing call statements
private static void RecordCallLhs(IdentifierExpr! e,
List<IdentifierExpr!>! lhs,
List<AutoVarDecl!>! newVars) {
int index = lhs.Count;
lhs.Add(e);
if (parseVarScope.Find(e.Name) == null) {
AutoVarDecl d = new AutoVarDecl(e.tok, e.Name, new InferredTypeProxy(), index);
newVars.Add(d);
parseVarScope.Push(e.Name, e.Name);
}
}
// helper routine for parsing call statements
private static Expression! ConvertToLocal(Expression! e)
{
FieldSelectExpr fse = e as FieldSelectExpr;
if (fse != null && fse.Obj is ImplicitThisExpr) {
return new IdentifierExpr(fse.tok, fse.FieldName);
}
return e; // cannot convert to IdentifierExpr (or is already an IdentifierExpr)
}
///<summary>
/// Parses top-level things (modules, classes, datatypes, class members) from "filename"
/// and appends them in appropriate form to "modules".
/// Returns the number of parsing errors encountered.
/// Note: first initialize the Scanner.
///</summary>
public static int Parse (string! filename, List<ModuleDecl!>! modules) /* throws System.IO.IOException */ {
string s;
if (filename == "stdin.dfy") {
s = Microsoft.Boogie.ParserHelper.Fill(System.Console.In, new List<string!>());
return Parse(s, filename, modules);
} else {
using (System.IO.StreamReader reader = new System.IO.StreamReader(filename)) {
s = Microsoft.Boogie.ParserHelper.Fill(reader, new List<string!>());
return Parse(s, filename, modules);
}
}
}
///<summary>
/// Parses top-level things (modules, classes, datatypes, class members)
/// and appends them in appropriate form to "modules".
/// Returns the number of parsing errors encountered.
/// Note: first initialize the Scanner.
///</summary>
public static int Parse (string! s, string! filename, List<ModuleDecl!>! modules) {
List<ModuleDecl!> oldModules = theModules;
theModules = modules;
byte[]! buffer = (!) UTF8Encoding.Default.GetBytes(s);
MemoryStream ms = new MemoryStream(buffer,false);
Errors errors = new Errors();
Scanner scanner = new Scanner(ms, errors, filename);
Parser parser = new Parser(scanner, errors);
parser.Parse();
theModules = oldModules;
return parser.errors.count;
}
/*--------------------------------------------------------------------------*/
CHARACTERS
letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".
digit = "0123456789".
special = "'_?\\".
glyph = "`~!@#$%^&*()-_=+[{]}|;:',<.>/?\\".
cr = '\r'.
lf = '\n'.
tab = '\t'.
space = ' '.
quote = '"'.
nondigit = letter + special.
nonquote = letter + digit + space + glyph.
/*------------------------------------------------------------------------*/
TOKENS
ident = nondigit {nondigit | digit}.
digits = digit {digit}.
string = quote {nonquote} quote.
COMMENTS FROM "/*" TO "*/" NESTED
COMMENTS FROM "//" TO lf
IGNORE cr + lf + tab
/*------------------------------------------------------------------------*/
PRODUCTIONS
Dafny
= (. ClassDecl! c; DatatypeDecl! dt;
Attributes attrs; IToken! id; List<string!> theImports;
List<MemberDecl!> membersDefaultClass = new List<MemberDecl!>();
ModuleDecl module;
// to support multiple files, create a default module only if theModules doesn't already contain one
DefaultModuleDecl defaultModule = null;
foreach (ModuleDecl mdecl in theModules) {
defaultModule = mdecl as DefaultModuleDecl;
if (defaultModule != null) { break; }
}
bool defaultModuleCreatedHere = false;
if (defaultModule == null) {
defaultModuleCreatedHere = true;
defaultModule = new DefaultModuleDecl();
}
.)
{ "module" (. attrs = null; theImports = new List<string!>(); .)
{ Attribute<ref attrs> }
Ident<out id>
[ "imports" Idents<theImports> ] (. module = new ModuleDecl(id, id.val, theImports, attrs); .)
"{"
{ ClassDecl<module, out c> (. module.TopLevelDecls.Add(c); .)
| DatatypeDecl<module, out dt> (. module.TopLevelDecls.Add(dt); .)
} (. theModules.Add(module); .)
"}"
| ClassDecl<defaultModule, out c> (. defaultModule.TopLevelDecls.Add(c); .)
| DatatypeDecl<defaultModule, out dt> (. defaultModule.TopLevelDecls.Add(dt); .)
| ClassMemberDecl<membersDefaultClass>
}
(. if (defaultModuleCreatedHere) {
defaultModule.TopLevelDecls.Add(new DefaultClassDecl(defaultModule, membersDefaultClass));
theModules.Add(defaultModule);
} else {
// find the default class in the default module, then append membersDefaultClass to its member list
foreach (TopLevelDecl topleveldecl in defaultModule.TopLevelDecls) {
DefaultClassDecl defaultClass = topleveldecl as DefaultClassDecl;
if (defaultClass != null) {
defaultClass.Members.AddRange(membersDefaultClass);
break;
}
}
}
.)
EOF
.
ClassDecl<ModuleDecl! module, out ClassDecl! c>
= (. IToken! id;
Attributes attrs = null;
List<TypeParameter!> typeArgs = new List<TypeParameter!>();
IToken! idRefined;
IToken optionalId = null;
List<MemberDecl!> members = new List<MemberDecl!>();
.)
"class"
{ Attribute<ref attrs> }
Ident<out id>
[ GenericParameters<typeArgs> ]
[ "refines" Ident<out idRefined> (. optionalId = idRefined; .)
]
"{"
{ ClassMemberDecl<members>
}
"}"
(. if (optionalId == null)
c = new ClassDecl(id, id.val, module, typeArgs, members, attrs);
else
c = new ClassRefinementDecl(id, id.val, module, typeArgs, members, attrs, optionalId);
.)
.
ClassMemberDecl<.List<MemberDecl!>! mm.>
= (. Method! m;
Function! f;
MemberModifiers mmod = new MemberModifiers();
.)
{ "ghost" (. mmod.IsGhost = true; .)
| "static" (. mmod.IsStatic = true; .)
| "unlimited" (. mmod.IsUnlimited = true; .)
}
( FieldDecl<mmod, mm>
| FunctionDecl<mmod, out f> (. mm.Add(f); .)
| MethodDecl<mmod, out m> (. mm.Add(m); .)
| CouplingInvDecl<mmod, mm>
)
.
DatatypeDecl<ModuleDecl! module, out DatatypeDecl! dt>
= (. IToken! id;
Attributes attrs = null;
List<TypeParameter!> typeArgs = new List<TypeParameter!>();
List<DatatypeCtor!> ctors = new List<DatatypeCtor!>();
.)
"datatype"
{ Attribute<ref attrs> }
Ident<out id>
[ GenericParameters<typeArgs> ]
"{"
{ DatatypeMemberDecl<ctors>
}
"}" (. dt = new DatatypeDecl(id, id.val, module, typeArgs, ctors, attrs); .)
.
DatatypeMemberDecl<.List<DatatypeCtor!>! ctors.>
= (. Attributes attrs = null;
IToken! id;
List<TypeParameter!> typeArgs = new List<TypeParameter!>();
List<Formal!> formals = new List<Formal!>();
.)
{ Attribute<ref attrs> }
Ident<out id>
[ GenericParameters<typeArgs> ]
(. parseVarScope.PushMarker(); .)
[ FormalsOptionalIds<formals> ]
(. parseVarScope.PopMarker();
ctors.Add(new DatatypeCtor(id, id.val, typeArgs, formals, attrs));
.)
";"
.
FieldDecl<.MemberModifiers mmod, List<MemberDecl!>! mm.>
= (. Attributes attrs = null;
IToken! id; Type! ty;
.)
"var"
(. if (mmod.IsUnlimited) { SemErr(t, "fields cannot be declared 'unlimited'"); }
if (mmod.IsStatic) { SemErr(t, "fields cannot be declared 'static'"); }
.)
{ Attribute<ref attrs> }
IdentType<out id, out ty> (. mm.Add(new Field(id, id.val, mmod.IsGhost, ty, attrs)); .)
{ "," IdentType<out id, out ty> (. mm.Add(new Field(id, id.val, mmod.IsGhost, ty, attrs)); .)
}
";"
.
CouplingInvDecl<.MemberModifiers mmod, List<MemberDecl!>! mm.>
= (. Attributes attrs = null;
List<IToken!> ids = new List<IToken!>();;
IToken! id;
Expression! e;
parseVarScope.PushMarker();
.)
"replaces"
(. if (mmod.IsUnlimited) { SemErr(t, "coupling invariants cannot be declared 'unlimited'"); }
if (mmod.IsStatic) { SemErr(t, "coupling invariants cannot be declared 'static'"); }
if (mmod.IsGhost) { SemErr(t, "coupling invariants cannot be declared 'ghost'"); }
.)
{ Attribute<ref attrs> }
Ident<out id> (. ids.Add(id); parseVarScope.Push(id.val, id.val); .)
{ "," Ident<out id> (. ids.Add(id); parseVarScope.Push(id.val, id.val); .)
}
"by"
Expression<out e>
";"
(. mm.Add(new CouplingInvariant(ids, e, attrs));
parseVarScope.PopMarker();
.)
.
GIdentType<bool allowGhost, out IToken! id, out Type! ty, out bool isGhost>
/* isGhost always returns as false if allowGhost is false */
= (. isGhost = false; .)
[ "ghost" (. if (allowGhost) { isGhost = true; } else { SemErr(t, "formal cannot be declared 'ghost' in this context"); } .)
]
IdentType<out id, out ty>
.
IdentType<out IToken! id, out Type! ty>
= Ident<out id>
":"
Type<out ty>
.
IdentTypeOptional<out BoundVar! var>
= (. IToken! id; Type! ty; Type optType = null;
.)
Ident<out id>
[ ":" Type<out ty> (. optType = ty; .)
]
(. var = new BoundVar(id, id.val, optType == null ? new InferredTypeProxy() : optType); .)
.
TypeIdentOptional<out IToken! id, out string! identName, out Type! ty, out bool isGhost>
= (. string name = null; isGhost = false; .)
[ "ghost" (. isGhost = true; .)
]
TypeAndToken<out id, out ty>
[ ":"
(. /* try to convert ty to an identifier */
UserDefinedType udt = ty as UserDefinedType;
if (udt != null && udt.TypeArgs.Count == 0) {
name = udt.Name;
} else {
SemErr(id, "invalid formal-parameter name in datatype constructor");
}
.)
Type<out ty>
]
(. if (name != null) {
identName = name;
} else {
identName = "#" + anonymousIds++;
}
.)
.
/*------------------------------------------------------------------------*/
GenericParameters<.List<TypeParameter!>! typeArgs.>
= (. IToken! id; .)
"<"
Ident<out id> (. typeArgs.Add(new TypeParameter(id, id.val)); .)
{ "," Ident<out id> (. typeArgs.Add(new TypeParameter(id, id.val)); .)
}
">"
.
/*------------------------------------------------------------------------*/
MethodDecl<MemberModifiers mmod, out Method! m>
= (. IToken! id;
Attributes attrs = null;
List<TypeParameter!>! typeArgs = new List<TypeParameter!>();
List<Formal!> ins = new List<Formal!>();
List<Formal!> outs = new List<Formal!>();
List<MaybeFreeExpression!> req = new List<MaybeFreeExpression!>();
List<FrameExpression!> mod = new List<FrameExpression!>();
List<MaybeFreeExpression!> ens = new List<MaybeFreeExpression!>();
List<Expression!> dec = new List<Expression!>();
Statement! bb; BlockStmt body = null;
bool isRefinement = false;
.)
( "method"
| "refines" (. isRefinement = true; .)
)
(. if (mmod.IsUnlimited) { SemErr(t, "methods cannot be declared 'unlimited'"); }
.)
{ Attribute<ref attrs> }
Ident<out id>
[ GenericParameters<typeArgs> ]
(. parseVarScope.PushMarker(); .)
Formals<true, true, ins>
[ "returns"
Formals<false, true, outs>
]
( ";" { MethodSpec<req, mod, ens, dec> }
| { MethodSpec<req, mod, ens, dec> } BlockStmt<out bb> (. body = (BlockStmt)bb; .)
)
(. parseVarScope.PopMarker();
if (isRefinement)
m = new MethodRefinement(id, id.val, mmod.IsStatic, mmod.IsGhost, typeArgs, ins, outs, req, mod, ens, dec, body, attrs);
else
m = new Method(id, id.val, mmod.IsStatic, mmod.IsGhost, typeArgs, ins, outs, req, mod, ens, dec, body, attrs);
.)
.
MethodSpec<.List<MaybeFreeExpression!>! req, List<FrameExpression!>! mod, List<MaybeFreeExpression!>! ens,
List<Expression!>! decreases.>
= (. Expression! e; FrameExpression! fe; bool isFree = false;
.)
( "modifies" [ FrameExpression<out fe> (. mod.Add(fe); .)
{ "," FrameExpression<out fe> (. mod.Add(fe); .)
}
] ";"
| [ "free" (. isFree = true; .)
]
( "requires" Expression<out e> ";" (. req.Add(new MaybeFreeExpression(e, isFree)); .)
| "ensures" Expression<out e> ";" (. ens.Add(new MaybeFreeExpression(e, isFree)); .)
)
| "decreases" Expressions<decreases> ";"
)
.
Formals<.bool incoming, bool allowGhosts, List<Formal!>! formals.>
= (. IToken! id; Type! ty; bool isGhost; .)
"("
[
GIdentType<allowGhosts, out id, out ty, out isGhost> (. formals.Add(new Formal(id, id.val, ty, incoming, isGhost)); parseVarScope.Push(id.val, id.val); .)
{ "," GIdentType<allowGhosts, out id, out ty, out isGhost> (. formals.Add(new Formal(id, id.val, ty, incoming, isGhost)); parseVarScope.Push(id.val, id.val); .)
}
]
")"
.
FormalsOptionalIds<.List<Formal!>! formals.>
= (. IToken! id; Type! ty; string! name; bool isGhost; .)
"("
[
TypeIdentOptional<out id, out name, out ty, out isGhost> (. formals.Add(new Formal(id, name, ty, true, isGhost)); parseVarScope.Push(name, name); .)
{ "," TypeIdentOptional<out id, out name, out ty, out isGhost> (. formals.Add(new Formal(id, name, ty, true, isGhost)); parseVarScope.Push(name, name); .)
}
]
")"
.
/*------------------------------------------------------------------------*/
Type<out Type! ty>
= (. IToken! tok; .)
TypeAndToken<out tok, out ty>
.
TypeAndToken<out IToken! tok, out Type! ty>
= (. tok = Token.NoToken; ty = new BoolType(); /*keep compiler happy*/
List<Type!>! gt;
.)
( "bool" (. tok = t; .)
| "int" (. tok = t; ty = new IntType(); .)
| "set" (. tok = t; gt = new List<Type!>(); .)
GenericInstantiation<gt> (. if (gt.Count != 1) {
SemErr("set type expects exactly one type argument");
}
ty = new SetType(gt[0]);
.)
| "seq" (. tok = t; gt = new List<Type!>(); .)
GenericInstantiation<gt> (. if (gt.Count != 1) {
SemErr("seq type expects exactly one type argument");
}
ty = new SeqType(gt[0]);
.)
| ReferenceType<out tok, out ty>
)
.
ReferenceType<out IToken! tok, out Type! ty>
= (. tok = Token.NoToken; ty = new BoolType(); /*keep compiler happy*/
List<Type!>! gt;
.)
( "object" (. tok = t; ty = new ObjectType(); .)
| "array" (. tok = t; gt = new List<Type!>(); .)
GenericInstantiation<gt> (. if (gt.Count != 1) {
SemErr("array type expects exactly one type argument");
}
ty = UserDefinedType.ArrayType(tok, gt[0]);
.)
| Ident<out tok> (. gt = new List<Type!>(); .)
[ GenericInstantiation<gt> ] (. ty = new UserDefinedType(tok, tok.val, gt); .)
)
.
GenericInstantiation<.List<Type!>! gt.>
= (. Type! ty; .)
"<"
Type<out ty> (. gt.Add(ty); .)
{ "," Type<out ty> (. gt.Add(ty); .)
}
">"
.
/*------------------------------------------------------------------------*/
FunctionDecl<MemberModifiers mmod, out Function! f>
= (. Attributes attrs = null;
IToken! id;
List<TypeParameter!> typeArgs = new List<TypeParameter!>();
List<Formal!> formals = new List<Formal!>();
Type! returnType;
List<Expression!> reqs = new List<Expression!>();
List<FrameExpression!> reads = new List<FrameExpression!>();
List<Expression!> decreases = new List<Expression!>();
Expression! bb; Expression body = null;
bool isFunctionMethod = false;
.)
"function"
[ "method" (. isFunctionMethod = true; .)
]
(. if (mmod.IsGhost) { SemErr(t, "functions cannot be declared 'ghost' (they are ghost by default)"); }
.)
{ Attribute<ref attrs> }
Ident<out id>
[ GenericParameters<typeArgs> ]
(. parseVarScope.PushMarker(); .)
Formals<true, false, formals>
":"
Type<out returnType>
( ";"
{ FunctionSpec<reqs, reads, decreases> }
| { FunctionSpec<reqs, reads, decreases> }
FunctionBody<out bb> (. body = bb; .)
)
(. parseVarScope.PopMarker();
f = new Function(id, id.val, mmod.IsStatic, !isFunctionMethod, mmod.IsUnlimited, typeArgs, formals, returnType, reqs, reads, decreases, body, attrs);
.)
.
FunctionSpec<.List<Expression!>! reqs, List<FrameExpression!>! reads, List<Expression!>! decreases.>
= (. Expression! e; FrameExpression! fe; .)
( "requires" Expression<out e> ";" (. reqs.Add(e); .)
| "reads" [ PossiblyWildFrameExpression<out fe> (. reads.Add(fe); .)
{ "," PossiblyWildFrameExpression<out fe> (. reads.Add(fe); .)
}
] ";"
| "decreases" Expressions<decreases> ";"
)
.
PossiblyWildExpression<out Expression! e>
= (. e = dummyExpr; .)
/* A decreases clause on a loop asks that no termination check be performed.
* Use of this feature is sound only with respect to partial correctness.
*/
( "*" (. e = new WildcardExpr(t); .)
| Expression<out e>
)
.
PossiblyWildFrameExpression<out FrameExpression! fe>
= (. fe = dummyFrameExpr; .)
/* A reads clause can list a wildcard, which allows the enclosing function to
* read anything. In many cases, and in particular in all cases where
* the function is defined recursively, this makes it next to impossible to make
* any use of the function. Nevertheless, as an experimental feature, the
* language allows it (and it is sound).
*/
( "*" (. fe = new FrameExpression(new WildcardExpr(t), null); .)
| FrameExpression<out fe>
)
.
FrameExpression<out FrameExpression! fe>
= (. Expression! e; IToken! id; string fieldName = null; .)
Expression<out e>
[ "`" Ident<out id> (. fieldName = id.val; .)
]
(. fe = new FrameExpression(e, fieldName); .)
.
FunctionBody<out Expression! e>
= (. e = dummyExpr; .)
"{"
( MatchExpression<out e>
| Expression<out e>
)
"}"
.
MatchExpression<out Expression! e>
= (. IToken! x; MatchCaseExpr! c;
List<MatchCaseExpr!> cases = new List<MatchCaseExpr!>();
.)
"match" (. x = t; .)
Expression<out e>
{ CaseExpression<out c> (. cases.Add(c); .)
}
(. e = new MatchExpr(x, e, cases); .)
.
CaseExpression<out MatchCaseExpr! c>
= (. IToken! x, id, arg;
List<BoundVar!> arguments = new List<BoundVar!>();
Expression! body;
.)
"case" (. x = t; parseVarScope.PushMarker(); .)
Ident<out id>
[ "("
Ident<out arg> (. arguments.Add(new BoundVar(arg, arg.val, new InferredTypeProxy()));
parseVarScope.Push(arg.val, arg.val); .)
{ "," Ident<out arg> (. arguments.Add(new BoundVar(arg, arg.val, new InferredTypeProxy()));
parseVarScope.Push(arg.val, arg.val); .)
}
")" ]
"=>"
Expression<out body> (. c = new MatchCaseExpr(x, id.val, arguments, body);
parseVarScope.PopMarker(); .)
.
/*------------------------------------------------------------------------*/
BlockStmt<out Statement! block>
= (. IToken! x;
List<Statement!> body = new List<Statement!>();
Statement! s;
.)
(. parseVarScope.PushMarker(); .)
"{" (. x = t; .)
{ Stmt<body>
}
"}" (. block = new BlockStmt(x, body); .)
(. parseVarScope.PopMarker(); .)
.
Stmt<.List<Statement!>! ss.>
= (. Statement! s; .)
/* By first reading a sequence of block statements, we avoid problems in the generated parser, despite
the ambiguity in the grammar. See Note in ConstAtomExpression production.
*/
{ BlockStmt<out s> (. ss.Add(s); .)
}
( OneStmt<out s> (. ss.Add(s); .)
| VarDeclStmts<ss>
)
.
OneStmt<out Statement! s>
= (. IToken! x; IToken! id; string label = null;
s = dummyStmt; /* to please the compiler */
.)
/* This list does not contain BlockStmt, see comment above in Stmt production. */
( AssertStmt<out s>
| AssumeStmt<out s>
| UseStmt<out s>
| PrintStmt<out s>
| AssignStmt<out s>
| HavocStmt<out s>
| CallStmt<out s>
| IfStmt<out s>
| WhileStmt<out s>
| MatchStmt<out s>
| ForeachStmt<out s>
| "label" (. x = t; .)
Ident<out id> ":" (. s = new LabelStmt(x, id.val); .)
| "break" (. x = t; .)
[ Ident<out id> (. label = id.val; .)
] ";" (. s = new BreakStmt(x, label); .)
| "return" (. x = t; .)
";" (. s = new ReturnStmt(x); .)
)
.
AssignStmt<out Statement! s>
= (. IToken! x;
Expression! lhs;
Expression rhs;
Type ty;
s = dummyStmt;
.)
LhsExpr<out lhs>
":=" (. x = t; .)
AssignRhs<out rhs, out ty> (. if (ty == null) {
assert rhs != null;
s = new AssignStmt(x, lhs, rhs);
} else if (rhs == null) {
s = new AssignStmt(x, lhs, ty);
} else {
s = new AssignStmt(x, lhs, ty, rhs);
}
.)
";"
.
AssignRhs<out Expression e, out Type ty>
/* ensures e != null || ty != null; */
= (. IToken! x; Expression! ee; Type! tt;
e = null; ty = null;
.)
( "new" TypeAndToken<out x, out tt> (. ty = tt; .)
[ "[" Expression<out ee> "]" (. e = ee; .)
]
| Expression<out ee> (. e = ee; .)
) (. if (e == null && ty == null) { e = dummyExpr; } .)
.
HavocStmt<out Statement! s>
= (. IToken! x; Expression! lhs; .)
"havoc" (. x = t; .)
LhsExpr<out lhs> ";" (. s = new AssignStmt(x, lhs); .)
.
LhsExpr<out Expression! e>
= SelectExpression<out e>
.
VarDeclStmts<.List<Statement!>! ss.>
= (. VarDecl! d; bool isGhost = false; .)
[ "ghost" (. isGhost = true; .)
]
"var"
IdentTypeRhs<out d, isGhost> (. ss.Add(d); parseVarScope.Push(d.Name, d.Name); .)
{ "," IdentTypeRhs<out d, isGhost> (. ss.Add(d); parseVarScope.Push(d.Name, d.Name); .)
}
";"
.
IdentTypeRhs<out VarDecl! d, bool isGhost>
= (. IToken! id; Type! ty; Expression! e;
Expression rhs = null; Type newType = null;
Type optionalType = null; DeterminedAssignmentRhs optionalRhs = null;
.)
Ident<out id>
[ ":" Type<out ty> (. optionalType = ty; .)
]
[ ":="
AssignRhs<out rhs, out newType>
]
(. if (newType == null && rhs != null) {
optionalRhs = new ExprRhs(rhs);
} else if (newType != null) {
if (rhs == null) {
optionalRhs = new TypeRhs(newType);
} else {
optionalRhs = new TypeRhs(newType, rhs);
}
} else if (optionalType == null) {
optionalType = new InferredTypeProxy();
}
d = new VarDecl(id, id.val, optionalType, isGhost, optionalRhs);
.)
.
IfStmt<out Statement! ifStmt>
= (. IToken! x;
Expression guard;
Statement! thn;
Statement! s;
Statement els = null;
.)
"if" (. x = t; .)
Guard<out guard>
BlockStmt<out thn>
[ "else"
( IfStmt<out s> (. els = s; .)
| BlockStmt<out s> (. els = s; .)
)
]
(. ifStmt = new IfStmt(x, guard, thn, els); .)
.
WhileStmt<out Statement! stmt>
= (. IToken! x;
Expression guard;
bool isFree; Expression! e;
List<MaybeFreeExpression!> invariants = new List<MaybeFreeExpression!>();
List<Expression!> decreases = new List<Expression!>();
Statement! body;
.)
"while" (. x = t; .)
Guard<out guard> (. assume guard == null || Owner.None(guard); .)
{ (. isFree = false; .)
[ "free" (. isFree = true; .)
]
"invariant"
Expression<out e> (. invariants.Add(new MaybeFreeExpression(e, isFree)); .)
";"
| "decreases"
PossiblyWildExpression<out e> (. decreases.Add(e); .)
{ "," PossiblyWildExpression<out e> (. decreases.Add(e); .)
}
";"
}
BlockStmt<out body> (. stmt = new WhileStmt(x, guard, invariants, decreases, body); .)
.
Guard<out Expression e> /* null represents demonic-choice */
= (. Expression! ee; e = null; .)
"("
( "*" (. e = null; .)
| Expression<out ee> (. e = ee; .)
)
")"
.
MatchStmt<out Statement! s>
= (. Token x; Expression! e; MatchCaseStmt! c;
List<MatchCaseStmt!> cases = new List<MatchCaseStmt!>(); .)
"match" (. x = t; .)
Expression<out e>
"{"
{ CaseStatement<out c> (. cases.Add(c); .)
}
"}"
(. s = new MatchStmt(x, e, cases); .)
.
CaseStatement<out MatchCaseStmt! c>
= (. IToken! x, id, arg;
List<BoundVar!> arguments = new List<BoundVar!>();
List<Statement!> body = new List<Statement!>();
.)
"case" (. x = t; parseVarScope.PushMarker(); .)
Ident<out id>
[ "("
Ident<out arg> (. arguments.Add(new BoundVar(arg, arg.val, new InferredTypeProxy()));
parseVarScope.Push(arg.val, arg.val); .)
{ "," Ident<out arg> (. arguments.Add(new BoundVar(arg, arg.val, new InferredTypeProxy()));
parseVarScope.Push(arg.val, arg.val); .)
}
")" ]
"=>"
(. parseVarScope.PushMarker(); .)
{ Stmt<body> }
(. parseVarScope.PopMarker(); .)
(. c = new MatchCaseStmt(x, id.val, arguments, body); .)
(. parseVarScope.PopMarker(); .)
.
CallStmt<out Statement! s>
= (. IToken! x, id;
Expression! e;
List<IdentifierExpr!> lhs = new List<IdentifierExpr!>();
List<AutoVarDecl!> newVars = new List<AutoVarDecl!>();
.)
"call" (. x = t; .)
CallStmtSubExpr<out e>
[ "," /* call a,b,c,... := ... */
(. e = ConvertToLocal(e);
if (e is IdentifierExpr) {
RecordCallLhs((IdentifierExpr)e, lhs, newVars);
} else if (e is FieldSelectExpr) {
SemErr(e.tok, "each LHS of call statement must be a variable, not a field");
} else {
SemErr(e.tok, "each LHS of call statement must be a variable");
}
.)
Ident<out id> (. RecordCallLhs(new IdentifierExpr(id, id.val), lhs, newVars); .)
{ "," Ident<out id> (. RecordCallLhs(new IdentifierExpr(id, id.val), lhs, newVars); .)
}
":="
CallStmtSubExpr<out e>
| ":=" /* call a := ... */
(. e = ConvertToLocal(e);
if (e is IdentifierExpr) {
RecordCallLhs((IdentifierExpr)e, lhs, newVars);
} else if (e is FieldSelectExpr) {
SemErr(e.tok, "each LHS of call statement must be a variable, not a field");
} else {
SemErr(e.tok, "each LHS of call statement must be a variable");
}
.)
CallStmtSubExpr<out e>
]
";"
/* "e" has now been parsed as one of: IdentifierExpr, FunctionCallExpr, FieldSelectExpr.
It denotes the RHS, so to be legal it must be a FunctionCallExpr. */
(. if (e is FunctionCallExpr) {
FunctionCallExpr fce = (FunctionCallExpr)e;
s = new CallStmt(x, newVars, lhs, fce.Receiver, fce.Name, fce.Args); // this actually does an ownership transfer of fce.Args
} else {
SemErr("RHS of call statement must denote a method invocation");
s = new CallStmt(x, newVars, lhs, dummyExpr, "dummyMethodName", new List<Expression!>());
}
.)
.
/*------------------------------------------------------------------------*/
ForeachStmt<out Statement! s>
= (. IToken! x, boundVar;
Type! ty;
Expression! collection;
Expression! range;
List<PredicateStmt!> bodyPrefix = new List<PredicateStmt!>();
AssignStmt bodyAssign = null;
.)
(. parseVarScope.PushMarker(); .)
"foreach" (. x = t;
range = new LiteralExpr(x, true);
ty = new InferredTypeProxy();
.)
"(" Ident<out boundVar>
[ ":" Type<out ty> ]
"in" Expression<out collection>
(. parseVarScope.Push(boundVar.val, boundVar.val); .)
[ "|" Expression<out range> ]
")"
"{"
{ AssertStmt<out s> (. if (s is PredicateStmt) { bodyPrefix.Add((PredicateStmt)s); } .)
| AssumeStmt<out s> (. if (s is PredicateStmt) { bodyPrefix.Add((PredicateStmt)s); } .)
| UseStmt<out s> (. if (s is PredicateStmt) { bodyPrefix.Add((PredicateStmt)s); } .)
}
( AssignStmt<out s> (. if (s is AssignStmt) { bodyAssign = (AssignStmt)s; } .)
| HavocStmt<out s> (. if (s is AssignStmt) { bodyAssign = (AssignStmt)s; } .)
)
"}" (. if (bodyAssign != null) {
s = new ForeachStmt(x, new BoundVar(boundVar, boundVar.val, ty), collection, range, bodyPrefix, bodyAssign);
} else {
s = dummyStmt; // some error occurred in parsing the bodyAssign
}
.)
(. parseVarScope.PopMarker(); .)
.
AssertStmt<out Statement! s>
= (. IToken! x; Expression! e; .)
"assert" (. x = t; .)
Expression<out e> ";" (. s = new AssertStmt(x, e); .)
.
AssumeStmt<out Statement! s>
= (. IToken! x; Expression! e; .)
"assume" (. x = t; .)
Expression<out e> ";" (. s = new AssumeStmt(x, e); .)
.
UseStmt<out Statement! s>
= (. IToken! x; Expression! e; .)
"use" (. x = t; .)
Expression<out e> ";" (. s = new UseStmt(x, e); .)
.
PrintStmt<out Statement! s>
= (. IToken! x; Attributes.Argument! arg;
List<Attributes.Argument!> args = new List<Attributes.Argument!>();
.)
"print" (. x = t; .)
AttributeArg<out arg> (. args.Add(arg); .)
{ "," AttributeArg<out arg> (. args.Add(arg); .)
}
";" (. s = new PrintStmt(x, args); .)
.
/*------------------------------------------------------------------------*/
Expression<out Expression! e>
= (. IToken! x; Expression! e0; Expression! e1 = dummyExpr;
e = dummyExpr;
.)
( "if" (. x = t; .)
Expression<out e>
"then" Expression<out e0>
"else" Expression<out e1> (. e = new ITEExpr(x, e, e0, e1); .)
| EquivExpression<out e>
)
.
/*------------------------------------------------------------------------*/
EquivExpression<out Expression! e0>
= (. IToken! x; Expression! e1; .)
ImpliesExpression<out e0>
{ EquivOp (. x = t; .)
ImpliesExpression<out e1> (. e0 = new BinaryExpr(x, BinaryExpr.Opcode.Iff, e0, e1); .)
}
.
EquivOp = "<==>" | '\u21d4'.
/*------------------------------------------------------------------------*/
ImpliesExpression<out Expression! e0>
= (. IToken! x; Expression! e1; .)
LogicalExpression<out e0>
[ ImpliesOp (. x = t; .)
ImpliesExpression<out e1> (. e0 = new BinaryExpr(x, BinaryExpr.Opcode.Imp, e0, e1); .)
]
.
ImpliesOp = "==>" | '\u21d2'.
/*------------------------------------------------------------------------*/
LogicalExpression<out Expression! e0>
= (. IToken! x; Expression! e1; .)
RelationalExpression<out e0>
[ AndOp (. x = t; .)
RelationalExpression<out e1> (. e0 = new BinaryExpr(x, BinaryExpr.Opcode.And, e0, e1); .)
{ AndOp (. x = t; .)
RelationalExpression<out e1> (. e0 = new BinaryExpr(x, BinaryExpr.Opcode.And, e0, e1); .)
}
| OrOp (. x = t; .)
RelationalExpression<out e1> (. e0 = new BinaryExpr(x, BinaryExpr.Opcode.Or, e0, e1); .)
{ OrOp (. x = t; .)
RelationalExpression<out e1> (. e0 = new BinaryExpr(x, BinaryExpr.Opcode.Or, e0, e1); .)
}
]
.
AndOp = "&&" | '\u2227'.
OrOp = "||" | '\u2228'.
/*------------------------------------------------------------------------*/
RelationalExpression<out Expression! e0>
= (. IToken! x; Expression! e1; BinaryExpr.Opcode op; .)
Term<out e0>
[ RelOp<out x, out op>
Term<out e1> (. e0 = new BinaryExpr(x, op, e0, e1); .)
]
.
RelOp<out IToken! x, out BinaryExpr.Opcode op>
= (. x = Token.NoToken; op = BinaryExpr.Opcode.Add/*(dummy)*/; .)
( "==" (. x = t; op = BinaryExpr.Opcode.Eq; .)
| "<" (. x = t; op = BinaryExpr.Opcode.Lt; .)
| ">" (. x = t; op = BinaryExpr.Opcode.Gt; .)
| "<=" (. x = t; op = BinaryExpr.Opcode.Le; .)
| ">=" (. x = t; op = BinaryExpr.Opcode.Ge; .)
| "!=" (. x = t; op = BinaryExpr.Opcode.Neq; .)
| "!!" (. x = t; op = BinaryExpr.Opcode.Disjoint; .)
| "in" (. x = t; op = BinaryExpr.Opcode.In; .)
| "!in" (. x = t; op = BinaryExpr.Opcode.NotIn; .)
| '\u2260' (. x = t; op = BinaryExpr.Opcode.Neq; .)
| '\u2264' (. x = t; op = BinaryExpr.Opcode.Le; .)
| '\u2265' (. x = t; op = BinaryExpr.Opcode.Ge; .)
)
.
/*------------------------------------------------------------------------*/
Term<out Expression! e0>
= (. IToken! x; Expression! e1; BinaryExpr.Opcode op; .)
Factor<out e0>
{ AddOp<out x, out op>
Factor<out e1> (. e0 = new BinaryExpr(x, op, e0, e1); .)
}
.
AddOp<out IToken! x, out BinaryExpr.Opcode op>
= (. x = Token.NoToken; op=BinaryExpr.Opcode.Add/*(dummy)*/; .)
( "+" (. x = t; op = BinaryExpr.Opcode.Add; .)
| "-" (. x = t; op = BinaryExpr.Opcode.Sub; .)
)
.
/*------------------------------------------------------------------------*/
Factor<out Expression! e0>
= (. IToken! x; Expression! e1; BinaryExpr.Opcode op; .)
UnaryExpression<out e0>
{ MulOp<out x, out op>
UnaryExpression<out e1> (. e0 = new BinaryExpr(x, op, e0, e1); .)
}
.
MulOp<out IToken! x, out BinaryExpr.Opcode op>
= (. x = Token.NoToken; op = BinaryExpr.Opcode.Add/*(dummy)*/; .)
( "*" (. x = t; op = BinaryExpr.Opcode.Mul; .)
| "/" (. x = t; op = BinaryExpr.Opcode.Div; .)
| "%" (. x = t; op = BinaryExpr.Opcode.Mod; .)
)
.
/*------------------------------------------------------------------------*/
UnaryExpression<out Expression! e>
= (. IToken! x; e = dummyExpr; .)
( "-" (. x = t; .)
UnaryExpression<out e> (. e = new BinaryExpr(x, BinaryExpr.Opcode.Sub, new LiteralExpr(x, 0), e); .)
| NegOp (. x = t; .)
UnaryExpression<out e> (. e = new UnaryExpr(x, UnaryExpr.Opcode.Not, e); .)
| SelectExpression<out e>
| ConstAtomExpression<out e>
)
.
NegOp = "!" | '\u00ac'.
ConstAtomExpression<out Expression! e>
= (. IToken! x, dtName, id; BigInteger n; List<Expression!>! elements;
e = dummyExpr;
.)
( "false" (. e = new LiteralExpr(t, false); .)
| "true" (. e = new LiteralExpr(t, true); .)
| "null" (. e = new LiteralExpr(t); .)
| Nat<out n> (. e = new LiteralExpr(t, n); .)
| "#" (. x = t; .)
Ident<out dtName>
"."
Ident<out id> (. elements = new List<Expression!>(); .)
[ "("
[ Expressions<elements> ]
")" ] (. e = new DatatypeValue(t, dtName.val, id.val, elements); .)
| "fresh" (. x = t; .)
"(" Expression<out e> ")" (. e = new FreshExpr(x, e); .)
| "|" (. x = t; .)
Expression<out e> (. e = new UnaryExpr(x, UnaryExpr.Opcode.SeqLength, e); .)
"|"
| "{" (. x = t; elements = new List<Expression!>(); .)
[ Expressions<elements> ] (. e = new SetDisplayExpr(x, elements); .)
"}"
| "[" (. x = t; elements = new List<Expression!>(); .)
[ Expressions<elements> ] (. e = new SeqDisplayExpr(x, elements); .)
"]"
)
.
/*------------------------------------------------------------------------*/
/* returns one of:
-- IdentifierExpr
-- FunctionCallExpr
-- FieldSelectExpr
*/
CallStmtSubExpr<out Expression! e>
= (. e = dummyExpr; .)
( IdentOrFuncExpression<out e>
| ObjectExpression<out e>
SelectOrCallSuffix<ref e>
)
{ SelectOrCallSuffix<ref e> }
.
SelectExpression<out Expression! e>
= (. IToken! id; e = dummyExpr; .)
( IdentOrFuncExpression<out e>
| ObjectExpression<out e>
)
{ SelectOrCallSuffix<ref e> }
.
IdentOrFuncExpression<out Expression! e>
= (. IToken! id; e = dummyExpr; List<Expression!>! args; .)
Ident<out id>
[ "(" (. args = new List<Expression!>(); .)
[ Expressions<args> ]
")" (. e = new FunctionCallExpr(id, id.val, new ImplicitThisExpr(id), args); .)
] (. if (e == dummyExpr) {
if (parseVarScope.Find(id.val) != null) {
e = new IdentifierExpr(id, id.val);
} else {
e = new FieldSelectExpr(id, new ImplicitThisExpr(id), id.val);
}
}
.)
.
SelectOrCallSuffix<ref Expression! e>
= (. IToken! id, x; List<Expression!>! args;
Expression e0 = null; Expression e1 = null; Expression! ee; bool anyDots = false;
bool func = false;
.)
( "."
Ident<out id>
[ "(" (. args = new List<Expression!>(); func = true; .)
[ Expressions<args> ]
")" (. e = new FunctionCallExpr(id, id.val, e, args); .)
] (. if (!func) { e = new FieldSelectExpr(id, e, id.val); } .)
| "[" (. x = t; .)
( Expression<out ee> (. e0 = ee; .)
[ ".." (. anyDots = true; .)
[ Expression<out ee> (. e1 = ee; .)
]
| ":="
Expression<out ee> (. e1 = ee; .)
]
| ".." Expression<out ee> (. anyDots = true; e1 = ee; .)
) (. if (!anyDots && e0 == null) {
/* a parsing error occurred */
e0 = dummyExpr;
}
assert !anyDots ==> e0 != null;
if (anyDots) {
assert e0 != null || e1 != null;
e = new SeqSelectExpr(x, false, e, e0, e1);
} else if (e1 == null) {
assert e0 != null;
e = new SeqSelectExpr(x, true, e, e0, null);
} else {
assert e0 != null;
e = new SeqUpdateExpr(x, e, e0, e1);
}
.)
"]"
)
.
/* ObjectExpression represents those expressions E that could possibly be used in E.f
or E(...), except Ident. Since the lookahead is just 1, quantifier expressions are also
parsed here. The expression returned is never an lvalue.
*/
ObjectExpression<out Expression! e>
= (. IToken! x; e = dummyExpr; .)
( "this" (. e = new ThisExpr(t); .)
| "old" (. x = t; .)
"("
Expression<out e>
")" (. e = new OldExpr(x, e); .)
| "(" ( QuantifierGuts<out e>
| Expression<out e>
)
")"
)
.
/*------------------------------------------------------------------------*/
QuantifierGuts<out Expression! q>
= (. IToken! x = Token.NoToken;
bool univ = false;
BoundVar! bv;
List<BoundVar!> bvars = new List<BoundVar!>();
IToken! tok; Expr! e; ExprSeq! es;
Attributes attrs = null;
Triggers trigs = null;
Expression! body;
.)
( Forall (. x = t; univ = true; .)
| Exists (. x = t; .)
)
(. parseVarScope.PushMarker(); .)
IdentTypeOptional<out bv> (. bvars.Add(bv); parseVarScope.Push(bv.Name, bv.Name); .)
{ ","
IdentTypeOptional<out bv> (. bvars.Add(bv); parseVarScope.Push(bv.Name, bv.Name); .)
}
{ AttributeOrTrigger<ref attrs, ref trigs> }
QSep
Expression<out body>
(. if (univ) {
q = new ForallExpr(x, bvars, body, trigs, attrs);
} else {
q = new ExistsExpr(x, bvars, body, trigs, attrs);
}
parseVarScope.PopMarker();
.)
.
Forall = "forall" | '\u2200'.
Exists = "exists" | '\u2203'.
QSep = "::" | '\u2022'.
Expressions<.List<Expression!>! args.>
= (. Expression! e; .)
Expression<out e> (. args.Add(e); .)
{ "," Expression<out e> (. args.Add(e); .)
}
.
/*------------------------------------------------------------------------*/
Attribute<ref Attributes attrs>
= "{"
AttributeBody<ref attrs>
"}"
.
AttributeBody<ref Attributes attrs>
= (. string aName;
List<Attributes.Argument!> aArgs = new List<Attributes.Argument!>();
Attributes.Argument! aArg;
.)
":" ident (. aName = t.val; .)
[ AttributeArg<out aArg> (. aArgs.Add(aArg); .)
{ "," AttributeArg<out aArg> (. aArgs.Add(aArg); .)
}
] (. attrs = new Attributes(aName, aArgs, attrs); .)
.
AttributeArg<out Attributes.Argument! arg>
= (. Expression! e; arg = dummyAttrArg; .)
( string (. arg = new Attributes.Argument(t.val.Substring(1, t.val.Length-2)); .)
| Expression<out e> (. arg = new Attributes.Argument(e); .)
)
.
AttributeOrTrigger<ref Attributes attrs, ref Triggers trigs>
= (. List<Expression!> es = new List<Expression!>();
.)
"{"
( AttributeBody<ref attrs>
| (. es = new List<Expression!>(); .)
Expressions<es> (. trigs = new Triggers(es, trigs); .)
)
"}"
.
/*------------------------------------------------------------------------*/
Idents<.List<string!>! ids.>
= (. IToken! id; .)
Ident<out id> (. ids.Add(id.val); .)
{ "," Ident<out id> (. ids.Add(id.val); .)
}
.
Ident<out IToken! x>
=
ident (. x = t; .)
.
Nat<out BigInteger n>
=
digits
(. try {
n = BigInteger.Parse(t.val);
} catch (System.FormatException) {
SemErr("incorrectly formatted number");
n = BigInteger.Zero;
}
.)
.
END Dafny.
|