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 : search-range ( i string reverse? -- seq )
54 [ drop -1 ] [ length ] if [a,b] ; inline
56 :: (next-match) ( i string regexp quot: ( i string regexp -- j ) reverse? -- start end ? )
57 i string regexp quot call dup
58 [| j | reverse? [ j i ] [ i j ] if string ] [ drop f f f ] if ; inline
60 :: next-match ( i string regexp quot: ( i string regexp -- j ) reverse? -- start end ? )
62 i string reverse? search-range
63 [ [ 3drop ] dip string regexp quot reverse? (next-match) dup ] find 2drop ; inline
65 : do-next-match ( i string regexp -- start end ? )
67 execute( i string regexp -- start end ? ) ; inline
69 :: (each-match-forward) ( ... i string regexp quot: ( ... start end string -- ... ) -- ... )
71 i string regexp do-next-match [| start end |
72 start end string quot call
73 start end eq? [ end 1 + ] [ end ] if
74 string regexp quot (each-match-forward)
76 ] when ; inline recursive
78 :: (each-match-backward) ( ... i string regexp quot: ( ... start end string -- ... ) -- ... )
80 i string regexp do-next-match [| start end |
81 start 1 + end 1 + string quot call
82 start end eq? [ start 1 - ] [ start ] if
83 string regexp quot (each-match-backward)
85 ] when ; inline recursive
87 : (each-match) ( ... i string regexp quot: ( ... start end string -- ... ) -- ... )
88 over reverse-regexp? [ (each-match-backward) ] [ (each-match-forward) ] if ; inline
90 GENERIC: match-iterator-start ( string regexp -- start )
91 M: regexp match-iterator-start 2drop 0 ;
92 M: reverse-regexp match-iterator-start drop length ;
94 : prepare-match-iterator ( string regexp -- i string regexp )
95 [ check-string ] dip [ match-iterator-start ] 2keep ; inline
99 : each-match ( ... string regexp quot: ( ... start end string -- ... ) -- ... )
100 [ prepare-match-iterator ] dip (each-match) ; inline
102 : map-matches ( ... string regexp quot: ( ... start end string -- ... obj ) -- ... seq )
103 collector [ each-match ] dip >array ; inline
105 : all-matching-slices ( string regexp -- seq )
106 [ slice boa ] map-matches ;
108 : all-matching-subseqs ( string regexp -- seq )
109 [ subseq ] map-matches ;
111 : count-matches ( string regexp -- n )
112 [ 0 ] 2dip [ 3drop 1 + ] each-match ;
116 :: (re-split) ( string regexp quot -- new-slices )
117 0 string regexp [| end start end' string |
118 end' ! leave it on the stack for the next iteration
119 end start string quot call
122 swap string length string quot call suffix ; inline
126 :: first-match ( string regexp -- slice/f )
127 string regexp prepare-match-iterator do-next-match [
128 regexp reverse-regexp? [ [ 1 + ] bi@ ] when
132 : re-contains? ( string regexp -- ? )
133 prepare-match-iterator do-next-match [ 2drop ] dip >boolean ;
135 : re-split ( string regexp -- seq )
136 [ slice boa ] (re-split) ;
138 : re-replace ( string regexp replacement -- result )
139 [ [ subseq ] (re-split) ] dip join ;
143 : get-ast ( regexp -- ast )
144 [ parse-tree>> ] [ options>> ] bi <with-options> ;
146 GENERIC: compile-regexp ( regex -- regexp )
148 : regexp-initial-word ( i string regexp -- i/f )
149 [ compile-regexp ] with-compilation-unit match-index-from ;
151 M: regexp compile-regexp ( regexp -- regexp )
153 dup \ regexp-initial-word =
154 [ drop _ get-ast ast>dfa dfa>word ] when
157 M: reverse-regexp compile-regexp ( regexp -- regexp )
158 t backwards? [ call-next-method ] with-variable ;
160 DEFER: compile-next-match
162 : next-initial-word ( i string regexp -- start end string )
163 [ compile-next-match ] with-compilation-unit do-next-match ;
165 : compile-next-match ( regexp -- regexp )
167 dup \ next-initial-word = [
168 drop _ [ compile-regexp dfa>> def>> ] [ reverse-regexp? ] bi
169 '[ { array-capacity string regexp } declare _ _ next-match ]
170 ( i string regexp -- start end string ) define-temp
172 ] change-next-match ;
176 : new-regexp ( string ast options class -- regexp )
177 [ \ regexp-initial-word \ next-initial-word ] dip boa ; inline
179 : make-regexp ( string ast -- regexp )
180 f f <options> regexp new-regexp ;
182 : <optioned-regexp> ( string options -- regexp )
183 [ dup parse-regexp ] [ string>options ] bi*
184 dup on>> reversed-regexp swap member?
185 [ reverse-regexp new-regexp ]
186 [ regexp new-regexp ] if ;
188 : <regexp> ( string -- regexp ) "" <optioned-regexp> ;
192 ! The following two should do some caching
194 : find-regexp-syntax ( string -- prefix suffix )
207 } swap [ subseq? not nip ] curry assoc-find drop ;
209 : take-until ( end lexer -- string )
214 ] change-lexer-column ;
216 : parse-noblank-token ( lexer -- str/f )
217 dup still-parsing-line? [ (parse-token) ] [ drop f ] if ;
219 : parsing-regexp ( accum end -- accum )
220 lexer get [ take-until ] [ parse-noblank-token ] bi
221 <optioned-regexp> compile-next-match suffix! ;
225 SYNTAX: R! CHAR: ! parsing-regexp ;
226 SYNTAX: R" CHAR: " parsing-regexp ;
227 SYNTAX: R# CHAR: # parsing-regexp ;
228 SYNTAX: R' CHAR: ' parsing-regexp ;
229 SYNTAX: R( CHAR: ) parsing-regexp ;
230 SYNTAX: R/ CHAR: / parsing-regexp ;
231 SYNTAX: R@ CHAR: @ parsing-regexp ;
232 SYNTAX: R[ CHAR: ] parsing-regexp ;
233 SYNTAX: R` CHAR: ` parsing-regexp ;
234 SYNTAX: R{ CHAR: } parsing-regexp ;
235 SYNTAX: R| CHAR: | parsing-regexp ;
239 { "prettyprint" "regexp" } "regexp.prettyprint" require-when