This file is indexed.

/usr/share/common-lisp/source/regex/closure.lisp is in cl-regex 1-4.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: REGEX; Base: 10 -*-

(in-package :REGEX)

;;;
;;; Pass 6 - Code generation to closures
;;;

; returns array of info structures
(defun gen-closures (cpscode)
  (let ((info (make-array (length cpscode))))
    (loop for instr in cpscode
          for offset = (first instr)
          for opcode = (second instr)
          for args = (cddr instr)
          do (setf (aref info offset) (gen-closure opcode args)))
    info))

(defun gen-closure (opcode args)
  (destructuring-bind (&optional arg1 arg2 arg3) args
    (case opcode
      (char
       (gen-char-closure arg1 arg2))
      (string
       (gen-string-closure (length arg1) arg1 arg2))
      (classseq
       (let ((num-classes (length arg1)))
         (gen-classseq-closure
          num-classes
          (make-array num-classes :initial-contents arg1)
          arg2)))
      (backmatch
       (gen-backmatch-closure arg1 arg2))
      (str-greedy-kleene
       (gen-str-greedy-kleene-closure (length arg1) arg1 arg2))
      (char-greedy-kleene
       (gen-char-greedy-kleene-closure arg1 arg2))
      (not-char-greedy-kleene
       (gen-not-char-greedy-kleene-closure arg1 arg2))
      (cclass-2-greedy-kleene
       (gen-cclass-2-greedy-kleene-closure arg1 arg2 arg3))
      (not-cclass-2-greedy-kleene
       (gen-not-cclass-2-greedy-kleene-closure arg1 arg2 arg3))
      (cclass-greedy-kleene
       (gen-cclass-greedy-kleene-closure arg1 arg2))
      (not-cclass-greedy-kleene
       (gen-not-cclass-greedy-kleene-closure arg1 arg2))
      (specclass-greedy-kleene
       (gen-specclass-greedy-kleene-closure
        (get-spec-pat-fxn arg1) arg2))
      (not-specclass-greedy-kleene
       (gen-not-specclass-greedy-kleene-closure
        (get-spec-pat-fxn arg1) arg2))
      (alt-2-simple-termcheck-1
       (gen-alt-2-simple-termcheck-1-closure arg1 arg2))
      (alt-2-simple-termcheck-2
       (gen-alt-2-simple-termcheck-2-closure arg1 arg2))
      (alt-2-full-termcheck
       (gen-alt-2-full-termcheck-closure arg1 arg2))
      (alt-2-no-termcheck
       (gen-alt-2-no-termcheck-closure arg1 arg2))
      (alt-no-termcheck
       (let ((num-branches (length arg1)))
         (gen-alt-no-termcheck-closure
          num-branches
          (make-array num-branches :initial-contents arg1))))
      (casealt
       (multiple-value-bind (numbranches jmptbl)
           (make-casealt-jmptable arg1)
         (gen-casealt-closure numbranches jmptbl)))
      (startanchor
       (gen-startanchor-closure arg1))
      (endanchor
       (gen-endanchor-closure arg1))
      (left-rstart
       (gen-left-rstart-closure arg1 arg2))
      (right-rstart
       (gen-right-rstart-closure arg1 arg2))
      (rend
       (gen-rend-closure arg1 arg2))
      (cclass-2
       (gen-cclass-2-closure arg1 arg2 arg3))
      (cclass
       (gen-cclass-closure arg1 arg2))
      (not-char
       (gen-not-char-closure arg1 arg2))
      (not-cclass-2
       (gen-not-cclass-2-closure arg1 arg2 arg3))
      (not-cclass
       (gen-not-cclass-closure arg1 arg2))
      (specclass
       (gen-specclass-closure (get-spec-pat-fxn arg1) arg2))
      (not-specclass
       (gen-not-specclass-closure (get-spec-pat-fxn arg1) arg2))
      (any
       (gen-any-closure arg1))
      (startword
       (gen-startword-closure arg1))
      (endword
       (gen-endword-closure arg1))
      (lookahead
       (gen-lookahead-closure arg1 arg2))
      (nlookahead
       (gen-nlookahead-closure arg1 arg2))
      (hook
       (gen-hook-closure arg1 arg2))
      (success
       (gen-success-closure arg1))
      (t (error "gen-closure: Unknown instruction ~S ~S" opcode args)))) )


(defun make-casealt-jmptable (jumps)
  (let* ((num-jump-targets (length jumps))
         (jump-tbl (make-array (* 2 num-jump-targets))))
    (loop for discriminator-idx from 0 by 2
          for destination-idx from 1 by 2
          for (discriminator destination) in jumps
          do (progn
               (setf (aref jump-tbl discriminator-idx) discriminator)
               (setf (aref jump-tbl destination-idx) destination))
          finally (return (values num-jump-targets jump-tbl)))))

(defmacro make-text-closure (&key matcher initializer linker)
  `(make-closure-info
    :matchfn
    (if *match-simple-strings-only*
        (macrolet ((re-char (str idx)
                     `(the character (schar (the simple-string ,str)
                                            (the fixnum ,idx)))))
          ,matcher)
      (macrolet ((re-char (str idx)
                   `(the character (char (the string ,str)
                                         (the fixnum ,idx)))))
        ,matcher))
    :initfn
    ,initializer
    :linkfn
    ,linker))

(defmacro make-nontext-closure (&key matcher initializer linker)
  `(make-closure-info
    :matchfn
    ,matcher
    :initfn
    ,initializer
    :linkfn
    ,linker))


(defun gen-char-closure (chr next)
  (make-text-closure
   :matcher
   #'(lambda (pos)
       #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
                                        #+:lispworks (hcl:fixnum-safety 0)))
        (declare (fixnum pos)
                 (character chr)
                 (ftype (function (fixnum) t) next)
                 (special *str* *end*)
                 (string *str*) (fixnum *end*))
	(cond ((and (< pos *end*) (char= chr (re-char *str* pos)))
	       (funcall next (1+ pos)))))
   :initializer
   nil
   :linker
   #'(lambda (link-info)
       (setq next (resolve-instr link-info next)) )))


(defun gen-string-closure (len patstr next)
  (make-text-closure
   :matcher
   #'(lambda (pos)
       #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
                                        #+:lispworks (hcl:fixnum-safety 0) ))
       (declare (fixnum pos)
                (fixnum len) (string patstr)
                (ftype (function (fixnum) t) next)
                (special *str* *end*)
                (string *str*) (fixnum *end*))
       (let ((end (+ pos len)))
         (declare (fixnum end))
         (when (<= end *end*)
           (loop for i fixnum from pos below end
                 for j fixnum from 0 below len
                 when (char/= (re-char *str* i) (re-char patstr j))
                 return nil
                 finally (funcall next i)))))
   :initializer
   nil
   :linker
   #'(lambda (link-info)
       (setq next (resolve-instr link-info next))) ))


(defun gen-classseq-closure (len chrclasses next)
  (make-text-closure
   :matcher
   #'(lambda (pos)
       #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
                                        #+:lispworks (hcl:fixnum-safety 0) ))
       (declare (fixnum pos)
                (fixnum len) (simple-vector chrclasses)
                (ftype (function (fixnum) t) next)
                (special *str* *end*)
                (string *str*) (fixnum *end*))
       (let ((reg-str-end (+ pos len)))
         (declare (fixnum reg-str-end))
         (when (<= reg-str-end *end*)
           (loop for i fixnum from pos below reg-str-end
                 for j fixnum from 0 below len
                 unless (find (re-char *str* i) (svref chrclasses j))
                   return nil
                 finally (funcall next reg-str-end)))))
   :initializer
   nil
   :linker
   #'(lambda (link-info)
       (setq next (resolve-instr link-info next))) ))


(defun gen-backmatch-closure (regnum next)
  (make-text-closure
   :matcher
   #'(lambda (pos)
       #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
                                        #+:lispworks (hcl:fixnum-safety 0)))
       (declare (fixnum pos)
                (fixnum regnum) (ftype (function (fixnum) t) next)
                (special *str* *regs* *end*)
                (string *str*) (type simple-vector *regs*) (fixnum *end*))
       (let ((reg-start (register-start *regs* regnum))
             (reg-end (register-end *regs* regnum)))
         (cond ((numberp reg-start)
                ;; If reg-start is set, but reg-end isn't, then we're matching
                ;; inside that register's context.  So use POS as the end.
                (unless (integerp reg-end)
                  (setq reg-end pos))
                (let* ((reg-len (- reg-end reg-start))
                       (reg-str-end (+ pos reg-len)))
                  (declare (fixnum reg-len reg-str-end))
                  (when (<= reg-str-end *end*)
                    (loop for i fixnum from pos below reg-str-end
                          for j fixnum from reg-start below reg-end
                          when (char/= (re-char *str* i) (re-char *str* j))
                            return nil
                          finally (funcall next i)))))
               ;; backmatching an unmatched register is ok -- it may mean
               ;; that the register was for ()* or ()? or something, so treat
               ;; it as a 0-length register
               (t (funcall next pos)))))
   :initializer
   nil
   :linker
   #'(lambda (link-info)
       (setq next (resolve-instr link-info next))) ))


(defun gen-str-greedy-kleene-closure (len patstr next)
  (make-text-closure
   :matcher
   #'(lambda (pos)
       #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
                                        #+:lispworks (hcl:fixnum-safety 0)))
       (declare (fixnum pos)
                (fixnum len) (string patstr)
                (character chr) (ftype (function (fixnum) t) next)
                (special *str* *end*)
                (string *str*) (fixnum *end*))
       (let ((firstend (+ pos len))
             (lastpos pos)
             (end (- *end* len)))
         (declare (fixnum firstend lastpos end))
         (when (<= firstend *end*)
           (loop for testpos fixnum from pos upto end by len
                 while (loop for j fixnum from 0 below len
                             for i fixnum from testpos
                             when (char/= (re-char *str* i) (re-char patstr j))
                               do (return nil)
                             finally (return t))
                 do (incf lastpos len)))
         (loop for testpos fixnum from lastpos downto pos by len
               do (funcall next testpos))))
   :initializer
   nil
   :linker
   #'(lambda (link-info)
       (setq next (resolve-instr link-info next)))))


(defun gen-char-greedy-kleene-closure (chr next)
  (make-text-closure
   :matcher
   #'(lambda (pos)
       #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
                                        #+:lispworks (hcl:fixnum-safety 0)))
       (declare (fixnum pos)
                (character chr) (ftype (function (fixnum) t) next)
                (special *str* *end*)
                (string *str*) (fixnum *end*))
       (let ((newpos pos))
         (declare (fixnum newpos))
         (loop while (and (< newpos *end*) (char= chr (re-char *str* newpos)))
               do (incf newpos))
         (loop while (>= newpos pos)
               do (progn
                    (funcall next newpos)
                    (decf newpos)))
         nil))
   :initializer
   nil
   :linker
   #'(lambda (link-info)
       (setq next (resolve-instr link-info next))) ))


(defun gen-not-char-greedy-kleene-closure (chr next)
  (make-text-closure
   :matcher
   #'(lambda (pos)
       #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
                                        #+:lispworks (hcl:fixnum-safety 0)))
       (declare (fixnum pos)
                (ftype (function (fixnum) t) next) (character chr)
                (special *str* *end*)
                (string *str*) (fixnum *end*))
       (let ((newpos pos))
         (declare (fixnum newpos))
         (loop while (and (< newpos *end*)
                          (char/= chr (re-char *str* newpos)))
               do (incf newpos))
         (loop while (>= newpos pos)
               do (progn
                    (funcall next newpos)
                    (decf newpos)))
         nil))
   :initializer
   nil
   :linker
   #'(lambda (link-info)
       (setq next (resolve-instr link-info next))) ))


(defun gen-cclass-2-greedy-kleene-closure (chr1 chr2 next)
  (make-text-closure
   :matcher
   #'(lambda (pos)
       #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
                                        #+:lispworks (hcl:fixnum-safety 0)))
       (declare (fixnum pos)
                (character chr1 chr2)
                (ftype (function (fixnum) t) next)
                (special *str* *end*)
                (fixnum *start* *end*) (string *str*))
       (let ((newpos pos))
         (declare (fixnum newpos))
         (loop while (and (< newpos *end*)
                          (let ((chr (re-char *str* newpos)))
                            (declare (character chr))
                            (or (char= chr1 chr)
                                (char= chr2 chr))))
               do (incf newpos))
         (loop while (>= newpos pos)
               do (progn
                    (funcall next newpos)
                    (decf newpos)))
         nil))
   :initializer
   nil
   :linker
   #'(lambda (link-info)
       (setq next (resolve-instr link-info next))) ))


(defun gen-not-cclass-2-greedy-kleene-closure (chr1 chr2 next)
  (make-text-closure
   :matcher
   #'(lambda (pos)
       #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
                                        #+:lispworks (hcl:fixnum-safety 0)))
       (declare (fixnum pos)
                (character chr1 chr2)
                (ftype (function (fixnum) t) next)
                (special *str* *end*)
                (fixnum *start* *end*) (string *str*))
       (let ((newpos pos))
         (declare (fixnum newpos))
         (loop while (and (< newpos *end*)
                          (not (let ((chr (re-char *str* newpos)))
                                 (declare (character chr))
                                 (or (char= chr1 chr)
                                     (char= chr2 chr)))))
               do (incf newpos))
         (loop while (>= newpos pos)
               do (progn
                    (funcall next newpos)
                    (decf newpos)))
         nil))
   :initializer
   nil
   :linker
   #'(lambda (link-info)
       (setq next (resolve-instr link-info next))) ))


(defun gen-cclass-greedy-kleene-closure (chrs next)
  (make-text-closure
   :matcher
   #'(lambda (pos)
       #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
                                        #+:lispworks (hcl:fixnum-safety 0)))
       (declare (fixnum pos)
                (simple-string chrs)
                (ftype (function (fixnum) t) next)
                (special *str* *end*)
                (fixnum *end*) (string *str*))
       (let ((newpos pos))
         (declare (fixnum newpos))
         (loop while (and (< newpos *end*)
                          (find (re-char *str* newpos) chrs))
               do (incf newpos))
         (loop while (>= newpos pos)
               do (progn
                    (funcall next newpos)
                    (decf newpos)))
         nil))
   :initializer
   nil
   :linker
   #'(lambda (link-info)
       (setq next (resolve-instr link-info next))) ))


(defun gen-not-cclass-greedy-kleene-closure (chrs next)
  (make-text-closure
   :matcher
   #'(lambda (pos)
       #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
                                        #+:lispworks (hcl:fixnum-safety 0)))
       (declare (fixnum pos)
                (simple-string chrs)
                (ftype (function (fixnum) t) next)
                (special *str* *end*)
                (fixnum *end*) (string *str*))
       (let ((newpos pos))
         (declare (fixnum newpos))
         (loop while (and (< newpos *end*)
                          (not (find (re-char *str* newpos) chrs)))
               do (incf newpos))
         (loop while (>= newpos pos)
               do (progn
                    (funcall next newpos)
                    (decf newpos)))
         nil))
   :initializer
   nil
   :linker
   #'(lambda (link-info)
       (setq next (resolve-instr link-info next))) ))


(defun gen-specclass-greedy-kleene-closure (classfn next)
  (make-text-closure
   :matcher
   #'(lambda (pos)
       #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
                                        #+:lispworks (hcl:fixnum-safety 0)))
       (declare (fixnum pos)
                (ftype (function (character) t) classfn)
                (ftype (function (fixnum) t) next)
                (special *str* *end*)
                (string *str*) (fixnum *end*))
       (let ((newpos pos))
         (declare (fixnum newpos))
         (loop while (and (< newpos *end*)
                          (funcall classfn (re-char *str* newpos)))
               do (incf newpos))
         (loop while (>= newpos pos)
               do (progn
                    (funcall next newpos)
                    (decf newpos)))
         nil))
   :initializer
   nil
   :linker
   #'(lambda (link-info)
       (setq next (resolve-instr link-info next))) ))


(defun gen-not-specclass-greedy-kleene-closure (classfn next)
  (make-text-closure
   :matcher
   #'(lambda (pos)
       #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
                                        #+:lispworks (hcl:fixnum-safety 0)))
       (declare (fixnum pos)
                (ftype (function (character) t) classfn)
                (ftype (function (fixnum) t) next)
                (special *str* *end*)
                (string *str*) (fixnum *end*))
       (let ((newpos pos))
         (declare (fixnum newpos))
         (loop while (and (< newpos *end*)
                          (not (funcall classfn (re-char *str* newpos))))
               do (incf newpos))
         (loop while (>= newpos pos)
               do (progn
                    (funcall next newpos)
                    (decf newpos)))
         nil))
   :initializer
   nil
   :linker
   #'(lambda (link-info)
       (setq next (resolve-instr link-info next))) ))


(defun gen-alt-2-simple-termcheck-1-closure (next1 next2 &aux (oldpos -1))
  (make-nontext-closure
   :matcher
   #'(lambda (pos)
       #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
                                        #+:lispworks (hcl:fixnum-safety 0)))
       (declare (fixnum pos)
                (ftype (function (fixnum) t) next1 next2)
                (fixnum oldpos)
                (special *str* *end*)
                (string *str*) (fixnum *end*))
       (when (> pos oldpos)
         (setq oldpos pos)
         (funcall next1 pos))
       (funcall next2 pos)
       (setq oldpos -1)
       nil)
   :initializer
   #'(lambda ()
       (setq oldpos -1))
   :linker
   #'(lambda (link-info)
       (setq next1 (resolve-instr link-info next1))
       (setq next2 (resolve-instr link-info next2))) ))


(defun gen-alt-2-simple-termcheck-2-closure (next1 next2 &aux (oldpos -1))
  (make-nontext-closure
   :matcher
   #'(lambda (pos)
       #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
                                        #+:lispworks (hcl:fixnum-safety 0)))
       (declare (fixnum pos)
                (ftype (function (fixnum) t) next1 next2)
                (fixnum oldpos))
       (funcall next1 pos)
       (when (> pos oldpos)
         (setq oldpos pos)
         (funcall next2 pos))
       (setq oldpos -1)
       nil)
   :initializer
   #'(lambda ()
       (setq oldpos -1))
   :linker
   #'(lambda (link-info)
       (setq next1 (resolve-instr link-info next1))
       (setq next2 (resolve-instr link-info next2))) ))


(defun gen-alt-2-full-termcheck-closure (next1 next2
                                         &aux (firsttimep t)
                                              (seen1 (make-hash-table))
                                              (seen2 (make-hash-table)))
  (make-nontext-closure
   :matcher
   #'(lambda (pos)
       #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
                                        #+:lispworks (hcl:fixnum-safety 0)))
       (declare (fixnum pos)
                (fixnum firstpos)
                (ftype (function (fixnum) t) next1 next2))
       (cond (firsttimep
              (setq firsttimep nil)
              (unless (gethash pos seen1)
                (setf (gethash pos seen1) t)
                (funcall next1 pos))
              (unless (gethash pos seen2)
                (setf (gethash pos seen2) t)
                (funcall next2 pos))
              (clrhash seen1)
              (clrhash seen2)
              (setq firsttimep t)
              nil)
             (t (unless (gethash pos seen1)
                  (setf (gethash pos seen1) t)
                  (funcall next1 pos))
                (unless (gethash pos seen2)
                  (setf (gethash pos seen2) t)
                  (funcall next2 pos)))))
   :initializer
   #'(lambda ()
       (setq firsttimep t))
   :linker
   #'(lambda (link-info)
       (setq next1 (resolve-instr link-info next1))
       (setq next2 (resolve-instr link-info next2))) ))


(defun gen-alt-2-no-termcheck-closure (next1 next2)
  (make-nontext-closure
   :matcher
   #'(lambda (pos)
       #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
                                        #+:lispworks (hcl:fixnum-safety 0)))
       (declare (fixnum pos)
                (ftype (function (fixnum) t) next1 next2))
       (funcall next1 pos)
       (funcall next2 pos))
   :initializer
   nil
   :linker
   #'(lambda (link-info)
       (setq next1 (resolve-instr link-info next1))
       (setq next2 (resolve-instr link-info next2))) ))


(defun gen-alt-no-termcheck-closure (num-nexts nexts)
  (make-nontext-closure
   :matcher
   #'(lambda (pos)
       #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
                                        #+:lispworks (hcl:fixnum-safety 0)))
       (declare (fixnum pos num-nexts))
       (dotimes (i num-nexts)
         (funcall (svref nexts i) pos)))
   :initializer
   nil
   :linker
   #'(lambda (link-info)
       (dotimes (i num-nexts nil)
         (declare (fixnum i))
         (setf (svref nexts i)
               (resolve-instr link-info (svref nexts i))))) ))

(defun gen-casealt-closure (num-jump-entries jmptbl)
  (make-text-closure
   :matcher
   #'(lambda (pos)
       #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
                                        #+:lispworks (hcl:fixnum-safety 0)))
       (declare (fixnum pos)
                (special *str* *end*)
                (string *str*) (fixnum *end*))
       (when (< pos *end*)
         (let ((chr (re-char *str* pos)))
           (declare (character chr))
           (loop for discriminator-idx fixnum from 0 by 2 below (* 2 num-jump-entries)
                 when (char= chr (svref jmptbl discriminator-idx))
                 do (funcall (svref jmptbl (1+ discriminator-idx)) pos)
                 finally (return nil)))))
   :initializer
   nil
   :linker
   #'(lambda (link-info)
       (loop for destination-idx from 1 by 2 below (* 2 num-jump-entries)
             do (setf (svref jmptbl destination-idx)
                      (resolve-instr link-info (svref jmptbl destination-idx))))) ))

(defun gen-startanchor-closure (next)
  (make-text-closure
   :matcher
    #'(lambda (pos)
        #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
                                         #+:lispworks (hcl:fixnum-safety 0)))
        (declare (fixnum pos)
                 (ftype (function (fixnum) t) next)
                 (special *str* *start* *start-is-anchor*)
                 (string *str*) (fixnum *start*))
	(if (or (and *start-is-anchor* (= pos *start*))
                (and (> pos 0) (char= (re-char *str* (1- pos))
                                      #\newline)))
	    (funcall next pos)))
   :initializer
   nil
   :linker
    #'(lambda (link-info)
	(setq next (resolve-instr link-info next))) ))


(defun gen-endanchor-closure (next)
  (make-text-closure
   :matcher
    #'(lambda (pos)
        #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
                                         #+:lispworks (hcl:fixnum-safety 0)))
        (declare (fixnum pos)
                 (ftype (function (fixnum) t) next)
                 (special *str* *end* *end-is-anchor*)
                 (fixnum *end*) (string *str*))
	(if (or (and *end-is-anchor* (= pos *end*))
                (and (< pos *end*) (char= (re-char *str* pos)
                                          #\newline)))
	    (funcall next pos)))
    :initializer
    nil
    :linker
    #'(lambda (link-info)
	(setq next (resolve-instr link-info next))) ))


;(defun gen-left-rstart-closure (regnum next)
;  (make-nontext-closure
;   :matcher
;   #'(lambda (pos)
;       #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
;                                        #+:lispworks (hcl:fixnum-safety 0)))
;       (declare (fixnum pos)
;                (fixnum regnum)
;                (ftype (function (fixnum) t) next)
;                (special *regs*)
;                (type simple-vector *regs*))
;       (let ((reg (svref *regs* regnum)))
;         (declare (cons reg))
;         (setf (car reg) pos (cdr reg) nil)
;         (funcall next pos)
;         (setf (car reg) nil)
;         nil))
;   :initializer
;   nil
;   :linker
;   #'(lambda (link-info)
;       (setq next (resolve-instr link-info next))) ))




(defun gen-left-rstart-closure (regnum next)
  (make-nontext-closure
   :matcher
   #'(lambda (pos)
       #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
                                        #+:lispworks (hcl:fixnum-safety 0)))
       (declare (fixnum pos)
                (fixnum regnum)
                (ftype (function (fixnum) t) next)
                (special *regs*)
                (type simple-vector *regs*))
       (let ((reg (svref *regs* regnum)))
         (declare (cons reg))
         (cond ((and (car reg) (cdr reg))
                (funcall next pos))
               (t
                (setf (car reg) pos (cdr reg) nil)
                (funcall next pos)
                (setf (car reg) nil)
                nil))))
   :initializer
   nil
   :linker
   #'(lambda (link-info)
       (setq next (resolve-instr link-info next))) ))



(defun gen-right-rstart-closure (regnum next)
  (make-nontext-closure
   :matcher
   #'(lambda (pos)
       #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
                                        #+:lispworks (hcl:fixnum-safety 0)))
       (declare (fixnum pos)
                (fixnum regnum)
                (ftype (function (fixnum) t) next)
                (special *regs*)
                (type simple-vector *regs*))
       (let ((reg (svref *regs* regnum)))
         (declare (cons reg))
         (let ((prevstart (car reg))
               (prevend (cdr reg)))
           (setf (car reg) pos (cdr reg) nil)
           (funcall next pos)
           (setf (car reg) prevstart (cdr reg) prevend)))
         nil)
   :initializer
   nil
   :linker
   #'(lambda (link-info)
       (setq next (resolve-instr link-info next))) ))


(defun gen-rend-closure (regnum next)
  (make-nontext-closure
   :matcher
   #'(lambda (pos)
       #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
                                        #+:lispworks (hcl:fixnum-safety 0)))
       (declare (fixnum pos)
                (fixnum regnum)
                (ftype (function (fixnum) t) next)
                (special *regs*)
                (type simple-vector *regs*))
       (cond ((register-end *regs* regnum)
              (funcall next pos))
             (t
              (setf (register-end *regs* regnum) pos)
              (funcall next pos)
              (setf (register-end *regs* regnum) nil))))
   :initializer
   nil
   :linker
   #'(lambda (link-info)
       (setq next (resolve-instr link-info next))) ))


(defun gen-cclass-2-closure (chr1 chr2 next)
  (make-text-closure
   :matcher
   #'(lambda (pos)
       #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
                                        #+:lispworks (hcl:fixnum-safety 0)))
       (declare (fixnum pos)
                (character chr1 chr2)
                (ftype (function (fixnum) t) next-fn)
                (special *str* *end*)
                (string *str*) (fixnum *end*))
       (if (< pos *end*)
           (let ((chr (re-char *str* pos)))
             (declare (character chr))
             (if (or (char= chr chr1) (char= chr chr2))
                 (funcall next (1+ pos))))))
   :initializer
   nil
   :linker
   #'(lambda (link-info)
       (setq next (resolve-instr link-info next))) ))


(defun gen-cclass-closure (chrs next)
  (make-text-closure
   :matcher
   #'(lambda (pos)
       #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
                                        #+:lispworks (hcl:fixnum-safety 0)))
       (declare (fixnum pos)
                (simple-string chrs)
                (ftype (function (fixnum) t) next)
                (special *str* *end*)
                (string *str*) (fixnum *end*))
       (if (and (< pos *end*)
                (find (re-char *str* pos) chrs))
           (funcall next (1+ pos))))
   :initializer
   nil
   :linker
   #'(lambda (link-info)
       (setq next (resolve-instr link-info next))) ))


(defun gen-not-char-closure (chr next)
  (make-text-closure
   :matcher
   #'(lambda (pos)
       #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
                                        #+:lispworks (hcl:fixnum-safety 0)))
       (declare (fixnum pos)
                (character chr)
                (ftype (function (fixnum) t) next)
                (special *str* *end*)
                (string *str*) (fixnum *end*))
       (if (and (< pos *end*)
                (char/= (re-char *str* pos) chr))
           (funcall next (1+ pos))))
   :initializer
   nil
   :linker
   #'(lambda (link-info)
       (setq next (resolve-instr link-info next))) ))


(defun gen-not-cclass-2-closure (chr1 chr2 next)
  (make-text-closure
   :matcher
   #'(lambda (pos)
       #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
                                        #+:lispworks (hcl:fixnum-safety 0)))
       (declare (fixnum pos)
                (character chr1 chr2)
                (ftype (function (fixnum) t) next)
                (special *str* *end*)
                (string *str*) (fixnum *end*))
       (if (< pos *end*)
           (let ((chr (re-char *str* pos)))
             (declare (character chr))
             (if (not (or (char= chr chr1) (char= chr chr2)))
                 (funcall next (1+ pos))))))
   :initializer
   nil
   :linker
   #'(lambda (link-info)
       (setq next (resolve-instr link-info next))) ))


(defun gen-not-cclass-closure (chrs next)
  (make-text-closure
   :matcher
   #'(lambda (pos)
       #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
                                        #+:lispworks (hcl:fixnum-safety 0)))
       (declare (fixnum pos)
                (simple-string chrs)
                (ftype (function (fixnum) t) next)
                (special *str* *end*)
                (string *str*) (fixnum *end*))
       (if (and (< pos *end*)
                (not (find (re-char *str* pos) chrs)))
           (funcall next (1+ pos))))
   :initializer
   nil
   :linker
   #'(lambda (link-info)
       (setq next (resolve-instr link-info next))) ))


(defun gen-specclass-closure (classfn next)
  (make-text-closure
   :matcher
   #'(lambda (pos)
       #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
                                        #+:lispworks (hcl:fixnum-safety 0)))
       (declare (fixnum pos)
                (ftype (function (character) t) classfn)
                (ftype (function (fixnum) t) next)
                (special *str* *end*)
                (string *str*) (fixnum *end*))
       (if (and (< pos *end*)
                (funcall classfn (re-char *str* pos)))
           (funcall next (1+ pos))))
   :initializer
   nil
   :linker
   #'(lambda (link-info)
       (setq next (resolve-instr link-info next))) ))

  
(defun gen-not-specclass-closure (classfn next)
  (make-text-closure
   :matcher
   #'(lambda (pos)
       #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
                                        #+:lispworks (hcl:fixnum-safety 0)))
       (declare (fixnum pos)
                (ftype (function (character) t) classfn)
                (ftype (function (fixnum) t) next)
                (special *str* *end*)
                (string *str*) (fixnum *end*))
       (if (and (< pos *end*)
                (not (funcall classfn (re-char *str* pos))))
           (funcall next (1+ pos))))
   :initializer
   nil
   :linker
   #'(lambda (link-info)
       (setq next (resolve-instr link-info next))) ))


(defun gen-any-closure (next)
  (make-text-closure
   :matcher
   #'(lambda (pos)
       #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
                                        #+:lispworks (hcl:fixnum-safety 0)))
       (declare (fixnum pos)
                (ftype (function (fixnum) t) next)
                (special *str* *end*)
                (fixnum pos *start* *end*)
                (string *str*)
                (type simple-vector *regs*))
       (if (and (< pos *end*)
                (or *dot-matches-newline*
                    (char/= (re-char *str* pos) #\newline)))
           (funcall next (1+ pos))))
   :initializer
   nil
   :linker
   #'(lambda (link-info)
       (setq next (resolve-instr link-info next))) ))


(defun gen-startword-closure (next)
  (make-text-closure
   :matcher
   #'(lambda (pos)
       #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
                                        #+:lispworks (hcl:fixnum-safety 0)))
       (declare (fixnum pos)
                (ftype (function (fixnum) t) next)
                (special *str* *end* *start*)
                (fixnum pos *start* *end* *start*)
                (string *str*)
                (type simple-vector *regs*))
       ;; at start-of-word if previous char is nonword (or
       ;; start-of-str) and this char is word
       (if (and (or (= pos *start*)
                    (and (> pos *start*) (not (wordcharp (re-char *str* (1- pos))))))
                (< pos *end*)
                (wordcharp (re-char *str* pos)))
           (funcall next pos)))
   :initializer
   nil
   :linker
   #'(lambda (link-info)
       (setq next (resolve-instr link-info next))) ))
       

(defun gen-endword-closure (next)
  (make-text-closure
   :matcher
   #'(lambda (pos)
       #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
                                        #+:lispworks (hcl:fixnum-safety 0)))
       (declare (fixnum pos)
                (ftype (function (fixnum) t) next)
                (special *str* *end* *start*)
                (fixnum pos *start* *end* *start*)
                (string *str*)
                (type simple-vector *regs*))
       ;; at end-of-word if previous char is word and this char is
       ;; nonword or end-of-string
       (if (and (> pos *start*)
                (wordcharp (re-char *str* (1- pos)))
                (or (= pos *end*)
                    (and (< pos *end*) (not (wordcharp (re-char *str* pos))))))
           (funcall next pos)))
   :initializer
   nil
   :linker
   #'(lambda (link-info)
       (setq next (resolve-instr link-info next))) ))



(defun gen-hook-closure (hookfn next)
  (make-nontext-closure
   :matcher
   #'(lambda (pos)
       (declare (fixnum pos)
                (ftype (function (fixnum) t) next)
                (special *end* *hooks*)
                (fixnum *end*))
       (let ((rc (cond ((integerp hookfn)
                        (funcall (aref *hooks* hookfn) pos))
                       ((or (functionp hookfn) (symbolp hookfn))
                        (funcall hookfn pos))
                       (t nil))))
         (cond
          ;; user hook returned an integer --> new matching position
          ((integerp rc)
           (funcall next rc))
          ;; user hook returned t --> continue matching at pos
          (rc (funcall next pos))
          ;; user hook returned nil --> fail
          (t nil))))
   :initializer
   nil
   :linker
   #'(lambda (link-info)
       (setq next (resolve-instr link-info next))) ))

(defun gen-lookahead-closure (childfn next)
  (make-nontext-closure
   :matcher
   #'(lambda (pos)
       (declare (fixnum pos)
                (ftype (function (fixnum) t) next)
                (special *end* *hooks*)
                (fixnum *end*))
       (let ((*acceptfn* nil))
         (declare (special *acceptfn*))
         (if (catch 'cease-matching (funcall childfn pos))
             (funcall next pos)
           nil)))
   :initializer
   nil
   :linker
   #'(lambda (link-info)
       (setq childfn (resolve-instr link-info childfn))
       (setq next (resolve-instr link-info next))) ))
       
(defun gen-nlookahead-closure (childfn next)
  (make-nontext-closure
   :matcher
   #'(lambda (pos)
       (declare (fixnum pos)
                (ftype (function (fixnum) t) next)
                (special *end* *hooks*)
                (fixnum *end*))
       (let ((*acceptfn* nil))
         (declare (special *acceptfn*))
         (if (not (catch 'cease-matching (funcall childfn pos)))
             (funcall next pos)
           nil)))
   :initializer
   nil
   :linker
   #'(lambda (link-info)
       (setq childfn (resolve-instr link-info childfn))
       (setq next (resolve-instr link-info next))) ))

(defun gen-success-closure (rc)
  (make-nontext-closure
   :matcher
    #'(lambda (pos)
        #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
                                         #+:lispworks (hcl:fixnum-safety 0)))
        (declare (ignore pos)
                 (special *regs* *acceptfn*)
                 (type simple-vector *regs*))
	(let* ((match-start (register-start *regs* 0))
	       (match-end (register-end *regs* 0))
               (valid-acceptfn-p (or (functionp *acceptfn*)
                                     (and (symbolp *acceptfn*)
                                          (fboundp *acceptfn*)
                                          (functionp (symbol-function *acceptfn*)))))
               (really-succeeded-p (if valid-acceptfn-p
                                       (funcall *acceptfn* match-start match-end)
                                     t)))
          (declare (fixnum match-start match-end))
          (when really-succeeded-p
            (throw 'cease-matching
                   (values rc
                           match-start
                           (- match-end match-start)
                           *regs*)))))
   :initializer
   nil
   :linker
   nil ))


(defun make-init-closure (reset-fns next)
  #'(lambda (pos)
      #-:debug-regex(declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
                                       #+:lispworks (hcl:fixnum-safety 0)))
      (dolist (reset-fn reset-fns)
        (funcall reset-fn))
      (funcall next pos)) )
  




;;;
;;; Special pattern support
;;;

(defun get-spec-pat-fxn (patclass)
  "Return the function to test for the special pattern?"
  (case patclass
    (alpha #'alpha-char-p)
    (upper #'upper-case-p)
    (lower #'lower-case-p)
    (digit #'digit-char-p)
    (alnum #'alphanumericp)
    (xdigit #'xdigitp)
    (odigit #'odigitp)
    (punct #'punctp)
    (space #'spacep)
    (t (error "Couldn't find special class ~S" patclass))))


(defun xdigitp (ch)
  "Is this character a hexidecimal digit?"
  (or (digit-char-p ch)
      (find (char-upcase ch) "ABCDEF")))

(defun odigitp (ch)
  "Is this character an octal digit?"
  (find ch "01234567"))

(defun punctp (ch)
  "Is this character a punctuation mark?"
  (find ch "!.,;:'\"?`")
  )

(defun spacep (ch)
  "Is this character some type of whitespace?"
  (or (char= ch #\tab) (char= ch #\Space) (char= ch #\newline) (char= ch #\return)))

(defun wordcharp (ch)
  (or (alphanumericp ch) (char= ch #\_)))