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