-
Notifications
You must be signed in to change notification settings - Fork 27
/
asl.ott
988 lines (838 loc) · 43.2 KB
/
asl.ott
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ASL grammar description
%
% Copyright Arm Limited (c) 2017-2019
% SPDX-Licence-Identifier: BSD-3-Clause
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
metavar id ::= {{ ocaml string }} {{ ocamllex ['a'-'z' 'A'-'Z' '_'] ['a'-'z' 'A'-'Z' '0'-'9' '_']* }}
metavar typeid ::= {{ ocaml string }} {{ ocamllex ['a'-'z' 'A'-'Z' '_'] ['a'-'z' 'A'-'Z' '0'-'9' '_']* }}
metavar qualifier ::= {{ phantom }} {{ ocaml string }} {{ ocamllex-remove }} %{{ ocamllex ['a'-'z' 'A'-'Z' '_'] ['a'-'z' 'A'-'Z' '0'-'9' '_']* }}
metavar intLit ::= {{ ocaml string }} {{ ocamllex ['0'-'9']+ }}
metavar bitsLit ::= {{ ocaml string }} {{ ocamllex '\'' ['0' '1' ' ']* '\'' }}
metavar maskLit ::= {{ ocaml string }} {{ ocamllex '\'' ['0' '1' 'x' ' ']* '\'' }}
metavar realLit ::= {{ ocaml string }} {{ ocamllex ['0'-'9']+ '.' ['0'-'9']+ }}
metavar hexLit ::= {{ ocaml string }} {{ ocamllex '0''x'['0'-'9' 'A' - 'F' 'a'-'f' '_']+ }}
metavar stringLit ::= {{ ocaml string }} {{ ocamllex '"' [^'"']* '"' }} {{ phantom }}
metavar eol ::= {{ ocaml unit }} {{ ocamllex-remove }} {{ phantom }}
indexvar i, m, n ::= {{ ocaml int }}
% The following is the OCaml type of source locations.
embed
{{ ocaml
(** Location tracking *)
type l =
| Unknown
| Int of string * l option
| Generated of l
| Range of Lexing.position * Lexing.position
type 'a annot = l * 'a
let pp_lexing_position (p: Lexing.position): string =
Printf.sprintf "file \"%s\" line %d char %d"
p.Lexing.pos_fname p.Lexing.pos_lnum (p.Lexing.pos_cnum - p.Lexing.pos_bol)
let rec pp_loc (l: l): string = match l with
| Unknown -> "no location information available"
| Generated l -> Printf.sprintf "Generated: %s" (pp_loc l)
| Range(p1, p2) ->
if String.equal p1.Lexing.pos_fname p2.Lexing.pos_fname then begin
if p1.Lexing.pos_lnum = p2.Lexing.pos_lnum then
Printf.sprintf "file \"%s\" line %d char %d - %d"
p1.Lexing.pos_fname
p1.Lexing.pos_lnum
(p1.Lexing.pos_cnum - p1.Lexing.pos_bol)
(p2.Lexing.pos_cnum - p2.Lexing.pos_bol)
else
Printf.sprintf "file \"%s\" line %d char %d - line %d char %d"
p1.Lexing.pos_fname
p1.Lexing.pos_lnum
(p1.Lexing.pos_cnum - p1.Lexing.pos_bol)
p2.Lexing.pos_lnum
(p2.Lexing.pos_cnum - p2.Lexing.pos_bol)
end else begin
Printf.sprintf "file \"%s\" line %d char %d - file \"%s\" line %d char %d"
p1.Lexing.pos_fname
p1.Lexing.pos_lnum
(p1.Lexing.pos_cnum - p1.Lexing.pos_bol)
p2.Lexing.pos_fname
p2.Lexing.pos_lnum
(p2.Lexing.pos_cnum - p2.Lexing.pos_bol)
end
| Int(s,lo) -> Printf.sprintf "%s %s" s (match lo with Some l -> pp_loc l | None -> "none")
(** Parsing exceptions (1/2) *)
exception Parse_error_locn of l * string
(** Identifiers used for variable names, function names, etc.
There are two kinds of identifier:
- Ident is generated by the parser - it is just a string
- FIdent is generated by the disambiguation part of the typechecker and
includes a unique label to distinguish different entities with
the same name in the source syntax.
*)
type ident =
| Ident of string
| FIdent of string * int
let pprint_ident (x: ident): string =
(match x with
| Ident(s) -> s
| FIdent(s,t) -> s ^"."^ string_of_int t
)
let addTag (x: ident) (tag: int): ident =
(match x with
| Ident(s) -> FIdent (s, tag)
| FIdent(s,_) -> failwith "addTag"
)
let name_of_FIdent (x: ident): string =
(match x with
| Ident(s) -> failwith "name_of_FIdent"
| FIdent(s,_) -> s
)
let addQualifier (p: string) (x: ident): ident =
(match x with
| Ident(s) -> Ident (p ^ "." ^ s)
| FIdent(s,_) -> failwith "addQualifier"
)
let addPrefix (p: string) (x: ident): ident =
(match x with
| Ident(q) -> Ident (p ^ "." ^ q)
| FIdent(s,_) -> failwith "addQualifier"
)
let addSuffix (x: ident) (s: string): ident =
(match x with
| Ident(p) -> Ident (p ^ "." ^ s)
| FIdent(s,_) -> failwith "addQualifier"
)
let genericTyvar (i: int): ident =
let v = "$" ^ string_of_int i in
Ident v
let isGenericTyvar (x: ident): bool =
(match x with
| Ident(s) -> s.[0] = '$'
| FIdent(s,_) -> failwith "addQualifier"
)
module Id = struct
type t = ident
let compare (x: ident) (y: ident): int =
(match (x, y) with
| (Ident x, Ident y) ->
String.compare x y
| (FIdent (x,i), FIdent (y,j)) ->
let cx = String.compare x y in
if cx <> 0 then cx else compare i j
| _ -> failwith ("Id.compare: " ^ (pprint_ident x) ^" "^ (pprint_ident y))
)
end
(** Type Identifiers *)
module IdentSet = Set.Make(String)
let typeIdents = ref IdentSet.empty
let addTypeIdent (x: ident): unit = begin
(* ignore (Printf.printf "New type identifier %s\n" (pprint_ident x)); *)
typeIdents := IdentSet.add (pprint_ident x) !typeIdents
end
let isTypeIdent (x: string): bool = IdentSet.mem x !typeIdents
}}
grammar
l :: '' ::= {{ phantom }}
{{ ocaml l }}
{{ com Source location }}
{{ pp l = pp_loc l }}
{{ pp-raw l = PPrintEngine.string (pp_loc l) }}
| :: :: Unknown {{ ocaml Unknown }}
ident :: '' ::= {{ phantom }}
{{ ocaml ident }}
{{ com Identifier }}
{{ pp l = PPrintEngine.string (pprint_ident l) }}
{{ pp-raw l = PPrintEngine.string (pprint_ident l) }}
| id :: :: Ident {{ ocaml Ident [[id]] }}
typeident :: '' ::= {{ phantom }}
{{ ocaml ident }}
{{ com Type Identifier }}
{{ pp l = PPrintEngine.string (pprint_ident l) }}
{{ pp-raw l = PPrintEngine.string (pprint_ident l) }}
| typeid :: :: TypeIdent {{ ocaml Ident [[typeid]] }}
grammar
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Declarations
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
leadingblank :: 'Leading' ::=
| EOL :: :: Blank
| :: :: Nothing
declarations :: 'Decls_' ::= {{ menhir-start }}
{{ phantom }} {{ ocaml declaration list }}
{{ pp-raw es = string "[ "^^ separate (string ";\n") (List.map pp_raw_declaration es) ^^ string " ]" }}
{{ pp es = separate (string "\n") (List.map pp_declaration es) }}
| leadingblank declaration1 .. declarationn :: :: Decls
{{ ocaml [[declaration1..declarationn]] }}
declaration :: 'Decl_' ::=
{{ aux _ l }}
| type_declaration :: :: TDecl {{ quotient-remove }} {{ ocaml [[type_declaration]] }}
| variable_declaration :: :: VDecl {{ quotient-remove }} {{ ocaml [[variable_declaration]] }}
| function_declaration :: :: FDecl {{ quotient-remove }} {{ ocaml [[function_declaration]] }}
| procedure_declaration :: :: PDecl {{ quotient-remove }} {{ ocaml [[procedure_declaration]] }}
| getter_declaration :: :: GDecl {{ quotient-remove }} {{ ocaml [[getter_declaration]] }}
| setter_declaration :: :: SDecl {{ quotient-remove }} {{ ocaml [[setter_declaration]] }}
| instruction_definition :: :: IDecl {{ quotient-remove }} {{ ocaml [[instruction_definition]] }}
| internal_definition :: :: ADecl {{ quotient-remove }} {{ ocaml [[internal_definition]] }}
type_declaration :: 'Decl_' ::= {{ quotient-with declaration }}
{{ aux _ l }}
| __builtin type tidentdecl ; EOL :: :: BuiltinType
| type tidentdecl ; EOL :: :: Forward % says there is a type decl elsewhere
| record tidentdecl { field1 ... fieldn } ; EOL :: :: Record
| type tidentdecl is ( field_ns1 , ... , field_nsn ) EOL :: S :: Dep_Record {{ ocaml Decl_Record([[tidentdecl]], [[field_ns1 ... field_nsn]], Range($symbolstartpos,$endpos)) }} {{ com DEPRECATED }}
| type tidentdecl = ty ; EOL :: :: Typedef
| enumeration tidentdecl { ident1 , .. , identn } ; EOL :: :: Enum
field_ns :: 'FieldNS_' ::=
{{ phantom }} {{ ocaml ty * ident }}
{{ pp-raw f = string "(" ^^ pp_raw_ty (fst f) ^^ string ", " ^^ string (snd f) ^^ string ")" }}
{{ pp f = pp_ty (fst f) ^^ string " " ^^ string (snd f) }}
| ty ident :: :: Field {{ ocaml ([[ty]], [[ident]]) }}
field :: 'Field_' ::=
{{ phantom }} {{ ocaml ty * ident }}
{{ pp-raw f = string "(" ^^ pp_raw_ty (fst f) ^^ string ", " ^^ pp_raw_ident (snd f) ^^ string ")" }}
{{ pp f = pp_ty (fst f) ^^ string " " ^^ pp_ident (snd f) }}
| ty ident ; :: :: Field {{ ocaml ([[ty]], [[ident]]) }}
variable_declaration :: 'Decl_' ::= {{ quotient-with declaration }}
{{ aux _ l }}
| ty qualident ; EOL :: :: Var
| constant ty qualident = expr ; EOL :: :: Const
| array ty qualident [ ixtype ] ; EOL :: S :: Array {{ ocaml Decl_Var(Type_Array([[ixtype]],[[ty]]), [[qualident]], Range($symbolstartpos,$endpos)) }}
ixtype :: 'Index_' ::=
| tident :: :: Enum
| expr1 '..' expr2 :: :: Range
function_declaration :: 'Decl_' ::= {{ quotient-with declaration }}
{{ aux _ l }}
| __builtin ty qualident ( formal1 , .. , formaln ) ; EOL :: :: BuiltinFunction
| ty qualident ( formal1 , .. , formaln ) ; EOL :: :: FunType
| ty qualident ( formal1 , .. , formaln ) opt_indented_block :: :: FunDefn
procedure_declaration :: 'Decl_' ::= {{ quotient-with declaration }}
{{ aux _ l }}
| qualident ( formal1 , .. , formaln ) ; EOL :: :: ProcType
| qualident ( formal1 , .. , formaln ) opt_indented_block :: :: ProcDefn
formal :: 'Formal_' ::=
{{ phantom }} {{ ocaml ty * ident }}
{{ pp-raw f = string "(" ^^ pp_raw_ty (fst f) ^^ string ", " ^^ pp_raw_ident (snd f) ^^ string ")" }}
{{ pp f = pp_ty (fst f) ^^ string " " ^^ pp_ident (snd f) }}
| ty ident :: :: In2 {{ ocaml ([[ty]], [[ident]]) }}
getter_declaration :: 'Decl_' ::= {{ quotient-with declaration }}
{{ aux _ l }}
% Note: VarGetterType is an unofficial extension because ASL has no official way
% to declare a getter because syntax would be same as declaring a variable
| __function ty qualident ; EOL :: :: VarGetterType
| ty qualident opt_indented_block :: :: VarGetterDefn
| ty qualident [ formal1 , .. , formaln ] ; EOL :: :: ArrayGetterType
| ty qualident [ formal1 , .. , formaln ] opt_indented_block :: :: ArrayGetterDefn
setter_declaration :: 'Decl_' ::= {{ quotient-with declaration }}
{{ aux _ l }}
| qualident = ty ident ; EOL :: :: VarSetterType
| qualident = ty ident opt_indented_block :: :: VarSetterDefn
| qualident [ sformal1 , .. , sformaln ] = ty ident ; EOL :: :: ArraySetterType
| qualident [ sformal1 , .. , sformaln ] = ty ident opt_indented_block :: :: ArraySetterDefn
sformal :: 'Formal_' ::=
| ty ident :: :: In
| ty & ident :: :: InOut
instruction_definition :: 'Decl_' ::= {{ quotient-with declaration }}
{{ aux _ l }}
| __instruction ident EOL INDENT
encoding1 ... encodingn
opt_postdecode
__execute opt_conditional opt_indented_block
DEDENT
:: :: InstructionDefn
| __decode ident EOL INDENT
decode_case
DEDENT
:: :: DecoderDefn
encoding :: 'Encoding_' ::=
{{ aux _ l }}
| __encoding ident1 EOL INDENT
__instruction_set ident2 EOL
instr_field1 .. instr_fieldm
__opcode opcode_value EOL
__guard expr EOL
instr_unpred1 .. instr_unpredn
__decode opt_indented_block
DEDENT
:: :: Block
opt_conditional :: 'OptConditional_' ::=
{{ phantom }} {{ ocaml bool }}
{{ pp-raw x = if x then string "__conditional" else string "" }}
{{ pp x = if x then string "__conditional" else string "" }}
| __conditional :: :: True {{ ocaml true }}
| :: :: False {{ ocaml false }}
opt_postdecode :: 'OptPostDecode_' ::=
{{ phantom }} {{ ocaml (stmt list) option }}
{{ pp-raw x = match x with Some(ys) -> string "Some(" ^^ separate (string "\n") (List.map pp_raw_stmt ys) ^^ string ")" | None -> string "None" }}
{{ pp x = match x with Some(ys) -> string "postdecode" ^^ hardline
^^ (nest 4 (separate (string "\n") (List.map pp_stmt ys)))
| None -> string "" }}
| __postdecode indented_block :: :: Some {{ ocaml Some([[indented_block]]) }}
| :: :: None {{ ocaml None }}
instr_field :: 'IField_' ::=
| __field ident offset1 +: offset2 EOL :: :: Field
% OTT workaround: I can't write "intlit1 : intLit2" in a rule
% because intLit is a terminal not a non-terminal
offset :: 'Offset_' ::=
{{ phantom }} {{ ocaml int }}
{{ pp-raw x = string "??" }}
{{ pp x = string "??" }}
| intLit :: :: Int {{ ocaml int_of_string [[intLit]] }}
opcode_value :: 'Opcode_' ::=
| bitsLit :: :: Bits
| maskLit :: :: Mask
instr_unpred :: 'Instr_Unpred_' ::=
{{ phantom }} {{ ocaml (int * bitsLit) }}
{{ pp-raw x = string "??" }}
{{ pp x = string "??" }}
| __unpredictable_unless intLit == bitsLit EOL :: :: Unpred {{ ocaml (int_of_string [[intLit]], [[bitsLit]]) }}
decode_case :: 'DecoderCase_' ::=
{{ aux _ l }}
| case ( decode_slice1 , .. , decode_slicem ) of EOL
INDENT
decode_alt1 ... decode_altn
DEDENT
:: :: Case
decode_slice :: 'DecoderSlice_' ::=
| offset1 +: offset2 :: :: Slice
| ident :: :: FieldName
| ident1 : .... : identn :: :: Concat
decode_alt :: 'DecoderAlt_' ::=
| when ( decode_pattern1 , .. , decode_patternm ) => decode_body :: :: Alt
decode_pattern :: 'DecoderPattern_' ::=
| bitsLit :: :: Bits
| maskLit :: :: Mask
| ident :: :: Wildcard {{ com todo: wildcard should be underscore }}
| ! decode_pattern :: :: Not
decode_body :: 'DecoderBody_' ::=
{{ aux _ l }}
| __UNPREDICTABLE EOL :: :: UNPRED
| __UNALLOCATED EOL :: :: UNALLOC
| __NOP EOL :: :: NOP
| __encoding ident EOL :: :: Encoding
| EOL INDENT
instr_field1 .. instr_fieldm
decode_case
DEDENT
:: :: Decoder
% features not found in published ASL but potentially used in glue code
internal_definition :: 'Decl_' ::= {{ quotient-with declaration }}
{{ aux _ l }}
| __operator1 unop = ident1 , ... , identn ; EOL :: :: Operator1
| __operator2 binop_or_concat = ident1 , ... , identn ; EOL :: :: Operator2
| __newevent qualident ( formal1 , .. , formaln ) ; EOL :: :: NewEventDefn
| __event qualident possibly_empty_block :: :: EventClause
| __newmap ty qualident ( formal1 , .. , formaln ) opt_indented_block :: :: NewMapDefn
| __map qualident mapfield1 , .. , mapfieldn optmapcond then possibly_empty_block :: :: MapClause
| __config ty qualident = expr ; EOL :: :: Config
operator :: 'Operator_' ::=
{{ phantom }} {{ ocaml string }}
{{ pp-raw x = string x }}
{{ pp x = string x }}
| unop :: :: Unary {{ ocaml Utils.to_string (Asl_parser_pp.pp_unop [[unop]]) }}
| binop :: :: Binary {{ ocaml Utils.to_string (Asl_parser_pp.pp_binop [[binop]]) }}
| : :: :: In {{ ocaml ":" }}
optmapcond :: 'MapCond_' ::=
{{ phantom }} {{ ocaml expr option }}
{{ pp-raw x = match x with Some(y) -> string "Some(" ^^ pp_raw_expr y ^^ string ")" | None -> string "None" }}
{{ pp x = match x with Some(y) -> pp_expr y | None -> string "" }}
| when expr :: :: Some {{ ocaml Some([[expr]]) }}
| :: :: None {{ ocaml None }}
mapfield :: 'MapField_' ::=
| ident = pattern :: :: Field
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Identifiers
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
qualident :: 'QIdent_' ::=
{{ phantom }} {{ ocaml ident }}
{{ pp-raw x = pp_raw_ident x }}
{{ pp x = pp_ident x }}
| ident :: :: Plain {{ ocaml [[ident]] }}
| qualifier . ident :: :: Decorated {{ ocaml addQualifier [[qualifier]] [[ident]] }}
tidentdecl :: 'QIdent_' ::= {{ quotient-with qualident }}
| typeident :: :: Plain2 {{ quotient-remove }} {{ ocaml [[typeident]] }}
| ident :: :: Plain3 {{ quotient-remove }} {{ ocaml addTypeIdent([[ident]]); [[ident]] }}
| qualifier . ident :: :: Decorated2 {{ quotient-remove }} {{ ocaml addTypeIdent([[ident]]); addQualifier [[qualifier]] [[ident]] }}
tident :: 'QIdent_' ::= {{ quotient-with qualident }}
| typeident :: :: Plain4 {{ quotient-remove }} {{ ocaml [[typeident]] }}
| qualifier . typeident :: :: Decorated3 {{ quotient-remove }} {{ ocaml addQualifier [[qualifier]] [[typeident]] }}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Types
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ty :: 'Type_' ::=
| tident :: :: Constructor
| bits ( expr ) :: :: Bits
| tident ( expr1 , ... , exprn ) :: :: App
| typeof ( expr ) :: :: OfExpr
| __register intLit { regfields } :: :: Register
| array [ ixtype ] of ty :: :: Array
% I would prefer not to have a tuple type and restrict it to return types.
| ( ty1 , .. , tyn ) :: :: Tuple
% possibly empty list of regfields separated by comma, with optional trailing comma
% If it was not for the trailing comma, we would have just written 'regfield1 .. regfieldn'
regfields :: 'RegFields' ::=
{{ phantom }} {{ ocaml (slice list * ident) list }}
{{ pp-raw rfs = separate (string ",\n") (List.map pp_raw_regfield rfs) }}
{{ pp rfs = separate (string ",\n") (List.map pp_regfield rfs) }}
| :: :: Empty {{ ocaml [] }}
| regfield :: :: Single {{ ocaml [ [[regfield]] ] }}
| regfield , regfields :: :: Multiple {{ ocaml [[regfield]] :: [[regfields]] }}
regfield :: 'RegField_' ::=
{{ phantom }} {{ ocaml slice list * ident }}
{{ pp-raw rf = string "([ "^^ separate (string ";\n") (List.map pp_raw_slice (fst rf)) ^^ string " ], " ^^ pp_raw_ident (snd rf) ^^ string ")" }}
{{ pp rf = separate (string "\n") (List.map pp_slice (fst rf)) ^^ string " " ^^ pp_ident (snd rf) }}
| slice1 , ... , slicen ident :: :: Field {{ ocaml ([[slice1...slicen]], [[ident]]) }}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Statements
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
stmt :: 'Stmt_' ::=
{{ aux _ l }}
| simple_stmt :: :: Simple {{ quotient-remove }} {{ ocaml [[simple_stmt]] }}
| compound_stmt :: :: Compound {{ quotient-remove }} {{ ocaml [[compound_stmt]] }}
compound_stmt :: 'Stmt_' ::= {{ quotient-with stmt }}
{{ aux _ l }}
| conditional_stmt :: :: Conditional {{ quotient-remove }} {{ ocaml [[conditional_stmt]] }}
| repetitive_stmt :: :: Repetitive {{ quotient-remove }} {{ ocaml [[repetitive_stmt]] }}
| catch_stmt :: :: Catch {{ quotient-remove }} {{ ocaml [[catch_stmt]] }}
simple_stmt_list :: 'SimpleStmtList_' ::=
{{ phantom }} {{ ocaml stmt list }}
| simple_stmt1 ... simple_stmtn :: :: Simple {{ quotient-remove }} {{ ocaml [[simple_stmt1...simple_stmtn]] }}
% It may be possible to simplify the following because we made normal if statements more flexible - experimentation needed
simple_if_stmt :: 'SimpleIfStmts_' ::=
{{ phantom }} {{ ocaml stmt }}
| if expr then simple_stmt_list1 simple_elsif1 .. simple_elsifn else simple_stmt_list2 EOL :: :: If {{ quotient-remove }} {{ ocaml Stmt_If([[expr]], [[simple_stmt_list1]], [[simple_elsif1 .. simple_elsifn]], [[simple_stmt_list2]], Range($symbolstartpos, $endpos)) }}
| if expr then simple_stmt_list1 simple_elsif1 .. simple_elsifn EOL :: :: IfNoElse {{ quotient-remove }} {{ ocaml Stmt_If([[expr]], [[simple_stmt_list1]], [[simple_elsif1 .. simple_elsifn]], [], Range($symbolstartpos, $endpos)) }}
simple_elsif :: 'S_Elsif_' ::= {{ quotient-with s_elsif }}
| elsif expr then simple_stmt_list :: :: Simple {{ quotient-remove }} {{ ocaml S_Elsif_Cond([[expr]], [[simple_stmt_list]]) }}
simple_stmts :: 'SimpleStmts_' ::=
{{ phantom }} {{ ocaml stmt list }}
| simple_stmt_list simple_if_stmt :: :: ListIf {{ quotient-remove }} {{ ocaml [[simple_stmt_list]] @ [ [[simple_if_stmt]] ] }}
| simple_stmt_list EOL :: :: List {{ quotient-remove }} {{ ocaml [[simple_stmt_list]] }}
stmts :: 'Stmts_' ::=
{{ phantom }} {{ ocaml stmt list }}
| simple_stmts :: :: Simple {{ quotient-remove }} {{ ocaml [[simple_stmts]] }}
| compound_stmt :: :: Compound {{ quotient-remove }} {{ ocaml [ [[compound_stmt]] ] }}
indented_block :: 'Block_' ::=
{{ phantom }} {{ ocaml stmt list }}
{{ pp-raw x = (nest 4 (lbracket
^^ hardline
^^ (separate hardline (List.map pp_raw_stmt x))))
^^ hardline
^^ rbracket
}}
{{ pp x = (nest 4 (lbrace
^^ hardline
^^ if (match x with [] -> true | _ -> false) then
string "pass;"
else
(separate hardline (List.map pp_stmt x))
))
^^ hardline
^^ rbrace
}}
| EOL INDENT stmts1 ... stmtsn DEDENT :: :: Indented
{{ ocaml List.concat [[stmts1...stmtsn]] }}
possibly_empty_block :: 'Block_' ::= {{ quotient-with indented_block }}
| indented_block :: :: EIndented {{ quotient-remove }} {{ ocaml [[indented_block]] }}
| simple_stmts :: :: ESimple {{ quotient-remove }} {{ com statements on same line }} {{ ocaml [[simple_stmts]] }}
| EOL :: :: Empty {{ quotient-remove }} {{ ocaml [] }}
opt_indented_block :: 'Block_' ::= {{ quotient-with indented_block }}
| indented_block :: :: PE_EIndented {{ quotient-remove }} {{ ocaml [[indented_block]] }}
| EOL :: :: PE_Empty {{ quotient-remove }} {{ ocaml [] }}
% special type of block only found in if-statements
% (distinguishing this helps avoid the dangling else problem)
nonempty_block :: 'Block_' ::= {{ quotient-with indented_block }}
| indented_block :: :: NEIndented {{ quotient-remove }} {{ ocaml [[indented_block]] }}
| simple_stmts :: :: NESimple {{ quotient-remove }} {{ com statements on same line }} {{ ocaml [[simple_stmts]] }}
assignment_stmt :: 'Stmt_' ::= {{ quotient-with stmt }}
{{ aux _ l }}
| ty ident1 , ... , identn ; :: :: VarDeclsNoInit
| ty ident = expr ; :: :: VarDecl
| constant ty ident = expr ; :: :: ConstDecl
| lexpr = expr ; :: :: Assign
lexpr :: 'LExpr_' ::=
| - :: :: Wildcard
| qualident :: :: Var
| lexpr . ident :: :: Field
| lexpr . [ ident1 , ... , identn ] :: :: Fields
| lexpr [ slice1 , .. , slicen ] :: :: Slices
| [ lexpr1 , .... , lexprn ] :: :: BitTuple
| ( lexpr1 , .... , lexprn ) :: :: Tuple
lexpr_spice :: 'LExpr_' ::= {{ quotient-with lexpr }}
| __array lexpr [ expr ] :: :: Array {{ com spice for desugaring array assignment }}
| __write ident {{ expr1' , .. , exprm' }} [ expr1 , .. , exprn ] :: :: Write {{ com spice for desugaring setter procedure call }}
| __readwrite ident1 ident2 {{ expr1' , .. , exprm' }} [ expr1 , .. , exprn ] :: :: ReadWrite {{ com spice for desugaring read-modify-write function+procedure call }}
simple_stmt :: 'Stmt_' ::= {{ quotient-with stmt }}
{{ aux _ l }}
| assignment_stmt :: :: Assignment {{ quotient-remove }} {{ ocaml [[assignment_stmt]] }}
| qualident ( expr1 , .. , exprn ) ; :: S :: Call {{ com procedure call }} {{ ocaml Stmt_TCall([[qualident]], [], [[expr1 .. exprn]], Range($symbolstartpos,$endpos)) }}
| return expr ; :: :: FunReturn {{ com function return }}
| return ; :: :: ProcReturn {{ com procedure return }}
| assert expr ; :: :: Assert {{ com assertion }}
| UNPREDICTABLE ( ) ; :: :: Unpred {{ com underspecified behaviour }}
| CONSTRAINED_UNPREDICTABLE ; :: :: ConstrainedUnpred
| IMPLEMENTATION_DEFINED ( ident ) ; :: :: ImpDef {{ com underspecified behaviour }}
| UNDEFINED ( ) ; :: :: Undefined
| __ExceptionTaken ( ) ; :: :: ExceptionTaken
| UNPREDICTABLE ; :: :: Dep_Unpred {{ com DEPRECATED }}
| IMPLEMENTATION_DEFINED stringLit ; :: :: Dep_ImpDef {{ com DEPRECATED }}
| UNDEFINED ; :: :: Dep_Undefined {{ com DEPRECATED }}
| SEE ( expr ) ; :: :: See
| SEE stringLit ; :: S :: Dep_SeeString {{ ocaml Stmt_See(Expr_LitString([[stringLit]]), Range($symbolstartpos, $endpos)) }} {{ com DEPRECATED }}
| SEE ident ; :: S :: Dep_SeeIdent {{ ocaml Stmt_See(Expr_LitString(pprint_ident [[ident]]), Range($symbolstartpos, $endpos)) }} {{ com DEPRECATED }}
| throw ident ; :: :: Throw
stmt_spice :: 'Stmt_' ::= {{ quotient-with stmt }}
| qualident {{ expr1' , .. , exprm' }} ( expr1 , .. , exprn ) ; :: :: TCall {{ com spice for procedure call with explicit type parameters }}
conditional_stmt :: 'Stmt_' ::= {{ quotient-with stmt }}
{{ aux _ l }}
| if expr then opt_indented_block
s_elsif1
..
s_elsifn
optional_else
:: :: If
| if expr then simple_stmts
s_elsif1
..
s_elsifn
optional_else
:: S :: If2 {{ ocaml Stmt_If([[expr]],[[simple_stmts]],[[s_elsif1 .. s_elsifn]],[[optional_else]], Range($symbolstartpos, $endpos)) }}
| if expr then simple_stmt_list1 simple_elsif1 .. simple_elsifn else simple_stmt_list2 EOL :: :: If3 {{ quotient-remove }} {{ ocaml Stmt_If([[expr]], [[simple_stmt_list1]], [[simple_elsif1 .. simple_elsifn]], [[simple_stmt_list2]], Range($symbolstartpos, $endpos)) }}
| case expr of EOL
INDENT
alt1 ... altn
opt_otherwise
DEDENT
:: :: Case
s_elsif :: 'S_Elsif_' ::=
| elsif expr then opt_indented_block :: :: Cond
| elsif expr then simple_stmts :: S :: Cond2 {{ ocaml S_Elsif_Cond([[expr]], [[simple_stmts]]) }}
optional_else :: 'S_Else' ::=
{{ phantom }} {{ ocaml stmt list }}
{{ pp-raw x = match x with [] -> string ""
| ys -> string "(else " ^^ separate (string "\n") (List.map pp_raw_stmt ys) ^^ string ")" }}
{{ pp x = match x with [] -> string ""
| ys -> string "else" ^^ hardline
^^ (nest 4 (separate (string "\n") (List.map pp_stmt ys)))
}}
| else opt_indented_block :: :: ElseBlock {{ ocaml [[opt_indented_block]] }}
| else simple_stmts :: :: ElseSimple {{ ocaml [[simple_stmts]] }}
| :: :: NoElse {{ ocaml [] }}
alt :: 'Alt_' ::=
| when pattern1 , ... , patternn opt_altcond possibly_empty_block :: :: Alt
| when pattern1 , ... , patternn opt_altcond simple_if_stmt :: S :: Alt2 {{ ocaml Alt_Alt([[pattern1 ... patternn]], [[opt_altcond]], [ [[simple_if_stmt]] ]) }}
opt_otherwise :: 'OptOtherwise_' ::=
{{ phantom }} {{ ocaml (stmt list) option }}
{{ pp-raw x = match x with Some(ys) -> string "Some(" ^^ separate (string "\n") (List.map pp_raw_stmt ys) ^^ string ")" | None -> string "None" }}
{{ pp x = match x with Some(ys) -> string "otherwise" ^^ hardline
^^ (nest 4 (separate (string "\n") (List.map pp_stmt ys)))
| None -> string "" }}
| otherwise possibly_empty_block :: :: Some {{ ocaml Some([[possibly_empty_block]]) }}
| :: :: None {{ ocaml None }}
opt_altcond :: 'AltCond_' ::= {{ ocaml expr option }}
{{ phantom }} {{ ocaml expr option }}
{{ pp-raw x = match x with Some(y) -> string "Some(" ^^ pp_raw_expr y ^^ string ")" | None -> string "None" }}
{{ pp x = match x with Some(y) -> pp_expr y | None -> string "" }}
| && expr => :: :: Some {{ ocaml Some([[expr]]) }}
| => :: :: None0 {{ ocaml None }}
| :: :: None {{ ocaml None }}
pattern :: 'Pat_' ::=
| intLit :: :: LitInt
| hexLit :: :: LitHex
| bitsLit :: :: LitBits
| maskLit :: :: LitMask
| qualident :: :: Const
| - :: :: Wildcard
| ( pattern1 , .... , patternn ) :: :: Tuple
| { apattern1 , .. , apatternn } :: :: Set
apattern :: 'Pat_' ::= {{ quotient-with pattern }}
| expr1 '..' expr2 :: :: Range
| expr :: :: Single
repetitive_stmt :: 'Stmt_' ::= {{ quotient-with stmt }}
{{ aux _ l }}
| for ident = expr1 direction expr2 indented_block :: :: For
| while expr do indented_block :: :: While
| repeat indented_block until expr ; EOL :: :: Repeat
direction :: 'Direction_' ::=
| to :: :: Up
| downto :: :: Down
catch_stmt :: 'Stmt_' ::= {{ quotient-with stmt }}
{{ aux _ l }}
| try indented_block
catch ident EOL
INDENT
catcher1 .. catchern
opt_otherwise
DEDENT
:: :: Try
catcher :: 'Catcher_' ::=
| when expr opt_indented_block :: :: Guarded
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Expressions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
expr :: 'Expr_' ::=
| conditional_expression :: :: Conditional {{ quotient-remove }} {{ ocaml [[conditional_expression]] }}
% dexpr in hand parser
conditional_expression :: 'Expr_' ::= {{ quotient-with expr }}
| if cexpr1 then expr1 e_elsif1 .. e_elsifn else expr2 :: :: If
| cexpr :: :: CExpr {{ quotient-remove }} {{ ocaml [[cexpr]] }}
e_elsif :: 'E_Elsif_' ::= {{ ocaml expr * expr }}
| elsif expr1 then expr2 :: :: Cond
cexpr :: 'Expr_' ::= {{ quotient-with expr }}
| bexpr factor1 .. factorn :: :: Binops {{ quotient-remove }} {{ ocaml buildExpression [[bexpr]] [[factor1..factorn]] (Range($startpos([[bexpr]]),$endpos([[factor1..factorn]]))) }}
% the following definition is not referenced but is included to cause Expr_Binop to be defined
zexpr :: 'Expr_' ::= {{ quotient-with expr }}
| expr1 binop expr2 :: :: Binop
factor :: 'Factor_' ::=
| binop_or_concat bexpr :: :: BinOp
binop_or_concat :: 'Binop_' ::= {{ quotient-with binop }}
| binop :: :: NotConcat {{ quotient-remove }} {{ ocaml [[binop]] }}
| : :: :: Concat
binop :: 'Binop_' ::=
| == :: :: Eq
| != :: :: NtEq
| > :: :: Gt
| >= :: :: GtEq
| < :: :: Lt
| <= :: :: LtEq
| + :: :: Plus
| - :: :: Minus
| * :: :: Multiply
| / :: :: Divide
| ^ :: :: Power
| QUOT :: :: Quot
| REM :: :: Rem
| DIV :: :: Div
| MOD :: :: Mod
| << :: :: ShiftL
| >> :: :: ShiftR
| && :: :: BoolAnd
| || :: :: BoolOr
| IFF :: :: BoolIff
| IMPLIES :: :: BoolImplies
| OR :: :: BitOr
| EOR :: :: BitEor
| AND :: :: BitAnd
| ++ :: :: Append
dummy_binop :: 'Binop_' ::= {{ quotient-with binop }}
| :: :: DUMMY
bexpr :: 'Expr_' ::= {{ quotient-with expr }}
| unop fexpr :: :: Unop {{ com unary operator }}
| fexpr :: :: FExpr {{ quotient-remove }} {{ ocaml [[fexpr]] }}
fexpr :: 'Expr_' ::= {{ quotient-with expr }}
| fexpr . ident :: :: Field {{ com field selection }}
| fexpr . [ ident1 , ... , identn ] :: :: Fields {{ com multiple field selection }}
| fexpr [ slice1 , .. , slicen ] :: :: Slices {{ com bitslice }}
| fexpr 'IN' pattern :: :: In {{ com pattern match }}
| aexpr :: :: AExpr {{ quotient-remove }} {{ ocaml [[aexpr]] }}
aexpr :: 'Expr_' ::= {{ quotient-with expr }}
| literal_expression :: :: Lit {{ quotient-remove }} {{ ocaml [[literal_expression]] }}
| qualident :: :: Var
| qualident ( expr1 , .. , exprn ) :: S :: Apply {{ ocaml Expr_TApply([[qualident]], [], [[expr1 .. exprn]]) }}
| ( expr ) :: :: Parens
| ( expr1 , .... , exprn ) :: :: Tuple {{ com tuple }}
| ty UNKNOWN :: :: Unknown
| ty IMPLEMENTATION_DEFINED opt_stringLit :: :: ImpDef
expr_spice :: 'Expr_' ::= {{ quotient-with expr }}
| qualident {{ expr1' , .. , exprm' }} ( expr1 , .. , exprn ) :: :: TApply {{ com spice for desugaring function call with explicit type parameters }}
| __array expr1 [ expr2 ] :: :: Array {{ com spice for desugaring array accesses }}
opt_stringLit :: 'String_' ::=
{{ phantom }} {{ ocaml string option }}
{{ pp-raw x = match x with Some(y) -> string "Some(" ^^ string y ^^ string ")" | None -> string "None" }}
{{ pp x = match x with Some(y) -> string y | None -> string "" }}
| stringLit :: :: Some {{ ocaml Some([[stringLit]]) }}
| :: :: None {{ ocaml None }}
unop :: 'Unop_' ::=
| - :: :: Negate
| ! :: :: BoolNot
| NOT :: :: BitsNot
slice :: 'Slice_' ::=
| sexpr :: :: Single
| sexpr1 : sexpr2 :: :: HiLo
| sexpr1 +: sexpr2 :: :: LoWd
% almost identical to conditional_expression except that it omits : binop
sexpr :: 'Expr_' ::= {{ quotient-with expr }}
| scexpr :: :: CExpr2 {{ quotient-remove }} {{ ocaml [[scexpr]] }}
| if cexpr1 then expr1 e_elsif1 .. e_elsifn else scexpr2 :: :: If2 {{ quotient-remove }} {{ ocaml Expr_If([[cexpr1]], [[expr1]], [[e_elsif1 .. e_elsifn]], [[scexpr2]]) }}
scexpr :: 'Expr_' ::= {{ quotient-with expr }}
| bexpr sfactor1 .. sfactorn :: :: Binop2 {{ quotient-remove }} {{ ocaml buildExpression [[bexpr]] [[sfactor1..sfactorn]] (Range($startpos([[bexpr]]),$endpos([[sfactor1..sfactorn]]))) }}
sfactor :: 'Factor_' ::= {{ quotient-with factor }}
| binop bexpr :: :: BinOp2 {{ quotient-remove }} {{ ocaml Factor_BinOp([[binop]], [[bexpr]]) }}
literal_expression :: 'Expr_' ::= {{ quotient-with expr }}
| intLit :: :: LitInt {{ com literal decimal integer }}
| hexLit :: :: LitHex {{ com literal hexadecimal integer }}
| realLit :: :: LitReal {{ com literal real }}
| bitsLit :: :: LitBits {{ com literal bitvector }}
| maskLit :: :: LitMask {{ com literal bitmask }}
| stringLit :: :: LitString {{ com literal string }}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Additional entry points used by ASLi
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
expr_command :: 'CLI_' ::= {{ menhir-start }}
{{ phantom }} {{ ocaml expr }}
{{ pp-raw x = pp_raw_expr x }}
{{ pp x = pp_expr x }}
| EOL expr :: :: Expr {{ ocaml [[expr]] }}
stmt_command :: 'CLI_' ::= {{ menhir-start }}
{{ phantom }} {{ ocaml stmt }}
{{ pp-raw x = pp_raw_stmt x }}
{{ pp x = pp_stmt x }}
| EOL stmt :: :: Stmt {{ ocaml [[stmt]] }}
impdef_command :: 'CLI_' ::= {{ menhir-start }}
{{ pp-raw se = match se with (s,e) -> string s ^^ string "=" ^^ pp_raw_expr e }}
{{ pp se = match se with (s,e) -> string s ^^ string "=" ^^ pp_expr e }}
| EOL stringLit = expr :: :: Impdef {{ ocaml ([[stringLit]], [[expr]]) }}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Misc
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
terminals :: 'terminals_' ::=
| INDENT :: :: Indent {{ tex \mbox{$\{\!\mid$} }}
| DEDENT :: :: Dedent {{ tex \mbox{$\mid\!\}$} }}
| EOL :: :: EndOfLine {{ tex \mbox{$\hookleftarrow$} }}
| ^ :: :: Caret {{ tex \mbox{$\,\hat{}$} }}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% End of grammar
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
embed
{{ ocaml
let associativeOperators: binop list =
[ Binop_Plus
; Binop_Multiply
; Binop_BoolAnd
; Binop_BoolOr
; Binop_BitOr
; Binop_BitEor
; Binop_BitAnd
; Binop_Concat
; Binop_Append
]
(* boolean operators bind least tightly *)
let booleanOperators: binop list =
[ Binop_BoolAnd
; Binop_BoolOr
; Binop_BoolIff
; Binop_BoolImplies
]
(* comparision operators bind less tightly than arithmetic, etc. *)
let comparisionOperators: binop list =
[ Binop_Eq
; Binop_NtEq
; Binop_Gt
; Binop_GtEq
; Binop_Lt
; Binop_LtEq
]
(* arithmetic and similar operations bind more tightly than comparisions and &&/|| *)
let miscOperators: binop list =
[ Binop_Plus
; Binop_Minus
; Binop_Multiply
; Binop_Divide
; Binop_Power
; Binop_Quot
; Binop_Rem
; Binop_Div
; Binop_Mod
; Binop_ShiftL
; Binop_ShiftR
; Binop_BitOr
; Binop_BitEor
; Binop_BitAnd
; Binop_Concat
]
let isAssociative (x: binop): bool = List.mem x associativeOperators
let isBoolean (x: binop): bool = List.mem x booleanOperators
let isComparision (x: binop): bool = List.mem x comparisionOperators
let isMisc (x: binop): bool = List.mem x miscOperators
(* Is operator x higher priority than y
* (Binop_DUMMY acts as the lowest priority operation - see below)
*)
let higherPriorityThan (x: binop) (y: binop): bool option =
if y = Binop_DUMMY then Some(true)
else if x = Binop_Power && y = Binop_Multiply then Some(true)
else if x = Binop_Power && y = Binop_Divide then Some(true)
else if x = Binop_Power && y = Binop_Plus then Some(true)
else if x = Binop_Power && y = Binop_Minus then Some(true)
else if x = Binop_Multiply && y = Binop_Plus then Some(true)
else if x = Binop_Multiply && y = Binop_Minus then Some(true)
else if x = Binop_Plus && y = Binop_Minus then Some(true)
else if isMisc x && isBoolean y then Some(true)
else if isMisc x && isComparision y then Some(true)
else if isComparision x && isBoolean y then Some(true)
else if x = Binop_DUMMY then Some(false)
else if y = Binop_Power && x = Binop_Multiply then Some(false)
else if y = Binop_Power && x = Binop_Divide then Some(false)
else if y = Binop_Power && x = Binop_Plus then Some(false)
else if y = Binop_Power && x = Binop_Minus then Some(false)
else if y = Binop_Multiply && x = Binop_Plus then Some(false)
else if y = Binop_Multiply && x = Binop_Minus then Some(false)
else if isMisc y && isBoolean x then Some(false)
else if isMisc y && isComparision x then Some(false)
else if isComparision y && isBoolean x then Some(false)
(* The following rules might be a mistake - though they do seem
* to match common usage.
*)
else if x = Binop_Minus && y = Binop_Plus then Some(true)
else if x = Binop_Minus && y = Binop_Minus then Some(true)
else None
(** Parsing exceptions (2/2) *)
exception PrecedenceError of l * binop * binop
(* Support function for parsing expression trees of the form
*
* ... op x op_1 y_1 op_2 y_2 ... op_n y_n
*
* Consumes input until it finds an operator y_i of lower precedence
* than op returning
*
* 1) an expression representing "x op_1 ... y_i-1"
* 2) the remainder if the input "op_i y_i ... op_n y_n"
*
* As in Dijkstra's "Shunting Yard" algorithm, we work left to right across
* the expression comparing the next two operators:
* - op1 > op2 => (x op1 y1) op2 ...
* - op1 < op2 => x op1 (y1 op2 ...) ...
* - op1 = op2 => (x op1 y1) op2 ... if op1 is associative
* - _ => error
*)
let rec buildExpr (op: binop) (x: expr) (ys: factor list) (loc: l): (expr * factor list) =
( match ys with
| [] ->
(x, [])
| (Factor_BinOp(op1, y1) :: ys1) ->
( match higherPriorityThan op op1 with
| Some(false) ->
( match ys1 with
| (Factor_BinOp(op2, _) :: _) ->
( match higherPriorityThan op1 op2 with
| Some(true) ->
buildExpr op (Expr_Binop(x, op1, y1)) ys1 loc
| Some(false) ->
let (r, rs) = buildExpr op1 y1 ys1 loc in
buildExpr op (Expr_Binop(x, op1, r)) rs loc
| None ->
if op1 = op2 && isAssociative(op1) then
buildExpr op (Expr_Binop(x, op1, y1)) ys1 loc
else
raise (PrecedenceError(loc, op1, op2))
)
| [] ->
(Expr_Binop(x, op1, y1), [])
)
| _ -> (x, ys)
)
)
(* Construct an expression tree based on precedence rules
*
* Given parser output of the form x op_1 y_1 op_2 y_2 ...op_n y_n,
* construct a tree based on the relative priorities of op1, ... opn.
* If any adjacent operators op_i, op_i+1 are unordered, report
* a parsing ambiguity.
*
* We use a recursive variant on Dijkstra's Shunting Yard algorithm to
* parse a list of operator-expression pairs into an expression tree
* based on operator precedences
* All operators are treated as left-associative
*)
let buildExpression (x: expr) (fs: factor list) (loc: l): expr =
( match buildExpr Binop_DUMMY x fs loc with
| (e, []) -> e
| (e, _) -> raise (Parse_error_locn(loc, "Impossible: unable to resolve precedence"))
)
}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% End
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%