File: utils.lisp

package info (click to toggle)
acl2 8.5dfsg-5
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 991,452 kB
  • sloc: lisp: 15,567,759; javascript: 22,820; cpp: 13,929; ansic: 12,092; perl: 7,150; java: 4,405; xml: 3,884; makefile: 3,507; sh: 3,187; ruby: 2,633; ml: 763; python: 746; yacc: 723; awk: 295; csh: 186; php: 171; lex: 154; tcl: 49; asm: 23; haskell: 17
file content (1387 lines) | stat: -rw-r--r-- 60,503 bytes parent folder | download | duplicates (5)
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
; VL 2014 -- VL Verilog Toolkit, 2014 Edition
; Copyright (C) 2008-2015 Centaur Technology
;
; Contact:
;   Centaur Technology Formal Verification Group
;   7600-C N. Capital of Texas Highway, Suite 300, Austin, TX 78731, USA.
;   http://www.centtech.com/
;
; License: (An MIT/X11-style license)
;
;   Permission is hereby granted, free of charge, to any person obtaining a
;   copy of this software and associated documentation files (the "Software"),
;   to deal in the Software without restriction, including without limitation
;   the rights to use, copy, modify, merge, publish, distribute, sublicense,
;   and/or sell copies of the Software, and to permit persons to whom the
;   Software is furnished to do so, subject to the following conditions:
;
;   The above copyright notice and this permission notice shall be included in
;   all copies or substantial portions of the Software.
;
;   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
;   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
;   DEALINGS IN THE SOFTWARE.
;
; Original author: Jared Davis <jared@centtech.com>

(in-package "VL2014")
(include-book "parsestate")
(include-book "../config")
(include-book "../lexer/tokens")
(include-book "../../util/warnings")
(include-book "seq")
(include-book "misc/untranslate-patterns" :dir :system)
(include-book "tools/flag" :dir :system)
(include-book "tools/rulesets" :dir :system)
(local (include-book "../../util/arithmetic"))

(defxdoc parse-utils
  :parents (parser)
  :short "Supporting functions for the parser.")

(local (xdoc::set-default-parents parse-utils))


(defval *vl-default-token*
  (make-vl-plaintoken :type :vl-ws
                      :etext (list (make-vl-echar
                                    :char #\Space
                                    :loc *vl-fakeloc*))))



(define vl-tokenlist-fix ((x vl-tokenlist-p))
  :verify-guards nil
  :inline t
  (mbe :logic
       (if (atom x)
           x
         (cons (if (vl-token-p (car x))
                   (car x)
                 *vl-default-token*)
               (vl-tokenlist-fix (cdr x))))
       :exec x)
  ///
  (defthm vl-tokenlist-p-of-vl-tokenlist-fix
    (vl-tokenlist-p (vl-tokenlist-fix x)))
  (defthm vl-tokenlist-fix-when-vl-tokenlist-p
    (implies (vl-tokenlist-p x)
             (equal (vl-tokenlist-fix x)
                    x)))
  (verify-guards+ vl-tokenlist-fix)
  (defthm len-of-vl-tokenlist-fix
    (equal (len (vl-tokenlist-fix x))
           (len x)))
  (defthm consp-of-vl-tokenlist-fix
    (equal (consp (vl-tokenlist-fix x))
           (consp x)))
  (defthm cdr-of-vl-tokenlist-fix
    (equal (cdr (vl-tokenlist-fix (cons a x)))
           (vl-tokenlist-fix x)))
  (defthm len-of-cdr-of-vl-tokenlist-fix
    (equal (len (cdr (vl-tokenlist-fix x)))
           (len (cdr x)))))



(make-event
 `(defstobj tokstream
    (tokens :type (satisfies vl-tokenlist-p)
            :initially nil)
    (pstate :type (satisfies vl-parsestate-p)
            :initially ,(make-vl-parsestate))
    :inline t
    :non-memoizable t
    :renaming
    ((tokens        vl-tokstream->tokens-raw)
     (pstate        vl-tokstream->pstate-raw)
     (update-tokens vl-tokstream-update-tokens-fn)
     (update-pstate vl-tokstream-update-pstate-fn))))

(define vl-tokstream->tokens (&key (tokstream 'tokstream))
  :returns (tokens vl-tokenlist-p)
  :inline t
  (mbe :logic (vl-tokenlist-fix (vl-tokstream->tokens-raw tokstream))
       :exec (vl-tokstream->tokens-raw tokstream)))

(define vl-tokstream->pstate (&key (tokstream 'tokstream))
  :returns (pstate vl-parsestate-p)
  :inline t
  (mbe :logic (vl-parsestate-fix (vl-tokstream->pstate-raw tokstream))
       :exec (vl-tokstream->pstate-raw tokstream)))

(defmacro vl-tokstream-update-tokens (tokens &key (tokstream 'tokstream))
  `(vl-tokstream-update-tokens-fn ,tokens ,tokstream))

(defmacro vl-tokstream-update-pstate (pstate &key (tokstream 'tokstream))
  `(vl-tokstream-update-pstate-fn ,pstate ,tokstream))

(add-macro-alias vl-tokstream-update-tokens vl-tokstream-update-tokens-fn)
(add-macro-alias vl-tokstream-update-pstate vl-tokstream-update-pstate-fn)

(in-theory (disable vl-tokstream-update-tokens
                    vl-tokstream-update-pstate))

(encapsulate
  ()
  (local (in-theory (enable vl-tokstream->tokens
                            vl-tokstream->pstate
                            vl-tokstream-update-tokens
                            vl-tokstream-update-pstate)))

  (defthm vl-tokstream->tokens-of-vl-tokstream-update-tokens
    (equal (vl-tokstream->tokens :tokstream (vl-tokstream-update-tokens new-tokens))
           (vl-tokenlist-fix new-tokens)))

  (defthm vl-tokstream->pstate-of-vl-tokstream-update-tokens
    (equal (vl-tokstream->pstate :tokstream (vl-tokstream-update-tokens new-tokens))
           (vl-tokstream->pstate :tokstream tokstream)))

  (defthm vl-tokstream->tokens-of-vl-tokstream-update-pstate
    (equal (vl-tokstream->tokens :tokstream (vl-tokstream-update-pstate new-pstate))
           (vl-tokstream->tokens :tokstream tokstream)))

  (defthm vl-tokstream->pstate-of-vl-tokstream-update-pstate
    (equal (vl-tokstream->pstate :tokstream (vl-tokstream-update-pstate new-pstate))
           (vl-parsestate-fix new-pstate))))

(defmacro vl-tokstream-measure (&key (tokstream 'tokstream))
  `(len (vl-tokstream->tokens :tokstream ,tokstream)))

(define vl-tokstream-fix (&key (tokstream 'tokstream))
  (mbe :logic (b* ((tokens (vl-tokstream->tokens))
                   (pstate (vl-tokstream->pstate)))
                (non-exec
                 (list tokens pstate)))
       :exec tokstream)
  :prepwork
  ((local (in-theory (enable vl-tokstream->pstate
                             vl-tokstream->tokens))))
  ///
  (defthm vl-tokstream->pstate-of-vl-tokstream-fix
    (equal (vl-tokstream->pstate :tokstream (vl-tokstream-fix))
           (vl-tokstream->pstate)))

  (defthm vl-tokstream->tokens-of-vl-tokstream-fix
    (equal (vl-tokstream->tokens :tokstream (vl-tokstream-fix))
           (vl-tokstream->tokens)))

  (defthm vl-tokstream-measure-of-vl-tokstream-fix
    (equal (vl-tokstream-measure :tokstream (vl-tokstream-fix))
           (vl-tokstream-measure))))


(defaggregate vl-tokstream-backup
  :tag nil
  :layout :fulltree
  ((tokens vl-tokenlist-p)
   (pstate vl-parsestate-p)))

(define vl-tokstream-save (&key (tokstream 'tokstream))
  :returns (backup vl-tokstream-backup-p)
  (make-vl-tokstream-backup :tokens (vl-tokstream->tokens)
                            :pstate (vl-tokstream->pstate))
  ///
  (defthm vl-tokstream-backup->tokens-of-vl-tokstream-save
    (equal (vl-tokstream-backup->tokens (vl-tokstream-save))
           (vl-tokstream->tokens))))

(define vl-tokstream-restore ((backup vl-tokstream-backup-p) &key (tokstream 'tokstream))
  (b* (((vl-tokstream-backup backup))
       ;; Bah.  We don't really want to rebuild the usertypes hash table if it
       ;; hasn't changed.  But we do want to free the old one, if it has.  So,
       ;; do a dumb hack.  This EQUAL check should usually be very fast, we
       ;; think.
       (curr-pstate (vl-tokstream->pstate))
       (- (if (equal (vl-parsestate->usertypes curr-pstate)
                     (vl-parsestate->usertypes backup.pstate))
              nil
            (vl-parsestate-free curr-pstate)))
       (new-pstate (vl-parsestate-restore backup.pstate))
       (tokstream (vl-tokstream-update-pstate new-pstate))
       (tokstream (vl-tokstream-update-tokens backup.tokens)))
    (vl-tokstream-fix))
  ///
  (local (in-theory (enable vl-tokstream->tokens
                            vl-tokstream->pstate
                            vl-tokstream-update-tokens
                            vl-tokstream-update-pstate
                            vl-tokstream-fix
                            vl-tokstream-save
                            vl-parsestate-restore)))
  (defthm vl-tokstream-restore-of-vl-tokstream-save
    (equal (vl-tokstream-restore (vl-tokstream-save) :tokstream anything)
           (vl-tokstream-fix))))


(define vl-tokstream-add-warning ((warning vl-warning-p)
                                  &key (tokstream 'tokstream))
  :returns (new-tokstream)
  (b* ((pstate (vl-tokstream->pstate))
       (pstate (vl-parsestate-add-warning warning pstate))
       (tokstream (vl-tokstream-update-pstate pstate)))
    tokstream)
  ///
  (defthm vl-tokstream->tokens-of-vl-tokstream-add-warning
    (equal (vl-tokstream->tokens :tokstream (vl-tokstream-add-warning warning))
           (vl-tokstream->tokens))))


(defxdoc defparser
  :short "Core macro for writing recursive-descent parsers, used throughout
VL's parsing routines."
  :long "<h3>General Form</h3>

<h3>BOZO warning this documentation is very out of date</h3>

@({
     (defparser name (x y)

        [:guard (blah x y)]
        [:verify-guards { t, nil }]
        [:count { weak, strong, strong-on-value }]
        [:result (blah-blah val)]
        [:resultp-of-nil { t, nil }]
        [:true-listp { t, nil }]
        [:fails { never, gracefully }]
        [:result-hints ((\"Goal\" ...))]

        [(declare ...)]
        [(declare ...)]
        <body>             ;; usually (seq tokens pstate ...)
        )

<p>Such an event always introduces:</p>

<ul>
<li>A macro, (foo x y), that just calls (foo-fn x y tokstream).</li>

<li>A function, (foo-fn x y tokstream), that implicitly binds the variable
    __function__ to 'foo and otherwise has the given bodies and declares, with
    the :guard thrown in if that is provided.</li>

<li>An add-macro-alias so foo can be used in place of foo-fn in enables and
    disables.</li>

<li>An untranslate-pattern so that (foo-fn x y tokstream) is displayed as
    (foo x y) during theorems.</li.

</ul>

<p>In addition to these events, several theorems may be generated depending
upon the optional keyword arguments given above.  In particular, if any of the
following keywords are provided, then some theorems will be generated:</p>

@({ result, resultp-of-nil, true-listp, count, fails })

<p>The RESULT theorem will forcibly assume (vl-tokenlist-p tokens), and you can
 also sort of assume that no error has occurred (the actual rule we produce is
 influenced by the RESULTP-OF-NIL setting).  All you have to say is the
 property you want to establish, targetting the variable VAL.</p>

<p>The TRUE-LISTP flag may be set when foo unconditionally returns a true-listp,
 and is only used to generate a type-prescription rule for foo.</p>

<p>The FAILS keyword can be used to introduce a theorem about the failure
behavior of a parsing function.  Recall that we have standardized upon the (mv
err val tokens) format for all of our parsing functions.  We say that our
functions fail GRACEFULLY if, whenever they return a non-nil err, the val
returned is nil.  Almost all of our functions fail gracefully, and we can
sometimes exploit this fact along with resultp-of-nil to create stronger
rewrite rules for the result theorem.  Another common paradigm is a parser that
NEVER fails -- e.g., perhaps it is for an \"optional\" production or for \"zero
or more occurrences\" of something.</p>

<p>The COUNT theorem is used to create a theorem about (len tokens).  In
practice, most parsers have a STRONG count-decreasing behavior, which is to say
that they always decrease the length on success.  But functions which never
fail, such as \"match zero or more\" parsers, usually have a weaker property
which we call STRONG-ON-VALUE, which means that whenever the value they return
is non-nil, the count decreases.  We also allow for WEAK count theorems, which
just say the count never increases, but these do not occur very
frequently.</p>")

(defun wrap-list-in-forces (x)
  (if (consp x)
      (cons `(force ,(car x))
            (wrap-list-in-forces (cdr x)))
    nil))

(defun adjust-guard-for-theorems (x)
  (if (and (consp x)
           (eq (car x) 'and))
      `(and . ,(wrap-list-in-forces (cdr x)))
    `(force ,x)))

(defun expand-and-maybe-induct-hint-fn
  (fncall world id disables enables)
  (and (not (cdr (car id))) ;; not under a top-level induction
       (let* ((fn (car fncall))
              ;; JCD: sometimes I ran into trouble with the expand hint not
              ;; working right because of formals other than "tokens" being
              ;; simplified into constants.  I now let them be free.

              ;; After adding token fixing, I found that I could run into weird
              ;; loops due to (vl-warninglist-fix warnings) and
              ;; (vl-tokenlist-fix tokens) being around.  To try to avoid this,
              ;; I no longer let warnings be free, either.
              (frees (set-difference-eq (cdr fncall) '(tokstream)))
              (recursivep (acl2::getprop
                           fn
                           'acl2::recursivep nil
                           'acl2::current-acl2-world
                           world)))
         (if recursivep
             `(:expand ((:free ,frees ,fncall))
                       :in-theory (e/d ((:induction ,fn) . ,enables)
                                       ((:definition ,fn) . ,disables))
                       :induct ,fncall)
           `(:in-theory (e/d ,enables
                             ((:definition ,fn) . ,disables))
             :expand ((:free ,frees ,fncall)))))))

(defmacro expand-and-maybe-induct-hint
  (fncall &key disable enable)
  `(expand-and-maybe-induct-hint-fn ,fncall world id ,disable ,enable))

(def-ruleset! defparser-type-prescriptions
  ;; This is a ruleset for type-prescription rules for each defparser we
  ;; introduce.  Normally these rules are not helpful, but they are needed for
  ;; the guard verification of mv-lets.
  nil)

(defun defparser-fn (name formals args single-p)
  (declare (xargs :guard (and (symbolp name)
                              (true-listp formals)
                              ;; Tokstream and config will be provided automatically
                              ;; We also prohibit tokens and pstate for historic reasons
                              (not (member 'tokens formals))
                              (not (member 'pstate formals))
                              (not (member 'config formals))
                              (not (member 'tokstream formals)))
                  :mode :program))
  (b* ((fn-name
        ;; BOZO want to use defguts or something instead
        (intern-in-package-of-symbol (cat (symbol-name name) "-FN") name))
       (fn-formals
        (append formals '(tokstream config)))

       ((mv args rest-events) (std::split-/// name args))

       (args-for-def   (throw-away-keyword-parts args))
       (decls          (butlast args-for-def 1))
       (body           (car (last args)))
       (count          (cdr (extract-keyword-from-args :count args)))
       (result         (cdr (extract-keyword-from-args :result args)))
       (result-hints   (cdr (extract-keyword-from-args :result-hints args)))
       (resultp-of-nil (extract-keyword-from-args :resultp-of-nil args))
       (true-listp     (cdr (extract-keyword-from-args :true-listp args)))
       (fails          (cdr (extract-keyword-from-args :fails args)))
       (guard          (extract-keyword-from-args :guard args))
       (guard-debug    (extract-keyword-from-args :guard-debug args))
       (prepwork       (extract-keyword-from-args :prepwork args))

       (parents        (extract-keyword-from-args :parents args))
       (short          (extract-keyword-from-args :short args))
       (long           (extract-keyword-from-args :long args))

       (measure        (or (cdr (extract-keyword-from-args :measure args))
                           '(vl-tokstream-measure :tokstream tokstream)))
       (hint-chicken-switch (cdr (extract-keyword-from-args
                                  :hint-chicken-switch args)))
       (verify-guards  (extract-keyword-from-args :verify-guards args))
       (thm-hyps       (if guard
                           `(and ;(force (vl-tokenlist-p tokens))
                                 ,(adjust-guard-for-theorems (cdr guard)))
                         `(and ;(force (vl-tokenlist-p tokens))
                           t)))
       (define-form    `(define ,name
                          ,(append formals
                                   '(&key
                                     (tokstream 'tokstream)
                                     ((config vl-loadconfig-p) 'config)
                                     ))
                          :returns (mv (errmsg?)
                                       (value)
                                       (new-tokstream))
                          ,@(and parents       `(:parents ,(cdr parents)))
                          ,@(and short         `(:short ,(cdr short)))
                          ,@(and long          `(:long ,(cdr long)))
                          ,@(and guard         `(:guard ,(cdr guard)))
                          ,@(and guard-debug   `(:guard-debug ,(cdr guard-debug)))
                          ,@(and verify-guards `(:verify-guards ,(cdr verify-guards)))
                          ,@(and prepwork      `(:prepwork ,(cdr prepwork)))
                          :measure ,measure
                          ,@decls
                          (declare (ignorable config))
                          ;; (b* ((pstate (mbe :logic (vl-parsestate-fix pstate)
                          ;;                   :exec pstate))
                          ;;      (tokens   (mbe :logic (vl-tokenlist-fix tokens)
                          ;;                     :exec tokens)))
                          ;;   ,body)
                          ,body
                          ///

                          (ACL2::add-to-ruleset defparser-type-prescriptions '((:t ,name)))

                          ;; (defthm ,(intern-in-package-of-symbol
                          ;;           (cat (symbol-name name) "-OF-VL-TOKENLIST-FIX")
                          ;;           name)
                          ;;   (equal (,fn-name . ,(subst '(vl-tokenlist-fix tokens) 'tokens fn-formals))
                          ;;          (,fn-name . ,fn-formals))
                          ;;   :hints(("Goal"
                          ;;           :expand ((,fn-name . ,(subst '(vl-tokenlist-fix tokens)
                          ;;                                        'tokens fn-formals))
                          ;;                    (,fn-name . ,fn-formals)))))

                          ;; (defthm ,(intern-in-package-of-symbol
                          ;;           (cat (symbol-name name) "-OF-VL-PARSESTATE-FIX")
                          ;;           name)
                          ;;   (equal (,fn-name . ,(subst '(vl-parsestate-fix pstate)
                          ;;                              'pstate fn-formals))
                          ;;          (,fn-name . ,fn-formals))
                          ;;   :hints(("Goal"
                          ;;           :expand ((,fn-name . ,(subst '(vl-parsestate-fix pstate)
                          ;;                                        'pstate fn-formals))
                          ;;                    (,fn-name . ,fn-formals)))))

                          ;; ,@(if (not (or count result resultp-of-nil result-hints true-listp fails))
                          ;;       nil
                          ;;     `((defthm ,(intern-in-package-of-symbol
                          ;;                 (cat "VL-TOKENLIST-P-OF-" (symbol-name name))
                          ;;                 name)
                          ;;         (vl-tokenlist-p (mv-nth 2 (,name . ,formals)))
                          ;;         :hints (,@(if hint-chicken-switch
                          ;;                       nil
                          ;;                     `((expand-and-maybe-induct-hint
                          ;;                        '(,fn-name . ,fn-formals))))))

                          ;;       (defthm ,(intern-in-package-of-symbol
                          ;;                 (cat "VL-PARSESTATE-P-OF-" (symbol-name name))
                          ;;                 name)
                          ;;         (vl-parsestate-p (mv-nth 3 (,name . ,formals)))
                          ;;         :hints (,@(if hint-chicken-switch
                          ;;                       nil
                          ;;                     `((expand-and-maybe-induct-hint
                          ;;                        '(,fn-name . ,fn-formals))))))))

                          ,@(cond ((not fails)
                                   nil)
                                  ((equal (symbol-name fails) "NEVER")
                                   `((defthm ,(intern-in-package-of-symbol
                                               (cat (symbol-name name) "-NEVER-FAILS")
                                               name)
                                       (not (mv-nth 0 (,name . ,formals)))
                                       :hints(,@(if hint-chicken-switch
                                                    '(("Goal" :in-theory (disable (force))))
                                                  `((expand-and-maybe-induct-hint
                                                     '(,fn-name . ,fn-formals) :disable '((force)))))))))
                                  ((equal (symbol-name fails) "GRACEFULLY")
                                   `((defthm ,(intern-in-package-of-symbol
                                               (cat (symbol-name name) "-FAILS-GRACEFULLY")
                                               name)
                                       (implies (mv-nth 0 (,name . ,formals))
                                                (not (mv-nth 1 (,name . ,formals))))
                                       :hints(,@(if hint-chicken-switch
                                                    '(("Goal" :in-theory (disable (force))))
                                                  `((expand-and-maybe-induct-hint
                                                     '(,fn-name . ,fn-formals) :disable '((force)))))))
                                     (defthm ,(intern-in-package-of-symbol
                                               (cat "VL-WARNING-P-OF-" (symbol-name name))
                                               name)
                                       (iff (vl-warning-p (mv-nth 0 (,name . ,formals)))
                                            (mv-nth 0 (,name . ,formals)))
                                       :hints(,@(if hint-chicken-switch
                                                    '(("Goal" :in-theory (disable (force))))
                                                  `((expand-and-maybe-induct-hint
                                                     '(,fn-name . ,fn-formals) :disable '((force)))))))))
                                  (t
                                   (er hard? 'defparser "Bad :fails: ~s0." fails)))

                          ,@(cond ((not result)
                                   nil)
                                  (t
                                   `((defthm ,(intern-in-package-of-symbol
                                               (cat (symbol-name name) "-RESULT")
                                               name)
                                       ,(cond
                                         ((and resultp-of-nil
                                               (cdr resultp-of-nil)
                                               (equal (symbol-name fails) "GRACEFULLY"))
                                          ;; On failure val is nil, and resultp is true of
                                          ;; nil, so resultp is always true of val.
                                          `(implies ,thm-hyps
                                                    ,(ACL2::subst `(mv-nth 1 (,name . ,formals))
                                                                  'val result)))

                                         ((and resultp-of-nil
                                               (not (cdr resultp-of-nil))
                                               (equal (symbol-name fails) "GRACEFULLY"))
                                          ;; Resultp is not true of nil, and fails
                                          ;; gracefully, so result is true exactly when
                                          ;; it does not fail.
                                          `(implies ,thm-hyps
                                                    (equal ,(ACL2::subst
                                                             `(mv-nth 1 (,name . ,formals))
                                                             'val result)
                                                           (not (mv-nth 0 (,name . ,formals))))))

                                         (t
                                          ;; We don't know enough to make the theorem
                                          ;; better, so just make it conditional.
                                          `(implies (and (not (mv-nth 0 (,name . ,formals)))
                                                         ,thm-hyps)
                                                    ,(ACL2::subst
                                                      `(mv-nth 1 (,name . ,formals))
                                                      'val result))))
                                       :hints (,@(if hint-chicken-switch
                                                     nil
                                                   `((expand-and-maybe-induct-hint
                                                      '(,fn-name . ,fn-formals))))
                                               . ,result-hints)))))

                          ,@(cond ((not true-listp)
                                   nil)
                                  (t
                                   `((defthm ,(intern-in-package-of-symbol
                                               (cat (symbol-name name) "-TRUE-LISTP")
                                               name)
                                       (true-listp (mv-nth 1 (,name . ,formals)))
                                       :rule-classes :type-prescription
                                       :hints(,@(if hint-chicken-switch
                                                    '(("Goal" :in-theory (disable (force))))
                                                  `((expand-and-maybe-induct-hint
                                                     '(,fn-name . ,fn-formals)
                                                     :disable '((force))))))))))

                          ,@(cond ((not count)
                                   nil)
                                  ((equal (symbol-name count) "WEAK")
                                   `((defthm ,(intern-in-package-of-symbol
                                               (cat (symbol-name name) "-COUNT-WEAK")
                                               name)
                                       (<= (vl-tokstream-measure :tokstream (mv-nth 2 (,name . ,formals)))
                                           (vl-tokstream-measure))
                                       :rule-classes ((:rewrite) (:linear))
                                       :hints(,@(if hint-chicken-switch
                                                    '(("Goal" :in-theory (disable (force))))
                                                  `((expand-and-maybe-induct-hint
                                                     '(,fn-name . ,fn-formals)
                                                     :disable '((force)))))))))

                                  ((equal (symbol-name count) "STRONG")
                                   `((defthm ,(intern-in-package-of-symbol
                                               (cat (symbol-name name) "-COUNT-STRONG")
                                               name)
                                       (and (<= (vl-tokstream-measure :tokstream (mv-nth 2 (,name . ,formals)))
                                                (vl-tokstream-measure))
                                            (implies (not (mv-nth 0 (,name . ,formals)))
                                                     (< (vl-tokstream-measure :tokstream (mv-nth 2 (,name . ,formals)))
                                                        (vl-tokstream-measure))))
                                       :rule-classes ((:rewrite) (:linear))
                                       :hints(,@(if hint-chicken-switch
                                                    '(("Goal" :in-theory (disable (force))))
                                                  `((expand-and-maybe-induct-hint
                                                     '(,fn-name . ,fn-formals)
                                                     :disable '((force)))))))))

                                  ((equal (symbol-name count) "STRONG-ON-VALUE")
                                   `((defthm ,(intern-in-package-of-symbol
                                               (cat (symbol-name name) "-COUNT-STRONG-ON-VALUE")
                                               name)
                                       (and (<= (vl-tokstream-measure :tokstream (mv-nth 2 (,name . ,formals)))
                                                (vl-tokstream-measure))
                                            (implies (mv-nth 1 (,name . ,formals))
                                                     (< (vl-tokstream-measure :tokstream (mv-nth 2 (,name . ,formals)))
                                                        (vl-tokstream-measure))))
                                       :rule-classes ((:rewrite) (:linear))
                                       :hints(,@(if hint-chicken-switch
                                                    '(("Goal" :in-theory (disable (force))))
                                                  `((expand-and-maybe-induct-hint
                                                     '(,fn-name . ,fn-formals)
                                                     :disable '((force)))))))))

                                  (t
                                   (er hard? 'defparser "Bad :count: ~s0." count)))

                          . ,rest-events)))
    (if single-p
        `(encapsulate
           ()

; The generated parser can sensibly be recursive or non-recursive, and in the
; recursive case, by default we want to use a particular measure which isn't
; just acl2-count.  So, it seems convenient not to have to give an explicit
; measure when you introduce a recursive function (since we know what measure
; it should be), and it seems like it should not be an error to introduce a
; non-recursive parser.  Given all of that, we turn off the default check for
; recursion when providing a :measure.

           (set-bogus-measure-ok t)
           ,define-form)
      define-form)))

(defmacro defparser (name formals &rest args)
  (defparser-fn name formals args t))

(defun defparser-top-fn (name guard resulttype state)
  (declare (xargs :mode :program :stobjs state))
  (b* ((wrld (w state))
       (fnname (acl2::deref-macro-name name (acl2::macro-aliases wrld)))
       (formals (acl2::formals fnname wrld))
       (formals (set-difference-eq formals '(tokstream config))))
    `(define ,(intern-in-package-of-symbol (cat (symbol-name name) "-TOP") name)
       ,(append formals '(&key ((tokens vl-tokenlist-p) 'tokens)
                               ((pstate vl-parsestate-p) 'pstate)
                               ((config vl-loadconfig-p) 'config)))
       :guard ,guard
       :returns (mv erp
                    (result . ,(and resulttype `((implies (and (not erp) ,guard)
                                                          (,resulttype result)))))
                    (tokens vl-tokenlist-p)
                    (pstate vl-parsestate-p))
       (b* (((acl2::local-stobjs tokstream)
             (mv erp result tokens pstate tokstream))
            (tokstream (vl-tokstream-update-tokens tokens))
            (tokstream (vl-tokstream-update-pstate pstate))
            ((mv erp result tokstream) (,name . ,formals)))
         (mv erp result (vl-tokstream->tokens) (vl-tokstream->pstate) tokstream)))))

(defmacro defparser-top (name &key (guard 't) resulttype)
  `(make-event (defparser-top-fn ',name ',guard ',resulttype state)))

(define expand-defparsers (forms)
  ;; For mutually recursive parsers.  FORMS should be a list of defparser
  ;; invocations, e.g.,: ((DEFPARSER FOO ...) ... (DEFPARSER BAR ...))
  :mode :program
  (b* (((when (atom forms))
        nil)
       (form1 (car forms))
       ((when (eq form1 '///))
        ;; Pass any /// part transparently through to DEFINES
        forms)
       ((when (and (keywordp form1)
                   (consp (cdr forms))))
        ;; Pass any :key val pairs transparently through to DEFINES
        (list* form1
               (second forms)
               (expand-defparsers (rest-n 2 forms))))
       ((unless (and (< 3 (len form1))
                     (equal (first form1) 'defparser)))
        (raise "Expected defparser forms, but found ~x0." form1))
       (name1    (second form1))
       (formals1 (third form1))
       (args1    (rest-n 3 form1)))
    (cons (defparser-fn name1 formals1 args1 nil)
          (expand-defparsers (cdr forms)))))

(defmacro defparsers (name &rest defparser-forms)
  ;; (defparsers expr (defparser parse-expr ...) (defparser parse-exprlist ...))
  `(encapsulate ; See the comment on the encapsulate in defparser-fn
     ()
     (set-bogus-measure-ok t)
     (defines ,name . ,(expand-defparsers defparser-forms))))


(define vl-is-token?
  :short "Check whether the current token has a particular type."
  ((type "Type of token to match.")
   &key (tokstream 'tokstream))
  :returns bool
  :inline t
  (b* ((tokens (vl-tokstream->tokens)))
    (and (consp tokens)
         (eq (vl-token->type (car tokens)) type)))
  ///
  ;; (defthm vl-is-token?-of-vl-tokenlist-fix
  ;;   (equal (vl-is-token? type :tokens (vl-tokenlist-fix tokens))
  ;;          (vl-is-token? type)))

  (defthm vl-is-token?-fn-when-atom-of-tokens
    (implies (atom (vl-tokstream->tokens))
             (equal (vl-is-token? type)
                    nil))
    ;; I found this rule to be expensive as a rewrite, so making it a
    ;; type-prescription instead
    :rule-classes :type-prescription))


(define vl-is-some-token?
  :short "Check whether the current token is one of some particular types."
  ((types true-listp)
   &key (tokstream 'tokstream))
  :returns bool
  :inline t
  (b* ((tokens (vl-tokstream->tokens)))
    (and (consp tokens)
         (member-eq (vl-token->type (car tokens)) types)
         t))
  ///
  (defthm vl-is-some-token?-when-atom-of-tokens
    (implies (atom (vl-tokstream->tokens))
             (equal (vl-is-some-token? type)
                    nil)))

  (defthm vl-is-some-token?-when-atom-of-types
    (implies (atom types)
             (equal (vl-is-some-token? types)
                    nil)))

  ;; (defthm vl-is-some-token?-of-vl-tokenlist-fix
  ;;   (equal (vl-is-some-token? types :tokens (vl-tokenlist-fix tokens))
  ;;          (vl-is-some-token? types)))
  )


(define vl-lookahead-is-token?
  :short "Check whether the current token has a particular type."
  ((type   "Type of token to match.")
   (tokens vl-tokenlist-p))
  :returns bool
  :inline t
  (and (consp tokens)
       (eq (vl-token->type (car tokens)) type))
  ///
  (defthm vl-lookahead-is-token?-fn-when-atom-of-tokens
    (implies (atom tokens)
             (equal (vl-lookahead-is-token? type tokens)
                    nil))
    ;; I found this rule to be expensive as a rewrite, so making it a
    ;; type-prescription instead
    :rule-classes :type-prescription))


(define vl-lookahead-is-some-token?
  :short "Check whether the current token is one of some particular types."
  ((types  true-listp)
   (tokens vl-tokenlist-p))
  :returns bool
  :inline t
  (and (consp tokens)
       (member-eq (vl-token->type (car tokens)) types)
       t)
  ///
  (defthm vl-lookahead-is-some-token?-when-atom-of-tokens
    (implies (atom tokens)
             (equal (vl-lookahead-is-some-token? type tokens)
                    nil)))

  (defthm vl-lookahead-is-some-token?-when-atom-of-types
    (implies (atom types)
             (equal (vl-lookahead-is-some-token? types tokens)
                    nil))))


(define vl-parse-error
  :short "Compatible with @(see seq).  Produce an error message (stopping
execution) that includes the current location."
  ((description stringp "Short description of what went wrong.")
   &key
   ((function symbolp) '__function__)
   (tokstream 'tokstream))
  :returns
  (mv (errmsg   vl-warning-p)
      (value    "Always just @('nil')." (equal value nil))
      (new-tokstream (equal new-tokstream tokstream)))
  (b* ((tokens (vl-tokstream->tokens)))
    (mv (make-vl-warning :type :vl-parse-error
                         :msg "Parse error at ~a0: ~s1"
                         :args (list (if (consp tokens)
                                         (vl-token->loc (car tokens))
                                       "EOF")
                                     description)
                         :fn function
                         :fatalp t)
        nil tokstream))
  ///
  (more-returns (errmsg (iff errmsg t)
                        :name vl-parse-error-0-under-iff)))

(define vl-parse-warning
  :short "Compatible with @(see seq).  Produce a non-fatal warning (not an
error, doesn't stop execution) that includes the current location."
  ((type        symbolp "Type for this warning.")
   (description stringp "Short message about what happened.")
   &key
   ((function symbolp) '__function__)
   (tokstream 'tokstream))
  :returns
  (mv (errmsg (not errmsg) "Never produces an error.")
      (value  (not value)  "Value is always @('nil').")
      (new-tokstream))
  (b* ((tokens  (vl-tokstream->tokens))
       (warning (make-vl-warning :type type
                                 :msg "At ~a0: ~s1"
                                 :args (list (if (consp tokens)
                                                 (vl-token->loc (car tokens))
                                               "EOF")
                                             description)
                                 :fn function
                                 :fatalp nil))
       (tokstream (vl-tokstream-add-warning warning)))
    (mv nil nil tokstream))
  ///
  (more-returns
   (new-tokstream (equal (vl-tokstream->tokens :tokstream new-tokstream)
                         (vl-tokstream->tokens))
                  :name vl-tokstream->tokens-of-vl-parse-warning)))


(defmacro vl-unimplemented ()
  `(vl-parse-error "Unimplemented Verilog production."))

(define vl-match-token
  :short "Compatible with @(see seq).  Consume and return a token of exactly
some particular type, or cause an error if the desired kind of token is not at
the start of the input stream."
  ((type "Kind of token to match.") ;; BOZO why not a stronger guard?
   &key
   ((function symbolp) '__function__)
   (tokstream 'tokstream))
  :returns
  (mv (errmsg?   (and (iff errmsg? (not (vl-is-token? type)))
                      (iff (vl-warning-p errmsg?) errmsg?)))
      (token     vl-token-p :hyp (vl-is-token? type))
      (new-tokstream))

  (b* ((tokens (vl-tokstream->tokens))
       ((when (atom tokens))
        (vl-parse-error "Unexpected EOF." :function function))
       (token1 (car tokens))
       ((unless (eq type (vl-token->type token1)))
        ;; We want a custom error message, so we don't use vl-parse-error-fn.
        (mv (make-vl-warning :type :vl-parse-error
                             :msg "Parse error at ~a0: expected ~s1 but found ~s2."
                             :args (list (vl-token->loc (car tokens))
                                         type
                                         (vl-token->type token1))
                             :fatalp t
                             :fn function)
            nil tokstream))
       (tokstream (vl-tokstream-update-tokens (cdr tokens))))
    (mv nil token1 tokstream))

  :prepwork
  ((local (in-theory (enable vl-is-token?))))
  ///
  ;; (defthm vl-match-token-of-vl-tokenlist-fix
  ;;   (equal (vl-match-token type :tokens (vl-tokenlist-fix tokens))
  ;;          (vl-match-token type)))

  (defthm vl-match-token-fails-gracefully
    (implies (not (vl-is-token? type))
             (equal (mv-nth 1 (vl-match-token type)) nil)))

  (local (defthmd car-of-vl-tokenlist-under-iff
           (implies (vl-tokenlist-p x)
                    (iff (car x) (consp x)))
           :hints(("Goal" :in-theory (enable vl-tokenlist-p)))))

  (defthm vl-match-token-under-iff
    (iff (mv-nth 1 (vl-match-token type))
         (vl-is-token? type))
    :hints(("Goal" :in-theory (enable car-of-vl-tokenlist-under-iff))))

  (defthm vl-token->type-of-vl-match-token
    (implies (vl-is-token? type)
             (equal (vl-token->type (mv-nth 1 (vl-match-token type)))
                    type)))

  (defthm vl-match-token-count-strong-on-value
    (and (<= (vl-tokstream-measure :tokstream (mv-nth 2 (vl-match-token type)))
             (vl-tokstream-measure))
         (implies (mv-nth 1 (vl-match-token type))
                  (< (vl-tokstream-measure :tokstream (mv-nth 2 (vl-match-token type)))
                     (vl-tokstream-measure))))
    :rule-classes ((:rewrite) (:linear)))

  (defthm equal-of-vl-match-token-count
    (equal (equal (vl-tokstream-measure :tokstream (mv-nth 2 (vl-match-token type)))
                  (vl-tokstream-measure))
           (if (mv-nth 0 (vl-match-token type))
               t
             nil))))


(define vl-match-some-token
  :short "Compatible with @(see seq).  Consume and return a token if it has
one of several types.  Cause an error if the first token isn't one of the
acceptable types."
  ((types true-listp) ;; BOZO why not a stronger guard?
   &key
   ((function symbolp) '__function__)
   (tokstream 'tokstream))
  :returns
  (mv (errmsg?   (and (iff errmsg? (not (vl-is-some-token? types)))
                      (iff (vl-warning-p errmsg?) errmsg?)))
      (token     vl-token-p :hyp (vl-is-some-token? types))
      (new-tokstream))

  (b* ((tokens (vl-tokstream->tokens))
       ((when (atom tokens))
        (vl-parse-error "Unexpected EOF." :function function))
       (token1 (car tokens))
       ((unless (member-eq (vl-token->type token1) types))
        (mv (make-vl-warning :type :vl-parse-error
                             :msg  "Parse error at ~a0: expected one of ~x1, but found ~s2."
                             :args (list (vl-token->loc token1)
                                         types
                                         (vl-token->type token1))
                             :fatalp t
                             :fn function)
            nil tokstream))
       (tokstream (vl-tokstream-update-tokens (cdr tokens))))
    (mv nil token1 tokstream))

  :prepwork
  ((local (in-theory (enable vl-is-some-token?))))

  ///
  (defthm vl-match-some-token-fails-gracefully
    (implies (not (vl-is-some-token? types))
             (equal (mv-nth 1 (vl-match-some-token types))
                    nil)))

  (defthm vl-match-some-token-count-strong-on-value
    (and (<= (vl-tokstream-measure :tokstream (mv-nth 2 (vl-match-some-token types)))
             (vl-tokstream-measure))
         (implies (mv-nth 1 (vl-match-some-token types))
                  (< (vl-tokstream-measure :tokstream (mv-nth 2 (vl-match-some-token types)))
                     (vl-tokstream-measure))))
    :rule-classes ((:rewrite) (:linear)))

  (defthm equal-of-vl-match-some-token-count
    (equal (equal (vl-tokstream-measure :tokstream (mv-nth 2 (vl-match-some-token types)))
                  (vl-tokstream-measure))
           (if (mv-nth 0 (vl-match-some-token types))
               t
             nil)))

  ;; (defthm vl-match-some-token-of-vl-tokenlist-fix
  ;;   (equal (vl-match-some-token types :tokens (vl-tokenlist-fix tokens))
  ;;          (vl-match-some-token types)))
  )

(define vl-maybe-match-token
  :short "Compatible with @(see seq).  Consume and return a token if it is of
the given type, but if not, don't consume anything and return nil."
  ((type "Kind of token to match.") ;; BOZO why not a stronger guard?
   &key
   (tokstream 'tokstream))

  :returns
  (mv (errmsg?   (equal errmsg? nil))
      (token     (and (iff (vl-token-p token)
                           (vl-is-token? type))
                      (iff token (vl-is-token? type)))
                 :hints(("Goal" :in-theory (enable car-of-vl-tokenlist-under-iff))))
      (new-tokstream))

  (b* ((tokens (vl-tokstream->tokens))
       ((when (atom tokens)) (mv nil nil tokstream))
       (token1 (car tokens))
       ((unless (eq type (vl-token->type token1)))
        (mv nil nil tokstream))
       (tokstream (vl-tokstream-update-tokens (cdr tokens))))
    (mv nil token1 tokstream))

  :prepwork
  ((local (in-theory (enable vl-is-token?)))
   (local (defthmd car-of-vl-tokenlist-under-iff
            (implies (vl-tokenlist-p x)
                     (iff (car x) (consp x)))
            :hints(("Goal" :in-theory (enable vl-tokenlist-p))))))
  ///
  ;; (defthm vl-match-token-of-vl-tokenlist-fix
  ;;   (equal (vl-match-token type :tokens (vl-tokenlist-fix tokens))
  ;;          (vl-match-token type)))

  (defthm vl-maybe-match-token-fails-gracefully
    (implies (not (vl-is-token? type))
             (equal (mv-nth 1 (vl-maybe-match-token type)) nil)))

  (defthm vl-token->type-of-vl-maybe-match-token
    (implies (vl-is-token? type)
             (equal (vl-token->type (mv-nth 1 (vl-maybe-match-token type)))
                    type)))

  (defthm vl-maybe-match-token-count-strong-on-value
    (and (<= (vl-tokstream-measure :tokstream (mv-nth 2 (vl-maybe-match-token type)))
             (vl-tokstream-measure))
         (implies (mv-nth 1 (vl-maybe-match-token type))
                  (< (vl-tokstream-measure :tokstream (mv-nth 2 (vl-maybe-match-token type)))
                     (vl-tokstream-measure))))
    :rule-classes ((:rewrite) (:linear))))



(define vl-type-of-matched-token
  :short "Reasoning trick.  Alias for the type of the token returned by @(see
vl-match-some-token)."
  ((types true-listp)
   (tokens vl-tokenlist-p))

  :long "<p>We added this when we disabled the functions @(see
vl-is-some-token?) and @(see vl-match-some-token) to address the problem of
reasoning about which kind of token was matched.  We could prove, for instance,
that</p>

@({
    (implies (vl-is-some-token? types tokens)
             (member (mv-nth 1 (vl-match-some-token types))
                     types))
})

<p>But it was difficult to get ACL2 to actually make use of this rule, because
the @('member') term never arose.</p>

<p>Instead, we use this new alias so that we can rewrite</p>

@({
     (equal (mv-nth 1 (vl-match-some-token types))
            (vl-type-of-matched-tokens types tokens))
})"

  (b* ((tokens (vl-tokenlist-fix tokens)))
    (if (and (consp tokens)
             (member-eq (vl-token->type (car tokens)) types))
        (vl-token->type (car tokens))
      nil))

  ///
  (local (in-theory (enable vl-match-some-token)))

  (defthm vl-token->type-of-vl-match-some-token
    (equal (vl-token->type (mv-nth 1 (vl-match-some-token types)))
           (vl-type-of-matched-token types (vl-tokstream->tokens))))

  (defthm vl-type-of-matched-token-when-atom-of-types
    (implies (atom types)
             (equal (vl-type-of-matched-token types tokens)
                    nil)))

  (defthm vl-type-of-matched-token-when-atom-of-tokens
    (implies (atom tokens)
             (equal (vl-type-of-matched-token types tokens)
                    nil)))

  (defthm vl-type-of-matched-token-of-vl-tokenlist-fix
    (equal (vl-type-of-matched-token types (vl-tokenlist-fix tokens))
           (vl-type-of-matched-token types tokens))))



;; Experimental rules to kick ass.

(defthm magically-reduce-possible-types-from-vl-is-some-token?
  (implies (and (not (equal (vl-type-of-matched-token types2 (vl-tokstream->tokens)) exclude))
                (syntaxp (quotep types))
                (syntaxp (quotep types2))
                (member-equal exclude types)
                (subsetp-equal types types2))
           (equal (vl-is-some-token? types)
                  (vl-is-some-token? (remove-equal exclude types))))
  :hints(("Goal" :in-theory (enable vl-type-of-matched-token
                                    vl-is-some-token?))))

(defthm magically-reduce-possible-types-from-vl-type-of-matched-token
  (implies (and (not (equal (vl-type-of-matched-token types2 tokens) exclude))
                (syntaxp (quotep types))
                (syntaxp (quotep types2))
                (member-equal exclude types)
                (subsetp-equal types types2))
           (equal (vl-type-of-matched-token types tokens)
                  (vl-type-of-matched-token (remove-equal exclude types) tokens)))
  :hints(("Goal" :in-theory (enable vl-type-of-matched-token))))

(defthm magically-resolve-vl-is-some-token?
  (implies (and (equal (vl-type-of-matched-token types2 (vl-tokstream->tokens)) value)
                (member-equal value types)
                value)
           (equal (vl-is-some-token? types)
                  t))
  :hints(("Goal" :in-theory (enable vl-is-some-token?
                                    vl-type-of-matched-token))))

(defthm magically-resolve-type-of-matched-token
  (implies (and (equal (vl-type-of-matched-token types2 tokens) value)
                (member-equal value types)
                value)
           (equal (vl-type-of-matched-token types tokens)
                  value))
  :hints(("Goal" :in-theory (enable vl-type-of-matched-token))))

(defthm vl-is-some-token?-under-iff
  (implies (not (member-equal nil types))
           (iff (vl-is-some-token? types)
                (vl-type-of-matched-token types (vl-tokstream->tokens))))
  :hints(("Goal" :in-theory (enable vl-type-of-matched-token
                                    vl-is-some-token?))))




(define vl-current-loc
  :short "Compatible with @(see seq).  Get the current location."
  (&key (tokstream 'tokstream))
  :returns (mv (errmsg        (not errmsg))
               (loc           vl-location-p)
               (new-tokstream (equal new-tokstream tokstream)))
  :long "<p>We just return the location of the first token, if there is one.
Or, if the token stream is empty, we just return @(see *vl-fakeloc*)</p>"
  (b* ((tokens (vl-tokstream->tokens)))
    (mv nil
        (if (consp tokens)
            (vl-token->loc (car tokens))
          *vl-fakeloc*)
        tokstream)))

;; (defmacro vl-copy-tokens (&key (tokstream 'tokstream))
;;   "Returns (MV ERROR TOKENS TOKENS PSTATE) for SEQ compatibility."
;;   (declare (xargs :guard t))
;;   `(let ((tokens ,tokens)
;;          (pstate ,pstate))
;;      (mv nil tokens tokens pstate)))


(define vl-match (&key
                  (tokstream 'tokstream))
  :guard (consp (vl-tokstream->tokens))
  :short "Compatible with @(see seq).  Get the first token, no matter what
kind of token it is.  Only usable when you know there is a token there (via the
guard)."
  :inline t
  :returns (mv (errmsg?   (not errmsg?))
               (token     (equal (vl-token-p token)
                                 (consp (vl-tokstream->tokens))))
               (new-tokstream))
  (b* ((tokens    (vl-tokstream->tokens))
       (tokstream (vl-tokstream-update-tokens (cdr tokens))))
    (mv nil (car tokens) tokstream))
  ///
  ;; (defthm vl-match-of-vl-tokenlist-fix
  ;;   (equal (vl-match :tokens (vl-tokenlist-fix tokens))
  ;;          (vl-match)))

  ;; (defthm vl-match-of-vl-parsestate-fix
  ;;   (equal (vl-match :pstate (vl-parsestate-fix pstate))
  ;;          (vl-match)))

  (local (defthm l0
           (implies (vl-tokenlist-p x)
                    (iff (consp x)
                         (car x)))))

  (defthm vl-match-under-iff
    (iff (mv-nth 1 (vl-match))
         (consp (vl-tokstream->tokens))))

  (defthm vl-match-count-strong-on-value
    (and (<= (vl-tokstream-measure :tokstream (mv-nth 2 (vl-match)))
             (vl-tokstream-measure))
         (implies (mv-nth 1 (vl-match))
                  (< (vl-tokstream-measure :tokstream (mv-nth 2 (vl-match)))
                     (vl-tokstream-measure))))
    :rule-classes ((:rewrite) (:linear))
    :hints(("Goal" :expand (len (vl-tokstream->tokens)))))

  (defthm equal-of-vl-match-count
    (equal (equal (vl-tokstream-measure :tokstream (mv-nth 2 (vl-match)))
                  (vl-tokstream-measure))
           (atom (vl-tokstream->tokens))))

  (defthm vl-token->type-of-vl-match-when-vl-is-token?
    (implies (vl-is-token? type)
             (equal (vl-token->type (mv-nth 1 (vl-match)))
                    type))
    :hints(("Goal" :in-theory (enable vl-is-token?))))

  (defthm vl-token->type-of-vl-match-when-vl-is-some-token?
    (implies (vl-type-of-matched-token types (vl-tokstream->tokens))
             (equal (vl-token->type (mv-nth 1 (vl-match)))
                    (vl-type-of-matched-token types (vl-tokstream->tokens))))
    :hints(("Goal" :in-theory (enable vl-type-of-matched-token
                                      vl-is-some-token?)))))


(define vl-match-any
  :short "Compatible with @(see seq).  Get the first token, no matter what
kind of token it is.  Causes an error on EOF."
  (&key
   ((function symbolp)       '__function__)
   (tokstream 'tokstream))
  :inline t
  :returns
  (mv (errmsg?)
      (token)
      (new-tokstream))
  (b* ((tokens (vl-tokstream->tokens))
       ((when (atom tokens))
        (vl-parse-error "Unexpected EOF." :function function))
       (tokstream (vl-tokstream-update-tokens (cdr tokens))))
    (mv nil (car tokens) tokstream))
  ///
  ;; (defthm vl-match-any-of-vl-tokenlist-fix
  ;;   (equal (vl-match-any :tokens (vl-tokenlist-fix tokens))
  ;;          (vl-match-any)))

  ;; (defthm vl-match-any-of-vl-parsestate-fix
  ;;   (equal (vl-match-any :pstate (vl-parsestate-fix pstate))
  ;;          (vl-match-any)))

  (defthm vl-match-any-fails-gracefully
    (implies (mv-nth 0 (vl-match-any))
             (equal (mv-nth 1 (vl-match-any))
                    nil)))

  (defthm vl-warning-p-of-vl-match-any
    (iff (vl-warning-p (mv-nth 0 (vl-match-any)))
         (mv-nth 0 (vl-match-any))))

  (defthm vl-token-p-of-vl-match-any
    (implies (not (mv-nth 0 (vl-match-any)))
             (vl-token-p (mv-nth 1 (vl-match-any)))))

  (defthm vl-match-any-fn-count-strong-on-value
    (and (<= (vl-tokstream-measure :tokstream (mv-nth 2 (vl-match-any)))
             (vl-tokstream-measure))
         (implies (mv-nth 1 (vl-match-any))
                  (< (vl-tokstream-measure :tokstream (mv-nth 2 (vl-match-any)))
                     (vl-tokstream-measure))))
  :rule-classes ((:rewrite) (:linear)))

  (defthm equal-of-vl-match-any-fn-count
    (equal (equal (vl-tokstream-measure :tokstream (mv-nth 2 (vl-match-any)))
                  (vl-tokstream-measure))
           (if (mv-nth 0 (vl-match-any))
               t
             nil))))

(define vl-match-any-except
  :short "Compatible with @(see seq).  Match any token that is not of the
listed types.  Causes an error on EOF."
  ((types true-listp) ;; BOZO stronger guard?
   &key
   ((function symbolp)       '__function__)
   (tokstream 'tokstream))
  :returns
  (mv (errmsg? (iff (vl-warning-p errmsg?) errmsg?))
      (token)
      (new-tokstream))
  (b* ((tokens (vl-tokstream->tokens))
       ((when (atom tokens))
        (vl-parse-error "Unexpected EOF." :function function))
       (token1 (car tokens))
       ((when (member-eq (vl-token->type token1) types))
        (mv (make-vl-warning :type :vl-parse-error
                             :msg "Parse error at ~a0: unexpected ~s1."
                             :args (list (vl-token->loc token1)
                                         (vl-token->type token1))
                             :fn function
                             :fatalp t)
            nil tokstream))
       (tokstream (vl-tokstream-update-tokens (cdr tokens))))
    (mv nil token1 tokstream))
  ///
  (local (in-theory (enable vl-is-some-token?)))

  ;; (defthm vl-match-any-except-of-vl-tokenlist-fix
  ;;   (equal (vl-match-any-except types :tokens (vl-tokenlist-fix tokens))
  ;;          (vl-match-any-except types)))

  ;; (defthm vl-match-any-except-of-vl-parsestate-fix
  ;;   (equal (vl-match-any-except types :pstate (vl-parsestate-fix pstate))
  ;;          (vl-match-any-except types)))

  (defthm vl-match-any-except-fails-when-vl-is-some-token?
    (iff (mv-nth 0 (vl-match-any-except types))
         (or (atom (vl-tokstream->tokens))
             (vl-is-some-token? types))))

  (defthm vl-match-any-except-fails-gracefully
    (implies (or (atom (vl-tokstream->tokens))
                 (vl-is-some-token? types))
             (equal (mv-nth 1 (vl-match-any-except types))
                    nil)))

  (defthm vl-match-any-except-when-atom-of-tokens
    (implies (atom (vl-tokstream->tokens))
             (equal (mv-nth 2 (vl-match-any-except types))
                    tokstream)))

  (defthm vl-token-p-of-vl-match-any-except
    (implies (and (not (vl-is-some-token? types))
                  (consp (vl-tokstream->tokens)))
             (vl-token-p (mv-nth 1 (vl-match-any-except types)))))

  (defthm vl-match-any-except-count-strong-on-value
    (and (<= (vl-tokstream-measure :tokstream (mv-nth 2 (vl-match-any-except types)))
             (vl-tokstream-measure))
         (implies (mv-nth 1 (vl-match-any-except types))
                  (< (vl-tokstream-measure :tokstream (mv-nth 2 (vl-match-any-except types)))
                     (vl-tokstream-measure))))
    :rule-classes ((:rewrite) (:linear)))

  (defthm equal-of-vl-match-any-except-fn-count
    (equal (equal (vl-tokstream-measure :tokstream (mv-nth 2 (vl-match-any-except types)))
                  (vl-tokstream-measure))
           (if (mv-nth 0 (vl-match-any-except types))
               t
             nil))))



(defaggregate vl-endinfo
  :short "Temporary structure created to parse named endings like
@('endmodule : foo')."

  :long "<p>SystemVerilog allows many syntactic constructs such as modules,
user-defined primitives, programs, packages, etc., to optionally be closed
using a verbose syntax that repeats the name.  For instance, one can write:</p>

@({
       module foo (...);
         ...
       endmodule : foo     // <-- named ending
})

<p>The ending name must match or it's an error, see \"Block Names\", Section
9.3.4 of the SystemVerilog-2012 spec.  Note that these named endings aren't
allowed in Verilog-2005.</p>

<p>An @('vl-endinfo-p') structure is just a temporary structure used by our
parser when we encounter such an ending.</p>"

  :layout :fulltree

  ((name maybe-stringp :rule-classes :type-prescription
         "The name we matched after the colon, e.g., @('foo') above.")

   (loc  vl-location-p
         "The location of this name, for mismatch reporting.")))

(defparser vl-parse-endblock-name (name blktype)
  :guard (and (stringp name) (stringp blktype))
  :count weak
  :fails :gracefully
  (seq tokstream
       (unless (and (not (eq (vl-loadconfig->edition config) :verilog-2005))
                    (vl-is-token? :vl-colon))
         (return nil))
       (:= (vl-match))
       (endname := (vl-match-token :vl-idtoken))
       (when (equal name (vl-idtoken->name endname))
         (return name))
       (return-raw
        (mv (make-vl-warning :type :vl-parse-error
                             :msg "At ~a0: mismatched ~s1 names: ~s2 vs. ~s3."
                             :args (list (vl-token->loc endname)
                                         blktype
                                         name
                                         (vl-idtoken->name endname)))
            nil
            tokstream))))