1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs combinators kernel math
4 quotations sequences regexp.parser regexp.classes fry arrays
5 combinators.short-circuit regexp.utils prettyprint regexp.nfa
18 last-state current-state
21 start-index current-index
24 : <dfa-traverser> ( text regexp -- match )
25 [ dfa-table>> ] [ dfa-traversal-flags>> ] bi
27 swap >>traversal-flags
28 swap [ start-state>> >>current-state ] [ >>dfa-table ] bi
33 0 >>capture-group-index
35 V{ } clone >>capture-counters
36 V{ } clone >>lookbehind-counters
37 V{ } clone >>lookahead-counters
38 H{ } clone >>captured-groups ;
40 : final-state? ( dfa-traverser -- ? )
42 [ dfa-table>> final-states>> ] bi key? ;
44 : beginning-of-text? ( dfa-traverser -- ? )
45 current-index>> 0 <= ; inline
47 : end-of-text? ( dfa-traverser -- ? )
48 [ current-index>> ] [ text>> length ] bi >= ; inline
50 : text-finished? ( dfa-traverser -- ? )
52 [ current-state>> empty? ]
57 : save-final-state ( dfa-straverser -- )
58 [ current-index>> ] [ matches>> ] bi push ;
60 : match-done? ( dfa-traverser -- ? )
63 ] when text-finished? ;
65 : previous-text-character ( dfa-traverser -- ch )
66 [ text>> ] [ current-index>> 1- ] bi nth ;
68 : current-text-character ( dfa-traverser -- ch )
69 [ text>> ] [ current-index>> ] bi nth ;
71 : next-text-character ( dfa-traverser -- ch )
72 [ text>> ] [ current-index>> 1+ ] bi nth ;
74 GENERIC: flag-action ( dfa-traverser flag -- )
77 M: beginning-of-input flag-action ( dfa-traverser flag -- )
79 dup beginning-of-text? [ t >>match-failed? ] unless drop ;
81 M: end-of-input flag-action ( dfa-traverser flag -- )
83 dup end-of-text? [ t >>match-failed? ] unless drop ;
86 M: beginning-of-line flag-action ( dfa-traverser flag -- )
89 [ beginning-of-text? ]
90 [ previous-text-character terminator-class class-member? ]
91 } 1|| [ t >>match-failed? ] unless drop ;
93 M: end-of-line flag-action ( dfa-traverser flag -- )
97 [ next-text-character terminator-class class-member? ]
98 } 1|| [ t >>match-failed? ] unless drop ;
101 M: word-boundary flag-action ( dfa-traverser flag -- )
105 [ current-text-character terminator-class class-member? ]
106 } 1|| [ t >>match-failed? ] unless drop ;
109 M: lookahead-on flag-action ( dfa-traverser flag -- )
111 lookahead-counters>> 0 swap push ;
113 M: lookahead-off flag-action ( dfa-traverser flag -- )
115 dup lookahead-counters>>
116 [ drop ] [ pop '[ _ - ] change-current-index drop ] if-empty ;
118 M: lookbehind-on flag-action ( dfa-traverser flag -- )
121 [ 2 - ] change-current-index
122 lookbehind-counters>> 0 swap push ;
124 M: lookbehind-off flag-action ( dfa-traverser flag -- )
127 dup lookbehind-counters>>
128 [ drop ] [ pop '[ _ + 2 + ] change-current-index drop ] if-empty ;
130 M: capture-group-on flag-action ( dfa-traverser flag -- )
132 [ current-index>> 0 2array ]
133 [ capture-counters>> ] bi push ;
135 M: capture-group-off flag-action ( dfa-traverser flag -- )
137 dup capture-counters>> empty? [
141 [ capture-counters>> pop first2 dupd + ]
143 [ [ 1+ ] change-capture-group-index capture-group-index>> ]
144 [ captured-groups>> set-at ]
148 : process-flags ( dfa-traverser -- )
149 [ [ 1+ ] map ] change-lookahead-counters
150 [ [ 1+ ] map ] change-lookbehind-counters
151 [ [ first2 1+ 2array ] map ] change-capture-counters
152 ! dup current-state>> .
153 dup [ current-state>> ] [ traversal-flags>> ] bi
154 at [ flag-action ] with each ;
156 : increment-state ( dfa-traverser state -- dfa-traverser )
158 dup traverse-forward>>
159 [ [ 1+ ] change-current-index ]
160 [ [ 1- ] change-current-index ] if
161 dup current-state>> >>last-state
162 ] [ first ] bi* >>current-state ;
164 : match-literal ( transition from-state table -- to-state/f )
165 transitions>> at at ;
167 : match-class ( transition from-state table -- to-state/f )
169 [ drop class-member? ] assoc-with assoc-find [ nip ] [ drop ] if
172 : match-default ( transition from-state table -- to-state/f )
173 nipd transitions>> at t swap at ;
175 : match-transition ( obj from-state dfa -- to-state/f )
176 { [ match-literal ] [ match-class ] [ match-default ] } 3|| ;
178 : setup-match ( match -- obj state dfa-table )
179 [ [ current-index>> ] [ text>> ] bi nth ]
181 [ dfa-table>> ] tri ;
183 : do-match ( dfa-traverser -- dfa-traverser )
186 dup setup-match match-transition
187 [ increment-state do-match ] when*
190 : return-match ( dfa-traverser -- slice/f )
194 [ [ text>> ] [ start-index>> ] bi ]
195 [ peek ] bi* rot <slice>