forked from garduino/Cuis-Smalltalk-RegEx
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Regex-Core.pck.st
3761 lines (2895 loc) · 124 KB
/
Regex-Core.pck.st
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
'From Cuis 5.0 [latest update: #4689] on 2 August 2021 at 7:50:22 pm'!
'Description Please enter a description for this package '!
!provides: 'Regex-Core' 1 4!
!requires: 'Cuis-Base' 50 4689 nil!
SystemOrganization addCategory: #'Regex-Core'!
SystemOrganization addCategory: #'Regex-Core-Exceptions'!
!classDefinition: #RegexError category: #'Regex-Core-Exceptions'!
Error subclass: #RegexError
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'Regex-Core-Exceptions'!
!classDefinition: 'RegexError class' category: #'Regex-Core-Exceptions'!
RegexError class
instanceVariableNames: ''!
!classDefinition: #RegexCompilationError category: #'Regex-Core-Exceptions'!
RegexError subclass: #RegexCompilationError
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'Regex-Core-Exceptions'!
!classDefinition: 'RegexCompilationError class' category: #'Regex-Core-Exceptions'!
RegexCompilationError class
instanceVariableNames: ''!
!classDefinition: #RegexMatchingError category: #'Regex-Core-Exceptions'!
RegexError subclass: #RegexMatchingError
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'Regex-Core-Exceptions'!
!classDefinition: 'RegexMatchingError class' category: #'Regex-Core-Exceptions'!
RegexMatchingError class
instanceVariableNames: ''!
!classDefinition: #RegexSyntaxError category: #'Regex-Core-Exceptions'!
RegexError subclass: #RegexSyntaxError
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'Regex-Core-Exceptions'!
!classDefinition: 'RegexSyntaxError class' category: #'Regex-Core-Exceptions'!
RegexSyntaxError class
instanceVariableNames: ''!
!classDefinition: #RxCharSetParser category: #'Regex-Core'!
Object subclass: #RxCharSetParser
instanceVariableNames: 'source lookahead elements'
classVariableNames: ''
poolDictionaries: ''
category: 'Regex-Core'!
!classDefinition: 'RxCharSetParser class' category: #'Regex-Core'!
RxCharSetParser class
instanceVariableNames: ''!
!classDefinition: #RxMatchOptimizer category: #'Regex-Core'!
Object subclass: #RxMatchOptimizer
instanceVariableNames: 'ignoreCase prefixes nonPrefixes conditions testBlock methodPredicates nonMethodPredicates predicates nonPredicates'
classVariableNames: ''
poolDictionaries: ''
category: 'Regex-Core'!
!classDefinition: 'RxMatchOptimizer class' category: #'Regex-Core'!
RxMatchOptimizer class
instanceVariableNames: ''!
!classDefinition: #RxMatcher category: #'Regex-Core'!
Object subclass: #RxMatcher
instanceVariableNames: 'matcher ignoreCase startOptimizer stream markerPositions markerCount lastResult'
classVariableNames: 'Cr Lf'
poolDictionaries: ''
category: 'Regex-Core'!
!classDefinition: 'RxMatcher class' category: #'Regex-Core'!
RxMatcher class
instanceVariableNames: ''!
!classDefinition: #RxParser category: #'Regex-Core'!
Object subclass: #RxParser
instanceVariableNames: 'input lookahead'
classVariableNames: 'BackslashConstants BackslashSpecials'
poolDictionaries: ''
category: 'Regex-Core'!
!classDefinition: 'RxParser class' category: #'Regex-Core'!
RxParser class
instanceVariableNames: ''!
!classDefinition: #RxmLink category: #'Regex-Core'!
Object subclass: #RxmLink
instanceVariableNames: 'next'
classVariableNames: ''
poolDictionaries: ''
category: 'Regex-Core'!
!classDefinition: 'RxmLink class' category: #'Regex-Core'!
RxmLink class
instanceVariableNames: ''!
!classDefinition: #RxmBranch category: #'Regex-Core'!
RxmLink subclass: #RxmBranch
instanceVariableNames: 'loopback alternative'
classVariableNames: ''
poolDictionaries: ''
category: 'Regex-Core'!
!classDefinition: 'RxmBranch class' category: #'Regex-Core'!
RxmBranch class
instanceVariableNames: ''!
!classDefinition: #RxmMarker category: #'Regex-Core'!
RxmLink subclass: #RxmMarker
instanceVariableNames: 'index'
classVariableNames: ''
poolDictionaries: ''
category: 'Regex-Core'!
!classDefinition: 'RxmMarker class' category: #'Regex-Core'!
RxmMarker class
instanceVariableNames: ''!
!classDefinition: #RxmPredicate category: #'Regex-Core'!
RxmLink subclass: #RxmPredicate
instanceVariableNames: 'predicate'
classVariableNames: ''
poolDictionaries: ''
category: 'Regex-Core'!
!classDefinition: 'RxmPredicate class' category: #'Regex-Core'!
RxmPredicate class
instanceVariableNames: ''!
!classDefinition: #RxmSpecial category: #'Regex-Core'!
RxmLink subclass: #RxmSpecial
instanceVariableNames: 'matchSelector'
classVariableNames: ''
poolDictionaries: ''
category: 'Regex-Core'!
!classDefinition: 'RxmSpecial class' category: #'Regex-Core'!
RxmSpecial class
instanceVariableNames: ''!
!classDefinition: #RxmSubstring category: #'Regex-Core'!
RxmLink subclass: #RxmSubstring
instanceVariableNames: 'sample compare'
classVariableNames: ''
poolDictionaries: ''
category: 'Regex-Core'!
!classDefinition: 'RxmSubstring class' category: #'Regex-Core'!
RxmSubstring class
instanceVariableNames: ''!
!classDefinition: #RxmTerminator category: #'Regex-Core'!
Object subclass: #RxmTerminator
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'Regex-Core'!
!classDefinition: 'RxmTerminator class' category: #'Regex-Core'!
RxmTerminator class
instanceVariableNames: ''!
!classDefinition: #RxsNode category: #'Regex-Core'!
Object subclass: #RxsNode
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'Regex-Core'!
!classDefinition: 'RxsNode class' category: #'Regex-Core'!
RxsNode class
instanceVariableNames: ''!
!classDefinition: #RxsBranch category: #'Regex-Core'!
RxsNode subclass: #RxsBranch
instanceVariableNames: 'piece branch'
classVariableNames: ''
poolDictionaries: ''
category: 'Regex-Core'!
!classDefinition: 'RxsBranch class' category: #'Regex-Core'!
RxsBranch class
instanceVariableNames: ''!
!classDefinition: #RxsCharSet category: #'Regex-Core'!
RxsNode subclass: #RxsCharSet
instanceVariableNames: 'negated elements'
classVariableNames: ''
poolDictionaries: ''
category: 'Regex-Core'!
!classDefinition: 'RxsCharSet class' category: #'Regex-Core'!
RxsCharSet class
instanceVariableNames: ''!
!classDefinition: #RxsCharacter category: #'Regex-Core'!
RxsNode subclass: #RxsCharacter
instanceVariableNames: 'character'
classVariableNames: ''
poolDictionaries: ''
category: 'Regex-Core'!
!classDefinition: 'RxsCharacter class' category: #'Regex-Core'!
RxsCharacter class
instanceVariableNames: ''!
!classDefinition: #RxsContextCondition category: #'Regex-Core'!
RxsNode subclass: #RxsContextCondition
instanceVariableNames: 'kind'
classVariableNames: ''
poolDictionaries: ''
category: 'Regex-Core'!
!classDefinition: 'RxsContextCondition class' category: #'Regex-Core'!
RxsContextCondition class
instanceVariableNames: ''!
!classDefinition: #RxsEpsilon category: #'Regex-Core'!
RxsNode subclass: #RxsEpsilon
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'Regex-Core'!
!classDefinition: 'RxsEpsilon class' category: #'Regex-Core'!
RxsEpsilon class
instanceVariableNames: ''!
!classDefinition: #RxsMessagePredicate category: #'Regex-Core'!
RxsNode subclass: #RxsMessagePredicate
instanceVariableNames: 'selector negated'
classVariableNames: ''
poolDictionaries: ''
category: 'Regex-Core'!
!classDefinition: 'RxsMessagePredicate class' category: #'Regex-Core'!
RxsMessagePredicate class
instanceVariableNames: ''!
!classDefinition: #RxsPiece category: #'Regex-Core'!
RxsNode subclass: #RxsPiece
instanceVariableNames: 'atom min max'
classVariableNames: ''
poolDictionaries: ''
category: 'Regex-Core'!
!classDefinition: 'RxsPiece class' category: #'Regex-Core'!
RxsPiece class
instanceVariableNames: ''!
!classDefinition: #RxsPredicate category: #'Regex-Core'!
RxsNode subclass: #RxsPredicate
instanceVariableNames: 'predicate negation'
classVariableNames: 'EscapedLetterSelectors NamedClassSelectors'
poolDictionaries: ''
category: 'Regex-Core'!
!classDefinition: 'RxsPredicate class' category: #'Regex-Core'!
RxsPredicate class
instanceVariableNames: ''!
!classDefinition: #RxsRange category: #'Regex-Core'!
RxsNode subclass: #RxsRange
instanceVariableNames: 'first last'
classVariableNames: ''
poolDictionaries: ''
category: 'Regex-Core'!
!classDefinition: 'RxsRange class' category: #'Regex-Core'!
RxsRange class
instanceVariableNames: ''!
!classDefinition: #RxsRegex category: #'Regex-Core'!
RxsNode subclass: #RxsRegex
instanceVariableNames: 'branch regex'
classVariableNames: ''
poolDictionaries: ''
category: 'Regex-Core'!
!classDefinition: 'RxsRegex class' category: #'Regex-Core'!
RxsRegex class
instanceVariableNames: ''!
!RegexError commentStamp: 'Tbn 11/12/2010 22:37' prior: 0!
This is a common superclass for errors in regular expressions.!
!RegexCompilationError commentStamp: 'Tbn 11/12/2010 22:38' prior: 0!
This class represents compilation errors in regular expressions.!
!RegexMatchingError commentStamp: 'Tbn 11/12/2010 22:38' prior: 0!
This class represents matching errors in regular expressions.!
!RegexSyntaxError commentStamp: 'Tbn 11/12/2010 22:38' prior: 0!
This class represents syntax errors in regular expressions.!
!RxCharSetParser commentStamp: 'Tbn 11/12/2010 23:13' prior: 0!
-- Regular Expression Matcher v 1.1 (C) 1996, 1999 Vassili Bykov
--
I am a parser created to parse the insides of a character set ([...]) construct. I create and answer a collection of "elements", each being an instance of one of: RxsCharacter, RxsRange, or RxsPredicate.
Instance Variables:
source <Stream> open on whatever is inside the square brackets we have to parse.
lookahead <Character> The current lookahead character
elements <Collection of: <RxsCharacter|RxsRange|RxsPredicate>> Parsing result!
!RxMatchOptimizer commentStamp: 'Tbn 11/12/2010 23:13' prior: 0!
-- Regular Expression Matcher v 1.1 (C) 1996, 1999 Vassili Bykov
--
A match start optimizer, handy for searching a string. Takes a regex syntax tree and sets itself up so that prefix characters or matcher states that cannot start a match are later recognized with #canStartMatch:in: method.
Used by RxMatcher, but can be used by other matchers (if implemented) as well.!
!RxMatcher commentStamp: 'Tbn 11/12/2010 23:13' prior: 0!
-- Regular Expression Matcher v 1.1 (C) 1996, 1999 Vassili Bykov
--
This is a recursive regex matcher. Not strikingly efficient, but simple. Also, keeps track of matched subexpressions. The life cycle goes as follows:
1. Initialization. Accepts a syntax tree (presumably produced by RxParser) and compiles it into a matcher built of other classes in this category.
2. Matching. Accepts a stream or a string and returns a boolean indicating whether the whole stream or its prefix -- depending on the message sent -- matches the regex.
3. Subexpression query. After a successful match, and before any other match, the matcher may be queried about the range of specific stream (string) positions that matched to certain parenthesized subexpressions of the original expression.
Any number of queries may follow a successful match, and any number or matches may follow a successful initialization.
Note that `matcher' is actually a sort of a misnomer. The actual matcher is a web of Rxm* instances built by RxMatcher during initialization. RxMatcher is just the interface facade of this network. It is also a builder of it, and also provides a stream-like protocol to easily access the stream being matched.
Instance variables:
matcher <RxmLink> The entry point into the actual matcher.
stream <Stream> The stream currently being matched against.
markerPositions <Array of: Integer> Positions of markers' matches.
markerCount <Integer> Number of markers.
lastResult <Boolean> Whether the latest match attempt succeeded or not.
lastChar <Character | nil> character last seen in the matcher stream!
!RxParser commentStamp: 'Tbn 11/12/2010 23:13' prior: 0!
-- Regular Expression Matcher v 1.1 (C) 1996, 1999 Vassili Bykov
--
The regular expression parser. Translates a regular expression read from a stream into a parse tree. ('accessing' protocol). The tree can later be passed to a matcher initialization method. All other classes in this category implement the tree. Refer to their comments for any details.
Instance variables:
input <Stream> A stream with the regular expression being parsed.
lookahead <Character>!
!RxmLink commentStamp: 'Tbn 11/12/2010 23:14' prior: 0!
-- Regular Expression Matcher v 1.1 (C) 1996, 1999 Vassili Bykov
--
A matcher is built of a number of links interconnected into some intricate structure. Regardless of fancy stuff, any link (except for the terminator) has the next one. Any link can match against a stream of characters, recursively propagating the match to the next link. Any link supports a number of matcher-building messages. This superclass does all of the above.
The class is not necessarily abstract. It may double as an empty string matcher: it recursively propagates the match to the next link, thus always matching nothing successfully.
Principal method:
matchAgainst: aMatcher
Any subclass will reimplement this to test the state of the matcher, most
probably reading one or more characters from the matcher's stream, and
either decide it has matched and answer true, leaving matcher stream
positioned at the end of match, or answer false and restore the matcher
stream position to whatever it was before the matching attempt.
Instance variables:
next <RxmLink | RxmTerminator> The next link in the structure.!
!RxmBranch commentStamp: 'Tbn 11/12/2010 23:14' prior: 0!
-- Regular Expression Matcher v 1.1 (C) 1996, 1999 Vassili Bykov
--
This is a branch of a matching process. Either `next' chain should match, or `alternative', if not nil, should match. Since this is also used to build loopbacks to match repetitions, `loopback' variable indicates whether the instance is a loopback: it affects the matcher-building operations (which of the paths through the branch is to consider as the primary when we have to find the "tail" of a matcher construct).
Instance variables
alternative <RxmLink> to match if `next' fails to match.
loopback <Boolean>!
!RxmMarker commentStamp: 'Tbn 11/12/2010 23:14' prior: 0!
-- Regular Expression Matcher v 1.1 (C) 1996, 1999 Vassili Bykov
--
A marker is used to remember positions of match of certain points of a regular expression. The marker receives an identifying key from the Matcher and uses that key to report positions of successful matches to the Matcher.
Instance variables:
index <Object> Something that makes sense for the Matcher. Received from the latter during initalization and later passed to it to identify the receiver.!
!RxmPredicate commentStamp: 'Tbn 11/12/2010 23:14' prior: 0!
-- Regular Expression Matcher v 1.1 (C) 1996, 1999 Vassili Bykov
--
Instance holds onto a one-argument block and matches exactly one character if the block evaluates to true when passed the character as the argument.
Instance variables:
predicate <BlockClosure>!
!RxmSpecial commentStamp: 'Tbn 11/12/2010 23:14' prior: 0!
-- Regular Expression Matcher v 1.1 (C) 1996, 1999 Vassili Bykov
--
A special node that matches a specific matcher state rather than any input character.
The state is either at-beginning-of-line or at-end-of-line.!
!RxmSubstring commentStamp: 'Tbn 11/12/2010 23:14' prior: 0!
-- Regular Expression Matcher v 1.1 (C) 1996, 1999 Vassili Bykov
--
Instance holds onto a string and matches exactly this string, and exactly once.
Instance variables:
string <String>!
!RxmTerminator commentStamp: 'Tbn 11/12/2010 23:14' prior: 0!
-- Regular Expression Matcher v 1.1 (C) 1996, 1999 Vassili Bykov
--
Instances of this class are used to terminate matcher's chains. When a match reaches this (an instance receives #matchAgainst: message), the match is considered to succeed. Instances also support building protocol of RxmLinks, with some restrictions.!
!RxsNode commentStamp: 'Tbn 11/12/2010 23:14' prior: 0!
-- Regular Expression Matcher v 1.1 (C) 1996, 1999 Vassili Bykov
--
A generic syntax tree node, provides some common responses to the standard tests, as well as tree structure printing -- handy for debugging.!
!RxsBranch commentStamp: 'Tbn 11/12/2010 23:14' prior: 0!
-- Regular Expression Matcher v 1.1 (C) 1996, 1999 Vassili Bykov
--
A Branch is a Piece followed by a Branch or an empty string.
Instance variables:
piece <RxsPiece>
branch <RxsBranch|RxsEpsilon>!
!RxsCharSet commentStamp: 'Tbn 11/12/2010 23:14' prior: 0!
-- Regular Expression Matcher v 1.1 (C) 1996, 1999 Vassili Bykov
--
A character set corresponds to a [...] construct in the regular expression.
Instance variables:
elements <OrderedCollection> An element can be one of: RxsCharacter, RxsRange, or RxsPredicate.
negated <Boolean>!
!RxsCharacter commentStamp: 'Tbn 11/12/2010 23:14' prior: 0!
-- Regular Expression Matcher v 1.1 (C) 1996, 1999 Vassili Bykov
--
A character is a literal character that appears either in the expression itself or in a character set within an expression.
Instance variables:
character <Character>!
!RxsContextCondition commentStamp: 'Tbn 11/12/2010 23:14' prior: 0!
-- Regular Expression Matcher v 1.1 (C) 1996, 1999 Vassili Bykov
--
One of a few special nodes more often representing special state of the match rather than a predicate on a character. The ugly exception is the #any condition which *is* a predicate on a character.
Instance variables:
kind <Selector>!
!RxsEpsilon commentStamp: 'Tbn 11/12/2010 23:14' prior: 0!
-- Regular Expression Matcher v 1.1 (C) 1996, 1999 Vassili Bykov
--
This is an empty string. It terminates some of the recursive constructs.!
!RxsMessagePredicate commentStamp: 'Tbn 11/12/2010 23:14' prior: 0!
-- Regular Expression Matcher v 1.1 (C) 1996, 1999 Vassili Bykov
--
A message predicate represents a condition on a character that is tested (at the match time) by sending a unary message to the character expecting a Boolean answer.
Instance variables:
selector <Symbol>!
!RxsPiece commentStamp: 'Tbn 11/12/2010 23:14' prior: 0!
-- Regular Expression Matcher v 1.1 (C) 1996, 1999 Vassili Bykov
--
A piece is an atom, possibly optional or repeated a number of times.
Instance variables:
atom <RxsCharacter|RxsCharSet|RxsPredicate|RxsRegex|RxsSpecial>
min <Integer>
max <Integer|nil> nil means infinity!
!RxsPredicate commentStamp: 'Tbn 11/12/2010 23:15' prior: 0!
-- Regular Expression Matcher v 1.1 (C) 1996, 1999 Vassili Bykov
--
This represents a character that satisfies a certain predicate.
Instance Variables:
predicate <BlockClosure> A one-argument block. If it evaluates to the value defined by <negated> when it is passed a character, the predicate is considered to match.
negation <BlockClosure> A one-argument block that is a negation of <predicate>.!
!RxsRange commentStamp: 'Tbn 11/12/2010 23:15' prior: 0!
-- Regular Expression Matcher v 1.1 (C) 1996, 1999 Vassili Bykov
--
I represent a range of characters as appear in character classes such as
[a-ZA-Z0-9].
I appear in a syntax tree only as an element of RxsCharSet.
Instance Variables:
first <Character>
last <Character>!
!RxsRegex commentStamp: 'Tbn 11/12/2010 23:15' prior: 0!
-- Regular Expression Matcher v 1.1 (C) 1996, 1999 Vassili Bykov
--
The body of a parenthesized thing, or a top-level expression, also an atom.
Instance variables:
branch <RxsBranch>
regex <RxsRegex | RxsEpsilon>!
!RxCharSetParser methodsFor: 'parsing' stamp: 'vb 4/11/09 21:56'!
addChar: aChar
elements add: (RxsCharacter with: aChar)! !
!RxCharSetParser methodsFor: 'parsing' stamp: 'vb 4/11/09 21:56'!
addRangeFrom: firstChar to: lastChar
firstChar asInteger > lastChar asInteger ifTrue:
[RxParser signalSyntaxException: ' bad character range'].
elements add: (RxsRange from: firstChar to: lastChar)! !
!RxCharSetParser methodsFor: 'parsing' stamp: 'vb 4/11/09 21:56'!
match: aCharacter
aCharacter = lookahead
ifFalse: [RxParser signalSyntaxException: 'unexpected character: ', (String with: lookahead)].
^source atEnd
ifTrue: [lookahead := nil]
ifFalse: [lookahead := source next]! !
!RxCharSetParser methodsFor: 'parsing' stamp: 'vb 4/11/09 21:56'!
parseCharOrRange
| firstChar |
firstChar := lookahead.
self match: firstChar.
lookahead = $- ifTrue:
[self match: $-.
lookahead isNil
ifTrue: [^self addChar: firstChar; addChar: $-]
ifFalse:
[self addRangeFrom: firstChar to: lookahead.
^self match: lookahead]].
self addChar: firstChar! !
!RxCharSetParser methodsFor: 'parsing' stamp: 'vb 4/11/09 21:56'!
parseEscapeChar
self match: $\.
$- = lookahead
ifTrue: [elements add: (RxsCharacter with: $-)]
ifFalse: [elements add: (RxsPredicate forEscapedLetter: lookahead)].
self match: lookahead! !
!RxCharSetParser methodsFor: 'parsing' stamp: 'vb 4/11/09 21:56'!
parseNamedSet
| name |
self match: $[; match: $:.
name := (String with: lookahead), (source upTo: $:).
lookahead := source next.
self match: $].
elements add: (RxsPredicate forNamedClass: name)! !
!RxCharSetParser methodsFor: 'parsing' stamp: 'vb 4/11/09 21:56'!
parseStep
lookahead = $[ ifTrue:
[source peek = $:
ifTrue: [^self parseNamedSet]
ifFalse: [^self parseCharOrRange]].
lookahead = $\ ifTrue:
[^self parseEscapeChar].
lookahead = $- ifTrue:
[RxParser signalSyntaxException: 'invalid range'].
self parseCharOrRange! !
!RxCharSetParser methodsFor: 'initialize-release' stamp: 'vb 4/11/09 21:56'!
initialize: aStream
source := aStream.
lookahead := aStream next.
elements := OrderedCollection new! !
!RxCharSetParser methodsFor: 'accessing' stamp: 'vb 4/11/09 21:56'!
parse
lookahead = $- ifTrue:
[self addChar: $-.
self match: $-].
[lookahead isNil] whileFalse: [self parseStep].
^elements! !
!RxCharSetParser class methodsFor: 'instance creation' stamp: 'vb 4/11/09 21:56'!
on: aStream
^self new initialize: aStream! !
!RxMatchOptimizer methodsFor: 'accessing' stamp: 'vb 4/11/09 21:56'!
canStartMatch: aCharacter in: aMatcher
"Answer whether a match could commence at the given lookahead
character, or in the current state of <aMatcher>. True answered
by this method does not mean a match will definitly occur, while false
answered by this method *does* guarantee a match will never occur."
aCharacter isNil ifTrue: [^true].
^testBlock == nil or: [testBlock value: aCharacter value: aMatcher]! !
!RxMatchOptimizer methodsFor: 'accessing' stamp: 'vb 4/11/09 21:56'!
conditionTester
"#any condition is filtered at the higher level;
it cannot appear among the conditions here."
| matchCondition |
conditions isEmpty ifTrue: [^nil].
conditions size = 1 ifTrue:
[matchCondition := conditions detect: [:ignored | true].
"Special case all of the possible conditions."
#atBeginningOfLine = matchCondition ifTrue: [^[:c :matcher | matcher atBeginningOfLine]].
#atEndOfLine = matchCondition ifTrue: [^[:c :matcher | matcher atEndOfLine]].
#atBeginningOfWord = matchCondition ifTrue: [^[:c :matcher | matcher atBeginningOfWord]].
#atEndOfWord = matchCondition ifTrue: [^[:c :matcher | matcher atEndOfWord]].
#atWordBoundary = matchCondition ifTrue: [^[:c :matcher | matcher atWordBoundary]].
#notAtWordBoundary = matchCondition ifTrue: [^[:c :matcher | matcher notAtWordBoundary]].
RxParser signalCompilationException: 'invalid match condition'].
"More than one condition. Capture them as an array in scope."
matchCondition := conditions asArray.
^[:c :matcher |
matchCondition contains:
[:conditionSelector |
matcher perform: conditionSelector]]! !
!RxMatchOptimizer methodsFor: 'accessing' stamp: 'vb 4/11/09 21:56'!
methodPredicateTester
| p selector |
methodPredicates isEmpty ifTrue: [^nil].
p := self optimizeSet: methodPredicates. "also allows copying closures"
^p size = 1
ifTrue:
["might be a pretty common case"
selector := p first.
[:char :matcher |
RxParser doHandlingMessageNotUnderstood:
[char perform: selector]]]
ifFalse:
[[:char :m |
RxParser doHandlingMessageNotUnderstood:
[p contains: [:sel | char perform: sel]]]]! !
!RxMatchOptimizer methodsFor: 'accessing' stamp: 'vb 4/11/09 21:56'!
nonMethodPredicateTester
| p selector |
nonMethodPredicates isEmpty ifTrue: [^nil].
p := self optimizeSet: nonMethodPredicates. "also allows copying closures"
^p size = 1
ifTrue:
[selector := p first.
[:char :matcher |
RxParser doHandlingMessageNotUnderstood:
[(char perform: selector) not]]]
ifFalse:
[[:char :m |
RxParser doHandlingMessageNotUnderstood:
[p contains: [:sel | (char perform: sel) not]]]]! !
!RxMatchOptimizer methodsFor: 'private' stamp: 'vb 4/11/09 21:56'!
determineTestMethod
"Answer a block closure that will work as a can-match predicate.
Answer nil if no viable optimization is possible (too many chars would
be able to start a match)."
| testers |
(conditions includes: #any) ifTrue: [^nil].
testers := OrderedCollection new: 5.
#(#prefixTester #nonPrefixTester #conditionTester #methodPredicateTester #nonMethodPredicateTester #predicateTester #nonPredicateTester)
do:
[:selector |
| tester |
tester := self perform: selector.
tester notNil ifTrue: [testers add: tester]].
testers isEmpty ifTrue: [^nil].
testers size = 1 ifTrue: [^testers first].
testers := testers asArray.
^[:char :matcher | testers contains: [:t | t value: char value: matcher]]! !
!RxMatchOptimizer methodsFor: 'private' stamp: 'vb 4/11/09 21:56'!
nonPredicateTester
| p pred |
nonPredicates isEmpty ifTrue: [^nil].
p := self optimizeSet: nonPredicates. "also allows copying closures"
^p size = 1
ifTrue:
[pred := p first.
[:char :matcher | (pred value: char) not]]
ifFalse:
[[:char :m | p contains: [:some | (some value: char) not]]]! !
!RxMatchOptimizer methodsFor: 'private' stamp: 'vb 4/11/09 21:56'!
nonPrefixTester
| np nonPrefixChar |
nonPrefixes isEmpty ifTrue: [^nil].
np := self optimizeSet: nonPrefixes. "also allows copying closures"
^np size = 1 "might be be pretty common case"
ifTrue:
[nonPrefixChar := np first.
[:char :matcher | char ~= nonPrefixChar]]
ifFalse: [[:char : matcher | (np includes: char) not]]! !
!RxMatchOptimizer methodsFor: 'private' stamp: 'vb 4/11/09 21:56'!
optimizeSet: aSet
"If a set is small, convert it to array to speed up lookup
(Array has no hashing overhead, beats Set on small number
of elements)."
^aSet size < 10 ifTrue: [aSet asArray] ifFalse: [aSet]! !
!RxMatchOptimizer methodsFor: 'private' stamp: 'vb 4/11/09 21:56'!
predicateTester
| p pred |
predicates isEmpty ifTrue: [^nil].
p := self optimizeSet: predicates. "also allows copying closures"
^p size = 1
ifTrue:
[pred := p first.
[:char :matcher | pred value: char]]
ifFalse:
[[:char :m | p contains: [:some | some value: char]]]! !
!RxMatchOptimizer methodsFor: 'private' stamp: 'vb 4/11/09 21:56'!
prefixTester
| p prefixChar |
prefixes isEmpty ifTrue: [^nil].
p := self optimizeSet: prefixes. "also allows copying closures"
ignoreCase ifTrue: [p := p collect: [:each | each asUppercase]].
^p size = 1 "might be a pretty common case"
ifTrue:
[prefixChar := p first.
ignoreCase
ifTrue: [[:char :matcher | char sameAs: prefixChar]]
ifFalse: [[:char :matcher | char = prefixChar]]]
ifFalse:
[ignoreCase
ifTrue: [[:char :matcher | p includes: char asUppercase]]
ifFalse: [[:char :matcher | p includes: char]]]! !
!RxMatchOptimizer methodsFor: 'initialize-release' stamp: 'vb 4/11/09 21:56'!
initialize: aRegex ignoreCase: aBoolean
"Set `testMethod' variable to a can-match predicate block:
two-argument block which accepts a lookahead character
and a matcher (presumably built from aRegex) and answers
a boolean indicating whether a match could start at the given
lookahead. "
ignoreCase := aBoolean.
prefixes := Set new: 10.
nonPrefixes := Set new: 10.
conditions := Set new: 3.
methodPredicates := Set new: 3.
nonMethodPredicates := Set new: 3.
predicates := Set new: 3.
nonPredicates := Set new: 3.
aRegex dispatchTo: self. "If the whole expression is nullable,
end-of-line is an implicit can-match condition!!"
aRegex isNullable ifTrue: [conditions add: #atEndOfLine].
testBlock := self determineTestMethod! !
!RxMatchOptimizer methodsFor: 'double dispatch' stamp: 'vb 4/11/09 21:56'!
syntaxAny
"Any special char is among the prefixes."
conditions add: #any! !
!RxMatchOptimizer methodsFor: 'double dispatch' stamp: 'vb 4/11/09 21:56'!
syntaxBeginningOfLine
"Beginning of line is among the prefixes."
conditions add: #atBeginningOfLine! !
!RxMatchOptimizer methodsFor: 'double dispatch' stamp: 'vb 4/11/09 21:56'!
syntaxBeginningOfWord
"Beginning of line is among the prefixes."
conditions add: #atBeginningOfWord! !
!RxMatchOptimizer methodsFor: 'double dispatch' stamp: 'vb 4/11/09 21:56'!
syntaxBranch: branchNode
"If the head piece of the branch is transparent (allows 0 matches),
we must recurse down the branch. Otherwise, just the head atom
is important."
(branchNode piece isNullable and: [branchNode branch notNil])
ifTrue: [branchNode branch dispatchTo: self].
branchNode piece dispatchTo: self! !
!RxMatchOptimizer methodsFor: 'double dispatch' stamp: 'vb 4/11/09 21:56'!
syntaxCharSet: charSetNode
"All these (or none of these) characters is the prefix."
charSetNode isNegated
ifTrue: [nonPrefixes addAll: (charSetNode enumerableSetIgnoringCase: ignoreCase)]
ifFalse: [prefixes addAll: (charSetNode enumerableSetIgnoringCase: ignoreCase)].
charSetNode hasPredicates ifTrue:
[charSetNode isNegated
ifTrue: [nonPredicates addAll: charSetNode predicates]
ifFalse: [predicates addAll: charSetNode predicates]]! !
!RxMatchOptimizer methodsFor: 'double dispatch' stamp: 'vb 4/11/09 21:56'!
syntaxCharacter: charNode
"This character is the prefix, of one of them."
prefixes add: charNode character! !
!RxMatchOptimizer methodsFor: 'double dispatch' stamp: 'vb 4/11/09 21:56'!
syntaxEndOfLine
"Beginning of line is among the prefixes."
conditions add: #atEndOfLine! !
!RxMatchOptimizer methodsFor: 'double dispatch' stamp: 'vb 4/11/09 21:56'!
syntaxEndOfWord
conditions add: #atEndOfWord! !
!RxMatchOptimizer methodsFor: 'double dispatch' stamp: 'vb 4/11/09 21:56'!
syntaxEpsilon
"Empty string, terminate the recursion (do nothing)."! !
!RxMatchOptimizer methodsFor: 'double dispatch' stamp: 'vb 4/11/09 21:56'!
syntaxMessagePredicate: messagePredicateNode
messagePredicateNode negated
ifTrue: [nonMethodPredicates add: messagePredicateNode selector]
ifFalse: [methodPredicates add: messagePredicateNode selector]! !
!RxMatchOptimizer methodsFor: 'double dispatch' stamp: 'vb 4/11/09 21:56'!
syntaxNonWordBoundary
conditions add: #notAtWordBoundary! !
!RxMatchOptimizer methodsFor: 'double dispatch' stamp: 'vb 4/11/09 21:56'!
syntaxPiece: pieceNode
"Pass on to the atom."
pieceNode atom dispatchTo: self! !
!RxMatchOptimizer methodsFor: 'double dispatch' stamp: 'vb 4/11/09 21:56'!
syntaxPredicate: predicateNode
predicates add: predicateNode predicate! !
!RxMatchOptimizer methodsFor: 'double dispatch' stamp: 'vb 4/11/09 21:56'!
syntaxRegex: regexNode
"All prefixes of the regex's branches should be combined.
Therefore, just recurse."
regexNode branch dispatchTo: self.
regexNode regex notNil
ifTrue: [regexNode regex dispatchTo: self]! !
!RxMatchOptimizer methodsFor: 'double dispatch' stamp: 'vb 4/11/09 21:56'!
syntaxWordBoundary
conditions add: #atWordBoundary! !
!RxMatcher methodsFor: 'private' stamp: 'vb 4/11/09 21:56'!
allocateMarker
"Answer an integer to use as an index of the next marker."
markerCount := markerCount + 1.
^markerCount! !
!RxMatcher methodsFor: 'private' stamp: 'vb 4/11/09 21:56'!
hookBranchOf: regexNode onto: endMarker
"Private - Recurse down the chain of regexes starting at
regexNode, compiling their branches and hooking their tails
to the endMarker node."
| rest |
rest := regexNode regex isNil
ifTrue: [nil]
ifFalse: [self hookBranchOf: regexNode regex onto: endMarker].
^RxmBranch new
next: ((regexNode branch dispatchTo: self)
pointTailTo: endMarker;
yourself);
alternative: rest;
yourself! !
!RxMatcher methodsFor: 'private' stamp: 'vb 4/11/09 21:56'!
isWordChar: aCharacterOrNil
"Answer whether the argument is a word constituent character:
alphanumeric or _."
^aCharacterOrNil ~~ nil
and: [aCharacterOrNil isAlphaNumeric]! !
!RxMatcher methodsFor: 'private' stamp: 'vb 4/11/09 21:56'!
makeOptional: aMatcher
"Private - Wrap this matcher so that the result would match 0 or 1
occurrences of the matcher."
| dummy branch |
dummy := RxmLink new.
branch := (RxmBranch new beLoopback)
next: aMatcher;
alternative: dummy.
aMatcher pointTailTo: dummy.
^branch! !
!RxMatcher methodsFor: 'private' stamp: 'vb 4/11/09 21:56'!
makePlus: aMatcher
"Private - Wrap this matcher so that the result would match 1 and more
occurrences of the matcher."
| loopback |
loopback := (RxmBranch new beLoopback)
next: aMatcher.
aMatcher pointTailTo: loopback.
^aMatcher! !
!RxMatcher methodsFor: 'private' stamp: 'vb 4/11/09 21:56'!
makeStar: aMatcher
"Private - Wrap this matcher so that the result would match 0 and more
occurrences of the matcher."
| dummy detour loopback |
dummy := RxmLink new.
detour := RxmBranch new
next: aMatcher;
alternative: dummy.
loopback := (RxmBranch new beLoopback)
next: aMatcher;
alternative: dummy.
aMatcher pointTailTo: loopback.
^detour! !
!RxMatcher methodsFor: 'private' stamp: 'lr 1/15/2010 21:17'!
proceedSearchingStream: aStream
| position |
position := aStream position.
[aStream atEnd] whileFalse:
[self tryMatch ifTrue: [^true].
aStream position: position; next.
position := aStream position].
"Try match at the very stream end too!!"
self tryMatch ifTrue: [^true].
^false! !
!RxMatcher methodsFor: 'private' stamp: 'lr 1/15/2010 21:51'!
tryMatch
"Match thyself against the current stream."
| oldMarkerPositions |
oldMarkerPositions := markerPositions.
markerPositions := Array new: markerCount.
1 to: markerCount do: [ :i | markerPositions at: i put: OrderedCollection new ].
lastResult := startOptimizer isNil
ifTrue: [ matcher matchAgainst: self]
ifFalse: [ (startOptimizer canStartMatch: stream peek in: self) and: [ matcher matchAgainst: self ] ].
"check for duplicates"
(lastResult not or: [ oldMarkerPositions isNil or: [ oldMarkerPositions size ~= markerPositions size ] ])
ifTrue: [ ^ lastResult ].
oldMarkerPositions with: markerPositions do: [ :oldPos :newPos |
oldPos size = newPos size
ifFalse: [ ^ lastResult ].
oldPos with: newPos do: [ :old :new |
old = new
ifFalse: [ ^ lastResult ] ] ].
"this is a duplicate"
^ lastResult := false! !
!RxMatcher methodsFor: 'testing' stamp: 'lr 1/15/2010 21:12'!
atBeginningOfLine
^self position = 0 or: [self lastChar = Cr]! !
!RxMatcher methodsFor: 'testing' stamp: 'lr 1/15/2010 21:12'!
atBeginningOfWord
^(self isWordChar: self lastChar) not
and: [self isWordChar: stream peek]! !
!RxMatcher methodsFor: 'testing' stamp: 'vb 4/11/09 21:56'!
atEndOfLine
^self atEnd or: [stream peek = Cr]! !
!RxMatcher methodsFor: 'testing' stamp: 'lr 1/15/2010 21:12'!
atEndOfWord
^(self isWordChar: self lastChar)
and: [(self isWordChar: stream peek) not]! !
!RxMatcher methodsFor: 'testing' stamp: 'lr 1/15/2010 21:12'!
atWordBoundary
^(self isWordChar: self lastChar)
xor: (self isWordChar: stream peek)! !
!RxMatcher methodsFor: 'testing' stamp: 'vb 4/11/09 21:56'!
notAtWordBoundary
^self atWordBoundary not! !
!RxMatcher methodsFor: 'testing' stamp: 'vb 4/11/09 21:56'!
supportsSubexpressions
^true! !
!RxMatcher methodsFor: 'streaming' stamp: 'vb 4/11/09 21:56'!
atEnd
^stream atEnd! !
!RxMatcher methodsFor: 'streaming' stamp: 'lr 1/15/2010 21:13'!
next
^ stream next! !