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 call 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: not-class question>quot
21 class>> question>quot [ not ] compose ;
23 M: beginning-of-input question>quot
26 M: end-of-input question>quot
29 M: end-of-file question>quot
32 [ length swap - 2 <= ]
33 [ swap tail { "\n" "\r\n" "\r" "" } member? ]
38 drop [ { [ length = ] [ ?nth "\r\n" member? ] } 2|| ] ;
41 drop [ { [ drop zero? ] [ [ 1- ] dip ?nth "\r\n" member? ] } 2|| ] ;
43 M: word-break question>quot
44 drop [ word-break-at? ] ;
46 : (execution-quot) ( next-state -- quot )
47 ! The conditions here are for lookaround and anchors, etc
49 [ question>> question>quot ] [ yes>> ] [ no>> ] tri
50 [ (execution-quot) ] bi@
52 ] [ '[ _ execute ] ] if ;
54 : execution-quot ( next-state -- quot )
55 dup sequence? [ first ] when
61 : condition>quot ( condition -- quot )
62 ! Conditions here are for different classes
64 [ question>> ] [ yes>> ] [ no>> ] tri
65 [ condition>quot ] bi@
66 '[ dup _ class-member? _ _ if ]
69 [ [ 3drop ] ] [ execution-quot '[ drop @ ] ] if-empty
72 : non-literals>dispatch ( literals non-literals -- quot )
73 [ swap ] assoc-map ! we want state => predicate, and get the opposite as input
74 swap keys f assoc-answers
75 table>condition [ <box> ] condition-map condition>quot ;
77 : literals>cases ( literal-transitions -- case-body )
78 [ execution-quot ] assoc-map ;
80 : split-literals ( transitions -- case default )
81 { } assoc-like [ first integer? ] partition
82 [ [ literals>cases ] keep ] dip non-literals>dispatch ;
84 :: step ( last-match index str quot final? direction -- last-index/f )
85 final? index last-match ?
86 index str bounds-check? [
93 backwards? get -1 1 ? ;
95 : transitions>quot ( transitions final-state? -- quot )
96 dup shortest? get and [ 2drop [ drop nip ] ] [
97 [ split-literals swap case>quot ] dip direction
98 '[ { array-capacity string } declare _ _ _ step ]
101 : word>quot ( word dfa -- quot )
103 [ final-states>> key? ] 2bi
106 : states>code ( words dfa -- )
107 [ ! with-compilation-unit doesn't compile, so we need call( -- )
111 (( last-match index string -- ? ))
114 ] with-compilation-unit
115 ] call( words dfa -- ) ;
117 : states>words ( dfa -- words dfa )
118 dup transitions>> keys [ gensym ] H{ } map>assoc
123 : dfa>main-word ( dfa -- word )
124 states>words [ states>code ] keep start-state>> ;
128 : simple-define-temp ( quot effect -- word )
129 [ [ define-temp ] with-compilation-unit ] call( quot effect -- word ) ;
131 : dfa>word ( dfa -- quot )
132 dfa>main-word execution-quot '[ drop [ f ] 2dip @ ]
133 (( start-index string regexp -- i/f )) simple-define-temp ;
135 : dfa>shortest-word ( dfa -- word )
136 t shortest? [ dfa>word ] with-variable ;
138 : dfa>reverse-word ( dfa -- word )
139 t backwards? [ dfa>word ] with-variable ;
141 : dfa>reverse-shortest-word ( dfa -- word )
142 t backwards? [ dfa>shortest-word ] with-variable ;