fix negative lookbehind

db4
Doug Coleman 2008-09-22 14:55:17 -05:00
parent 80a9147691
commit 864f9ead99
3 changed files with 18 additions and 5 deletions

View File

@ -25,7 +25,7 @@ IN: regexp.dfa
: find-transitions ( seq1 regexp -- seq2 ) : find-transitions ( seq1 regexp -- seq2 )
nfa-table>> transitions>> nfa-table>> transitions>>
[ at keys ] curry map concat [ at keys ] curry gather
eps swap remove ; eps swap remove ;
: add-todo-state ( state regexp -- ) : add-todo-state ( state regexp -- )

View File

@ -299,3 +299,10 @@ IN: regexp-tests
! clear "a$" <regexp> "a\n" over match ! clear "a$" <regexp> "a\n" over match
! clear "a$" <regexp> "a\r" over match ! clear "a$" <regexp> "a\r" over match
! clear "a$" <regexp> "a\r\n" over match ! clear "a$" <regexp> "a\r\n" over match
! "(az)(?<=b)" <regexp> "baz" over first-match
! "a(?<=b*)" <regexp> "cbaz" over first-match
! "a(?<=b)" <regexp> "baz" over first-match
! "a(?<!b)" <regexp> "baz" over first-match
! "a(?<!b)" <regexp> "caz" over first-match

View File

@ -38,7 +38,11 @@ TUPLE: dfa-traverser
key? ; key? ;
: text-finished? ( dfa-traverser -- ? ) : text-finished? ( dfa-traverser -- ? )
[ current-index>> ] [ text>> length ] bi >= ; {
[ current-state>> empty? ]
[ [ current-index>> ] [ text>> length ] bi >= ]
! [ current-index>> 0 < ]
} 1|| ;
: save-final-state ( dfa-straverser -- ) : save-final-state ( dfa-straverser -- )
[ current-index>> ] [ matches>> ] bi push ; [ current-index>> ] [ matches>> ] bi push ;
@ -62,24 +66,26 @@ M: lookahead-off flag-action ( dfa-traverser flag -- )
M: lookbehind-on flag-action ( dfa-traverser flag -- ) M: lookbehind-on flag-action ( dfa-traverser flag -- )
drop drop
f >>traverse-forward f >>traverse-forward
[ 2 - ] change-current-index
lookbehind-counters>> 0 swap push ; lookbehind-counters>> 0 swap push ;
M: lookbehind-off flag-action ( dfa-traverser flag -- ) M: lookbehind-off flag-action ( dfa-traverser flag -- )
drop drop
t >>traverse-forward t >>traverse-forward
dup lookbehind-counters>> dup lookbehind-counters>>
[ drop ] [ pop '[ _ + ] change-current-index drop ] if-empty ; [ drop ] [ pop '[ _ + 2 + ] change-current-index drop ] if-empty ;
: process-flags ( dfa-traverser -- ) : process-flags ( dfa-traverser -- )
[ [ 1+ ] map ] change-lookahead-counters [ [ 1+ ] map ] change-lookahead-counters
[ [ 1+ ] map ] change-lookbehind-counters
! dup current-state>> .
dup [ current-state>> ] [ traversal-flags>> ] bi dup [ current-state>> ] [ traversal-flags>> ] bi
at [ dup . flag-action ] with each ; at [ dup . flag-action ] with each ;
: increment-state ( dfa-traverser state -- dfa-traverser ) : increment-state ( dfa-traverser state -- dfa-traverser )
[ [
dup traverse-forward>> dup traverse-forward>>
[ [ 1+ ] change-current-index ] [ 1+ ] [ 1- ] ? change-current-index
[ [ 1- ] change-current-index ] if
dup current-state>> >>last-state dup current-state>> >>last-state
] dip ] dip
first >>current-state ; first >>current-state ;