1 ! Copyright (C) 2008, 2009 Doug Coleman, Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays classes compiler.units kernel
4 kernel.private lexer make math ranges namespaces regexp.ast
5 regexp.compiler regexp.negation regexp.parser sequences
6 sequences.private splitting strings vocabs.loader words ;
11 { parse-tree read-only }
15 TUPLE: reverse-regexp < regexp ;
19 M: lookahead question>quot
20 ! Returns ( index string -- ? )
21 term>> ast>dfa dfa>shortest-word '[ f _ execute ] ;
23 : <reversed-option> ( ast -- reversed )
24 "r" string>options <with-options> ;
26 M: lookbehind question>quot
27 ! Returns ( index string -- ? )
28 term>> <reversed-option>
29 ast>dfa dfa>reverse-shortest-word
30 '[ [ 1 - ] dip f _ execute ] ;
32 : match-index-from ( i string regexp -- index/f )
33 ! This word is unsafe. It assumes that i is a fixnum
34 ! and that string is a string.
35 dup dfa>> execute( index string regexp -- i/f ) ; inline
37 GENERIC: end/start ( string regexp -- end start )
38 M: regexp end/start drop length 0 ;
39 M: reverse-regexp end/start drop length 1 - -1 swap ;
43 : matches? ( string regexp -- ? )
44 [ string check-instance ] dip
47 [ = ] [ drop f ] if* ;
51 : search-range ( i string reverse? -- seq )
52 [ drop -1 ] [ length ] if [a..b] ; inline
54 :: (next-match) ( i string regexp quot: ( i string regexp -- j ) reverse? -- start end ? )
55 i string regexp quot call dup
56 [| j | reverse? [ j i ] [ i j ] if string ] [ drop f f f ] if ; inline
58 :: next-match ( i string regexp quot: ( i string regexp -- j ) reverse? -- start end ? )
60 i string reverse? search-range
61 [ 3nip string regexp quot reverse? (next-match) dup ] find 2drop ; inline
63 : do-next-match ( i string regexp -- start end ? )
65 execute( i string regexp -- start end ? ) ; inline
67 :: (each-match-forward) ( ... i string regexp quot: ( ... start end string -- ... ) -- ... )
69 i string regexp do-next-match [| start end |
70 start end string quot call
71 start end eq? [ end 1 + ] [ end ] if
72 string regexp quot (each-match-forward)
74 ] when ; inline recursive
76 :: (each-match-backward) ( ... i string regexp quot: ( ... start end string -- ... ) -- ... )
78 i string regexp do-next-match [| start end |
79 start 1 + end 1 + string quot call
80 start end eq? [ start 1 - ] [ start ] if
81 string regexp quot (each-match-backward)
83 ] when ; inline recursive
85 : (each-match) ( ... i string regexp quot: ( ... start end string -- ... ) -- ... )
86 over reverse-regexp? [ (each-match-backward) ] [ (each-match-forward) ] if ; inline
88 GENERIC: match-iterator-start ( string regexp -- start )
89 M: regexp match-iterator-start 2drop 0 ;
90 M: reverse-regexp match-iterator-start drop length ;
92 : prepare-match-iterator ( string regexp -- i string regexp )
93 [ string check-instance ] dip [ match-iterator-start ] 2keep ; inline
97 : each-match ( ... string regexp quot: ( ... start end string -- ... ) -- ... )
98 [ prepare-match-iterator ] dip (each-match) ; inline
100 : map-matches ( ... string regexp quot: ( ... start end string -- ... obj ) -- ... seq )
101 collector [ each-match ] dip >array ; inline
103 : all-matching-slices ( string regexp -- seq )
104 [ <slice-unsafe> ] map-matches ;
106 : all-matching-subseqs ( string regexp -- seq )
107 [ subseq ] map-matches ;
109 : count-matches ( string regexp -- n )
110 [ 0 ] 2dip [ 3drop 1 + ] each-match ;
114 :: (re-split) ( string regexp quot: ( from to seq -- slice ) -- new-slices )
115 0 string regexp [| end start end' string |
116 end' ! leave it on the stack for the next iteration
117 end start string quot call
120 swap string length string quot call suffix ; inline
124 :: first-match ( string regexp -- slice/f )
125 string regexp prepare-match-iterator do-next-match [
126 regexp reverse-regexp? [ [ 1 + ] bi@ ] when
127 string <slice-unsafe>
130 : re-contains? ( string regexp -- ? )
131 prepare-match-iterator do-next-match 2nip >boolean ;
133 : re-split ( string regexp -- seq )
134 [ <slice-unsafe> ] (re-split) ;
136 : re-replace ( string regexp replacement -- result )
137 [ [ subseq ] (re-split) ] dip join ;
139 :: re-replace-with ( string regexp quot: ( slice -- replacement ) -- result )
142 drop [ [ string <slice-unsafe> , ] keep ] dip
143 [ string <slice-unsafe> quot call( x -- x ) , ] keep
144 ] each-match string [ length ] [ <slice-unsafe> ] bi ,
149 : get-ast ( regexp -- ast )
150 [ parse-tree>> ] [ options>> ] bi <with-options> ;
152 GENERIC: compile-regexp ( regex -- regexp )
154 : regexp-initial-word ( i string regexp -- i/f )
155 [ compile-regexp ] with-compilation-unit match-index-from ;
157 M: regexp compile-regexp
159 dup \ regexp-initial-word =
160 [ drop _ get-ast ast>dfa dfa>word ] when
163 M: reverse-regexp compile-regexp
164 t backwards? [ call-next-method ] with-variable ;
166 DEFER: compile-next-match
168 : next-initial-word ( i string regexp -- start end string )
169 [ compile-next-match ] with-compilation-unit do-next-match ;
171 : compile-next-match ( regexp -- regexp )
173 dup \ next-initial-word = [
174 drop _ [ compile-regexp dfa>> def>> ] [ reverse-regexp? ] bi
175 '[ { array-capacity string regexp } declare _ _ next-match ]
176 ( i string regexp -- start end string ) define-temp
178 ] change-next-match ;
182 : new-regexp ( string ast options class -- regexp )
183 [ \ regexp-initial-word \ next-initial-word ] dip boa ; inline
185 : make-regexp ( string ast -- regexp )
186 f f <options> regexp new-regexp ;
188 : <optioned-regexp> ( string options -- regexp )
189 [ dup parse-regexp ] [ string>options ] bi*
190 dup on>> reversed-regexp swap member?
191 [ reverse-regexp new-regexp ]
192 [ regexp new-regexp ] if ;
194 : <regexp> ( string -- regexp ) "" <optioned-regexp> ;
198 : take-until ( lexer -- string )
201 [ [ "\\/" member? ] find-from ] keep swap [
202 CHAR: \ = [ [ 2 + ] dip t ] [ f ] if
204 "Unterminated regexp" throw
206 ] loop over [ subseq ] dip 1 +
207 ] change-lexer-column ;
209 : parse-noblank-token ( lexer -- str/f )
210 dup still-parsing-line? [ (parse-raw) ] [ drop f ] if ;
212 : parse-regexp ( accum -- accum )
213 lexer get [ take-until "\\/" "/" replace ] [ parse-noblank-token ] bi
214 <optioned-regexp> compile-next-match suffix! ;
218 SYNTAX: R/ parse-regexp ;
221 { "prettyprint" "regexp" } "regexp.prettyprint" require-when