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 [ = ] [ drop f ] if* ;
59 :: (next-match) ( i string regexp word: ( i string -- j ) reverse? -- i start end ? )
60 i string regexp word execute dup [| j |
62 reverse? [ swap [ 1+ ] bi@ ] when
64 ] [ drop f f f f ] if ; inline
66 : search-range ( i string reverse? -- seq )
67 [ drop 0 [a,b] ] [ length [a,b) ] if ; inline
69 :: next-match ( i string regexp word reverse? -- i start end ? )
71 i string reverse? search-range
72 [ [ 2drop 2drop ] dip string regexp word reverse? (next-match) dup ] find 2drop ; inline
74 : do-next-match ( i string regexp -- i start end ? )
76 execute-unsafe( i string regexp -- i start end ? ) ; inline
78 :: (each-match) ( i string regexp quot: ( start end string -- ) -- )
79 i string regexp do-next-match [| i' start end |
80 start end string quot call
81 i' string regexp quot (each-match)
82 ] [ 3drop ] if ; inline recursive
84 : prepare-match-iterator ( string regexp -- i string regexp )
85 [ check-string ] dip [ end/start nip ] 2keep ; inline
89 : each-match ( string regexp quot: ( start end string -- ) -- )
90 [ prepare-match-iterator ] dip (each-match) ; inline
92 : map-matches ( string regexp quot: ( start end string -- obj ) -- seq )
93 accumulator [ each-match ] dip >array ; inline
95 : all-matching-slices ( string regexp -- seq )
96 [ slice boa ] map-matches ;
98 : all-matching-subseqs ( string regexp -- seq )
99 [ subseq ] map-matches ;
101 : count-matches ( string regexp -- n )
102 [ 0 ] 2dip [ 3drop 1+ ] each-match ;
106 :: (re-split) ( string regexp quot -- new-slices )
107 0 string regexp [| end start end' string |
108 end' ! leave it on the stack for the next iteration
109 end start string quot call
112 swap string length string quot call suffix ; inline
116 : first-match ( string regexp -- slice/f )
117 [ prepare-match-iterator do-next-match ] [ drop ] 2bi
118 '[ _ slice boa nip ] [ 3drop f ] if ;
120 : re-contains? ( string regexp -- ? )
121 prepare-match-iterator do-next-match [ 3drop ] dip >boolean ;
123 : re-split ( string regexp -- seq )
124 [ slice boa ] (re-split) ;
126 : re-replace ( string regexp replacement -- result )
127 [ [ subseq ] (re-split) ] dip join ;
131 : get-ast ( regexp -- ast )
132 [ parse-tree>> ] [ options>> ] bi <with-options> ;
134 GENERIC: compile-regexp ( regex -- regexp )
136 : regexp-initial-word ( i string regexp -- i/f )
137 compile-regexp match-index-from ;
139 : do-compile-regexp ( regexp -- regexp )
141 dup \ regexp-initial-word =
142 [ drop _ get-ast ast>dfa dfa>word ] when
145 M: regexp compile-regexp ( regexp -- regexp )
148 M: reverse-regexp compile-regexp ( regexp -- regexp )
149 t backwards? [ do-compile-regexp ] with-variable ;
151 DEFER: compile-next-match
153 : next-initial-word ( i string regexp -- i start end string )
154 compile-next-match do-next-match ;
156 : compile-next-match ( regexp -- regexp )
158 dup \ next-initial-word = [
159 drop _ [ compile-regexp dfa>> ] [ reverse-regexp? ] bi
161 (( i string regexp -- i start end string )) simple-define-temp
163 ] change-next-match ;
167 : new-regexp ( string ast options class -- regexp )
168 [ \ regexp-initial-word \ next-initial-word ] dip boa ; inline
170 : make-regexp ( string ast -- regexp )
171 f f <options> regexp new-regexp ;
173 : <optioned-regexp> ( string options -- regexp )
174 [ dup parse-regexp ] [ string>options ] bi*
175 dup on>> reversed-regexp swap member?
176 [ reverse-regexp new-regexp ]
177 [ regexp new-regexp ] if ;
179 : <regexp> ( string -- regexp ) "" <optioned-regexp> ;
183 ! The following two should do some caching
185 : find-regexp-syntax ( string -- prefix suffix )
198 } swap [ subseq? not nip ] curry assoc-find drop ;
200 : take-until ( end lexer -- string )
205 ] change-lexer-column ;
207 : parse-noblank-token ( lexer -- str/f )
208 dup still-parsing-line? [ (parse-token) ] [ drop f ] if ;
210 : parsing-regexp ( accum end -- accum )
211 lexer get [ take-until ] [ parse-noblank-token ] bi
212 <optioned-regexp> compile-next-match parsed ;
216 : R! CHAR: ! parsing-regexp ; parsing
217 : R" CHAR: " parsing-regexp ; parsing
218 : R# CHAR: # parsing-regexp ; parsing
219 : R' CHAR: ' parsing-regexp ; parsing
220 : R( CHAR: ) parsing-regexp ; parsing
221 : R/ CHAR: / parsing-regexp ; parsing
222 : R@ CHAR: @ parsing-regexp ; parsing
223 : R[ CHAR: ] parsing-regexp ; parsing
224 : R` CHAR: ` parsing-regexp ; parsing
225 : R{ CHAR: } parsing-regexp ; parsing
226 : R| CHAR: | parsing-regexp ; parsing
231 [ raw>> dup find-regexp-syntax swap % swap % % ]
232 [ options>> options>string % ] bi
234 ] keep present-text ;