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 ]
38 : check-string ( string -- string )
39 ! Make this configurable
40 dup string? [ "String required" throw ] unless ;
42 : match-index-from ( i string regexp -- index/f )
43 ! This word is unsafe. It assumes that i is a fixnum
44 ! and that string is a string.
45 dup dfa>> execute( index string regexp -- i/f ) ;
47 GENERIC: end/start ( string regexp -- end start )
48 M: regexp end/start drop length 0 ;
49 M: reverse-regexp end/start drop length 1- -1 swap ;
53 : matches? ( string regexp -- ? )
57 [ swap = ] [ drop f ] if* ;
61 : match-slice ( i string quot -- slice/f )
63 [ swap <slice> ] [ 2drop f ] if* ; inline
65 : match-from ( i string quot -- slice/f )
66 [ [ length [a,b) ] keep ] dip
67 '[ _ _ match-slice ] map-find drop ; inline
69 : next-match ( i string quot -- i match/f )
70 match-from [ dup [ to>> ] when ] keep ; inline
72 : do-next-match ( i string regexp -- i match/f )
73 dup next-match>> execute( i string regexp -- i match/f ) ;
77 : all-matches ( string regexp -- seq )
79 [ 0 [ dup ] ] 2dip '[ _ _ do-next-match ] produce
82 : count-matches ( string regexp -- n )
87 :: split-slices ( string slices -- new-slices )
88 slices [ to>> ] map 0 prefix
89 slices [ from>> ] map string length suffix
90 [ string <slice> ] 2map ;
94 : first-match ( string regexp -- slice/f )
95 [ 0 ] [ check-string ] [ ] tri*
98 : re-contains? ( string regexp -- ? )
99 first-match >boolean ;
101 : re-split1 ( string regexp -- before after/f )
102 dupd first-match [ 1array split-slices first2 ] [ f ] if* ;
104 : re-split ( string regexp -- seq )
105 dupd all-matches split-slices ;
107 : re-replace ( string regexp replacement -- result )
108 [ re-split ] dip join ;
112 : get-ast ( regexp -- ast )
113 [ parse-tree>> ] [ options>> ] bi <with-options> ;
115 GENERIC: compile-regexp ( regex -- regexp )
117 : regexp-initial-word ( i string regexp -- i/f )
118 compile-regexp match-index-from ;
120 : do-compile-regexp ( regexp -- regexp )
122 dup \ regexp-initial-word =
123 [ drop _ get-ast ast>dfa dfa>word ] when
126 M: regexp compile-regexp ( regexp -- regexp )
129 M: reverse-regexp compile-regexp ( regexp -- regexp )
130 t backwards? [ do-compile-regexp ] with-variable ;
132 GENERIC: compile-next-match ( regexp -- regexp )
134 : next-initial-word ( i string regexp -- i slice/f )
135 compile-next-match do-next-match ;
137 M: regexp compile-next-match ( regexp -- regexp )
139 dup \ next-initial-word = [
140 drop _ compile-regexp dfa>>
141 '[ _ '[ _ _ execute ] next-match ]
142 (( i string -- i match/f )) simple-define-temp
144 ] change-next-match ;
146 ! Write M: reverse-regexp compile-next-match
150 : new-regexp ( string ast options class -- regexp )
151 [ \ regexp-initial-word \ next-initial-word ] dip boa ; inline
153 : make-regexp ( string ast -- regexp )
154 f f <options> regexp new-regexp ;
156 : <optioned-regexp> ( string options -- regexp )
157 [ dup parse-regexp ] [ string>options ] bi*
158 dup on>> reversed-regexp swap member?
159 [ reverse-regexp new-regexp ]
160 [ regexp new-regexp ] if ;
162 : <regexp> ( string -- regexp ) "" <optioned-regexp> ;
166 ! The following two should do some caching
168 : find-regexp-syntax ( string -- prefix suffix )
181 } swap [ subseq? not nip ] curry assoc-find drop ;
183 : take-until ( end lexer -- string )
188 ] change-lexer-column ;
190 : parse-noblank-token ( lexer -- str/f )
191 dup still-parsing-line? [ (parse-token) ] [ drop f ] if ;
193 : parsing-regexp ( accum end -- accum )
194 lexer get [ take-until ] [ parse-noblank-token ] bi
195 <optioned-regexp> compile-next-match parsed ;
199 : R! CHAR: ! parsing-regexp ; parsing
200 : R" CHAR: " parsing-regexp ; parsing
201 : R# CHAR: # parsing-regexp ; parsing
202 : R' CHAR: ' parsing-regexp ; parsing
203 : R( CHAR: ) parsing-regexp ; parsing
204 : R/ CHAR: / parsing-regexp ; parsing
205 : R@ CHAR: @ parsing-regexp ; parsing
206 : R[ CHAR: ] parsing-regexp ; parsing
207 : R` CHAR: ` parsing-regexp ; parsing
208 : R{ CHAR: } parsing-regexp ; parsing
209 : R| CHAR: | parsing-regexp ; parsing
214 [ raw>> dup find-regexp-syntax swap % swap % % ]
215 [ options>> options>string % ] bi
217 ] keep present-text ;