]> gitweb.factorcode.org Git - factor.git/blob - basis/regexp/regexp.factor
Merge branch 'gtk' into gtk-image-loader
[factor.git] / basis / regexp / regexp.factor
1 ! Copyright (C) 2008, 2009 Doug Coleman, Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors combinators kernel kernel.private math sequences
4 sequences.private strings sets assocs make lexer namespaces parser
5 arrays fry locals regexp.parser splitting sorting regexp.ast
6 regexp.negation regexp.compiler compiler.units words math.ranges ;
7 IN: regexp
8
9 TUPLE: regexp
10     { raw read-only }
11     { parse-tree read-only }
12     { options read-only }
13     dfa next-match ;
14
15 TUPLE: reverse-regexp < regexp ;
16
17 <PRIVATE
18
19 M: lookahead question>quot ! Returns ( index string -- ? )
20     term>> ast>dfa dfa>shortest-word '[ f _ execute ] ;
21
22 : <reversed-option> ( ast -- reversed )
23     "r" string>options <with-options> ;
24
25 M: lookbehind question>quot ! Returns ( index string -- ? )
26     term>> <reversed-option>
27     ast>dfa dfa>reverse-shortest-word
28     '[ [ 1 - ] dip f _ execute ] ;
29
30 : check-string ( string -- string )
31     ! Make this configurable
32     dup string? [ "String required" throw ] unless ;
33
34 : match-index-from ( i string regexp -- index/f )
35     ! This word is unsafe. It assumes that i is a fixnum
36     ! and that string is a string.
37     dup dfa>> execute( index string regexp -- i/f ) ; inline
38
39 GENERIC: end/start ( string regexp -- end start )
40 M: regexp end/start drop length 0 ;
41 M: reverse-regexp end/start drop length 1 - -1 swap ;
42
43 PRIVATE>
44
45 : matches? ( string regexp -- ? )
46     [ check-string ] dip
47     [ end/start ] 2keep
48     match-index-from
49     [ = ] [ drop f ] if* ;
50
51 <PRIVATE
52
53 : search-range ( i string reverse? -- seq )
54     [ drop -1 ] [ length ] if [a,b] ; inline
55
56 :: (next-match) ( i string regexp quot: ( i string regexp -- j ) reverse? -- start end ? )
57     i string regexp quot call dup
58     [| j | reverse? [ j i ] [ i j ] if string ] [ drop f f f ] if ; inline
59
60 :: next-match ( i string regexp quot: ( i string regexp -- j ) reverse? -- start end ? )
61     f f f
62     i string reverse? search-range
63     [ [ 3drop ] dip string regexp quot reverse? (next-match) dup ] find 2drop ; inline
64
65 : do-next-match ( i string regexp -- start end ? )
66     dup next-match>>
67     execute( i string regexp -- start end ? ) ; inline
68
69 :: (each-match-forward) ( ... i string regexp quot: ( ... start end string -- ... ) -- ... )
70     i string length <= [
71         i string regexp do-next-match [| start end |
72             start end string quot call
73             start end eq? [ end 1 +  ] [ end ] if
74             string regexp quot (each-match-forward)
75         ] [ 2drop ] if
76     ] when ; inline recursive
77
78 :: (each-match-backward) ( ... i string regexp quot: ( ... start end string -- ... ) -- ... )
79     i -1 >= [
80         i string regexp do-next-match [| start end |
81             start 1 + end 1 + string quot call
82             start end eq? [ start 1 - ] [ start ] if
83             string regexp quot (each-match-backward)
84         ] [ 2drop ] if
85     ] when ; inline recursive
86
87 : (each-match) ( ... i string regexp quot: ( ... start end string -- ... ) -- ... )
88     over reverse-regexp? [ (each-match-backward) ] [ (each-match-forward) ] if ; inline
89
90 GENERIC: match-iterator-start ( string regexp -- start )
91 M: regexp match-iterator-start 2drop 0 ;
92 M: reverse-regexp match-iterator-start drop length ;
93
94 : prepare-match-iterator ( string regexp -- i string regexp )
95     [ check-string ] dip [ match-iterator-start ] 2keep ; inline
96
97 PRIVATE>
98
99 : each-match ( ... string regexp quot: ( ... start end string -- ... ) -- ... )
100     [ prepare-match-iterator ] dip (each-match) ; inline
101
102 : map-matches ( ... string regexp quot: ( ... start end string -- ... obj ) -- ... seq )
103     collector [ each-match ] dip >array ; inline
104
105 : all-matching-slices ( string regexp -- seq )
106     [ slice boa ] map-matches ;
107
108 : all-matching-subseqs ( string regexp -- seq )
109     [ subseq ] map-matches ;
110
111 : count-matches ( string regexp -- n )
112     [ 0 ] 2dip [ 3drop 1 + ] each-match ;
113
114 <PRIVATE
115
116 :: (re-split) ( string regexp quot -- new-slices )
117     0 string regexp [| end start end' string |
118         end' ! leave it on the stack for the next iteration
119         end start string quot call
120     ] map-matches
121     ! Final chunk
122     swap string length string quot call suffix ; inline
123
124 PRIVATE>
125
126 :: first-match ( string regexp -- slice/f )
127     string regexp prepare-match-iterator do-next-match [
128         regexp reverse-regexp? [ [ 1 + ] bi@ ] when
129         string slice boa
130     ] [ 2drop f ] if ;
131
132 : re-contains? ( string regexp -- ? )
133     prepare-match-iterator do-next-match [ 2drop ] dip >boolean ;
134
135 : re-split ( string regexp -- seq )
136     [ slice boa ] (re-split) ;
137
138 : re-replace ( string regexp replacement -- result )
139     [ [ subseq ] (re-split) ] dip join ;
140
141 <PRIVATE
142
143 : get-ast ( regexp -- ast )
144     [ parse-tree>> ] [ options>> ] bi <with-options> ;
145
146 GENERIC: compile-regexp ( regex -- regexp )
147
148 : regexp-initial-word ( i string regexp -- i/f )
149     [ compile-regexp ] with-compilation-unit match-index-from ;
150
151 M: regexp compile-regexp ( regexp -- regexp )
152     dup '[
153         dup \ regexp-initial-word =
154         [ drop _ get-ast ast>dfa dfa>word ] when
155     ] change-dfa ;
156
157 M: reverse-regexp compile-regexp ( regexp -- regexp )
158     t backwards? [ call-next-method ] with-variable ;
159
160 DEFER: compile-next-match
161
162 : next-initial-word ( i string regexp -- start end string )
163     [ compile-next-match ] with-compilation-unit do-next-match ;
164
165 : compile-next-match ( regexp -- regexp )
166     dup '[
167         dup \ next-initial-word = [
168             drop _ [ compile-regexp dfa>> def>> ] [ reverse-regexp? ] bi
169             '[ { array-capacity string regexp } declare _ _ next-match ]
170             (( i string regexp -- start end string )) define-temp
171         ] when
172     ] change-next-match ;
173
174 PRIVATE>
175
176 : new-regexp ( string ast options class -- regexp )
177     [ \ regexp-initial-word \ next-initial-word ] dip boa ; inline
178
179 : make-regexp ( string ast -- regexp )
180     f f <options> regexp new-regexp ;
181
182 : <optioned-regexp> ( string options -- regexp )
183     [ dup parse-regexp ] [ string>options ] bi*
184     dup on>> reversed-regexp swap member?
185     [ reverse-regexp new-regexp ]
186     [ regexp new-regexp ] if ;
187
188 : <regexp> ( string -- regexp ) "" <optioned-regexp> ;
189
190 <PRIVATE
191
192 ! The following two should do some caching
193
194 : find-regexp-syntax ( string -- prefix suffix )
195     {
196         { "R/ "  "/"  }
197         { "R! "  "!"  }
198         { "R\" " "\"" }
199         { "R# "  "#"  }
200         { "R' "  "'"  }
201         { "R( "  ")"  }
202         { "R@ "  "@"  }
203         { "R[ "  "]"  }
204         { "R` "  "`"  }
205         { "R{ "  "}"  }
206         { "R| "  "|"  }
207     } swap [ subseq? not nip ] curry assoc-find drop ;
208
209 : take-until ( end lexer -- string )
210     dup skip-blank [
211         [ index-from ] 2keep
212         [ swapd subseq ]
213         [ 2drop 1 + ] 3bi
214     ] change-lexer-column ;
215
216 : parse-noblank-token ( lexer -- str/f )
217     dup still-parsing-line? [ (parse-token) ] [ drop f ] if ;
218
219 : parsing-regexp ( accum end -- accum )
220     lexer get [ take-until ] [ parse-noblank-token ] bi
221     <optioned-regexp> compile-next-match suffix! ;
222
223 PRIVATE>
224
225 SYNTAX: R! CHAR: ! parsing-regexp ;
226 SYNTAX: R" CHAR: " parsing-regexp ;
227 SYNTAX: R# CHAR: # parsing-regexp ;
228 SYNTAX: R' CHAR: ' parsing-regexp ;
229 SYNTAX: R( CHAR: ) parsing-regexp ;
230 SYNTAX: R/ CHAR: / parsing-regexp ;
231 SYNTAX: R@ CHAR: @ parsing-regexp ;
232 SYNTAX: R[ CHAR: ] parsing-regexp ;
233 SYNTAX: R` CHAR: ` parsing-regexp ;
234 SYNTAX: R{ CHAR: } parsing-regexp ;
235 SYNTAX: R| CHAR: | parsing-regexp ;
236
237 USE: vocabs.loader
238
239 { "prettyprint" "regexp" } "regexp.prettyprint" require-when