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 M: lookahead question>quot ! 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 ! Returns ( index string -- ? )
27 term>> <reversed-option>
28 ast>dfa dfa>reverse-shortest-word
29 '[ [ 1- ] dip f _ execute ] ;
31 : check-string ( string -- string )
32 ! Make this configurable
33 dup string? [ "String required" throw ] unless ;
35 : match-index-from ( i string regexp -- index/f )
36 ! This word is unsafe. It assumes that i is a fixnum
37 ! and that string is a string.
38 dup dfa>> execute-unsafe( index string regexp -- i/f ) ;
40 GENERIC: end/start ( string regexp -- end start )
41 M: regexp end/start drop length 0 ;
42 M: reverse-regexp end/start drop length 1- -1 swap ;
46 : matches? ( string regexp -- ? )
50 [ swap = ] [ drop f ] if* ;
54 TUPLE: match { i read-only } { j read-only } { seq read-only } ;
56 : match-slice ( i string quot -- match/f )
58 [ swap match boa ] [ 2drop f ] if* ; inline
60 : search-range ( i string reverse? -- seq )
61 [ drop 0 [a,b] ] [ length [a,b) ] if ; inline
63 : match>result ( match reverse? -- i start end string )
65 [ [ i>> ] [ j>> tuck ] [ seq>> ] tri ] dip
66 [ [ swap [ 1+ ] bi@ ] dip ] when
67 ] [ 2drop f f f f ] if ; inline
69 :: next-match ( i string quot reverse? -- i start end string )
70 i string reverse? search-range
71 [ string quot match-slice ] map-find drop
72 reverse? match>result ; inline
74 : do-next-match ( i string regexp -- i start end string )
76 execute-unsafe( i string regexp -- i start end string ) ;
78 : next-slice ( i string regexp -- i/f slice/f )
80 [ slice boa ] [ drop ] if* ; inline
90 : iterate ( iterator -- iterator'/f )
92 [ i>> ] [ string>> ] [ regexp>> ] tri next-slice
93 [ [ [ string>> ] [ regexp>> ] bi ] 2dip match-iterator boa ]
96 : value ( iterator/f -- value/f )
97 dup [ value>> ] when ;
99 : <match-iterator> ( string regexp -- match-iterator )
105 : all-matches ( string regexp -- seq )
106 <match-iterator> [ iterate ] follow [ value ] map ;
108 : count-matches ( string regexp -- n )
113 :: split-slices ( string slices -- new-slices )
114 slices [ to>> ] map 0 prefix
115 slices [ from>> ] map string length suffix
116 [ string <slice> ] 2map ;
120 : first-match ( string regexp -- slice/f )
121 <match-iterator> value ;
123 : re-contains? ( string regexp -- ? )
124 first-match >boolean ;
126 : re-split1 ( string regexp -- before after/f )
127 dupd first-match [ 1array split-slices first2 ] [ f ] if* ;
129 : re-split ( string regexp -- seq )
130 dupd all-matches split-slices ;
132 : re-replace ( string regexp replacement -- result )
133 [ re-split ] dip join ;
137 : get-ast ( regexp -- ast )
138 [ parse-tree>> ] [ options>> ] bi <with-options> ;
140 GENERIC: compile-regexp ( regex -- regexp )
142 : regexp-initial-word ( i string regexp -- i/f )
143 compile-regexp match-index-from ;
145 : do-compile-regexp ( regexp -- regexp )
147 dup \ regexp-initial-word =
148 [ drop _ get-ast ast>dfa dfa>word ] when
151 M: regexp compile-regexp ( regexp -- regexp )
154 M: reverse-regexp compile-regexp ( regexp -- regexp )
155 t backwards? [ do-compile-regexp ] with-variable ;
157 DEFER: compile-next-match
159 : next-initial-word ( i string regexp -- i start end string )
160 compile-next-match do-next-match ;
162 : compile-next-match ( regexp -- regexp )
164 dup \ next-initial-word = [
165 drop _ [ compile-regexp dfa>> ] [ reverse-regexp? ] bi
166 '[ _ '[ _ _ execute ] _ next-match ]
167 (( i string regexp -- i start end string )) simple-define-temp
169 ] change-next-match ;
173 : new-regexp ( string ast options class -- regexp )
174 [ \ regexp-initial-word \ next-initial-word ] dip boa ; inline
176 : make-regexp ( string ast -- regexp )
177 f f <options> regexp new-regexp ;
179 : <optioned-regexp> ( string options -- regexp )
180 [ dup parse-regexp ] [ string>options ] bi*
181 dup on>> reversed-regexp swap member?
182 [ reverse-regexp new-regexp ]
183 [ regexp new-regexp ] if ;
185 : <regexp> ( string -- regexp ) "" <optioned-regexp> ;
189 ! The following two should do some caching
191 : find-regexp-syntax ( string -- prefix suffix )
204 } swap [ subseq? not nip ] curry assoc-find drop ;
206 : take-until ( end lexer -- string )
211 ] change-lexer-column ;
213 : parse-noblank-token ( lexer -- str/f )
214 dup still-parsing-line? [ (parse-token) ] [ drop f ] if ;
216 : parsing-regexp ( accum end -- accum )
217 lexer get [ take-until ] [ parse-noblank-token ] bi
218 <optioned-regexp> compile-next-match parsed ;
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
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
237 [ raw>> dup find-regexp-syntax swap % swap % % ]
238 [ options>> options>string % ] bi
240 ] keep present-text ;