-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegex.pcode
More file actions
5555 lines (5008 loc) · 181 KB
/
Copy pathRegex.pcode
File metadata and controls
5555 lines (5008 loc) · 181 KB
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
/* This class is a port and enhancement of the ASF regex engine: https://github.com/ECP-Solutions/ASF/blob/main/src/ASF_RegexEngine.cls */
/* Original ASF source license:
MIT License
Copyright (c) 2025 ECP Solutions
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.
*/
class Regex
method Regex();
property Hash LastNamedCaptures get;
property boolean DEBUG_TRACE;
/* Optional Parameters for Init */
property string Pattern get;
property boolean IgnoreCase;
property number MaxSteps;
property boolean Multiline;
property boolean DotAll;
property number SnapshotCount;
property number SnapshotRestoreCount;
property boolean EnableSurrogatePairs;
property array of array of number CaptureRanges get;
/* Public API */
method Init(&pattern As string);
method Split(&subj As string) Returns array of string;
method SplitWithCaptures(&subj As string) Returns array of string;
method Escape(&aInput As string) Returns string;
method Replace(&subj As string, &replacement As string) Returns string;
method Exec(&subj As string) Returns array of string;
method ExecAt(&subj As string, &startPos As number) Returns array of string;
/* Add to the Public API section of the class declaration */
method ExecAll(&subj As string) Returns array of array of string;
method ReplaceFirst(&subj As string, &replacement As string) Returns string;
method Test(&subj As string) Returns boolean;
private
/* Config */
Constant &DEFAULT_MAX_STEPS = 200000;
Constant &MAX_EXEC_TIME = 5;
instance time &execStartTime;
/* State */
instance string &pPattern;
instance boolean &pIgnoreCase;
instance number &pMaxMatchSteps;
instance Hash &pAST;
instance number &pGroupCount;
instance boolean &pMultiline;
instance boolean &pDotAll;
/* Current subject & captures */
instance string &CurSubject;
instance array of number &capsStart;
instance array of number &capsEnd;
instance array of boolean &atomicLocks;
instance number &MatchSteps;
instance Hash &pNamedCaptures;
/* Core Infrastructure */
method SafeMid(&aStr As string, &aStart As number, &aLength As number) Returns string;
method newNode(&typ As string) Returns Hash;
/* Parsing Helpers */
method DetectUnsupportedCommentSyntax(&pat As string);
method ParseEscapeChar(&ch As string) Returns string;
method ParseEscapeNode(&ch As string) Returns Hash;
method FindClassEnd(&pat As string, &startPos As number, &pEnd As number) Returns number;
method FindGroupEnd(&pat As string, &startPos As number, &pEnd As number) Returns number;
method ParseClass(&pat As string, &pStart As number, &pEnd As number) Returns Hash;
method ParseClassFromSimple(&spec As string) Returns Hash;
/* Parser Core */
method ParseSequence(&pat As string, &p As number out, &pEnd As number, &groupCounter As number out, &stopAtPipeOrParen As boolean) Returns Hash;
method ParseAlternation(&pat As string, &p As number out, &pEnd As number, &groupCounter As number out) Returns Hash;
method CompilePattern(&pat As string) Returns Hash;
/* AST Post Processing */
method PropagateFlags(&node As any, &flags As string);
rem method GetGroupCount(&ast As Hash) Returns number;
method TraverseForGroups(&node As any, &maxIdx As number out);
method CollectGroupNodesRecursive(&node As any, &outColl As array of array of any out);
method BuildGroupNodesFromAST(&ast As Hash) Returns array of array of any;
method ComputeMinLen(&node As Hash) Returns number;
method ComputeMaxLen(&node As Hash) Returns number;
method ComputeFixedWidth(&node As Hash) Returns number;
method EnforceFixedWidthLookbehinds(&ast As Hash);
method TraverseEnforce(&node As Hash);
<* method AnnotateLookbehinds(&node As Hash); NOT IMPLEMENTED SINCE WIDTHS ARE SET DURING ENFORCE *>
/* Match Engine Core */
method MatchCharClassNode(&node As Hash, &ch As string) Returns boolean;
method CopyCaps() Returns array of array of number;
method RestoreCaps(&snapshot As array of array of number);
method MatchNode(&node As Hash, &subj As string, &pos As number, &outEnd As number out) Returns boolean;
method MatchSequence(&children As array of any, &subj As string, &pos As number, &outEnd As any out) Returns boolean;
method MatchSeqFrom(&children As array of any, &subj As string, &pos As number, &childIdx As number, &outEnd As any out) Returns boolean;
method FindGroupIndexByName(&ast As Hash, &name As string) Returns number;
method FindGroupIndexByOrdinal(&ast As Hash, &ord As number) Returns number;
method BuildCaptureCollection() Returns array of string;
method ExpandReplacement(&caps As array of string, &repl As string, &namedColl As Hash) Returns string;
method UpdateNamedCapturesFromAST();
method EnsureGroupNodes(&ast As Hash) Returns array of array of any;
/* Operations */
method ReplaceInternal(&subj As string, &replacement As string, &allMatches As boolean) Returns string;
/* Memoization */
instance Hash &failCache;
instance number &nodeIdCounter;
method MarkMemoSafe(&node As any) Returns boolean;
/* AST Optimization */
instance boolean &optDeadNodeElim;
instance boolean &optSingleChildElim;
instance boolean &optQuantSimplify;
instance boolean &optClassSimplify;
instance boolean &optGroupFlatten;
instance boolean &optLitConcat;
instance boolean &optAltFactor;
instance boolean &optAnchoredStart;
instance boolean &optPrefixExtract;
method OptimizeAST(&ast As Hash);
method OptimizeNode(&node As Hash) Returns Hash;
method GetLeadingChar(&node As Hash) Returns string;
method StripLeadingChar(&node As Hash) Returns Hash;
method FlushLitBuffer(&outArr As array of any out, &buf As string, &flags As string);
/* Full unicode surrogate support */
Constant &PUA_START = 57344; /* U+E000 */
Constant &PUA_END = 63743; /* U+F8FF */
Constant &HIGH_SURROGATE_START = 55296; /* U+D800 */
Constant &HIGH_SURROGATE_END = 56319; /* U+DBFF */
Constant &LOW_SURROGATE_START = 56320; /* U+DC00 */
Constant &LOW_SURROGATE_END = 57343; /* U+DFFF */
instance Hash &surrogateToPua;
instance Hash &puaToSurrogate;
instance number &nextPuaCode;
method NormalizeString(&str As string) Returns string;
method DenormalizeString(&str As string) Returns string;
method DenormalizeArray(&arr As array of string) Returns array of string;
method PrintAST(&astNode As any, &indent As string);
method PrintASTCompact(&astNode As any) Returns string;
method DebugTrace(&msg As string);
end-class;
get CaptureRanges
/+ Returns Array2 of Number +/
Local array of array of number &ranges = CreateArrayRept(CreateArrayRept(0, 0), 0);
Local integer &x;
If &capsStart [1] >= 1 And
&capsEnd [1] >= &capsStart [1] Then
&ranges.Push(CreateArray(&capsStart [1], &capsEnd [1]));
Else
If &ranges.Len = 0 Then
&ranges.Push(CreateArray(0, 0));
End-If;
End-If;
Local number &i;
/* Weird here because we want groups 1..N but they are in array slots (1+1)..(N+1) */
For &i = 2 To &pGroupCount + 1
If &capsStart [&i] >= 1 And
&capsEnd [&i] >= &capsStart [&i] Then
&ranges.Push(CreateArray(&capsStart [&i], &capsEnd [&i]));
Else
/* push empty for the group that wasn't used */
&ranges.Push(CreateArray(0, 0));
End-If;
End-For;
Return &ranges;
end-get;
method Regex
%This.IgnoreCase = False;
%This.MaxSteps = &DEFAULT_MAX_STEPS;
%This.Multiline = False;
%This.DotAll = False;
%This.DEBUG_TRACE = False;
%This.EnableSurrogatePairs = True;
/* optimizations */
&optDeadNodeElim = True;
&optSingleChildElim = True;
&optQuantSimplify = True;
&optClassSimplify = True;
&optGroupFlatten = True;
&optLitConcat = True;
&optAltFactor = True;
&optAnchoredStart = True;
&optPrefixExtract = True;
end-method;
method Init
/+ &pattern as String +/
&pPattern = &pattern;
&pIgnoreCase = %This.IgnoreCase;
&pMultiline = %This.Multiline;
&pDotAll = %This.DotAll;
&nodeIdCounter = 0;
If (%This.MaxSteps <= 0) Then
&pMaxMatchSteps = &DEFAULT_MAX_STEPS;
Else
&pMaxMatchSteps = %This.MaxSteps;
End-If;
/* ── NEW: Initialize normalization state ── */
&surrogateToPua = CreateHash();
&puaToSurrogate = CreateHash();
&nextPuaCode = &PUA_START;
/* ── NEW: Normalize pattern if FullUnicodeSupport is on ── */
Local string &compilePat = &pPattern;
If %This.EnableSurrogatePairs Then
&compilePat = %This.NormalizeString(&pPattern);
End-If;
&pAST = %This.CompilePattern(&compilePat);
If (&pAST <> Null) Then
If ( Not &pAST.Exists("groupNodes")) Then
Local array of array of any &groupNodes = %This.BuildGroupNodesFromAST(&pAST);
End-If;
End-If;
If &DEBUG_TRACE Then
%Response.Write("<br />");
%Response.Write("AST After Optimizations: <br />");
rem %This.PrintAST(&pAST, "");
%Response.Write(%This.PrintASTCompact(&pAST.GetValue("body", True)));
End-If;
%This.EnforceFixedWidthLookbehinds(&pAST);
rem &pGroupCount = %This.GetGroupCount(&pAST);
&pGroupCount = &pAST.GetValue("groupCount", True);
rem %Response.WriteLine("GetGroupCount = " | &pGroupCount);
end-method;
get LastNamedCaptures
/+ Returns Hash +/
If %This.EnableSurrogatePairs Then
Local string &k;
Local array of string &v;
&pNamedCaptures.IterStart();
While &pNamedCaptures.IterNext(&k, &v)
&v [2] = %This.DenormalizeString(&v [2]);
End-While;
End-If;
Return &pNamedCaptures;
end-get;
get Pattern
/+ Returns String +/
Return &pPattern;
end-get;
/* Node helpers - We can probably optimize these out after the port. */
method newNode
/+ &typ as String +/
/+ Returns Hash +/
Local Hash &h = CreateHash();
&h.SetValue("type", &typ);
&nodeIdCounter = &nodeIdCounter + 1;
&h.SetValue("nodeId", &nodeIdCounter);
Return &h;
end-method;
method CompilePattern
/+ &pat as String +/
/+ Returns Hash +/
%This.DetectUnsupportedCommentSyntax(&pat);
Local Hash &ast = CreateHash();
&ast.SetValue("raw", &pat);
Local number &pos = 1;
Local number &groupCounter = 0;
Local Hash &body = %This.ParseAlternation(&pat, &pos, Len(&pat), &groupCounter);
&ast.SetValue("body", &body);
If &DEBUG_TRACE Then
%Response.Write("<br />");
%Response.Write("Original AST: <br />");
%This.PrintAST(&ast, "");
End-If;
If &DEBUG_TRACE Then
%Response.Write("<br />");
%Response.Write("AST Before Optimizations: <br />");
rem %This.PrintAST(&pAST, "");
%Response.Write(%This.PrintASTCompact(&ast.GetValue("body", True)));
End-If;
/* Store group count BEFORE optimization can eliminate group nodes */
&ast.SetValue("groupCount", &groupCounter);
%This.OptimizeAST(&ast);
/* ── Ensure body is always a seq ──
This guarantees quantifiers always route through
MatchSeqFrom which has proper cross-occurrence
backtracking, rather than MatchNode's simpler path. */
&body = &ast.GetValue("body", True);
If &body.GetValue("type", True) <> "seq" Then
Local Hash &wrapSeq = %This.newNode("seq");
Local array of any &wrapKids = CreateArrayAny();
&wrapKids.Push(&body);
&wrapSeq.SetValue("child", &wrapKids);
&ast.SetValue("body", &wrapSeq);
End-If;
Local boolean &_ = %This.MarkMemoSafe(&ast.GetValue("body", True));
Local number &mlen = %This.ComputeMinLen(&body);
&ast.SetValue("minLen", &mlen);
Return *
end-method;
method ParseAlternation
/+ &pat as String, +/
/+ &p as Number out, +/
/+ &pEnd as Number, +/
/+ &groupCounter as Number out +/
/+ Returns Hash +/
Local Hash &firstSeq = %This.ParseSequence(&pat, &p, &pEnd, &groupCounter, False);
If ( Not (&p <= &pEnd And
%This.SafeMid(&pat, &p, 1) = "|")) Then
Return &firstSeq;
End-If;
Local Hash &altNode = %This.newNode("alt");
Local array of any &alts = CreateArrayAny();
&alts.Push(&firstSeq);
While &p <= &pEnd And
%This.SafeMid(&pat, &p, 1) = "|"
&p = &p + 1;
Local Hash &nextSeq = %This.ParseSequence(&pat, &p, &pEnd, &groupCounter, False);
&alts.Push(&nextSeq);
End-While;
&altNode.SetValue("child", &alts);
Return &altNode;
end-method;
method ParseSequence
/+ &pat as String, +/
/+ &p as Number out, +/
/+ &pEnd as Number, +/
/+ &groupCounter as Number out, +/
/+ &stopAtPipeOrParen as Boolean +/
/+ Returns Hash +/
Local Hash &seq = %This.newNode("seq");
Local array of any &children = CreateArrayAny();
Local number &thisIdx;
Local string &ch;
Local Hash &node;
While &p <= &pEnd
&ch = %This.SafeMid(&pat, &p, 1);
If (&ch = ")" Or
&ch = "|") Then
Break;
End-If;
&node = Null;
Evaluate &ch
When = "\"
&p = &p + 1;
If &p > &pEnd Then
Break;
End-If;
Local string &escCh = %This.SafeMid(&pat, &p, 1);
Local number &escCode = Code(&escCh);
/* ── Numeric backreference: \1 through \99 ── */
If &escCode >= 49 And
&escCode <= 57 Then
/* Digit 1-9: collect all following digits */
Local string &refDigits = &escCh;
Local number &peekPos = &p + 1;
While &peekPos <= &pEnd
Local string &peekCh = %This.SafeMid(&pat, &peekPos, 1);
Local number &peekCode = Code(&peekCh);
If &peekCode >= 48 And
&peekCode <= 57 Then
&refDigits = &refDigits | &peekCh;
&peekPos = &peekPos + 1;
Else
Break;
End-If;
End-While;
/* Greedy two-digit resolution: if two+ digits collected,
try the full number first. If only valid as smaller,
we resolve at match time — store the largest number.
At match time, if the index exceeds group count,
fall back to first digit only. */
Local number &refNum = Value(&refDigits);
&node = %This.newNode("backref");
&node.SetValue("idx", &refNum);
/* Store raw digits so match-time can do fallback */
&node.SetValue("raw", &refDigits);
&p = &p + Len(&refDigits);
Else
If &escCh = "k" Then
/* ── Named backreference: \k<name> or \k'name' ── */
&p = &p + 1;
If &p <= &pEnd Then
Local string &kDelim = %This.SafeMid(&pat, &p, 1);
Local string &kClose = "";
If &kDelim = "<" Then
&kClose = ">";
Else
If &kDelim = "'" Then
&kClose = "'";
Else
/* Not a valid \k sequence — treat \k as literal "k" */
&node = %This.newNode("lit");
&node.SetValue("ch", "k");
/* &p already advanced past 'k' */
End-If;
End-If;
If &kClose <> "" And
Len(&kClose) > 0 Then
&p = &p + 1;
Local string &kName = "";
While &p <= &pEnd
Local string &kc = %This.SafeMid(&pat, &p, 1);
If &kc = &kClose Then
&p = &p + 1;
Break;
End-If;
&kName = &kName | &kc;
&p = &p + 1;
End-While;
If Len(&kName) > 0 Then
&node = %This.newNode("backref");
&node.SetValue("name", &kName);
Else
/* Empty name — treat as literal */
&node = %This.newNode("lit");
&node.SetValue("ch", "k");
End-If;
End-If;
Else
/* \k at end of pattern — literal */
&node = %This.newNode("lit");
&node.SetValue("ch", "k");
End-If;
Else
/* ── All other escapes: delegate to existing handler ── */
&node = %This.ParseEscapeNode(&escCh);
&p = &p + 1;
End-If;
End-If;
Break;
When = "."
&node = %This.newNode("dot");
&p = &p + 1;
Break;
When = "["
Local number &clsEnd = %This.FindClassEnd(&pat, &p, &pEnd);
If &clsEnd = - 1 Then
&node = %This.newNode("lit");
&node.SetValue("ch", "[");
&p = &p + 1;
Else
&node = %This.ParseClass(&pat, &p + 1, &clsEnd - 1);
&p = &clsEnd + 1;
End-If;
Break;
When = "^"
&node = %This.newNode("anchor_start");
&p = &p + 1;
Break;
When = "$"
&node = %This.newNode("anchor_end");
&p = &p + 1;
Break;
When = "("
/* Locate matching ')' deterministically */
Local number &foundClose = %This.FindGroupEnd(&pat, &p, &pEnd);
If (&foundClose = - 1) Then
&node = %This.newNode("lit");
&node.SetValue("ch", "(");
&p = &p + 1;
Else
/* Consume '(' */
&p = &p + 1;
If (&p <= &pEnd) And
%This.SafeMid(&pat, &p, 1) = "?" Then
&p = &p + 1;
/* Support for (?P<name> named groups */
If (&p + 1 <= &pEnd And
%This.SafeMid(&pat, &p, 1) = "P" And
%This.SafeMid(&pat, &p + 1, 1) = "<") Then
&p = &p + 1;
End-If;
If (&p > &pEnd) Then
&node = %This.newNode("lit");
&node.SetValue("ch", "(");
Else
Local string &nextCh = %This.SafeMid(&pat, &p, 1);
Evaluate &nextCh
When "="
&p = &p + 1;
Local Hash &innerLA = %This.ParseSequence(&pat, &p, &foundClose - 1, &groupCounter, False);
If (&p <= &pEnd And
%This.SafeMid(&pat, &p, 1) = ")") Then
&p = &p + 1;
End-If;
Local Hash &laNode = %This.newNode("lookahead_pos");
&laNode.SetValue("child", &innerLA);
&node = &laNode;
Break;
When "!"
&p = &p + 1;
Local Hash &innerNL = %This.ParseSequence(&pat, &p, &foundClose - 1, &groupCounter, False);
If (&p <= &pEnd And
%This.SafeMid(&pat, &p, 1) = ")") Then
&p = &p + 1;
End-If;
Local Hash &nlaNode = %This.newNode("lookahead_neg");
&nlaNode.SetValue("child", &innerNL);
&node = &nlaNode;
Break;
When "<"
&p = &p + 1;
If &p <= &pEnd Then
Local string &sbch = %This.SafeMid(&pat, &p, 1);
Evaluate &sbch
When = "="
&p = &p + 1;
Local Hash &innerLB = %This.ParseSequence(&pat, &p, &foundClose - 1, &groupCounter, False);
If (&p < &pEnd And
%This.SafeMid(&pat, &p, 1) = ")") Then
&p = &p + 1;
End-If;
Local Hash &lbNode = %This.newNode("lookbehind_pos");
&lbNode.SetValue("child", &innerLB);
&node = &lbNode;
Break;
When = "!"
&p = &p + 1;
Local Hash &innerLBN = %This.ParseSequence(&pat, &p, &foundClose - 1, &groupCounter, False);
If (&p < &pEnd And
%This.SafeMid(&pat, &p, 1) = ")") Then
&p = &p + 1;
End-If;
Local Hash &lbnNode = %This.newNode("lookbehind_neg");
&lbnNode.SetValue("child", &innerLBN);
&node = &lbnNode;
Break;
When-Other
/* Named capture form (?<name>...) */
Local string &nameBuf = "";
While &p <= &pEnd
Local string &nc = %This.SafeMid(&pat, &p, 1);
Local number &ncC = Code(&nc);
If (&nc = ">") Then
Break;
End-If;
/* First char: a-z A-Z _ only (no digits) */
If Len(&nameBuf) = 0 Then
If (&ncC >= 97 And
&ncC <= 122) Or
(&ncC >= 65 And
&ncC <= 90) Or
(&ncC = 95) Then
&nameBuf = &nameBuf | &nc;
&p = &p + 1;
Else
Break;
End-If;
Else
/* Subsequent chars: a-z A-Z 0-9 _ - */
If (&ncC >= 97 And
&ncC <= 122) Or
(&ncC >= 65 And
&ncC <= 90) Or
(&ncC >= 48 And
&ncC <= 57) Or
(&ncC = 95) Or
(&ncC = 45) Then
&nameBuf = &nameBuf | &nc;
&p = &p + 1;
Else
Break;
End-If;
End-If;
End-While;
If &p < &pEnd And
%This.SafeMid(&pat, &p, 1) = ">" And
Len(&nameBuf) > 0 Then
&p = &p + 1;
&groupCounter = &groupCounter + 1;
&thisIdx = &groupCounter;
Local Hash &innerNamed = %This.ParseSequence(&pat, &p, &foundClose - 1, &groupCounter, False);
If (&p <= &pEnd And
%This.SafeMid(&pat, &p, 1) = ")") Then
&p = &p + 1;
End-If;
Local Hash &gnodeNamed = %This.newNode("group");
&gnodeNamed.SetValue("idx", &thisIdx);
&gnodeNamed.SetValue("child", &innerNamed);
&gnodeNamed.SetValue("name", &nameBuf);
&node = &gnodeNamed;
If &DEBUG_TRACE Then
MessageBox(0, "", 0, 0, "TRACE PARSE named-group -> name=" | &nameBuf | " idx=" | &thisIdx);
End-If;
Else
&node = %This.newNode("lit");
&node.SetValue("ch", "(");
End-If;
Break;
End-Evaluate;
Else
&node = %This.newNode("lit");
&node.SetValue("ch", "(");
End-If;
Break;
When ">"
&p = &p + 1;
Local Hash &innerAtomic = %This.ParseSequence(&pat, &p, &foundClose - 1, &groupCounter, False);
If (&p <= &pEnd And
%This.SafeMid(&pat, &p, 1) = ")") Then
&p = &p + 1;
End-If;
Local Hash &atomicNode = %This.newNode("atomic");
&atomicNode.SetValue("child", &innerAtomic);
&node = &atomicNode;
Break;
When ":"
&p = &p + 1;
Local Hash &innerNC = %This.ParseSequence(&pat, &p, &foundClose - 1, &groupCounter, False);
If (&p <= &pEnd And
%This.SafeMid(&pat, &p, 1) = ")") Then
&p = &p + 1;
End-If;
Local Hash &ncNode = %This.newNode("group");
&ncNode.SetValue("child", &innerNC);
&ncNode.SetValue("noncap_syntax", True);
&node = &ncNode;
Break;
When "R"
&p = &p + 1;
If (&p <= &pEnd And
%This.SafeMid(&pat, &p, 1) = ")") Then
&p = &p + 1;
End-If;
Local Hash &rnode = %This.newNode("recurse");
&rnode.SetValue("target", 0);
&node = &rnode;
Break;
When "("
&p = &p + 1;
Local number &condStart = &p;
Local number &condEnd = - 1;
While &p <= &pEnd
If %This.SafeMid(&pat, &p, 1) = ")" Then
&condEnd = &p - 1;
Break;
End-If;
&p = &p + 1;
End-While;
If &condEnd = - 1 Then
&node = %This.newNode("lit");
&node.SetValue("ch", "(");
Else
Local string &condText = Substring(&pat, &condStart, &condEnd - &condStart + 1);
&p = &p + 1;
Local Hash &yesNode = %This.ParseSequence(&pat, &p, &foundClose - 1, &groupCounter, True);
Local Hash &noNode = Null;
If (&p <= &pEnd And
%This.SafeMid(&pat, &p, 1) = "|") Then
&p = &p + 1;
&noNode = %This.ParseSequence(&pat, &p, &foundClose - 1, &groupCounter, True);
End-If;
If &foundClose <= &pEnd Then
&p = &foundClose + 1;
End-If;
Local Hash &condNode = %This.newNode("cond");
&condNode.SetValue("cond", &condText);
&condNode.SetValue("yes", &yesNode);
If (&noNode <> Null) Then
&condNode.SetValue("no", &noNode);
End-If;
&node = &condNode;
End-If;
Break;
When-Other
/* recursion by number or inline flags */
/* Char is >= "0" and <= "9" */
If (Code(%This.SafeMid(&pat, &p, 1)) >= 48 And
Code(%This.SafeMid(&pat, &p, 1)) <= 57) Then
Local string &ds = "";
While &p <= &pEnd
Local string &dc = %This.SafeMid(&pat, &p, 1);
If (Code(&dc) >= 48 And
Code(&dc) <= 57) Then
&ds = &ds | &dc;
&p = &p + 1;
Else
Break;
End-If;
End-While;
If (&p <= &pEnd And
%This.SafeMid(&pat, &p, 1) = ")") Then
&p = &p + 1;
End-If;
Local Hash &rn = %This.newNode("recurse");
&rn.SetValue("target", Value(&ds));
&node = &rn;
Else
Local string &flagsStr = "";
While &p <= &pEnd
Local string &fch = %This.SafeMid(&pat, &p, 1);
Local number &fchC = Code(&fch);
/* a-z A-Z - */
If (&fchC >= 97 And
&fchC <= 122) Or
(&fchC >= 65 And
&fchC <= 90) Or
(&fchC = 45) Then
&flagsStr = &flagsStr | &fch;
&p = &p + 1;
Else
Break;
End-If;
End-While;
If (&p <= &pEnd And
%This.SafeMid(&pat, &p, 1) = ":") Then
&p = &p + 1;
Local Hash &innerFlagged = %This.ParseSequence(&pat, &p, &foundClose - 1, &groupCounter, False);
If (&p <= &pEnd And
%This.SafeMid(&pat, &p, 1) = ")") Then
&p = &p + 1;
End-If;
Local Hash &gnodeF = %This.newNode("group");
&gnodeF.SetValue("noncap_syntax", True);
Local string &normFlags = "";
Local number &kch;
For &kch = 1 To Len(&flagsStr);
Local string &fc = Lower(Substring(&flagsStr, &kch, 1));
If (&fc = "i" Or
&fc = "m" Or
&fc = "s" Or
&fc = "-") Then
&normFlags = &normFlags | &fc;
End-If;
End-For;
If (Len(&normFlags) > 0) Then
&gnodeF.SetValue("flags_on", &normFlags);
%This.PropagateFlags(&innerFlagged, &normFlags);
End-If;
&gnodeF.SetValue("child", &innerFlagged);
&node = &gnodeF;
Else
throw CreateException(0, 0, "Inline flags without ':' are not supported at compile time (use (?i:...) for group-scoped flags).");
End-If;
End-If;
Break;
End-Evaluate;
End-If;
Else
/* normal capturing group */
&groupCounter = &groupCounter + 1;
&thisIdx = &groupCounter;
Local Hash &inner = %This.ParseSequence(&pat, &p, &foundClose - 1, &groupCounter, False);
If (&p <= &pEnd And
%This.SafeMid(&pat, &p, 1) = ")") Then
&p = &p + 1;
End-If;
Local Hash &gnode = %This.newNode("group");
&gnode.SetValue("idx", &thisIdx);
&gnode.SetValue("child", &inner);
&node = &gnode;
End-If;
/* defensive: if inner parsing didn't advance to foundClose, jump past it */
If (&foundClose <> - 1) Then
If (&p <= &foundClose And
%This.SafeMid(&pat, &p, 1) <> ")") Then
&p = &foundClose + 1;
End-If;
End-If;
End-If;
Break;
When-Other
&node = %This.newNode("lit");
&node.SetValue("ch", &ch);
&p = &p + 1;
Break;
End-Evaluate;
/* quantifier (same semantics as original) */
If (&node <> Null And
&p <= &pEnd) Then
Local string &qch = %This.SafeMid(&pat, &p, 1);
If (&qch = "*" Or
&qch = "+" Or
&qch = "?" Or
&qch = "{") Then
Local Hash &qnode = %This.newNode("quant");
&qnode.SetValue("child", &node);
&qnode.SetValue("possessive", False);
Evaluate &qch
When = "*"
&qnode.SetValue("min", 0);
&qnode.SetValue("max", - 1);
&p = &p + 1;
Break;
When = "+"
&qnode.SetValue("min", 1);
&qnode.SetValue("max", - 1);
&p = &p + 1;
Break;
When = "?"
&qnode.SetValue("min", 0);
&qnode.SetValue("max", 1);
&p = &p + 1;
Break;
When = "{"
Local number &closePos = Find("}", &pat, &p);
If (&closePos > 0) Then
Local string &content = %This.SafeMid(&pat, &p + 1, &closePos - &p - 1);
Local array of string &parts = Split(&content, ",");
Local number &mi, &ma;
If (&parts.Len = 1) Then
If (Len(RTrim(LTrim(&parts [1]))) = 0) Then
&mi = 0;
&ma = - 1;
Else
&mi = Value(RTrim(LTrim(&parts [1])));
/* {1,} produces only 1 element in the split, so we need to see if the , was at the end */
If (Right(RTrim(&content), 1) = ",") Then
&ma = - 1; /* Unbounded max */
Else
&ma = &mi;
End-If;
End-If;
Else
&mi = Value(RTrim(LTrim(&parts [1])));
&ma = Value(RTrim(LTrim(&parts [2])));
End-If;
&qnode.SetValue("min", &mi);
&qnode.SetValue("max", &ma);
&p = &closePos + 1;
Else
&qnode.SetValue("min", 0);
&qnode.SetValue("max", 1);
&p = &p + 1;
End-If;
Break;
End-Evaluate;
If (&p <= &pEnd) Then
Local string &nextQ = %This.SafeMid(&pat, &p, 1);
Evaluate &nextQ
When = "?"
&qnode.SetValue("lazy", True);
&p = &p + 1;
Break;
When = "+"
&qnode.SetValue("possessive", True);
&qnode.SetValue("lazy", False);
&p = &p + 1;
Break;
When-Other
&qnode.SetValue("lazy", False);
Break;
End-Evaluate;
Else
&qnode.SetValue("lazy", False);
End-If;
&node = &qnode;
End-If;
End-If;
If &node <> Null Then
&children.Push(&node);
End-If;
End-While;
&seq.SetValue("child", &children);
If Not &stopAtPipeOrParen Then
If (&p < &pEnd And
%This.SafeMid(&pat, &p, 1) = "|") Then
Local Hash &altNode = %This.newNode("alt");
Local array of any &alts = CreateArrayAny();
&alts.Push(&seq);
While &p <= &pEnd And
%This.SafeMid(&pat, &p, 1) = "|"
&p = &p + 1;
Local Hash &nextSeq = %This.ParseSequence(&pat, &p, &pEnd, &groupCounter, False);
&alts.Push(&nextSeq);
End-While;
&altNode.SetValue("child", &alts);
Return &altNode;
End-If;
End-If;
Return &seq;
end-method;
method SafeMid
/+ &aStr as String, +/
/+ &aStart as Number, +/
/+ &aLength as Number +/
/+ Returns String +/
If (&aLength = - 1) Then
&aLength = Len(&aStr) - &aStart + 1;
End-If;
If (&aStart >= 1) Then
Local string &ch = Substring(&aStr, &aStart, &aLength);
Return &ch;
Else
Return "";
End-If;
end-method;
method DetectUnsupportedCommentSyntax
/+ &pat as String +/
Local number &i;
Local number &L = Len(&pat);
Local boolean &esc = False;
Local boolean &inClass = False;
For &i = 1 To &L
Local string &c = Substring(&pat, &i, 1);
If (&esc) Then
&esc = False;
Continue;
End-If;
If (&c = "\") Then
&esc = True;
Continue;
End-If;
If (&inClass) Then
If &c = "]" Then
&inClass = False;
End-If;
Continue;