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 assocs fry math locals combinators
5 accessors words compiler.units kernel.private strings
6 sequences.private arrays namespaces unicode.breaks
7 regexp.transition-tables combinators.short-circuit ;
10 GENERIC: question>quot ( question -- quot )
17 M: t question>quot drop [ 2drop t ] ;
18 M: f question>quot drop [ 2drop f ] ;
20 M: beginning-of-input question>quot
23 M: end-of-input question>quot
26 M: end-of-file question>quot
29 [ length swap - 2 <= ]
30 [ swap tail { "\n" "\r\n" "\r" "" } member? ]
35 drop [ { [ length = ] [ ?nth "\r\n" member? ] } 2|| ] ;
38 drop [ { [ drop zero? ] [ [ 1- ] dip ?nth "\r\n" member? ] } 2|| ] ;
40 M: $unix question>quot
41 drop [ { [ length = ] [ ?nth CHAR: \n = ] } 2|| ] ;
43 M: ^unix question>quot
44 drop [ { [ drop zero? ] [ [ 1- ] dip ?nth CHAR: \n = ] } 2|| ] ;
46 M: word-break question>quot
47 drop [ word-break-at? ] ;
49 : (execution-quot) ( next-state -- quot )
50 ! The conditions here are for lookaround and anchors, etc
52 [ question>> question>quot ] [ yes>> ] [ no>> ] tri
53 [ (execution-quot) ] bi@
55 ] [ '[ _ execute ] ] if ;
57 : execution-quot ( next-state -- quot )
58 dup sequence? [ first ] when
64 : condition>quot ( condition -- quot )
65 ! Conditions here are for different classes
67 [ question>> ] [ yes>> ] [ no>> ] tri
68 [ condition>quot ] bi@
69 '[ dup _ class-member? _ _ if ]
72 [ [ 3drop ] ] [ execution-quot '[ drop @ ] ] if-empty
75 : non-literals>dispatch ( literals non-literals -- quot )
76 [ swap ] assoc-map ! we want state => predicate, and get the opposite as input
77 swap keys f assoc-answers
78 table>condition [ <box> ] condition-map condition>quot ;
80 : literals>cases ( literal-transitions -- case-body )
81 [ execution-quot ] assoc-map ;
83 : split-literals ( transitions -- case default )
84 { } assoc-like [ first integer? ] partition
85 [ [ literals>cases ] keep ] dip non-literals>dispatch ;
87 :: step ( last-match index str quot final? direction -- last-index/f )
88 final? index last-match ?
89 index str bounds-check? [
96 backwards? get -1 1 ? ;
98 : transitions>quot ( transitions final-state? -- quot )
99 dup shortest? get and [ 2drop [ drop nip ] ] [
100 [ split-literals swap case>quot ] dip direction
101 '[ { array-capacity string } declare _ _ _ step ]
104 : word>quot ( word dfa -- quot )
106 [ final-states>> key? ] 2bi
109 : states>code ( words dfa -- )
113 (( last-match index string -- ? ))
116 ] with-compilation-unit ;
118 : states>words ( dfa -- words dfa )
119 dup transitions>> keys [ gensym ] H{ } map>assoc
124 : dfa>main-word ( dfa -- word )
125 states>words [ states>code ] keep start-state>> ;
129 : simple-define-temp ( quot effect -- word )
130 [ define-temp ] with-compilation-unit ;
132 : dfa>word ( dfa -- quot )
133 dfa>main-word execution-quot '[ drop [ f ] 2dip @ ]
134 (( start-index string regexp -- i/f )) simple-define-temp ;
136 : dfa>shortest-word ( dfa -- word )
137 t shortest? [ dfa>word ] with-variable ;
139 : dfa>reverse-word ( dfa -- word )
140 t backwards? [ dfa>word ] with-variable ;
142 : dfa>reverse-shortest-word ( dfa -- word )
143 t backwards? [ dfa>shortest-word ] with-variable ;