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