factor/basis/regexp/traversal/traversal.factor

70 lines
2.0 KiB
Factor
Raw Normal View History

! Copyright (C) 2008 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
2008-11-22 18:31:40 -05:00
USING: accessors assocs combinators kernel math
2009-02-26 19:06:57 -05:00
quotations sequences regexp.classes fry arrays regexp.matchers
2009-02-18 13:27:07 -05:00
combinators.short-circuit prettyprint regexp.nfa ;
IN: regexp.traversal
TUPLE: dfa-traverser
dfa-table
2009-02-15 15:28:22 -05:00
current-state
text
2009-02-26 19:06:57 -05:00
current-index
match-index ;
2009-02-26 23:14:41 -05:00
: <dfa-traverser> ( start-index text dfa -- match )
dfa-traverser new
swap [ start-state>> >>current-state ] [ >>dfa-table ] bi
swap >>text
2009-02-26 23:14:41 -05:00
swap >>current-index ;
: final-state? ( dfa-traverser -- ? )
[ current-state>> ]
[ dfa-table>> final-states>> ] bi key? ;
: end-of-text? ( dfa-traverser -- ? )
[ current-index>> ] [ text>> length ] bi >= ; inline
: text-finished? ( dfa-traverser -- ? )
2008-09-22 15:55:17 -04:00
{
2009-02-19 01:11:45 -05:00
[ current-state>> not ]
[ end-of-text? ]
2008-09-22 15:55:17 -04:00
} 1|| ;
2009-02-26 19:06:57 -05:00
: save-final-state ( dfa-traverser -- dfa-traverser )
dup current-index>> >>match-index ;
: match-done? ( dfa-traverser -- ? )
2009-02-26 19:06:57 -05:00
dup final-state? [ save-final-state ] when text-finished? ;
2008-09-19 18:54:34 -04:00
: increment-state ( dfa-traverser state -- dfa-traverser )
>>current-state
[ 1 + ] change-current-index ;
: match-literal ( transition from-state table -- to-state/f )
transitions>> at at ;
: match-class ( transition from-state table -- to-state/f )
transitions>> at* [
2009-02-16 21:23:00 -05:00
swap '[ drop _ swap class-member? ] assoc-find spin ?
] [ drop ] if ;
: match-transition ( obj from-state dfa -- to-state/f )
{ [ match-literal ] [ match-class ] } 3|| ;
: setup-match ( match -- obj state dfa-table )
[ [ current-index>> ] [ text>> ] bi nth ]
[ current-state>> ]
[ dfa-table>> ] tri ;
: do-match ( dfa-traverser -- dfa-traverser )
dup match-done? [
dup setup-match match-transition
[ increment-state do-match ] when*
] unless ;
2009-02-26 19:06:57 -05:00
TUPLE: dfa-matcher dfa ;
C: <dfa-matcher> dfa-matcher
2009-02-26 23:14:41 -05:00
M: dfa-matcher match-index-from
2009-02-26 19:06:57 -05:00
dfa>> <dfa-traverser> do-match match-index>> ;