This repository was archived by the owner on Nov 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegExpUtils.lua
More file actions
1923 lines (1572 loc) · 43.5 KB
/
RegExpUtils.lua
File metadata and controls
1923 lines (1572 loc) · 43.5 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
local PackageName, Major, Minor, Patch = "RegExpUtils", 1, 0, 0
local PkgMajor, PkgMinor = PackageName, tonumber(string.format("%02d%02d%02d", Major, Minor, Patch))
local Pkg = Apollo.GetPackage(PkgMajor)
if Pkg and (Pkg.nVersion or 0) >= PkgMinor then
return -- no upgrade needed
end
-- Set a reference to the actual package or create an empty table
local RegExp = Pkg and Pkg.tPackage or {}
RegExp.cacheSize = 100 -- this is the size of regex cache
local g_sourceCache_re = {}
local g_objectCache_re = {}
function RegExp:new(args)
local new = { }
if args then
for key, val in pairs(args) do
new[key] = val
end
end
return setmetatable(new, RegExp)
end
local unpack = table.unpack or unpack
-- the base class of all
local __base_class__ = {}
function __base_class__:__init__() end
-- Creates a new class, deriving from a base (optional)
local function class(base)
base = base or __base_class__
local cls = {}
setmetatable(cls, { ["__index"] = base })
return cls
end
--- Creates a new object of a class
local function new(cls, ...)
--- cls: the class
--- ...: arguments for the constructor
local self = {}
setmetatable(self, { ["__index"] = cls })
self:__init__(...)
return self
end
-- Get object's class
local function classof(object)
return rawget(getmetatable(object), "__index")
end
-----------------------------------------------------------------------------
-- Nodes of expression tree
--
-- expression's base
--
local Expression = class()
function Expression:SetMatchee(matchee, pos)
-- Resets the state of the self and set matchee.
-- setting pos = nil just resets the expression
-- (or, lets NextMatch(submatches, flags) return false)
self.matchee = matchee
self.pos = pos
self:OnSetMatchee()
end
function Expression:NextMatch(submatches, flags)
-- Before first calling this function,
-- the user should have called self:SetMatchee(matchee, pos).
-- (otherwise, this function just returns false)
--
-- This function enumerates possible matches for the self.
-- Each time this is called, this returns (isOK, nextPos).
-- - if isOK == true,
-- nextPos denotes the position for the next expression.
-- - if isOK == false,
-- there was no match left.
--
-- Look also at the comment of Expression:OnNextMatch
local pos = self.pos
local isOK, nextPos
if pos then
isOK, nextPos = self:OnNextMatch(submatches, flags)
if not isOK then
self.pos = nil
end
end
if self.name then
if isOK then
submatches[self.name] = { pos, nextPos }
else
submatches[self.name] = nil
end
end
return isOK, nextPos
end
function Expression:SetName(name)
-- name: number or string
self.name = name
end
function Expression:CloneCoreStateTo(clone)
-- This should be called by Clone() of derived classes.
-- Clones the core states into 'clone'
clone.matchee = self.matchee
clone.pos = self.pos
clone.name = self.name
end
-- Override this if necessary
function Expression:OnSetMatchee() end
-- Define following functions in derived classes
-- function Expression:Clone()
-- Return a clone object of the self.
-- The state of the clone shall be the same as the self.
-- If the self has sub-objects, the sub-objects shall also be cloned.
--
-- function Expression:IsFixedLength()
-- Checks if the expression's length is fixed.
-- This functions returns (isFixed, length)
--
-- function Expression:OnNextMatch(submatches, flags)
-- When this function is called,
-- self.matchee and self.pos refer to the string to be matched.
--
-- - If there are one or more matches for the self, then
-- this function shall return (true, NEXT_POSITION),
-- in the favored order, one by one, each time it is called.
-- - If there are no matches, or if there are no matches left,
-- then this function shall return false.
--
-- It is guaranteed that this function is never called
-- after
-- - this function returns false, or
-- - this function sets self.pos = nil,
-- until the user calls self:SetMatchee again.
--
-- A matched group named 'name' (string or number)
-- can be obtained by
-- pos, nextPos = unpack(submatches[name])
-- str = self.matchee:sub(pos, nextPos-1)
--
-- expression AB
--
local ExpPair = class(Expression)
function ExpPair:__init__(sub1, sub2)
self.sub1 = sub1
self.sub2 = sub2
end
function ExpPair:OnSetMatchee()
self.sub1:SetMatchee(self.matchee, self.pos)
self.sub2:SetMatchee(nil, nil)
end
function ExpPair:Clone()
clone = new(classof(self), self.sub1:Clone(), self.sub2:Clone())
self:CloneCoreStateTo(clone)
return clone
end
function ExpPair:IsFixedLength()
local b, len1 = self.sub1:IsFixedLength()
local len2
if b then
b, len2 = self.sub2:IsFixedLength()
if b then
return true, len1 + len2
end
end
return false
end
function ExpPair:OnNextMatch(submatches, flags)
local isOK, nextPos = self.sub2:NextMatch(submatches, flags)
if isOK then
return isOK, nextPos
end
repeat
isOK, nextPos = self.sub1:NextMatch(submatches, flags)
if not isOK then
return false
end
self.sub2:SetMatchee(self.matchee, nextPos)
isOK, nextPos = self.sub2:NextMatch(submatches, flags)
until isOK
return isOK, nextPos
end
--
-- expression A|B
--
local ExpOr = class(Expression)
function ExpOr:__init__(sub1, sub2)
self.sub1 = sub1
self.sub2 = sub2
end
function ExpOr:OnSetMatchee()
self.sub1:SetMatchee(self.matchee, self.pos)
self.sub2:SetMatchee(self.matchee, self.pos)
end
function ExpOr:Clone()
clone = new(classof(self), self.sub1:Clone(), self.sub2:Clone())
self:CloneCoreStateTo(clone)
return clone
end
function ExpOr:IsFixedLength()
local b, len1 = self.sub1:IsFixedLength()
local len2
if b then
b, len2 = self.sub2:IsFixedLength()
if b and len1 == len2 then
return true, len1
end
end
return false
end
function ExpOr:OnNextMatch(submatches, flags)
local isOK, nextPos = self.sub1:NextMatch(submatches, flags)
if isOK then
return isOK, nextPos
end
return self.sub2:NextMatch(submatches, flags)
end
--
-- expression A{a,b}, which includes:
-- A* = A{,}
-- A+ = A{1,}
-- A? = A{,1}
-- A{n} = A{n,n}
-- a,b when omitted are assumed to be 0 and infinity, respectively
--
local ExpRepeat = class(Expression)
function ExpRepeat:__init__(sub, min, max)
self.sub = sub
self.min = min or 0
self.max = max
end
function ExpRepeat:OnSetMatchee()
local clone = self.sub:Clone()
clone:SetMatchee(self.matchee, self.pos)
self.stack = { {clone, self.pos} }
end
function ExpRepeat:Clone()
clone = new(classof(self), self.sub:Clone(), self.min, self.max)
self:CloneCoreStateTo(clone)
if self.stack then
local cloneStack = {}
for i,v in ipairs(self.stack) do
local sub, pos = unpack(v)
cloneStack[#cloneStack + 1] = {sub:Clone(), pos}
end
clone.stack = cloneStack
end
return clone
end
function ExpRepeat:IsFixedLength()
if self.min == self.max then
local b, len = self.sub:IsFixedLength()
if b then
return len * self.min
end
end
return false
end
function ExpRepeat:OnNextMatch(submatches, flags)
local stack = self.stack
local max = self.max
local isOK, nextPos
while self.pos do
local sub, pos
while true do
sub, pos = unpack(stack[#stack])
isOK, nextPos = sub:NextMatch(submatches, flags)
if isOK then
if not max or #stack < max then
local clone = self.sub:Clone()
clone:SetMatchee(self.matchee, nextPos)
stack[#stack+1] = {clone, nextPos}
else
break
end
else
stack[#stack] = nil
nextPos = pos
break
end
end
local iteration = #stack
if iteration == 0 then
self.pos = nil
end
if (self.min <= iteration)
and (not self.max or iteration <= self.max)
then
return true, nextPos
end
end
return false
end
--
-- expression A{a,b}?, which includes:
-- A*? = A{,}?
-- A+? = A{1,}?
-- A?? = A{,1}?
-- a,b when omitted are assumed to be 0 and infinity, respectively
--
local ExpVigorless = class(Expression)
function ExpVigorless:__init__(sub, min, max)
self.sub = sub
self.min = min or 0
self.max = max
end
function ExpVigorless:OnSetMatchee()
self.sub:SetMatchee(self.matchee, self.pos)
self.queue = nil
self.curExp = nil
self.curDepth = 0
end
function ExpVigorless:Clone()
clone = new(classof(self), self.sub:Clone(), self.min, self.max)
self:CloneCoreStateTo(clone)
if self.queue then
local cloneQ = {}
for i,v in ipairs(self.queue) do
cloneQ[#cloneQ + 1] = v
end
clone.queue = cloneQ
end
clone.curExp = self.curExp
clone.curDepth = self.curDepth
return clone
end
function ExpVigorless:IsFixedLength()
if self.min == self.max then
local b, len = self.sub:IsFixedLength()
if b then
return len * self.min
end
end
return false
end
function ExpVigorless:OnNextMatch(submatches, flags)
local min = self.min
local max = self.max
if not self.queue then
self.queue = { {self.pos, 1} }
if (min <= 0)
and (not max or 0 <= max)
then
return true, self.pos
end
end
local queue = self.queue
while true do
if self.curExp then
isOK, nextPos = self.curExp:NextMatch(submatches, flags)
if isOK then
if not max or self.curDepth < max then
queue[#queue+1] = { nextPos, self.curDepth + 1 }
end
if (min <= self.curDepth)
and (not max or self.curDepth <= max)
then
return isOK, nextPos
end
else
self.curExp = nil
end
elseif #queue > 0 then
nextPos, self.curDepth = unpack(table.remove(queue, 1))
local clone = self.sub:Clone()
clone:SetMatchee(self.matchee, nextPos)
self.curExp = clone
else
return false
end
end
end
--
-- expression (?=A), (?!A)
--
local ExpLookAhead = class(Expression)
function ExpLookAhead:__init__(sub, affirmative)
self.sub = sub
self.aff = affirmative
end
function ExpLookAhead:OnSetMatchee()
self.sub:SetMatchee(self.matchee, self.pos)
end
function ExpLookAhead:Clone()
clone = new(classof(self), self.sub:Clone())
self:CloneCoreStateTo(clone)
return clone
end
function ExpLookAhead:IsFixedLength()
return true, 0
end
function ExpLookAhead:OnNextMatch(submatches, flags)
local isOK, nextPos = self.sub:NextMatch(submatches, flags)
if (not self.aff) == (not isOK) then
nextPos = self.pos
self.pos = nil
return true, nextPos
end
return false
end
--
-- expression (?<=A), (?<!A)
--
local ExpLookBack = class(Expression)
function ExpLookBack:__init__(sub, affirmative)
local isFixed, len = sub:IsFixedLength()
assert(isFixed)
self.sub = sub
self.len = len
self.aff = affirmative
end
function ExpLookBack:OnSetMatchee()
if self.len < self.pos then
self.sub:SetMatchee(self.matchee, self.pos - self.len)
else
self.sub:SetMatchee(nil, nil)
end
end
function ExpLookBack:Clone()
clone = new(classof(self), self.sub:Clone())
self:CloneCoreStateTo(clone)
return clone
end
function ExpLookBack:IsFixedLength()
return true, 0
end
function ExpLookBack:OnNextMatch(submatches, flags)
local isOK, nextPos = self.sub:NextMatch(submatches, flags)
if (not self.aff) == (not isOK) then
nextPos = self.pos
self.pos = nil
return true, nextPos
end
return false
end
--
-- expression (?(NAME)A|B)
-- "|B" can be omitted
--
local ExpConditional = class(Expression)
function ExpConditional:__init__(refname, sub1, sub2)
self.refname = refname
self.sub1 = sub1
self.sub2 = sub2
end
function ExpConditional:OnSetMatchee()
self.sub1:SetMatchee(self.matchee, self.pos)
if self.sub2 then
self.sub2:SetMatchee(self.matchee, self.pos)
end
end
function ExpConditional:Clone()
local cloneSub1 = self.sub1:Clone()
local cloneSub2
if self.sub2 then
cloneSub2 = self.sub2:Clone()
end
clone = new(classof(self), self.refname, cloneSub1, cloneSub2)
self:CloneCoreStateTo(clone)
return clone
end
function ExpConditional:IsFixedLength()
local b, len1 = self.sub1:IsFixedLength()
if b then
if self.sub2 then
local len2
b, len2 = self.sub2:IsFixedLength()
if b and len1 == len2 then
return true, len1
end
elseif len1 == 0 then
return true, 0
end
end
return false
end
function ExpConditional:OnNextMatch(submatches, flags)
if submatches[self.refname] then
return self.sub1:NextMatch(submatches, flags)
elseif self.sub2 then
return self.sub2:NextMatch(submatches, flags)
else
local pos = self.pos
self.pos = nil
return true, pos
end
end
--
-- expression (?P=NAME)
--
local ExpReference = class(Expression)
function ExpReference:__init__(refname)
self.refname = refname
end
function ExpReference:Clone()
clone = new(classof(self), self.refname)
self:CloneCoreStateTo(clone)
return clone
end
function ExpReference:IsFixedLength()
return false
end
function ExpReference:OnNextMatch(submatches, flags)
local pos = self.pos
self.pos = nil
local refRange = submatches[self.refname]
if refRange then
local refBeg, refEnd = unpack(refRange)
local len = refEnd - refBeg
if self.matchee:sub(pos, pos + len - 1) == self.matchee:sub(refBeg, refEnd-1) then
return true, pos + len
else
return false
end
else
return true, pos
end
end
--
-- expression that matches just one char
--
local ExpOneChar = class(Expression)
function ExpOneChar:__init__(fnIsMatch)
-- fnIsMatch(char:byte()) -> bool
self.fnIsMatch = fnIsMatch
end
function ExpOneChar:Clone()
clone = new(classof(self), self.fnIsMatch)
self:CloneCoreStateTo(clone)
return clone
end
function ExpOneChar:IsFixedLength()
return true, 1
end
function ExpOneChar:OnNextMatch(submatches, flags)
local pos = self.pos
self.pos = nil
if pos > #self.matchee then return false end
if self.fnIsMatch(self.matchee:byte(pos)) then
return true, pos + 1
else
return false
end
end
--
-- expression ^
--
local ExpLineBegin = class(Expression)
function ExpLineBegin:Clone()
clone = new(classof(self))
self:CloneCoreStateTo(clone)
return clone
end
function ExpLineBegin:IsFixedLength()
return true, 0
end
function ExpLineBegin:OnNextMatch(submatches, flags)
local pos = self.pos
self.pos = nil
-- ^ matches even a null string
if pos == 1 then
return true, pos
end
if self.matchee:sub(pos-1, pos-1) == '\n' then
return true, pos
end
return false
end
--
-- expression $
--
local ExpLineEnd = class(Expression)
function ExpLineEnd:Clone()
clone = new(classof(self))
self:CloneCoreStateTo(clone)
return clone
end
function ExpLineEnd:IsFixedLength()
return true, 0
end
function ExpLineEnd:OnNextMatch(submatches, flags)
local pos = self.pos
self.pos = nil
-- $ matches even a null string
if pos == #self.matchee + 1 then
return true, pos
end
if self.matchee:sub(pos, pos) == '\n' then
return true, pos
end
return false
end
--
-- expression \A
--
local ExpBegin = class(Expression)
function ExpBegin:Clone()
clone = new(classof(self))
self:CloneCoreStateTo(clone)
return clone
end
function ExpBegin:IsFixedLength()
return true, 0
end
function ExpBegin:OnNextMatch(submatches, flags)
local pos = self.pos
self.pos = nil
-- ^ matches even a null string
if pos == 1 then
return true, pos
end
return false
end
--
-- expression \Z
--
local ExpEnd = class(Expression)
function ExpEnd:Clone()
clone = new(classof(self))
self:CloneCoreStateTo(clone)
return clone
end
function ExpEnd:IsFixedLength()
return true, 0
end
function ExpEnd:OnNextMatch(submatches, flags)
local pos = self.pos
self.pos = nil
-- $ matches even a null string
if pos == #self.matchee + 1 then
return true, pos
end
return false
end
--
-- expression \b
--
local ExpBorder = class(Expression)
function ExpBorder:Clone()
clone = new(classof(self))
self:CloneCoreStateTo(clone)
return clone
end
function ExpBorder:IsFixedLength()
return true, 0
end
function ExpBorder:OnNextMatch(submatches, flags)
local pos = self.pos
self.pos = nil
if self:IsWordAt(pos-1) ~= self:IsWordAt(pos) then
return true, pos
end
return false
end
function ExpBorder:IsWordAt(pos)
if pos <= 0 then return false end
local value = self.matchee:byte(pos)
if not value then return false end
local zero, nine, A, Z, a, z, ubar = ("09AZaz_"):byte(1,7)
return (zero <= value and value <= nine)
or (A <= value and value <= Z)
or (a <= value and value <= z)
or value == ubar
end
--
-- expression \B
--
local ExpNegBorder = class(ExpBorder)
function ExpNegBorder:OnNextMatch(submatches, flags)
local pos = self.pos
self.pos = nil
if self:IsWordAt(pos-1) == self:IsWordAt(pos) then
return true, pos
end
return false
end
--
-- expression that matches a terminal string
--
local ExpTerminals = class(Expression)
function ExpTerminals:__init__(str)
self.str = str
end
function ExpTerminals:Clone()
clone = new(classof(self), self.str)
self:CloneCoreStateTo(clone)
return clone
end
function ExpTerminals:IsFixedLength()
return true, #self.str
end
function ExpTerminals:OnNextMatch(submatches, flags)
local pos = self.pos
self.pos = nil
local len = #self.str
if self.matchee:sub(pos, pos + len - 1) == self.str then
return true, pos + len
else
return false
end
end
-----------------------------------------------------------------------------
-- Parser to compile regex-string to expression-tree
local Parser = class()
function Parser:__init__(regex, flags)
self.regex = regex
self.flags = flags
self.nextCapture = 1
local expOr, nextPos = self:GetExpOr(1)
if not expOr then
return
end
if nextPos ~= #regex + 1 then
if not self.errMsg then
self.errMsg = "cannot compile"
self.errPos = nextPos
end
return
end
self.errMsg = nil
self.errPos = nil
self.exp = expOr
end
function Parser:Error()
return self.errMsg, self.errPos
end
function Parser:Expression()
return self.exp
end
function Parser:GetExpOr(pos)
local expOr, nextPos = self:GetExpPair(pos)
if not expOr then return nil end
local expPair
while self.regex:sub(nextPos,nextPos) == '|' do
expPair, nextPos = self:GetExpPair(nextPos + 1)
if not expPair then return nil end
expOr = new(ExpOr, expOr, expPair)
end
return expOr, nextPos
end
function Parser:GetExpPair(pos)
local expPair, nextPos = self:GetExpRepeat(pos)
if not expPair then
return new(ExpTerminals, ""), pos
end
pos = nextPos
local expRepeat
while true do
expRepeat, nextPos = self:GetExpRepeat(pos)
if not expRepeat then
return expPair, pos
end
expPair = new(ExpPair, expPair, expRepeat)
pos = nextPos
end
end
function Parser:GetExpRepeat(pos)
local expRepeat, nextPos = self:GetExpPrimary(pos)
if not expRepeat then return nil end
pos = nextPos
local repeater
while true do
repeater, nextPos = self:GetRepeater(pos)
if not repeater then
return expRepeat, pos
end
local clsExp
if self.regex:sub(nextPos, nextPos) == '?' then
clsExp = ExpVigorless
nextPos = nextPos + 1
else
clsExp = ExpRepeat
end
local min = repeater.min
local max = repeater.max
expRepeat = new(clsExp, expRepeat, min, max)
pos = nextPos
end
end
function Parser:GetExpPrimary(pos)
local regex = self.regex
if regex:sub(pos,pos) == '(' then
pos = pos+1
local subExp, nextPos
if regex:sub(pos,pos) == '?' then
pos = pos+1
if regex:sub(pos,pos) == ':' then
subExp, nextPos = self:GetUnnamedGroup(pos+1)
elseif regex:sub(pos,pos+1) == 'P<' then
subExp, nextPos = self:GetUserNamedGroup(pos+2)
elseif regex:sub(pos,pos+1) == 'P=' then
subExp, nextPos = self:GetUserNamedRef(pos+2)
elseif regex:sub(pos,pos) == '=' then
subExp, nextPos = self:GetLookAhead(pos+1)
elseif regex:sub(pos,pos) == '!' then
subExp, nextPos = self:GetNegLookAhead(pos+1)
elseif regex:sub(pos,pos+1) == '<=' then
subExp, nextPos = self:GetLookBack(pos+2)
elseif regex:sub(pos,pos+1) == '<!' then
subExp, nextPos = self:GetNegLookBack(pos+2)
elseif regex:sub(pos,pos) == '(' then
subExp, nextPos = self:GetConditional(pos+1)
else
self.errMsg = "invalid char"
self.errPos = pos
return nil
end
else
subExp, nextPos = self:GetNamedGroup(pos)
end
if not subExp then return nil end
if self.regex:sub(nextPos,nextPos) == ')' then
return subExp, nextPos+1
else
self.errMsg = ") expected"