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 : match-index-head ( string regexp -- index/f )
48 [ 0 ] 2dip [ check-string ] dip match-index-from ;
52 : matches? ( string regexp -- ? )
54 [ swap length = ] [ drop f ] if* ;
58 : match-slice ( i string quot -- slice/f )
60 [ swap <slice> ] [ 2drop f ] if* ; inline
62 : match-from ( i string quot -- slice/f )
63 [ [ length [a,b) ] keep ] dip
64 '[ _ _ match-slice ] map-find drop ; inline
66 : next-match ( i string quot -- i match/f )
67 match-from [ dup [ to>> ] when ] keep ; inline
69 : do-next-match ( i string regexp -- i match/f )
70 dup next-match>> execute( i string regexp -- i match/f ) ;
74 : all-matches ( string regexp -- seq )
76 [ 0 [ dup ] ] 2dip '[ _ _ do-next-match ] produce
79 : count-matches ( string regexp -- n )
84 :: split-slices ( string slices -- new-slices )
85 slices [ to>> ] map 0 prefix
86 slices [ from>> ] map string length suffix
87 [ string <slice> ] 2map ;
89 : match-head ( str regexp -- slice/f )
91 [ 0 ] [ check-string ] [ dup dfa>> '[ _ _ execute ] ] tri*
93 ] call( str regexp -- slice/f ) ;
97 : re-split1 ( string regexp -- before after/f )
98 dupd match-head [ 1array split-slices first2 ] [ f ] if* ;
100 : re-split ( string regexp -- seq )
101 dupd all-matches split-slices ;
103 : re-replace ( string regexp replacement -- result )
104 [ re-split ] dip join ;
108 : get-ast ( regexp -- ast )
109 [ parse-tree>> ] [ options>> ] bi <with-options> ;
111 GENERIC: compile-regexp ( regex -- regexp )
113 : regexp-initial-word ( i string regexp -- i/f )
114 compile-regexp match-index-from ;
116 : do-compile-regexp ( regexp -- regexp )
118 dup \ regexp-initial-word =
119 [ drop _ get-ast ast>dfa dfa>word ] when
122 M: regexp compile-regexp ( regexp -- regexp )
125 M: reverse-regexp compile-regexp ( regexp -- regexp )
126 t backwards? [ do-compile-regexp ] with-variable ;
128 GENERIC: compile-next-match ( regexp -- regexp )
130 : next-initial-word ( i string regexp -- i slice/f )
131 compile-next-match do-next-match ;
133 M: regexp compile-next-match ( regexp -- regexp )
135 dup \ next-initial-word = [
136 drop _ compile-regexp dfa>>
137 '[ _ '[ _ _ execute ] next-match ]
138 (( i string -- i match/f )) simple-define-temp
140 ] change-next-match ;
142 ! Write M: reverse-regexp compile-next-match
146 : new-regexp ( string ast options class -- regexp )
147 [ \ regexp-initial-word \ next-initial-word ] dip boa ; inline
149 : make-regexp ( string ast -- regexp )
150 f f <options> regexp new-regexp ;
152 : <optioned-regexp> ( string options -- regexp )
153 [ dup parse-regexp ] [ string>options ] bi*
154 dup on>> reversed-regexp swap member?
155 [ reverse-regexp new-regexp ]
156 [ regexp new-regexp ] if ;
158 : <regexp> ( string -- regexp ) "" <optioned-regexp> ;
162 ! The following two should do some caching
164 : find-regexp-syntax ( string -- prefix suffix )
177 } swap [ subseq? not nip ] curry assoc-find drop ;
179 : take-until ( end lexer -- string )
184 ] change-lexer-column ;
186 : parse-noblank-token ( lexer -- str/f )
187 dup still-parsing-line? [ (parse-token) ] [ drop f ] if ;
189 : parsing-regexp ( accum end -- accum )
190 lexer get [ take-until ] [ parse-noblank-token ] bi
191 <optioned-regexp> compile-next-match parsed ;
195 : R! CHAR: ! parsing-regexp ; parsing
196 : R" CHAR: " parsing-regexp ; parsing
197 : R# CHAR: # parsing-regexp ; parsing
198 : R' CHAR: ' parsing-regexp ; parsing
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
210 [ raw>> dup find-regexp-syntax swap % swap % % ]
211 [ options>> options>string % ] bi
213 ] keep present-text ;