1 ! Copyright (C) 2009 Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: regexp.classes kernel sequences regexp.negation
4 quotations regexp.minimize assocs fry math locals combinators
5 accessors words compiler.units kernel.private strings
6 sequences.private arrays regexp.matchers call namespaces
7 regexp.transition-tables combinators.short-circuit ;
10 GENERIC: question>quot ( question -- quot )
17 M: t question>quot drop [ 2drop t ] ;
19 M: beginning-of-input question>quot
22 M: end-of-input question>quot
25 M: end-of-file question>quot
28 [ length swap - 2 <= ]
29 [ swap tail { "\n" "\r\n" "\r" "" } member? ]
34 drop [ { [ length = ] [ ?nth "\r\n" member? ] } 2|| ] ;
37 drop [ { [ drop zero? ] [ [ 1- ] dip ?nth "\r\n" member? ] } 2|| ] ;
39 : (execution-quot) ( next-state -- quot )
40 ! The conditions here are for lookaround and anchors, etc
42 [ question>> question>quot ] [ yes>> ] [ no>> ] tri
43 [ (execution-quot) ] bi@
45 ] [ '[ _ execute ] ] if ;
47 : execution-quot ( next-state -- quot )
48 dup sequence? [ first ] when
54 : condition>quot ( condition -- quot )
55 ! Conditions here are for different classes
57 [ question>> ] [ yes>> ] [ no>> ] tri
58 [ condition>quot ] bi@
59 '[ dup _ class-member? _ _ if ]
62 [ [ 3drop ] ] [ execution-quot '[ drop @ ] ] if-empty
65 : non-literals>dispatch ( literals non-literals -- quot )
66 [ swap ] assoc-map ! we want state => predicate, and get the opposite as input
68 table>condition [ <box> ] condition-map condition>quot ;
70 : literals>cases ( literal-transitions -- case-body )
71 [ execution-quot ] assoc-map ;
73 : expand-one-or ( or-class transition -- alist )
74 [ seq>> ] dip '[ _ 2array ] map ;
76 : expand-or ( alist -- new-alist )
79 [ expand-one-or ] [ 2array 1array ] if
82 : split-literals ( transitions -- case default )
83 >alist expand-or [ first integer? ] partition
84 [ [ literals>cases ] keep ] dip non-literals>dispatch ;
86 :: step ( last-match index str quot final? direction -- last-index/f )
87 final? index last-match ?
88 index str bounds-check? [
95 backwards? get -1 1 ? ;
97 : transitions>quot ( transitions final-state? -- quot )
98 dup shortest? get and [ 2drop [ drop nip ] ] [
99 [ split-literals swap case>quot ] dip direction
100 '[ { array-capacity string } declare _ _ _ step ]
103 : word>quot ( word dfa -- quot )
105 [ final-states>> key? ] 2bi
108 : states>code ( words dfa -- )
112 (( last-match index string -- ? ))
115 ] with-compilation-unit ;
117 : states>words ( dfa -- words dfa )
118 dup transitions>> keys [ gensym ] H{ } map>assoc
123 : dfa>word ( dfa -- word )
124 states>words [ states>code ] keep start-state>> ;
126 : check-string ( string -- string )
127 ! Make this configurable
128 dup string? [ "String required" throw ] unless ;
130 : setup-regexp ( start-index string -- f start-index string )
131 [ f ] [ >fixnum ] [ check-string ] tri* ; inline
135 ! The quotation returned is ( start-index string -- i/f )
137 : dfa>quotation ( dfa -- quot )
138 dfa>word execution-quot '[ setup-regexp @ ] ;
140 : dfa>shortest-quotation ( dfa -- quot )
141 t shortest? [ dfa>quotation ] with-variable ;
143 : dfa>reverse-quotation ( dfa -- quot )
144 t backwards? [ dfa>quotation ] with-variable ;
146 : dfa>reverse-shortest-quotation ( dfa -- quot )
147 t backwards? [ dfa>shortest-quotation ] with-variable ;
149 TUPLE: quot-matcher quot ;
150 C: <quot-matcher> quot-matcher
152 M: quot-matcher match-index-from
153 quot>> call( index string -- i/f ) ;