Source file script.ml
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
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
open Libparser
module S = Basic_types.String
type env = {
wordsize : int;
endianness : Machine.endianness;
define : Dba.Var.t -> Lexing.position -> unit;
origin : string -> Lexing.position option;
lookup : string -> Dba.LValue.t;
lookup_symbol : string -> Dba.Var.Tag.attribute -> Dba.Expr.t;
}
type 'a loc = 'a Ast.loc
module Symbol = Ast.Symbol
module Loc = Ast.Loc
module Expr = Ast.Expr
module Instr = Ast.Instr
exception Inference_failure of Expr.t loc
exception Invalid_size of Expr.t loc * int * int
exception Invalid_operation of Expr.t loc
exception Invalid_annotation of (Loc.t loc * int * Lexing.position option)
let _ =
Printexc.register_printer (function
| Inference_failure (e, pos) ->
Some
(Format.asprintf "@[<v>Unable to infer the size of %a %a@]" Expr.pp e
Parse_utils.pp_pos pos)
| Invalid_size ((e, pos), size, expect) ->
Some
(Format.asprintf "@[<v>Invalid size for %a (expected %d, got %d) %a@]"
Expr.pp e expect size Parse_utils.pp_pos pos)
| Invalid_operation (e, pos) ->
Some
(Format.asprintf "@[<v>Invalid operation in %a %a@]" Expr.pp e
Parse_utils.pp_pos pos)
| Invalid_annotation ((var, pos), _, Some pos') ->
Some
(Format.asprintf
"@[<v>Conflicting size annotation in %a %a@ Previous definition \
%a@]"
Loc.pp var Parse_utils.pp_pos pos Parse_utils.pp_pos pos')
| Invalid_annotation ((var, pos), size, None) ->
Some
(Format.asprintf
"@[<v>Invalid size annotation in %a (expected <%d>) %a@]" Loc.pp
var size Parse_utils.pp_pos pos)
| _ -> None)
let rec eval_expr ?size ((e, p) as t : Expr.t loc) env =
let e =
match e with
| Int (z, _) -> (
match size with
| None -> raise (Inference_failure t)
| Some size ->
(if Z.numbits z > size then
let line = p.pos_lnum and column = p.pos_cnum - p.pos_bol - 1 in
Options.Logger.warning
"integer %a (line %d, column %d) does not fit in a bitvector \
of %d bit%s"
Expr.pp e line column size
(if size > 1 then "s" else ""));
Dba.Expr.constant (Bitvector.create z size))
| Bv bv -> Dba.Expr.constant bv
| Symbol ((name, attr), _) -> env.lookup_symbol name attr
| Loc (Sub ({ hi; lo }, loc), _) ->
Dba.Expr.restrict lo hi
(Dba.LValue.to_expr (eval_loc ?size:None loc env))
| Loc loc -> Dba.LValue.to_expr (eval_loc ?size loc env)
| Unary (op, x) ->
let size =
match op with Restrict _ | Uext _ | Sext _ -> None | _ -> size
in
Dba.Expr.unary op (eval_expr ?size x env)
| Binary (op, x, y) ->
let x, y = eval_binary ?size ~op x y env in
Dba.Expr.binary op x y
| Ite (q, x, y) ->
let q = eval_expr ~size:1 q env in
let x, y = eval_binary ?size x y env in
Dba.Expr.ite q x y
in
Option.iter
(fun size ->
let size' = Dba.Expr.size_of e in
if size' <> size then raise (Invalid_size (t, size', size)))
size;
e
and eval_binary ?(first = true) ?size ?op x y env =
match
eval_expr
?size:
(match op with
| None -> size
| Some
( Plus | Minus | Mult | DivU | DivS | ModU | ModS | Or | And | Xor
| LShift | RShiftU | RShiftS | LeftRotate | RightRotate ) ->
size
| Some _ -> None)
x env
with
| x ->
( x,
eval_expr
?size:
(match op with
| Some Concat -> (
match size with
| None -> None
| Some size -> Some (size - Dba.Expr.size_of x))
| None | Some _ -> Some (Dba.Expr.size_of x))
y env )
| exception Inference_failure _ when first ->
let y, x = eval_binary ~first:false ?size ?op y x env in
(x, y)
and eval_int ((e, _) as t : Expr.t loc) env =
match e with
| Int (z, _) ->
if not (Z.fits_int z) then raise (Invalid_operation t);
Z.to_int z
| Bv bv ->
if not (Z.fits_int (Bitvector.signed_of bv)) then
raise (Invalid_operation t);
Bitvector.to_int bv
| Symbol ((name, attr), _) -> (
match env.lookup_symbol name attr with
| Var { info = Symbol (_, (lazy bv)); _ } ->
if not (Z.fits_int (Bitvector.value_of bv)) then
raise (Invalid_operation t);
Bitvector.to_uint bv
| _ -> raise (Invalid_operation t))
| Unary (UMinus, x) -> -eval_int x env
| Binary (Plus, x, y) -> eval_int x env + eval_int y env
| Binary (Minus, x, y) -> eval_int x env - eval_int y env
| Binary (Mult, x, y) -> eval_int x env * eval_int y env
| Binary (DivS, x, y) -> eval_int x env / eval_int y env
| Loc _ | Unary _ | Binary _ | Ite _ -> raise (Invalid_operation t)
and declare_var name size pos env =
let var = Dba.Var.create name ~bitsize:(Size.Bit.create size) ~tag:Empty in
env.define var pos;
Dba.LValue.v var
and eval_var ?size ((_, p) as t) name (annot : Ast.Size.t) env =
let lval =
match env.lookup name with
| lval ->
let size' =
match annot with
| Explicit size -> size
| Sizeof lval ->
let lval = eval_loc lval env in
Dba.LValue.size_of lval
| Eval expr ->
let size = eval_int expr env in
if size < 0 then raise (Invalid_operation expr);
size
| Implicit -> Dba.LValue.size_of lval
and size = Dba.LValue.size_of lval in
if size <> size' then
raise (Invalid_annotation (t, size, env.origin name));
lval
| exception Not_found -> (
match annot with
| Explicit size -> declare_var name size p env
| Sizeof lval ->
let lval = eval_loc lval env in
declare_var name (Dba.LValue.size_of lval) p env
| Eval expr ->
let size = eval_int expr env in
if size < 0 then raise (Invalid_operation expr);
declare_var name size p env
| Implicit -> (
match size with
| None -> raise (Inference_failure (Expr.loc t, p))
| Some size -> declare_var name size p env))
in
Option.iter
(fun size ->
let size' = Dba.LValue.size_of lval in
if size' <> size then raise (Invalid_size ((Expr.loc t, p), size', size)))
size;
lval
and eval_loc ?size ((l, p) as t : Loc.t loc) env =
let lval =
match l with
| Var (name, annot) -> eval_var ?size t name annot env
| Load (len, endianness, addr, array) ->
let endianness =
Option.fold ~none:env.endianness ~some:Fun.id endianness
in
let addr = eval_expr ~size:env.wordsize addr env in
Dba.LValue.store (Size.Byte.create len) endianness addr ?array
| Sub ({ hi; lo }, ((Var (name, annot), _) as t')) -> (
match eval_var ?size t' name annot env with
| Var var -> Dba.LValue.restrict var lo hi
| Restrict (var, { hi = hi'; lo = lo' }) ->
if hi' > hi + lo' then raise (Inference_failure (Expr.loc t, p));
Dba.LValue.restrict var (lo + lo') (hi + lo')
| _ -> raise (Invalid_operation (Expr.loc t, p)))
| Sub _ -> raise (Invalid_operation (Expr.loc t, p))
in
Option.iter
(fun size ->
let size' = Dba.LValue.size_of lval in
if size' <> size then raise (Invalid_size ((Expr.loc t, p), size', size)))
size;
lval
module Output = struct
type format = Types.Output.format = Bin | Dec | Hex | Ascii
type t =
| Model
| Formula
| Slice of (Expr.t loc * string) list
| Value of format * Expr.t loc
| Stream of string
| String of string
let eval env (t : t) : Types.Output.t =
match t with
| Model -> Model
| Formula -> Formula
| Slice values ->
Slice (List.map (fun (e, name) -> (eval_expr e env, name)) values)
| Value (fmt, e) -> Value (fmt, eval_expr e env)
| Stream name -> Stream name
| String name -> String name
let format_str = function
| Bin -> "bin"
| Dec -> "dec"
| Hex -> "hexa"
| Ascii -> "ascii"
let pp ppf = function
| Model -> Format.pp_print_string ppf "model"
| Formula -> Format.pp_print_string ppf "formula"
| Slice defs ->
Format.fprintf ppf "formula for %a"
(Format.pp_print_list
~pp_sep:(fun ppf () -> Format.pp_print_string ppf ", ")
(fun ppf ((expr, _), name) ->
Format.fprintf ppf "%a as %s" Expr.pp expr name))
defs
| Value (fmt, (e, _)) ->
Format.fprintf ppf "%s %a" (format_str fmt) Expr.pp e
| Stream name -> Format.fprintf ppf "ascii stream %s" name
| String name -> Format.fprintf ppf "c string %s" name
end
type Ast.Obj.t +=
| Int of int
| Int_list of int list
| Format of Output.format
| Output of Output.t
| Output_list of Output.t list
| String_list of string list
| Key_val of (string * string)
| Key_val_list of (string * string) list
| Symbol_list of Symbol.t loc list
| Loc_opt of Loc.t loc option
| Loc_opt_list of Loc.t loc option list
| Expr_opt of Expr.t loc option
| Expr_list of Expr.t loc list
| Named of (Expr.t loc * string)
| Named_list of (Expr.t loc * string) list
type Ast.Instr.t +=
| Argument of Loc.t loc * int (** [lval] := arg([i]) *)
| Return of Expr.t loc option (** return [rval] *)
| Cut of Expr.t loc option
| Print of Output.t
| Reach of int * Expr.t loc option * Output.t list
| Enumerate of int * Expr.t loc
| Error of string
type Ast.t +=
| Starting_from of Expr.t loc * Instr.t list
| Starting_from_core of Instr.t list
| Load_sections of string list
| Load_data of Loc.t loc
| Concretize_stack_pointer
| Import_symbols of Symbol.t loc list * string
| Hook of Expr.t loc list * Instr.t list * bool
| Return_hook of Ast.Symbol.t loc * Instr.t list
| Decode of Binstream.t * Instr.t list
| Init of Instr.t list
| Explore_all
let pp_comma_list pp =
Format.pp_print_list
~pp_sep:(fun ppf () -> Format.pp_print_string ppf ", ")
pp
let pp_exprs = pp_comma_list (fun ppf (e, _) -> Expr.pp ppf e)
let pp_outputs =
Format.pp_print_list
~pp_sep:(fun ppf () -> Format.pp_print_string ppf " and ")
(fun ppf output -> Format.fprintf ppf "print %a" Output.pp output)
let () =
Ast.Instr.register_pp (fun ppf inst ->
match inst with
| Argument ((lval, _), n) ->
Format.fprintf ppf "%a := arg(%d)" Loc.pp lval n;
true
| Return None ->
Format.pp_print_string ppf "return";
true
| Return (Some (rval, _)) ->
Format.fprintf ppf "return %a" Expr.pp rval;
true
| Cut guard ->
Format.fprintf ppf "cut %a"
(fun ppf guard ->
Option.iter
(fun (test, _) -> Format.fprintf ppf " if %a" Expr.pp test)
guard)
guard;
true
| Print output ->
Format.fprintf ppf "print %a" Output.pp output;
true
| Enumerate (n, (rval, _)) ->
Format.fprintf ppf "enumerate%s %a%a"
(if n = max_int then "*" else "")
Expr.pp rval
(fun ppf n ->
if n <> 1 && n <> max_int then Format.fprintf ppf " (%d)" n)
n;
true
| Reach (n, guard, outputs) ->
Format.fprintf ppf "reach%s%a%a%a"
(if n < 0 then "*" else "")
(fun ppf n -> if 1 < n then Format.fprintf ppf " %d times" n)
n
(fun ppf guard ->
Option.iter
(fun (test, _) ->
Format.fprintf ppf " such that %a" Expr.pp test)
guard)
guard
(fun ppf outputs ->
if outputs <> [] then
Format.fprintf ppf " then %a" pp_outputs outputs)
outputs;
true
| Error msg ->
Format.fprintf ppf "error %S" msg;
true
| _ -> false)
let pp_stmts = Format.pp_print_list ~pp_sep:Format.pp_print_space Instr.pp
let pp_with_stmts ppf stmts =
match stmts with
| [] -> Format.pp_close_box ppf ()
| stmts -> Format.fprintf ppf " with@ %a@]@ end" pp_stmts stmts
let decl_printers = ref []
let register_pp pp = decl_printers := pp :: !decl_printers
let rec resolve_pp ppf decl = function
| [] -> Format.pp_print_string ppf "unknown"
| pp :: printers -> if not (pp ppf decl) then resolve_pp ppf decl printers
let pp_options =
Format.pp_print_list
~pp_sep:(fun ppf () -> Format.pp_print_char ppf ',')
(fun ppf (k, v) ->
Format.fprintf ppf "%S" k;
if v <> "" then Format.fprintf ppf "=%S" v)
let pp ppf decl =
match decl with
| Starting_from ((addr, _), stmts) ->
Format.fprintf ppf "@[<v>@[<v 2>starting from %a%a@]" Expr.pp addr
pp_with_stmts stmts
| Starting_from_core stmts ->
Format.fprintf ppf "@[<v>@[<v 2>starting from core%a@]" pp_with_stmts
stmts
| Load_sections [ name ] ->
Format.fprintf ppf "load section %s from file" name
| Load_sections names ->
Format.fprintf ppf "load sections %a from file"
(Format.pp_print_list
~pp_sep:(fun ppf () -> Format.pp_print_string ppf ", ")
Format.pp_print_string)
names
| Load_data (store, _) -> Format.fprintf ppf "%a from file" Loc.pp store
| Concretize_stack_pointer ->
Format.pp_print_string ppf "with concrete stack pointer"
| Import_symbols (symbols, file) ->
Format.fprintf ppf "import %a from %s"
(pp_comma_list (fun ppf (sym, _) -> Symbol.pp ppf sym))
symbols file
| Hook (addr, stmts, _) ->
Format.fprintf ppf "@[<v>@[<v 2>hook %a with@ %a@]@ end@]" pp_exprs addr
pp_stmts stmts
| Return_hook ((symbol, _), stmts) ->
Format.fprintf ppf "@[<v>@[<v 2>hook %a return with@ %a@]@ end@]"
Ast.Symbol.pp symbol pp_stmts stmts
| Decode (opcode, stmts) ->
Format.fprintf ppf "@[<v>@[<v 2>hook opcode %a by@ %a@]@ end@]"
Binstream.pp opcode pp_stmts stmts
| Init stmts -> Format.fprintf ppf "@[<v>%a@]" pp_stmts stmts
| Explore_all -> Format.pp_print_string ppf "explore all"
| _ -> resolve_pp ppf decl !decl_printers
let _pp_global ppf global_data =
Format.fprintf ppf "@[<v>%a@]"
(fun ppf map ->
S.Map.iter
(fun name values ->
Format.fprintf ppf "@[<h>%s: %a@]@ " name
(fun ppf set ->
S.Set.iter (fun value -> Format.fprintf ppf "%s@ " value) set)
values)
map)
global_data
let grammar :
( unit,
Libparser.obj,
unit,
unit,
Libparser.obj Dyp.dyplexbuf )
Dyp.dyp_action
list =
[
Dyp.Bind_to_cons
[
("byte", "Obj");
("byte_rev_list", "Obj");
("key_value", "Obj");
("comma_separated_key_value_rev_list", "Obj");
("options", "Obj");
("format", "Obj");
("named", "Obj");
("comma_separated_named_rev_list", "Obj");
("output", "Obj");
("and_separated_output_rev_list", "Obj");
("outputs", "Obj");
("times", "Obj");
("such_that", "Obj");
("guard", "Obj");
("num", "Obj");
("qident", "String");
("section", "String");
("comma_separated_section_rev_list", "Obj");
("comma_separated_symbol_rev_list", "Obj");
("arg", "Obj");
("comma_separated_arg_rev_list", "Obj");
("arguments", "Stmt");
("expr_opt", "Obj");
("comma_separated_expr_rev_list", "Obj");
("with_stmts_end", "Stmt");
("directive", "Stmt");
("decl", "Decl");
("rev_program", "Program");
("program", "Program");
];
Dyp.Add_rules
[
( ( "byte",
[
Dyp.Regexp
(Dyp.RE_Seq [ Dyp.RE_Name "hexdigit"; Dyp.RE_Name "hexdigit" ]);
],
"default_priority",
[] ),
fun _ -> function
| [ Syntax.Lexeme_matched byte ] ->
(Syntax.Obj (Int (Z.to_int (Z.of_string_base 16 byte))), [])
| _ -> assert false );
( ("byte_rev_list", [], "default_priority", []),
fun _ _ -> (Syntax.Obj (Int_list []), []) );
( ( "byte_rev_list",
[
Dyp.Non_ter ("byte_rev_list", No_priority);
Dyp.Non_ter ("byte", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [ Syntax.Obj (Int_list bytes); Syntax.Obj (Int byte) ] ->
(Syntax.Obj (Int_list (byte :: bytes)), [])
| _ -> assert false );
( ( "key_value",
[ Dyp.Non_ter ("qident", No_priority) ],
"default_priority",
[] ),
fun _ -> function
| [ Syntax.String key ] -> (Syntax.Obj (Key_val (key, "")), [])
| _ -> assert false );
( ( "key_value",
[
Dyp.Non_ter ("qident", No_priority);
Dyp.Regexp (RE_Char '=');
Dyp.Non_ter ("qident", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [ Syntax.String key; _; Syntax.String value ] ->
(Syntax.Obj (Key_val (key, value)), [])
| _ -> assert false );
( ( "comma_separated_key_value_rev_list",
[ Dyp.Non_ter ("key_value", No_priority) ],
"default_priority",
[] ),
fun _ -> function
| [ Syntax.Obj (Key_val pair) ] ->
(Syntax.Obj (Key_val_list [ pair ]), [])
| _ -> assert false );
( ( "comma_separated_key_value_rev_list",
[
Dyp.Non_ter ("comma_separated_key_value_rev_list", No_priority);
Dyp.Regexp (RE_Char ',');
Dyp.Non_ter ("key_value", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [ Syntax.Obj (Key_val_list l); _; Syntax.Obj (Key_val pair) ] ->
(Syntax.Obj (Key_val_list (pair :: l)), [])
| _ -> assert false );
( ("options", [], "default_priority", []),
fun _ _ -> (Syntax.Obj (Key_val_list []), []) );
( ( "options",
[
Dyp.Regexp (RE_Char '[');
Dyp.Non_ter ("comma_separated_key_value_rev_list", No_priority);
Dyp.Regexp (RE_Char ']');
],
"default_priority",
[] ),
fun _ -> function
| [ _; Syntax.Obj (Key_val_list l); _ ] ->
(Syntax.Obj (Key_val_list (List.rev l)), [])
| _ -> assert false );
( ("num", [], "default_priority", []),
fun _ _ -> (Syntax.Obj (Int 1), []) );
( ( "num",
[
Dyp.Regexp (RE_Char '(');
Dyp.Non_ter ("int", No_priority);
Dyp.Regexp (RE_Char ')');
],
"default_priority",
[] ),
fun _ -> function
| [ _; Syntax.Obj_int n; _ ] -> (Syntax.Obj (Int (Z.to_int n)), [])
| _ -> assert false );
( ("times", [], "default_priority", []),
fun _ _ -> (Syntax.Obj (Int 1), []) );
( ( "times",
[ Dyp.Non_ter ("int", No_priority); Dyp.Regexp (RE_String "times") ],
"default_priority",
[] ),
fun _ -> function
| [ Syntax.Obj_int n; _ ] -> (Syntax.Obj (Int (Z.to_int n)), [])
| _ -> assert false );
( ("format", [], "default_priority", []),
fun _ _ -> (Syntax.Obj (Format Output.Hex), []) );
( ("format", [ Dyp.Regexp (RE_String "ascii") ], "default_priority", []),
fun _ -> function _ -> (Syntax.Obj (Format Output.Ascii), []) );
( ("format", [ Dyp.Regexp (RE_String "hexa") ], "default_priority", []),
fun _ -> function _ -> (Syntax.Obj (Format Output.Hex), []) );
( ("format", [ Dyp.Regexp (RE_String "bin") ], "default_priority", []),
fun _ -> function _ -> (Syntax.Obj (Format Output.Bin), []) );
( ("format", [ Dyp.Regexp (RE_String "dec") ], "default_priority", []),
fun _ -> function _ -> (Syntax.Obj (Format Output.Dec), []) );
( ("named", [ Dyp.Non_ter ("var", No_priority) ], "default_priority", []),
fun _ -> function
| [ Syntax.Loc ((Loc.Var (name, _), pos) as var) ] ->
(Syntax.Obj (Named ((Expr.loc var, pos), name)), [])
| _ -> assert false );
( ( "named",
[
Dyp.Non_ter ("expr", No_priority);
Dyp.Regexp (RE_String "as");
Dyp.Non_ter ("ident", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [ Syntax.Expr expr; _; Syntax.String alias ] ->
(Syntax.Obj (Named (expr, alias)), [])
| _ -> assert false );
( ( "comma_separated_named_rev_list",
[ Dyp.Non_ter ("named", No_priority) ],
"default_priority",
[] ),
fun _ -> function
| [ Syntax.Obj (Named term) ] ->
(Syntax.Obj (Named_list [ term ]), [])
| _ -> assert false );
( ( "comma_separated_named_rev_list",
[
Dyp.Non_ter ("comma_separated_named_rev_list", No_priority);
Dyp.Regexp (RE_Char ',');
Dyp.Non_ter ("named", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [ Syntax.Obj (Named_list l); _; Syntax.Obj (Named term) ] ->
(Syntax.Obj (Named_list (term :: l)), [])
| _ -> assert false );
( ( "output",
[ Dyp.Regexp (RE_String "print"); Dyp.Regexp (RE_String "formula") ],
"default_priority",
[] ),
fun _ -> function _ -> (Syntax.Obj (Output Output.Formula), []) );
( ( "output",
[
Dyp.Regexp (RE_String "print");
Dyp.Regexp (RE_String "formula");
Dyp.Regexp (RE_String "for");
Dyp.Non_ter ("comma_separated_named_rev_list", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [ _; _; _; Syntax.Obj (Named_list terms) ] ->
(Syntax.Obj (Output (Output.Slice (List.rev terms))), [])
| _ -> assert false );
( ( "output",
[ Dyp.Regexp (RE_String "print"); Dyp.Regexp (RE_String "model") ],
"default_priority",
[] ),
fun _ _ -> (Syntax.Obj (Output Output.Model), []) );
( ( "output",
[
Dyp.Regexp (RE_String "print");
Dyp.Non_ter ("format", No_priority);
Dyp.Non_ter ("expr", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [ _; Syntax.Obj (Format format); Syntax.Expr term ] ->
(Syntax.Obj (Output (Output.Value (format, term))), [])
| _ -> assert false );
( ( "output",
[
Dyp.Regexp (RE_String "print");
Dyp.Regexp (RE_String "ascii");
Dyp.Regexp (RE_String "stream");
Dyp.Non_ter ("ident", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [ _; _; _; Syntax.String name ] ->
(Syntax.Obj (Output (Output.Stream name)), [])
| _ -> assert false );
( ( "output",
[
Dyp.Regexp (RE_String "print");
Dyp.Regexp (RE_String "c");
Dyp.Regexp (RE_String "string");
Dyp.Non_ter ("ident", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [ _; _; _; Syntax.String name ] ->
(Syntax.Obj (Output (Output.String name)), [])
| _ -> assert false );
( ( "and_separated_output_rev_list",
[ Dyp.Non_ter ("output", No_priority) ],
"default_priority",
[] ),
fun _ -> function
| [ Syntax.Obj (Output output) ] ->
(Syntax.Obj (Output_list [ output ]), [])
| _ -> assert false );
( ( "and_separated_output_rev_list",
[
Dyp.Non_ter ("and_separated_output_rev_list", No_priority);
Dyp.Non_ter ("accept_newline", No_priority);
Dyp.Regexp (RE_String "and");
Dyp.Non_ter ("output", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [ Syntax.Obj (Output_list l); _; _; Syntax.Obj (Output output) ]
->
(Syntax.Obj (Output_list (output :: l)), [])
| _ -> assert false );
( ("outputs", [], "default_priority", []),
fun _ -> function _ -> (Syntax.Obj (Output_list []), []) );
( ( "outputs",
[
Dyp.Regexp (RE_String "then");
Dyp.Non_ter ("and_separated_output_rev_list", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [ _; Syntax.Obj (Output_list l) ] ->
(Syntax.Obj (Output_list (List.rev l)), [])
| _ -> assert false );
( ( "qident",
[ Dyp.Non_ter ("ident", No_priority) ],
"default_priority",
[] ),
fun _ -> function
| [ Syntax.String id ] -> (Syntax.String id, [])
| _ -> assert false );
( ("qident", [ Dyp.Ter "CONST" ], "default_priority", []),
fun _ -> function
| [ Syntax.Obj_CONST bv ] ->
(Syntax.String (Bitvector.to_asciistring bv), [])
| _ -> assert false );
( ( "section",
[ Dyp.Non_ter ("qident", No_priority) ],
"default_priority",
[] ),
fun _ -> function
| [ Syntax.String id ] -> (Syntax.String id, [])
| _ -> assert false );
( ( "section",
[ Dyp.Non_ter ("label", No_priority) ],
"default_priority",
[] ),
fun _ -> function
| [ Syntax.String id ] -> (Syntax.String ("." ^ id), [])
| _ -> assert false );
( ( "comma_separated_section_rev_list",
[ Dyp.Non_ter ("section", No_priority) ],
"default_priority",
[] ),
fun _ -> function
| [ Syntax.String section ] ->
(Syntax.Obj (String_list [ section ]), [])
| _ -> assert false );
( ( "comma_separated_section_rev_list",
[
Dyp.Non_ter ("comma_separated_section_rev_list", No_priority);
Dyp.Regexp (RE_Char ',');
Dyp.Non_ter ("section", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [ Syntax.Obj (String_list l); _; Syntax.String section ] ->
(Syntax.Obj (String_list (section :: l)), [])
| _ -> assert false );
( ( "comma_separated_symbol_rev_list",
[ Dyp.Non_ter ("symbol", No_priority) ],
"default_priority",
[] ),
fun _ -> function
| [ Syntax.Symbol sym ] -> (Syntax.Obj (Symbol_list [ sym ]), [])
| _ -> assert false );
( ( "comma_separated_symbol_rev_list",
[
Dyp.Non_ter ("comma_separated_symbol_rev_list", No_priority);
Dyp.Regexp (RE_Char ',');
Dyp.Non_ter ("symbol", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [ Syntax.Obj (Symbol_list l); _; Syntax.Symbol sym ] ->
(Syntax.Obj (Symbol_list (sym :: l)), [])
| _ -> assert false );
( ("arg", [ Dyp.Non_ter ("var", No_priority) ], "default_priority", []),
fun _ -> function
| [ Syntax.Loc var ] -> (Syntax.Obj (Loc_opt (Some var)), [])
| _ -> assert false );
( ("arg", [ Dyp.Regexp (RE_Char '_') ], "default_priority", []),
fun _ _ -> (Syntax.Obj (Loc_opt None), []) );
( ( "comma_separated_arg_rev_list",
[ Dyp.Non_ter ("arg", No_priority) ],
"default_priority",
[] ),
fun _ -> function
| [ Syntax.Obj (Loc_opt arg) ] ->
(Syntax.Obj (Loc_opt_list [ arg ]), [])
| _ -> assert false );
( ( "comma_separated_arg_rev_list",
[
Dyp.Non_ter ("comma_separated_arg_rev_list", No_priority);
Dyp.Regexp (RE_Char ',');
Dyp.Non_ter ("arg", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [ Syntax.Obj (Loc_opt_list l); _; Syntax.Obj (Loc_opt arg) ] ->
(Syntax.Obj (Loc_opt_list (arg :: l)), [])
| _ -> assert false );
( ("arguments", [], "default_priority", []),
fun _ _ -> (Syntax.Stmt [], []) );
( ( "arguments",
[ Dyp.Regexp (RE_Char '('); Dyp.Regexp (RE_Char ')') ],
"default_priority",
[] ),
fun _ _ -> (Syntax.Stmt [], []) );
( ( "arguments",
[
Dyp.Regexp (RE_Char '(');
Dyp.Non_ter ("comma_separated_arg_rev_list", No_priority);
Dyp.Regexp (RE_Char ')');
],
"default_priority",
[] ),
fun _ -> function
| [ _; Syntax.Obj (Loc_opt_list args); _ ] ->
( Syntax.Stmt
(List.mapi
(fun i arg ->
Option.fold ~none:Instr.Nop
~some:(fun name -> Argument (name, i))
arg)
(List.rev args)),
[] )
| _ -> assert false );
( ("expr_opt", [], "default_priority", []),
fun _ _ -> (Syntax.Obj (Expr_opt None), []) );
( ( "expr_opt",
[ Dyp.Non_ter ("expr", No_priority) ],
"default_priority",
[] ),
fun _ -> function
| [ Syntax.Expr expr ] -> (Syntax.Obj (Expr_opt (Some expr)), [])
| _ -> assert false );
( ( "control",
[
Dyp.Regexp (RE_String "return");
Dyp.Non_ter ("expr_opt", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [ _; Syntax.Obj (Expr_opt expr) ] ->
(Syntax.Stmt [ Return expr ], [])
| _ -> assert false );
( ( "comma_separated_expr_rev_list",
[ Dyp.Non_ter ("expr", No_priority) ],
"default_priority",
[] ),
fun _ -> function
| [ Syntax.Expr expr ] -> (Syntax.Obj (Expr_list [ expr ]), [])
| _ -> assert false );
( ( "comma_separated_expr_rev_list",
[
Dyp.Non_ter ("comma_separated_expr_rev_list", No_priority);
Dyp.Regexp (RE_Char ',');
Dyp.Non_ter ("expr", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [ Syntax.Obj (Expr_list l); _; Syntax.Expr expr ] ->
(Syntax.Obj (Expr_list (expr :: l)), [])
| _ -> assert false );
( ("with_stmts_end", [], "default_priority", []),
fun _ _ -> (Syntax.Stmt [], []) );
( ( "with_stmts_end",
[
Dyp.Regexp (RE_String "with");
Dyp.Non_ter ("stmts", No_priority);
Dyp.Regexp (RE_String "end");
],
"default_priority",
[] ),
fun _ -> function
| [ _; Syntax.Stmt stmts; _ ] -> (Syntax.Stmt stmts, [])
| _ -> assert false );
( ("such_that", [], "default_priority", []),
fun _ _ -> (Syntax.Obj (Expr_opt None), []) );
( ( "such_that",
[
Dyp.Regexp (RE_String "such");
Dyp.Regexp (RE_String "that");
Dyp.Non_ter ("expr", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [ _; _; Syntax.Expr guard ] ->
(Syntax.Obj (Expr_opt (Some guard)), [])
| _ -> assert false );
( ("guard", [], "default_priority", []),
fun _ _ -> (Syntax.Obj (Expr_opt None), []) );
( ( "guard",
[ Dyp.Regexp (RE_String "if"); Dyp.Non_ter ("expr", No_priority) ],
"default_priority",
[] ),
fun _ -> function
| [ _; Syntax.Expr guard ] ->
(Syntax.Obj (Expr_opt (Some guard)), [])
| _ -> assert false );
( ( "directive",
[
Dyp.Regexp (RE_String "enumerate");
Dyp.Non_ter ("expr", No_priority);
Dyp.Non_ter ("num", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [ _; Syntax.Expr expr; Syntax.Obj (Int n) ] ->
(Syntax.Stmt [ Enumerate (n, expr) ], [])
| _ -> assert false );
( ( "directive",
[
Dyp.Regexp (RE_String "enumerate");
Dyp.Regexp (RE_Char '*');
Dyp.Non_ter ("expr", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [ _; _; Syntax.Expr expr ] ->
(Syntax.Stmt [ Enumerate (max_int, expr) ], [])
| _ -> assert false );
( ( "directive",
[
Dyp.Regexp (RE_String "assume"); Dyp.Non_ter ("expr", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [ _; Syntax.Expr expr ] -> (Syntax.Stmt [ Instr.Assume expr ], [])
| _ -> assert false );
( ( "directive",
[
Dyp.Regexp (RE_String "assert"); Dyp.Non_ter ("expr", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [ _; Syntax.Expr expr ] -> (Syntax.Stmt [ Instr.Assert expr ], [])
| _ -> assert false );
( ( "fallthrough",
[ Dyp.Non_ter ("output", No_priority) ],
"default_priority",
[] ),
fun _ -> function
| [ Syntax.Obj (Output output) ] -> (Syntax.Instr (Print output), [])
| _ -> assert false );
( ( "fallthrough",
[
Dyp.Regexp (RE_String "enumerate");
Dyp.Non_ter ("expr", No_priority);
Dyp.Non_ter ("num", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [ _; Syntax.Expr expr; Syntax.Obj (Int n) ] ->
(Syntax.Instr (Enumerate (n, expr)), [])
| _ -> assert false );
( ( "fallthrough",
[
Dyp.Regexp (RE_String "reach");
Dyp.Non_ter ("times", No_priority);
Dyp.Non_ter ("accept_newline", No_priority);
Dyp.Non_ter ("such_that", No_priority);
Dyp.Non_ter ("accept_newline", No_priority);
Dyp.Non_ter ("outputs", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [
_;
Syntax.Obj (Int n);
_;
Syntax.Obj (Expr_opt guard);
_;
Syntax.Obj (Output_list outputs);
] ->
(Syntax.Instr (Reach (n, guard, outputs)), [])
| _ -> assert false );
( ( "fallthrough",
[
Dyp.Regexp (RE_String "cut");
Dyp.Non_ter ("accept_newline", No_priority);
Dyp.Non_ter ("guard", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [ _; _; Syntax.Obj (Expr_opt guard) ] ->
(Syntax.Instr (Cut guard), [])
| _ -> assert false );
( ( "decl",
[
Dyp.Regexp (RE_String "starting");
Dyp.Regexp (RE_String "from");
Dyp.Non_ter ("expr", No_priority);
Dyp.Non_ter ("with_stmts_end", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [ _; _; Syntax.Expr addr; Syntax.Stmt stmts ] ->
(Syntax.Decl (Starting_from (addr, stmts)), [])
| _ -> assert false );
( ( "decl",
[
Dyp.Regexp (RE_String "starting");
Dyp.Regexp (RE_String "from");
Dyp.Regexp (RE_String "core");
Dyp.Non_ter ("with_stmts_end", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [ _; _; _; Syntax.Stmt stmts ] ->
(Syntax.Decl (Starting_from_core stmts), [])
| _ -> assert false );
( ( "decl",
[
Dyp.Regexp (RE_String "load");
Dyp.Regexp (RE_String "section");
Dyp.Non_ter ("section", No_priority);
Dyp.Regexp (RE_String "from");
Dyp.Regexp (RE_String "file");
],
"default_priority",
[] ),
fun _ -> function
| [ _; _; Syntax.String section; _; _ ] ->
(Syntax.Decl (Load_sections [ section ]), [])
| _ -> assert false );
( ( "decl",
[
Dyp.Regexp (RE_String "load");
Dyp.Regexp (RE_String "sections");
Dyp.Non_ter ("comma_separated_section_rev_list", No_priority);
Dyp.Regexp (RE_String "from");
Dyp.Regexp (RE_String "file");
],
"default_priority",
[] ),
fun _ -> function
| [ _; _; Syntax.Obj (String_list sections); _; _ ] ->
(Syntax.Decl (Load_sections sections), [])
| _ -> assert false );
( ( "decl",
[
Dyp.Non_ter ("load", No_priority);
Dyp.Regexp (RE_String "from");
Dyp.Regexp (RE_String "file");
],
"default_priority",
[] ),
fun _ -> function
| [ Syntax.Loc load; _; _ ] -> (Syntax.Decl (Load_data load), [])
| _ -> assert false );
( ( "decl",
[
Dyp.Regexp (RE_String "import");
Dyp.Non_ter ("comma_separated_symbol_rev_list", No_priority);
Dyp.Regexp (RE_String "from");
Dyp.Non_ter ("qident", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [ _; Syntax.Obj (Symbol_list syms); _; Syntax.String file ] ->
(Syntax.Decl (Import_symbols (syms, file)), [])
| _ -> assert false );
( ( "decl",
[
Dyp.Regexp (RE_String "concretize");
Dyp.Regexp (RE_String "stack");
],
"default_priority",
[] ),
fun _ _ ->
Options.Logger.warning
"\"concretize stack\" is deprecated, use \"with concrete stack \
pointer\" instead";
(Syntax.Decl Concretize_stack_pointer, []) );
( ( "decl",
[
Dyp.Regexp (RE_String "with");
Dyp.Regexp (RE_String "concrete");
Dyp.Regexp (RE_String "stack");
Dyp.Regexp (RE_String "pointer");
],
"default_priority",
[] ),
fun _ _ -> (Syntax.Decl Concretize_stack_pointer, []) );
( ( "decl",
[
Dyp.Regexp (RE_String "replace");
Dyp.Regexp (RE_String "opcode");
Dyp.Non_ter ("byte_rev_list", No_priority);
Dyp.Regexp (RE_String "by");
Dyp.Non_ter ("stmts", No_priority);
Dyp.Regexp (RE_String "end");
],
"default_priority",
[] ),
fun _ -> function
| [ _; _; Syntax.Obj (Int_list bytes); _; Syntax.Stmt stmts; _ ] ->
( Syntax.Decl
(Decode (Binstream.of_list (List.rev bytes), stmts)),
[] )
| _ -> assert false );
( ( "decl",
[
Dyp.Regexp (RE_String "hook");
Dyp.Non_ter ("comma_separated_expr_rev_list", No_priority);
Dyp.Non_ter ("arguments", No_priority);
Dyp.Regexp (RE_String "with");
Dyp.Non_ter ("stmts", No_priority);
Dyp.Regexp (RE_String "end");
],
"default_priority",
[] ),
fun _ -> function
| [
_;
Syntax.Obj (Expr_list addr);
Syntax.Stmt args;
_;
Syntax.Stmt stmts;
_;
] ->
(Syntax.Decl (Hook (addr, List.rev_append args stmts, true)), [])
| _ -> assert false );
( ( "decl",
[
Dyp.Regexp (RE_String "replace");
Dyp.Non_ter ("comma_separated_expr_rev_list", No_priority);
Dyp.Non_ter ("arguments", No_priority);
Dyp.Regexp (RE_String "by");
Dyp.Non_ter ("stmts", No_priority);
Dyp.Regexp (RE_String "end");
],
"default_priority",
[] ),
fun _ -> function
| [
_;
Syntax.Obj (Expr_list addr);
Syntax.Stmt args;
_;
Syntax.Stmt stmts;
_;
] ->
( Syntax.Decl (Hook (addr, List.rev_append args stmts, false)),
[] )
| _ -> assert false );
( ( "decl",
[
Dyp.Regexp (RE_String "abort");
Dyp.Regexp (RE_String "at");
Dyp.Non_ter ("comma_separated_expr_rev_list", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [ _; _; Syntax.Obj (Expr_list addr) ] ->
( Syntax.Decl
(Hook
( addr,
[
Instr.dynamic_assert (Expr.zero, Lexing.dummy_pos);
Instr.halt;
],
false )),
[] )
| _ -> assert false );
( ( "decl",
[
Dyp.Regexp (RE_String "halt");
Dyp.Regexp (RE_String "at");
Dyp.Non_ter ("comma_separated_expr_rev_list", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [ _; _; Syntax.Obj (Expr_list addr) ] ->
(Syntax.Decl (Hook (addr, [ Instr.halt ], false)), [])
| _ -> assert false );
( ( "decl",
[ Dyp.Non_ter ("instr", No_priority) ],
"default_priority",
[] ),
fun _ -> function
| [ Syntax.Stmt instr ] -> (Syntax.Decl (Init instr), [])
| _ -> assert false );
( ( "decl",
[ Dyp.Regexp (RE_String "reach"); Dyp.Regexp (RE_String "all") ],
"default_priority",
[] ),
fun _ _ ->
Options.Logger.warning
"\"reach all\" is deprecated, use \"explore all\" instead";
(Syntax.Decl Explore_all, []) );
( ( "decl",
[ Dyp.Regexp (RE_String "explore"); Dyp.Regexp (RE_String "all") ],
"default_priority",
[] ),
fun _ _ -> (Syntax.Decl Explore_all, []) );
( ( "decl",
[
Dyp.Regexp (RE_String "reach");
Dyp.Non_ter ("expr", No_priority);
Dyp.Non_ter ("arguments", No_priority);
Dyp.Non_ter ("accept_newline", No_priority);
Dyp.Non_ter ("times", No_priority);
Dyp.Non_ter ("accept_newline", No_priority);
Dyp.Non_ter ("such_that", No_priority);
Dyp.Non_ter ("accept_newline", No_priority);
Dyp.Non_ter ("outputs", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [
_;
Syntax.Expr addr;
Syntax.Stmt args;
_;
Syntax.Obj (Int n);
_;
Syntax.Obj (Expr_opt guard);
_;
Syntax.Obj (Output_list outputs);
] ->
( Syntax.Decl
(Hook
( [ addr ],
List.rev_append args [ Reach (n, guard, outputs) ],
true )),
[] )
| _ -> assert false );
( ( "decl",
[
Dyp.Regexp (RE_String "reach");
Dyp.Regexp (RE_Char '*');
Dyp.Non_ter ("expr", No_priority);
Dyp.Non_ter ("arguments", No_priority);
Dyp.Non_ter ("accept_newline", No_priority);
Dyp.Non_ter ("such_that", No_priority);
Dyp.Non_ter ("accept_newline", No_priority);
Dyp.Non_ter ("outputs", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [
_;
_;
Syntax.Expr addr;
Syntax.Stmt args;
_;
Syntax.Obj (Expr_opt guard);
_;
Syntax.Obj (Output_list outputs);
] ->
( Syntax.Decl
(Hook
( [ addr ],
List.rev_append args [ Reach (-1, guard, outputs) ],
true )),
[] )
| _ -> assert false );
( ( "decl",
[
Dyp.Regexp (RE_String "cut");
Dyp.Regexp (RE_String "at");
Dyp.Non_ter ("expr", No_priority);
Dyp.Non_ter ("arguments", No_priority);
Dyp.Non_ter ("accept_newline", No_priority);
Dyp.Non_ter ("guard", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [
_;
_;
Syntax.Expr addr;
Syntax.Stmt args;
_;
Syntax.Obj (Expr_opt guard);
] ->
( Syntax.Decl
(Hook ([ addr ], List.rev_append args [ Cut guard ], true)),
[] )
| _ -> assert false );
( ( "decl",
[
Dyp.Regexp (RE_String "at");
Dyp.Non_ter ("expr", No_priority);
Dyp.Non_ter ("arguments", No_priority);
Dyp.Non_ter ("directive", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [ _; Syntax.Expr addr; Syntax.Stmt args; Syntax.Stmt stmts ] ->
( Syntax.Decl (Hook ([ addr ], List.rev_append args stmts, true)),
[] )
| _ -> assert false );
( ( "decl",
[
Dyp.Regexp (RE_String "hook");
Dyp.Non_ter ("symbol", No_priority);
Dyp.Regexp (RE_String "return");
Dyp.Regexp (RE_String "with");
Dyp.Non_ter ("stmts", No_priority);
Dyp.Regexp (RE_String "end");
],
"default_priority",
[] ),
fun _ -> function
| [ _; Syntax.Symbol sym; _; _; Syntax.Stmt stmts; _ ] ->
(Syntax.Decl (Return_hook (sym, stmts)), [])
| _ -> assert false );
( ( "decl",
[
Dyp.Regexp (RE_String "abort");
Dyp.Regexp (RE_String "at");
Dyp.Non_ter ("symbol", No_priority);
Dyp.Regexp (RE_String "return");
],
"default_priority",
[] ),
fun _ -> function
| [ _; _; Syntax.Symbol symbol; _ ] ->
( Syntax.Decl
(Return_hook
( symbol,
[
Instr.dynamic_assert (Expr.zero, Lexing.dummy_pos);
Instr.halt;
] )),
[] )
| _ -> assert false );
( ( "decl",
[
Dyp.Regexp (RE_String "halt");
Dyp.Regexp (RE_String "at");
Dyp.Non_ter ("symbol", No_priority);
Dyp.Regexp (RE_String "return");
],
"default_priority",
[] ),
fun _ -> function
| [ _; _; Syntax.Symbol symbol; _ ] ->
(Syntax.Decl (Return_hook (symbol, [ Instr.halt ])), [])
| _ -> assert false );
( ( "decl",
[
Dyp.Regexp (RE_String "reach");
Dyp.Non_ter ("symbol", No_priority);
Dyp.Regexp (RE_String "return");
Dyp.Non_ter ("accept_newline", No_priority);
Dyp.Non_ter ("times", No_priority);
Dyp.Non_ter ("accept_newline", No_priority);
Dyp.Non_ter ("such_that", No_priority);
Dyp.Non_ter ("accept_newline", No_priority);
Dyp.Non_ter ("outputs", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [
_;
Syntax.Symbol symbol;
_;
_;
Syntax.Obj (Int n);
_;
Syntax.Obj (Expr_opt guard);
_;
Syntax.Obj (Output_list outputs);
] ->
( Syntax.Decl
(Return_hook (symbol, [ Reach (n, guard, outputs) ])),
[] )
| _ -> assert false );
( ( "decl",
[
Dyp.Regexp (RE_String "reach");
Dyp.Regexp (RE_Char '*');
Dyp.Non_ter ("symbol", No_priority);
Dyp.Regexp (RE_String "return");
Dyp.Non_ter ("accept_newline", No_priority);
Dyp.Non_ter ("such_that", No_priority);
Dyp.Non_ter ("accept_newline", No_priority);
Dyp.Non_ter ("outputs", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [
_;
_;
Syntax.Symbol symbol;
_;
_;
Syntax.Obj (Expr_opt guard);
_;
Syntax.Obj (Output_list outputs);
] ->
( Syntax.Decl
(Return_hook (symbol, [ Reach (-1, guard, outputs) ])),
[] )
| _ -> assert false );
( ( "decl",
[
Dyp.Regexp (RE_String "cut");
Dyp.Regexp (RE_String "at");
Dyp.Non_ter ("symbol", No_priority);
Dyp.Regexp (RE_String "return");
Dyp.Non_ter ("accept_newline", No_priority);
Dyp.Non_ter ("guard", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [ _; _; Syntax.Symbol symbol; _; _; Syntax.Obj (Expr_opt guard) ]
->
(Syntax.Decl (Return_hook (symbol, [ Cut guard ])), [])
| _ -> assert false );
( ("rev_program", [], "default_priority", []),
fun _ _ -> (Syntax.Program [], []) );
( ( "rev_program",
[
Dyp.Non_ter ("rev_program", No_priority);
Dyp.Non_ter ("decl", No_priority);
Dyp.Non_ter ("separator", No_priority);
],
"default_priority",
[] ),
fun _ -> function
| [ Syntax.Program rev_program; Syntax.Decl decl; _ ] ->
(Syntax.Program (decl :: rev_program), [ Dyp.Keep_grammar ])
| _ -> assert false );
( ( "program",
[ Dyp.Non_ter ("rev_program", No_priority); Dyp.Regexp RE_Eof_char ],
"default_priority",
[] ),
fun _ -> function
| [ Syntax.Program rev_program; _ ] ->
(Syntax.Program (List.rev rev_program), [])
| _ -> assert false );
];
]
let try_and_parse grammar_extensions lexbuf =
let pilot =
Dyp.update_pp (Syntax.pp ()) (List.concat (grammar :: grammar_extensions))
in
match Dyp.lexparse pilot "program" lexbuf with
| [ (Syntax.Program ast, _) ] -> ast
| exception Failure _ ->
let s =
Format.asprintf "@[<v>Probable lexing error %a@]" Parse_utils.pp_pos
(Dyp.lexeme_end_p lexbuf)
in
raise (Parse_utils.UserFriendlyParseError s)
| _ | (exception Dyp.Syntax_error) ->
let s =
Format.asprintf "@[<v>Parse error at word `%s' %a@]" (Dyp.lexeme lexbuf)
Parse_utils.pp_pos (Dyp.lexeme_end_p lexbuf)
in
raise (Parse_utils.UserFriendlyParseError s)
type file_stream = Opened of in_channel * string list | Pending of string list
let rec refill_buf lexbuf file_stream buf len =
match !file_stream with
| Pending [] -> 0
| Pending (filename :: files) -> (
match open_in filename with
| exception Sys_error _ ->
Options.Logger.fatal "Cannot open file %s" filename
| ic ->
file_stream := Opened (ic, files);
Dyp.set_fname (Option.get !lexbuf) filename;
let std_lexbuf = Dyp.std_lexbuf (Option.get !lexbuf) in
Lexing.set_position std_lexbuf
{ pos_fname = filename; pos_lnum = 0; pos_bol = 0; pos_cnum = 0 };
refill_buf lexbuf file_stream buf len)
| Opened (ic, files) -> (
match input ic buf 0 len with
| 0 ->
close_in ic;
file_stream := Pending files;
Bytes.set buf 0 '\n';
1
| n -> n)
let read_files gramma_extensions files =
let file_stream = ref (Pending files) and lexbuf = ref None in
lexbuf :=
Some
(Dyp.from_function (Libparser.Syntax.pp ())
(refill_buf lexbuf file_stream));
Fun.protect
~finally:(fun () ->
match !file_stream with Opened (ic, _) -> close_in ic | _ -> ())
(fun () -> try_and_parse gramma_extensions (Option.get !lexbuf))