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