1 ! Copyright (C) 2008, 2009 Doug Coleman, Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays compiler.units kernel kernel.private
4 lexer make math 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 : check-string ( string -- string )
33 ! Make this configurable
34 dup string? [ "String required" throw ] unless ;
36 : match-index-from ( i string regexp -- index/f )
37 ! This word is unsafe. It assumes that i is a fixnum
38 ! and that string is a string.
39 dup dfa>> execute( index string regexp -- i/f ) ; inline
41 GENERIC: end/start ( string regexp -- end start )
42 M: regexp end/start drop length 0 ;
43 M: reverse-regexp end/start drop length 1 - -1 swap ;
47 : matches? ( string regexp -- ? )
51 [ = ] [ drop f ] if* ;
55 : search-range ( i string reverse? -- seq )
56 [ drop -1 ] [ length ] if [a..b] ; inline
58 :: (next-match) ( i string regexp quot: ( i string regexp -- j ) reverse? -- start end ? )
59 i string regexp quot call dup
60 [| j | reverse? [ j i ] [ i j ] if string ] [ drop f f f ] if ; inline
62 :: next-match ( i string regexp quot: ( i string regexp -- j ) reverse? -- start end ? )
64 i string reverse? search-range
65 [ 3nip string regexp quot reverse? (next-match) dup ] find 2drop ; inline
67 : do-next-match ( i string regexp -- start end ? )
69 execute( i string regexp -- start end ? ) ; inline
71 :: (each-match-forward) ( ... i string regexp quot: ( ... start end string -- ... ) -- ... )
73 i string regexp do-next-match [| start end |
74 start end string quot call
75 start end eq? [ end 1 + ] [ end ] if
76 string regexp quot (each-match-forward)
78 ] when ; inline recursive
80 :: (each-match-backward) ( ... i string regexp quot: ( ... start end string -- ... ) -- ... )
82 i string regexp do-next-match [| start end |
83 start 1 + end 1 + string quot call
84 start end eq? [ start 1 - ] [ start ] if
85 string regexp quot (each-match-backward)
87 ] when ; inline recursive
89 : (each-match) ( ... i string regexp quot: ( ... start end string -- ... ) -- ... )
90 over reverse-regexp? [ (each-match-backward) ] [ (each-match-forward) ] if ; inline
92 GENERIC: match-iterator-start ( string regexp -- start )
93 M: regexp match-iterator-start 2drop 0 ;
94 M: reverse-regexp match-iterator-start drop length ;
96 : prepare-match-iterator ( string regexp -- i string regexp )
97 [ check-string ] dip [ match-iterator-start ] 2keep ; inline
101 : each-match ( ... string regexp quot: ( ... start end string -- ... ) -- ... )
102 [ prepare-match-iterator ] dip (each-match) ; inline
104 : map-matches ( ... string regexp quot: ( ... start end string -- ... obj ) -- ... seq )
105 collector [ each-match ] dip >array ; inline
107 : all-matching-slices ( string regexp -- seq )
108 [ <slice-unsafe> ] map-matches ;
110 : all-matching-subseqs ( string regexp -- seq )
111 [ subseq ] map-matches ;
113 : count-matches ( string regexp -- n )
114 [ 0 ] 2dip [ 3drop 1 + ] each-match ;
118 :: (re-split) ( string regexp quot: ( from to seq -- slice ) -- new-slices )
119 0 string regexp [| end start end' string |
120 end' ! leave it on the stack for the next iteration
121 end start string quot call
124 swap string length string quot call suffix ; inline
128 :: first-match ( string regexp -- slice/f )
129 string regexp prepare-match-iterator do-next-match [
130 regexp reverse-regexp? [ [ 1 + ] bi@ ] when
131 string <slice-unsafe>
134 : re-contains? ( string regexp -- ? )
135 prepare-match-iterator do-next-match 2nip >boolean ;
137 : re-split ( string regexp -- seq )
138 [ <slice-unsafe> ] (re-split) ;
140 : re-replace ( string regexp replacement -- result )
141 [ [ subseq ] (re-split) ] dip join ;
143 :: re-replace-with ( string regexp quot: ( slice -- replacement ) -- result )
146 drop [ [ string <slice-unsafe> , ] keep ] dip
147 [ string <slice-unsafe> quot call( x -- x ) , ] keep
148 ] each-match string [ length ] [ <slice-unsafe> ] bi ,
153 : get-ast ( regexp -- ast )
154 [ parse-tree>> ] [ options>> ] bi <with-options> ;
156 GENERIC: compile-regexp ( regex -- regexp )
158 : regexp-initial-word ( i string regexp -- i/f )
159 [ compile-regexp ] with-compilation-unit match-index-from ;
161 M: regexp compile-regexp
163 dup \ regexp-initial-word =
164 [ drop _ get-ast ast>dfa dfa>word ] when
167 M: reverse-regexp compile-regexp
168 t backwards? [ call-next-method ] with-variable ;
170 DEFER: compile-next-match
172 : next-initial-word ( i string regexp -- start end string )
173 [ compile-next-match ] with-compilation-unit do-next-match ;
175 : compile-next-match ( regexp -- regexp )
177 dup \ next-initial-word = [
178 drop _ [ compile-regexp dfa>> def>> ] [ reverse-regexp? ] bi
179 '[ { array-capacity string regexp } declare _ _ next-match ]
180 ( i string regexp -- start end string ) define-temp
182 ] change-next-match ;
186 : new-regexp ( string ast options class -- regexp )
187 [ \ regexp-initial-word \ next-initial-word ] dip boa ; inline
189 : make-regexp ( string ast -- regexp )
190 f f <options> regexp new-regexp ;
192 : <optioned-regexp> ( string options -- regexp )
193 [ dup parse-regexp ] [ string>options ] bi*
194 dup on>> reversed-regexp swap member?
195 [ reverse-regexp new-regexp ]
196 [ regexp new-regexp ] if ;
198 : <regexp> ( string -- regexp ) "" <optioned-regexp> ;
202 : take-until ( lexer -- string )
205 [ [ "\\/" member? ] find-from ] keep swap [
206 CHAR: \ = [ [ 2 + ] dip t ] [ f ] if
208 "Unterminated regexp" throw
210 ] loop over [ subseq ] dip 1 +
211 ] change-lexer-column ;
213 : parse-noblank-token ( lexer -- str/f )
214 dup still-parsing-line? [ (parse-raw) ] [ drop f ] if ;
216 : parse-regexp ( accum -- accum )
217 lexer get [ take-until "\\/" "/" replace ] [ parse-noblank-token ] bi
218 <optioned-regexp> compile-next-match suffix! ;
222 SYNTAX: R/ parse-regexp ;
225 { "prettyprint" "regexp" } "regexp.prettyprint" require-when