1 ! Copyright (C) 2008, 2009 Doug Coleman, Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors combinators kernel math sequences strings sets
4 assocs prettyprint.backend prettyprint.custom make lexer
5 namespaces parser arrays fry locals regexp.parser splitting
6 sorting regexp.ast regexp.negation regexp.compiler words
7 call call.private math.ranges ;
12 { parse-tree read-only }
16 TUPLE: reverse-regexp < regexp ;
20 : maybe-negated ( lookaround quot -- regexp-quot )
21 '[ term>> @ ] [ positive?>> [ ] [ not ] ? ] bi compose ; inline
23 M: lookahead question>quot ! Returns ( index string -- ? )
24 [ ast>dfa dfa>shortest-word '[ f _ execute ] ] maybe-negated ;
26 : <reversed-option> ( ast -- reversed )
27 "r" string>options <with-options> ;
29 M: lookbehind question>quot ! Returns ( index string -- ? )
32 ast>dfa dfa>reverse-shortest-word
33 '[ [ 1- ] dip f _ execute ]
36 : check-string ( string -- string )
37 ! Make this configurable
38 dup string? [ "String required" throw ] unless ;
40 : match-index-from ( i string regexp -- index/f )
41 ! This word is unsafe. It assumes that i is a fixnum
42 ! and that string is a string.
43 dup dfa>> execute-unsafe( index string regexp -- i/f ) ;
45 GENERIC: end/start ( string regexp -- end start )
46 M: regexp end/start drop length 0 ;
47 M: reverse-regexp end/start drop length 1- -1 swap ;
51 : matches? ( string regexp -- ? )
55 [ swap = ] [ drop f ] if* ;
59 TUPLE: match { i read-only } { j read-only } { seq read-only } ;
61 : match-slice ( i string quot -- match/f )
63 [ swap match boa ] [ 2drop f ] if* ; inline
65 : search-range ( i string reverse? -- seq )
66 [ drop 0 [a,b] ] [ length [a,b) ] if ; inline
68 : match>result ( match reverse? -- i start end string )
70 [ [ i>> ] [ j>> tuck ] [ seq>> ] tri ] dip
71 [ [ swap [ 1+ ] bi@ ] dip ] when
72 ] [ 2drop f f f f ] if ; inline
74 :: next-match ( i string quot reverse? -- i start end string )
75 i string reverse? search-range
76 [ string quot match-slice ] map-find drop
77 reverse? match>result ; inline
79 : do-next-match ( i string regexp -- i start end string )
81 execute-unsafe( i string regexp -- i start end string ) ;
83 : next-slice ( i string regexp -- i/f slice/f )
85 [ slice boa ] [ drop ] if* ; inline
95 : iterate ( iterator -- iterator'/f )
97 [ i>> ] [ string>> ] [ regexp>> ] tri next-slice
98 [ [ [ string>> ] [ regexp>> ] bi ] 2dip match-iterator boa ]
101 : value ( iterator/f -- value/f )
102 dup [ value>> ] when ;
104 : <match-iterator> ( string regexp -- match-iterator )
110 : all-matches ( string regexp -- seq )
111 <match-iterator> [ iterate ] follow [ value ] map ;
113 : count-matches ( string regexp -- n )
118 :: split-slices ( string slices -- new-slices )
119 slices [ to>> ] map 0 prefix
120 slices [ from>> ] map string length suffix
121 [ string <slice> ] 2map ;
125 : first-match ( string regexp -- slice/f )
126 <match-iterator> value ;
128 : re-contains? ( string regexp -- ? )
129 first-match >boolean ;
131 : re-split1 ( string regexp -- before after/f )
132 dupd first-match [ 1array split-slices first2 ] [ f ] if* ;
134 : re-split ( string regexp -- seq )
135 dupd all-matches split-slices ;
137 : re-replace ( string regexp replacement -- result )
138 [ re-split ] dip join ;
142 : get-ast ( regexp -- ast )
143 [ parse-tree>> ] [ options>> ] bi <with-options> ;
145 GENERIC: compile-regexp ( regex -- regexp )
147 : regexp-initial-word ( i string regexp -- i/f )
148 compile-regexp match-index-from ;
150 : do-compile-regexp ( regexp -- regexp )
152 dup \ regexp-initial-word =
153 [ drop _ get-ast ast>dfa dfa>word ] when
156 M: regexp compile-regexp ( regexp -- regexp )
159 M: reverse-regexp compile-regexp ( regexp -- regexp )
160 t backwards? [ do-compile-regexp ] with-variable ;
162 DEFER: compile-next-match
164 : next-initial-word ( i string regexp -- i start end string )
165 compile-next-match do-next-match ;
167 : compile-next-match ( regexp -- regexp )
169 dup \ next-initial-word = [
170 drop _ [ compile-regexp dfa>> ] [ reverse-regexp? ] bi
171 '[ _ '[ _ _ execute ] _ next-match ]
172 (( i string regexp -- i start end string )) simple-define-temp
174 ] change-next-match ;
178 : new-regexp ( string ast options class -- regexp )
179 [ \ regexp-initial-word \ next-initial-word ] dip boa ; inline
181 : make-regexp ( string ast -- regexp )
182 f f <options> regexp new-regexp ;
184 : <optioned-regexp> ( string options -- regexp )
185 [ dup parse-regexp ] [ string>options ] bi*
186 dup on>> reversed-regexp swap member?
187 [ reverse-regexp new-regexp ]
188 [ regexp new-regexp ] if ;
190 : <regexp> ( string -- regexp ) "" <optioned-regexp> ;
194 ! The following two should do some caching
196 : find-regexp-syntax ( string -- prefix suffix )
209 } swap [ subseq? not nip ] curry assoc-find drop ;
211 : take-until ( end lexer -- string )
216 ] change-lexer-column ;
218 : parse-noblank-token ( lexer -- str/f )
219 dup still-parsing-line? [ (parse-token) ] [ drop f ] if ;
221 : parsing-regexp ( accum end -- accum )
222 lexer get [ take-until ] [ parse-noblank-token ] bi
223 <optioned-regexp> compile-next-match parsed ;
227 : R! CHAR: ! parsing-regexp ; parsing
228 : R" CHAR: " parsing-regexp ; parsing
229 : R# CHAR: # parsing-regexp ; parsing
230 : R' CHAR: ' parsing-regexp ; parsing
231 : R( CHAR: ) parsing-regexp ; parsing
232 : R/ CHAR: / parsing-regexp ; parsing
233 : R@ CHAR: @ parsing-regexp ; parsing
234 : R[ CHAR: ] parsing-regexp ; parsing
235 : R` CHAR: ` parsing-regexp ; parsing
236 : R{ CHAR: } parsing-regexp ; parsing
237 : R| CHAR: | parsing-regexp ; parsing
242 [ raw>> dup find-regexp-syntax swap % swap % % ]
243 [ options>> options>string % ] bi
245 ] keep present-text ;