Skip to content

Commit

Permalink
Create new bindings for the referenced register upon entry to subpatt…
Browse files Browse the repository at this point in the history
…ern-reference.

When entering a register x via subpattern-reference, the registers
local to x receive new dynamic bindings, which shadow the old bindings
for the duration of the subpattern call.  Previously, "local" did not
include the register itself--x in this case.  With this patch, the
referenced register now receives a new binding as well.

It's not entirely clear that this is the appropriate behavior.  In a
regex like "(.\1?)(?1)", the back-reference to '\1' now will always
fail, rather than potentially matching according to what was matched
in the first pass through the first register.
  • Loading branch information
nbtrap committed Feb 26, 2014
1 parent 2fc37a6 commit 9b97c91
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 14 deletions.
4 changes: 2 additions & 2 deletions closures.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ such that the call to NEXT-FN after the match would succeed."))
(labels
((pop-offsets (offsets offsets-stacks)
(declare (simple-vector offsets offsets-stacks))
(loop for idx from (1+ num) upto (+ num subregister-count)
(loop for idx from num upto (+ num subregister-count)
collect (let ()
(declare (fixnum idx))
(setf (svref offsets idx)
(pop (svref offsets-stacks idx))))))
(push-offsets (offsets saved-offsets offsets-stacks)
(declare (simple-vector offsets offsets-stacks) (list saved-offsets))
(loop for idx from (1+ num) upto (+ num subregister-count) do
(loop for idx from num upto (+ num subregister-count) do
(let ()
(declare (fixnum idx))
(push (svref offsets idx) (svref offsets-stacks idx))
Expand Down
16 changes: 4 additions & 12 deletions test/simple
Original file line number Diff line number Diff line change
Expand Up @@ -422,15 +422,11 @@ characters if there's a match."
(scan regex "fffffff"))
'(0 7 #(0 4 4 6) #(1 7 5 7))))

(equalp (multiple-value-list
(scan "^(.\\1?)(?1)$" "aba"))
'(0 3 #(0) #(1)))
(null (scan "^(.\\1?)(?1)$" "aba"))

(let ((*allow-named-registers* t)
(regex "^(?<regOne>.\\k<regOne>?)(?&regOne)$"))
(equalp (multiple-value-list
(scan regex "aba"))
'(0 3 #(0) #(1))))
(null (scan regex "aba")))

(equalp (multiple-value-list
(scan "^(.\\2?)(.)(?1)$" "abcb"))
Expand All @@ -454,13 +450,9 @@ characters if there's a match."
;; (scan regex "aaba"))
;; '(0 4 #(nil 0 0) #(nil 1 1))))

(equalp (multiple-value-list
(scan "^(a|\\3(?1)\\2|(?2))((b|c)(?4)?)(?1)(d(?1))$" "abbcdcabbda"))
'(0 11 #(0 1 1 9) #(1 2 2 11)))
(null (scan "^(a|\\3(?1)\\2|(?2))((b|c)(?4)?)(?1)(d(?1))$" "abbcdcabbda"))

;; Cf. #17
;; (let ((*allow-named-registers* t)
;; (regex "^(?<regOne>a|\\k<regThree>(?&regOne)\\k<regTwo>|(?&regTwo))(?<regTwo>(?<regThree>b|c)(?&regFour)?)(?&regOne)(?<regFour>d(?&regOne))$"))
;; (equalp (multiple-value-list
;; (scan regex "abbcdcabbda"))
;; '(0 11 #(0 1 1 9) #(1 2 2 11))))
;; (null (scan regex "abbcdcabbda")))

0 comments on commit 9b97c91

Please sign in to comment.