]> gitweb.factorcode.org Git - factor.git/blob - basis/regexp/regexp.factor
Adding word breaks to regexp
[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 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 ;
8 IN: regexp
9
10 TUPLE: regexp
11     { raw read-only }
12     { parse-tree read-only }
13     { options read-only }
14     dfa next-match ;
15
16 TUPLE: reverse-regexp < regexp ;
17
18 <PRIVATE
19
20 M: lookahead question>quot ! 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 ! Returns ( index string -- ? )
27     term>> <reversed-option>
28     ast>dfa dfa>reverse-shortest-word
29     '[ [ 1- ] dip f _ execute ] ;
30
31 : check-string ( string -- string )
32     ! Make this configurable
33     dup string? [ "String required" throw ] unless ;
34
35 : match-index-from ( i string regexp -- index/f )
36     ! This word is unsafe. It assumes that i is a fixnum
37     ! and that string is a string.
38     dup dfa>> execute-unsafe( index string regexp -- i/f ) ;
39
40 GENERIC: end/start ( string regexp -- end start )
41 M: regexp end/start drop length 0 ;
42 M: reverse-regexp end/start drop length 1- -1 swap ;
43
44 PRIVATE>
45
46 : matches? ( string regexp -- ? )
47     [ end/start ] 2keep
48     [ check-string ] dip
49     match-index-from
50     [ swap = ] [ drop f ] if* ;
51
52 <PRIVATE
53
54 TUPLE: match { i read-only } { j read-only } { seq read-only } ;
55
56 : match-slice ( i string quot -- match/f )
57     [ 2dup ] dip call
58     [ swap match boa ] [ 2drop f ] if* ; inline
59
60 : search-range ( i string reverse? -- seq )
61     [ drop 0 [a,b] ] [ length [a,b) ] if ; inline
62
63 : match>result ( match reverse? -- i start end string )
64     over [
65         [ [ i>> ] [ j>> tuck ] [ seq>> ] tri ] dip
66         [ [ swap [ 1+ ] bi@ ] dip ] when
67     ] [ 2drop f f f f ] if ; inline
68
69 :: next-match ( i string quot reverse? -- i start end string )
70     i string reverse? search-range
71     [ string quot match-slice ] map-find drop
72     reverse? match>result ; inline
73
74 : do-next-match ( i string regexp -- i start end string )
75     dup next-match>>
76     execute-unsafe( i string regexp -- i start end string ) ;
77
78 : next-slice ( i string regexp -- i/f slice/f )
79     do-next-match
80     [ slice boa ] [ drop ] if* ; inline
81
82 PRIVATE>
83
84 TUPLE: match-iterator
85     { string read-only }
86     { regexp read-only }
87     { i read-only }
88     { value read-only } ;
89
90 : iterate ( iterator -- iterator'/f )
91     dup
92     [ i>> ] [ string>> ] [ regexp>> ] tri next-slice
93     [ [ [ string>> ] [ regexp>> ] bi ] 2dip match-iterator boa ]
94     [ 2drop f ] if* ;
95
96 : value ( iterator/f -- value/f )
97     dup [ value>> ] when ;
98
99 : <match-iterator> ( string regexp -- match-iterator )
100     [ check-string ] dip
101     2dup end/start nip f
102     match-iterator boa
103     iterate ; inline
104
105 : all-matches ( string regexp -- seq )
106     <match-iterator> [ iterate ] follow [ value ] map ;
107
108 : count-matches ( string regexp -- n )
109     all-matches length ;
110
111 <PRIVATE
112
113 :: split-slices ( string slices -- new-slices )
114     slices [ to>> ] map 0 prefix
115     slices [ from>> ] map string length suffix
116     [ string <slice> ] 2map ;
117
118 PRIVATE>
119
120 : first-match ( string regexp -- slice/f )
121     <match-iterator> value ;
122
123 : re-contains? ( string regexp -- ? )
124     first-match >boolean ;
125
126 : re-split1 ( string regexp -- before after/f )
127     dupd first-match [ 1array split-slices first2 ] [ f ] if* ;
128
129 : re-split ( string regexp -- seq )
130     dupd all-matches split-slices ;
131
132 : re-replace ( string regexp replacement -- result )
133     [ re-split ] dip join ;
134
135 <PRIVATE
136
137 : get-ast ( regexp -- ast )
138     [ parse-tree>> ] [ options>> ] bi <with-options> ;
139
140 GENERIC: compile-regexp ( regex -- regexp )
141
142 : regexp-initial-word ( i string regexp -- i/f )
143     compile-regexp match-index-from ;
144
145 : do-compile-regexp ( regexp -- regexp )
146     dup '[
147         dup \ regexp-initial-word =
148         [ drop _ get-ast ast>dfa dfa>word ] when
149     ] change-dfa ;
150
151 M: regexp compile-regexp ( regexp -- regexp )
152     do-compile-regexp ;
153
154 M: reverse-regexp compile-regexp ( regexp -- regexp )
155     t backwards? [ do-compile-regexp ] with-variable ;
156
157 DEFER: compile-next-match
158
159 : next-initial-word ( i string regexp -- i start end string )
160     compile-next-match do-next-match ;
161
162 : compile-next-match ( regexp -- regexp )
163     dup '[
164         dup \ next-initial-word = [
165             drop _ [ compile-regexp dfa>> ] [ reverse-regexp? ] bi
166             '[ _ '[ _ _ execute ] _ next-match ]
167             (( i string regexp -- i start end string )) simple-define-temp
168         ] when
169     ] change-next-match ;
170
171 PRIVATE>
172
173 : new-regexp ( string ast options class -- regexp )
174     [ \ regexp-initial-word \ next-initial-word ] dip boa ; inline
175
176 : make-regexp ( string ast -- regexp )
177     f f <options> regexp new-regexp ;
178
179 : <optioned-regexp> ( string options -- regexp )
180     [ dup parse-regexp ] [ string>options ] bi*
181     dup on>> reversed-regexp swap member?
182     [ reverse-regexp new-regexp ]
183     [ regexp new-regexp ] if ;
184
185 : <regexp> ( string -- regexp ) "" <optioned-regexp> ;
186
187 <PRIVATE
188
189 ! The following two should do some caching
190
191 : find-regexp-syntax ( string -- prefix suffix )
192     {
193         { "R/ "  "/"  }
194         { "R! "  "!"  }
195         { "R\" " "\"" }
196         { "R# "  "#"  }
197         { "R' "  "'"  }
198         { "R( "  ")"  }
199         { "R@ "  "@"  }
200         { "R[ "  "]"  }
201         { "R` "  "`"  }
202         { "R{ "  "}"  }
203         { "R| "  "|"  }
204     } swap [ subseq? not nip ] curry assoc-find drop ;
205
206 : take-until ( end lexer -- string )
207     dup skip-blank [
208         [ index-from ] 2keep
209         [ swapd subseq ]
210         [ 2drop 1+ ] 3bi
211     ] change-lexer-column ;
212
213 : parse-noblank-token ( lexer -- str/f )
214     dup still-parsing-line? [ (parse-token) ] [ drop f ] if ;
215
216 : parsing-regexp ( accum end -- accum )
217     lexer get [ take-until ] [ parse-noblank-token ] bi
218     <optioned-regexp> compile-next-match parsed ;
219
220 PRIVATE>
221
222 : R! CHAR: ! parsing-regexp ; parsing
223 : R" CHAR: " parsing-regexp ; parsing
224 : R# CHAR: # parsing-regexp ; parsing
225 : R' CHAR: ' parsing-regexp ; parsing
226 : R( CHAR: ) parsing-regexp ; parsing
227 : R/ CHAR: / parsing-regexp ; parsing
228 : R@ CHAR: @ parsing-regexp ; parsing
229 : R[ CHAR: ] parsing-regexp ; parsing
230 : R` CHAR: ` parsing-regexp ; parsing
231 : R{ CHAR: } parsing-regexp ; parsing
232 : R| CHAR: | parsing-regexp ; parsing
233
234 M: regexp pprint*
235     [
236         [
237             [ raw>> dup find-regexp-syntax swap % swap % % ]
238             [ options>> options>string % ] bi
239         ] "" make
240     ] keep present-text ;
241