3838 ): NimNode {.nimcall , noSideEffect , raises : [].}
3939 Lookaround = object
4040 ahead, behind: Sig
41- smL: NimNode
4241
4342# todo : can not use unicodeplus due to
4443# https://github.com/nim-lang/Nim/issues/7059
@@ -240,9 +239,7 @@ func genLookaroundMatch(
240239 look: Lookaround
241240): NimNode =
242241 template nfa : untyped = n.subExp.nfa
243- template smL : untyped = look.smL
244- let smlA = quote do : lastA (`smL`)
245- let smlB = quote do : lastB (`smL`)
242+ defVars smlA, smlB
246243 var flags = {mfAnchored}
247244 if n.subExp.reverseCapts:
248245 flags.incl mfReverseCapts
@@ -262,10 +259,9 @@ func genLookaroundMatch(
262259 `matched` = not `matched`
263260 let nfaLenLit = newLit nfa.s.len
264261 result = quote do :
265- grow `smL`
266- `smL`.last. setLen `nfaLenLit`
262+ var `smlA` = initPstates (`nfaLenLit`)
263+ var `smlB` = initPstates ( `nfaLenLit`)
267264 `lookaroundStmt`
268- removeLast `smL`
269265
270266func getEpsilonTransitions (nfa: Nfa , n: Node , nti: int ): seq [int ] =
271267 doAssert not isEpsilonTransition (n)
@@ -293,7 +289,7 @@ func genMatchedBody(
293289 let eTransitions = getEpsilonTransitions (nfa, n, nti)
294290 if eTransitions.len == 0 :
295291 return quote do :
296- add (`smB`, (`ntLit`, `capt`, `bounds2`))
292+ add (`smB`, initPstate (`ntLit`, `capt`, `bounds2`))
297293 var matchedBody = newSeq [NimNode ]()
298294 matchedBody.add quote do :
299295 `matched` = true
@@ -325,7 +321,7 @@ func genMatchedBody(
325321 doAssert false
326322 matchedBody.add quote do :
327323 if `matched`:
328- add (`smB`, (`ntLit`, `captx`, `bounds2`))
324+ add (`smB`, initPstate (`ntLit`, `captx`, `bounds2`))
329325 return newStmtList matchedBody
330326
331327func genNextState (
@@ -339,10 +335,10 @@ func genNextState(
339335 #[
340336 case n
341337 of 0:
342- if not smB.hasState (1):
338+ if not smB.contains (1):
343339 if c == 'a':
344340 smB.add((1, capt, bounds))
345- if not smB.hasState (4):
341+ if not smB.contains (4):
346342 if c == 'b':
347343 smB.add((4, capt, bounds))
348344 of 1:
@@ -384,11 +380,11 @@ func genNextState(
384380 i, nti, nfa, look, flags)
385381 if mfAnchored in flags and s[nt].kind == reEoe:
386382 branchBodyN.add quote do :
387- if not hasState (`smB`, `ntLit`):
383+ if not contains (`smB`, `ntLit`):
388384 `matchedBodyStmt`
389385 else :
390386 branchBodyN.add quote do :
391- if not hasState (`smB`, `ntLit`) and `matchCond`:
387+ if not contains (`smB`, `ntLit`) and `matchCond`:
392388 `matchedBodyStmt`
393389 doAssert eoeOnly or branchBodyN.len > 0
394390 if branchBodyN.len > 0 :
@@ -418,12 +414,15 @@ func nextState(
418414 flags: set [MatchFlag ],
419415 eoeOnly = false
420416): NimNode =
421- defForVars n, capt, bounds
417+ defForVars pstate
418+ let n = quote do : `pstate`.ni
419+ let capt = quote do : `pstate`.ci
420+ let bounds = quote do : `pstate`.bounds
422421 let eoeBailOut = if mfAnchored in flags:
423422 quote do :
424423 if `n` == `eoe`:
425- if not hasState (`smB`, `n`):
426- add (`smB`, (`n`, `capt`, `bounds`))
424+ if not contains (`smB`, `n`):
425+ add (`smB`, initPstate (`n`, `capt`, `bounds`))
427426 break
428427 else :
429428 newEmptyNode ()
@@ -433,7 +432,7 @@ func nextState(
433432 flags, eoeOnly)
434433 result = quote do :
435434 `smB`.clear ()
436- for `n`, `capt`, `bounds ` in `smA`.items:
435+ for `pstate ` in `smA`.items:
437436 `eoeBailOut`
438437 `nextStateStmt`
439438 swap `smA`, `smB`
@@ -483,7 +482,7 @@ func matchImpl(
483482 if `start`- 1 in 0 .. `text`.len- 1 :
484483 `cPrev` = bwRuneAt (`text`, `start`- 1 ).int32
485484 clear (`smA`)
486- add (`smA`, (0 'i16 , `captIdx`, `i` .. `i`- 1 ))
485+ add (`smA`, initPstate (0 'i16 , `captIdx`, `i` .. `i`- 1 ))
487486 while `i` < `text`.len:
488487 fastRuneAt (`text`, iNext, `c`, true )
489488 `nextStateStmt`
@@ -534,7 +533,7 @@ func reversedMatchImpl(
534533 if `start` in 0 .. `text`.len- 1 :
535534 `cPrev` = runeAt (`text`, `start`).int32
536535 clear (`smA`)
537- add (`smA`, (0 'i16 , `captIdx`, `i` .. `i`- 1 ))
536+ add (`smA`, initPstate (0 'i16 , `captIdx`, `i` .. `i`- 1 ))
538537 while iNext > 0 :
539538 bwFastRuneAt (`text`, iNext, `c`)
540539 `nextStateStmt`
@@ -551,11 +550,11 @@ func reversedMatchImpl(
551550 `captsStmt`
552551 `matched` = `smA`.len > 0
553552
554- template look (smL: NimNode ) : untyped =
553+ template look : untyped =
555554 Lookaround (
556555 ahead: matchImpl,
557- behind: reversedMatchImpl,
558- smL: smL )
556+ behind: reversedMatchImpl
557+ )
559558
560559template constructSubmatches2 (
561560 captures, txt, capts, capt, size: untyped
@@ -578,24 +577,23 @@ proc matchImpl*(text, expLit, body: NimNode): NimNode =
578577 if not (expLit.kind == nnkCallStrLit and $ expLit[0 ] == " rex" ):
579578 error " not a regex literal; only rex\" regex\" is allowed" , expLit
580579 let exp = expLit[1 ]
581- defVars smA, smB, capts, capt, matched, smL
580+ defVars smA, smB, capts, capt, matched
582581 let regex = reCt (exp.strVal)
583582 let startLit = newLit 0
584583 let flags: set [MatchFlag ] = {}
585584 let matchImplStmt = matchImpl (
586585 smA, smB, capts, capt, matched,
587- text, startLit, regex.nfa, look (smL ), flags)
586+ text, startLit, regex.nfa, look (), flags)
588587 let nfaLenLit = newLit regex.nfa.s.len
589588 let nfaGroupsLen = int (regex.groupsCount)
590589 result = quote do :
591590 block :
592591 var
593- `smA` = newSubmatches `nfaLenLit`
594- `smB` = newSubmatches `nfaLenLit`
592+ `smA` = initPstates `nfaLenLit`
593+ `smB` = initPstates `nfaLenLit`
595594 `capts` = default (Capts )
596595 `capt` = - 1 'i32
597596 `matched` = false
598- `smL` {.used .} = default (SmLookaround )
599597 `matchImplStmt`
600598 if `matched`:
601599 var matches {.used , inject .} = newSeq [string ]()
0 commit comments