1 ! Copyright (C) 2008, 2009 Doug Coleman, Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors combinators kernel kernel.private math sequences
4 sequences.private strings sets assocs make lexer namespaces parser
5 arrays fry locals regexp.parser splitting sorting regexp.ast
6 regexp.negation regexp.compiler compiler.units words math.ranges ;
11 { parse-tree read-only }
15 TUPLE: reverse-regexp < regexp ;
19 M: lookahead question>quot ! Returns ( index string -- ? )
20 term>> ast>dfa dfa>shortest-word '[ f _ execute ] ;
22 : <reversed-option> ( ast -- reversed )
23 "r" string>options <with-options> ;
25 M: lookbehind question>quot ! Returns ( index string -- ? )
26 term>> <reversed-option>
27 ast>dfa dfa>reverse-shortest-word
28 '[ [ 1 - ] dip f _ execute ] ;
30 : check-string ( string -- string )
31 ! Make this configurable
32 dup string? [ "String required" throw ] unless ;
34 : match-index-from ( i string regexp -- index/f )
35 ! This word is unsafe. It assumes that i is a fixnum
36 ! and that string is a string.
37 dup dfa>> execute( index string regexp -- i/f ) ; inline
39 GENERIC: end/start ( string regexp -- end start )
40 M: regexp end/start drop length 0 ;
41 M: reverse-regexp end/start drop length 1 - -1 swap ;
45 : matches? ( string regexp -- ? )
49 [ = ] [ drop f ] if* ;
53 :: (next-match) ( i string regexp quot: ( i string regexp -- j ) reverse? -- i start end ? )
54 i string regexp quot call dup [| j |
56 reverse? [ swap [ 1 + ] bi@ ] when
58 ] [ drop f f f f ] if ; inline
60 : search-range ( i string reverse? -- seq )
61 [ drop dup 1 + -1 ] [ length 1 ] if range boa ; inline
63 :: next-match ( i string regexp quot: ( i string regexp -- j ) reverse? -- i start end ? )
65 i string reverse? search-range
66 [ [ 2drop 2drop ] dip string regexp quot reverse? (next-match) dup ] find 2drop ; inline
68 : do-next-match ( i string regexp -- i start end ? )
70 execute( i string regexp -- i start end ? ) ; inline
72 :: (each-match) ( ... i string regexp quot: ( ... start end string -- ... ) -- ... )
73 i string regexp do-next-match [| i' start end |
74 start end string quot call
75 i' string regexp quot (each-match)
76 ] [ 3drop ] if ; inline recursive
78 : prepare-match-iterator ( string regexp -- i string regexp )
79 [ check-string ] dip [ end/start nip ] 2keep ; inline
83 : each-match ( ... string regexp quot: ( ... start end string -- ... ) -- ... )
84 [ prepare-match-iterator ] dip (each-match) ; inline
86 : map-matches ( ... string regexp quot: ( ... start end string -- ... obj ) -- ... seq )
87 collector [ each-match ] dip >array ; inline
89 : all-matching-slices ( string regexp -- seq )
90 [ slice boa ] map-matches ;
92 : all-matching-subseqs ( string regexp -- seq )
93 [ subseq ] map-matches ;
95 : count-matches ( string regexp -- n )
96 [ 0 ] 2dip [ 3drop 1 + ] each-match ;
100 :: (re-split) ( string regexp quot -- new-slices )
101 0 string regexp [| end start end' string |
102 end' ! leave it on the stack for the next iteration
103 end start string quot call
106 swap string length string quot call suffix ; inline
110 : first-match ( string regexp -- slice/f )
111 [ prepare-match-iterator do-next-match ] [ drop ] 2bi
112 '[ _ slice boa nip ] [ 3drop f ] if ;
114 : re-contains? ( string regexp -- ? )
115 prepare-match-iterator do-next-match [ 3drop ] dip >boolean ;
117 : re-split ( string regexp -- seq )
118 [ slice boa ] (re-split) ;
120 : re-replace ( string regexp replacement -- result )
121 [ [ subseq ] (re-split) ] dip join ;
125 : get-ast ( regexp -- ast )
126 [ parse-tree>> ] [ options>> ] bi <with-options> ;
128 GENERIC: compile-regexp ( regex -- regexp )
130 : regexp-initial-word ( i string regexp -- i/f )
131 [ compile-regexp ] with-compilation-unit match-index-from ;
133 M: regexp compile-regexp ( regexp -- regexp )
135 dup \ regexp-initial-word =
136 [ drop _ get-ast ast>dfa dfa>word ] when
139 M: reverse-regexp compile-regexp ( regexp -- regexp )
140 t backwards? [ call-next-method ] with-variable ;
142 DEFER: compile-next-match
144 : next-initial-word ( i string regexp -- i start end string )
145 [ compile-next-match ] with-compilation-unit do-next-match ;
147 : compile-next-match ( regexp -- regexp )
149 dup \ next-initial-word = [
150 drop _ [ compile-regexp dfa>> def>> ] [ reverse-regexp? ] bi
151 '[ { array-capacity string regexp } declare _ _ next-match ]
152 (( i string regexp -- i start end string )) define-temp
154 ] change-next-match ;
158 : new-regexp ( string ast options class -- regexp )
159 [ \ regexp-initial-word \ next-initial-word ] dip boa ; inline
161 : make-regexp ( string ast -- regexp )
162 f f <options> regexp new-regexp ;
164 : <optioned-regexp> ( string options -- regexp )
165 [ dup parse-regexp ] [ string>options ] bi*
166 dup on>> reversed-regexp swap member?
167 [ reverse-regexp new-regexp ]
168 [ regexp new-regexp ] if ;
170 : <regexp> ( string -- regexp ) "" <optioned-regexp> ;
174 ! The following two should do some caching
176 : find-regexp-syntax ( string -- prefix suffix )
189 } swap [ subseq? not nip ] curry assoc-find drop ;
191 : take-until ( end lexer -- string )
196 ] change-lexer-column ;
198 : parse-noblank-token ( lexer -- str/f )
199 dup still-parsing-line? [ (parse-token) ] [ drop f ] if ;
201 : parsing-regexp ( accum end -- accum )
202 lexer get [ take-until ] [ parse-noblank-token ] bi
203 <optioned-regexp> compile-next-match suffix! ;
207 SYNTAX: R! CHAR: ! parsing-regexp ;
208 SYNTAX: R" CHAR: " parsing-regexp ;
209 SYNTAX: R# CHAR: # parsing-regexp ;
210 SYNTAX: R' CHAR: ' parsing-regexp ;
211 SYNTAX: R( CHAR: ) parsing-regexp ;
212 SYNTAX: R/ CHAR: / parsing-regexp ;
213 SYNTAX: R@ CHAR: @ parsing-regexp ;
214 SYNTAX: R[ CHAR: ] parsing-regexp ;
215 SYNTAX: R` CHAR: ` parsing-regexp ;
216 SYNTAX: R{ CHAR: } parsing-regexp ;
217 SYNTAX: R| CHAR: | parsing-regexp ;
221 { "prettyprint" "regexp" } "regexp.prettyprint" require-when