]> gitweb.factorcode.org Git - factor.git/blob - basis/regexp/classes/classes.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / basis / regexp / classes / classes.factor
1 ! Copyright (C) 2008, 2009 Doug Coleman, Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors kernel math math.order words combinators locals
4 ascii unicode.categories combinators.short-circuit sequences
5 fry macros arrays assocs sets classes ;
6 IN: regexp.classes
7
8 SINGLETONS: any-char any-char-no-nl
9 letter-class LETTER-class Letter-class digit-class
10 alpha-class non-newline-blank-class
11 ascii-class punctuation-class java-printable-class blank-class
12 control-character-class hex-digit-class java-blank-class c-identifier-class
13 unmatchable-class terminator-class word-boundary-class ;
14
15 SINGLETONS: beginning-of-input ^ end-of-input $ end-of-file ;
16
17 TUPLE: range from to ;
18 C: <range> range
19
20 GENERIC: class-member? ( obj class -- ? )
21
22 M: t class-member? ( obj class -- ? ) 2drop t ;
23
24 M: integer class-member? ( obj class -- ? ) = ;
25
26 M: range class-member? ( obj class -- ? )
27     [ from>> ] [ to>> ] bi between? ;
28
29 M: any-char class-member? ( obj class -- ? )
30     2drop t ;
31
32 M: any-char-no-nl class-member? ( obj class -- ? )
33     drop CHAR: \n = not ;
34
35 M: letter-class class-member? ( obj class -- ? )
36     drop letter? ;
37             
38 M: LETTER-class class-member? ( obj class -- ? )
39     drop LETTER? ;
40
41 M: Letter-class class-member? ( obj class -- ? )
42     drop Letter? ;
43
44 M: ascii-class class-member? ( obj class -- ? )
45     drop ascii? ;
46
47 M: digit-class class-member? ( obj class -- ? )
48     drop digit? ;
49
50 : c-identifier-char? ( ch -- ? )
51     { [ alpha? ] [ CHAR: _ = ] } 1|| ;
52
53 M: c-identifier-class class-member? ( obj class -- ? )
54     drop c-identifier-char? ;
55
56 M: alpha-class class-member? ( obj class -- ? )
57     drop alpha? ;
58
59 : punct? ( ch -- ? )
60     "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" member? ;
61
62 M: punctuation-class class-member? ( obj class -- ? )
63     drop punct? ;
64
65 : java-printable? ( ch -- ? )
66     { [ alpha? ] [ punct? ] } 1|| ;
67
68 M: java-printable-class class-member? ( obj class -- ? )
69     drop java-printable? ;
70
71 M: non-newline-blank-class class-member? ( obj class -- ? )
72     drop { [ blank? ] [ CHAR: \n = not ] } 1&& ;
73
74 M: control-character-class class-member? ( obj class -- ? )
75     drop control? ;
76
77 : hex-digit? ( ch -- ? )
78     {
79         [ CHAR: A CHAR: F between? ]
80         [ CHAR: a CHAR: f between? ]
81         [ CHAR: 0 CHAR: 9 between? ]
82     } 1|| ;
83
84 M: hex-digit-class class-member? ( obj class -- ? )
85     drop hex-digit? ;
86
87 : java-blank? ( ch -- ? )
88     {
89         CHAR: \s CHAR: \t CHAR: \n
90         HEX: b HEX: 7 CHAR: \r
91     } member? ;
92
93 M: java-blank-class class-member? ( obj class -- ? )
94     drop java-blank? ;
95
96 M: unmatchable-class class-member? ( obj class -- ? )
97     2drop f ;
98
99 M: terminator-class class-member? ( obj class -- ? )
100     drop "\r\n\u000085\u002029\u002028" member? ;
101
102 M: ^ class-member? ( obj class -- ? )
103     2drop f ;
104
105 M: $ class-member? ( obj class -- ? )
106     2drop f ;
107
108 M: f class-member? 2drop f ;
109
110 TUPLE: primitive-class class ;
111 C: <primitive-class> primitive-class
112
113 TUPLE: or-class seq ;
114
115 TUPLE: not-class class ;
116
117 TUPLE: and-class seq ;
118
119 GENERIC: combine-and ( class1 class2 -- combined ? )
120
121 : replace-if-= ( object object -- object ? )
122     over = ;
123
124 M: object combine-and replace-if-= ;
125
126 M: t combine-and
127     drop t ;
128
129 M: f combine-and
130     nip t ;
131
132 M: not-class combine-and
133     class>> 2dup = [ 2drop f t ] [
134         dup integer? [
135             2dup swap class-member?
136             [ 2drop f f ]
137             [ drop t ] if
138         ] [ 2drop f f ] if
139     ] if ;
140
141 M: integer combine-and
142     swap 2dup class-member? [ drop t ] [ 2drop f t ] if ;
143
144 GENERIC: combine-or ( class1 class2 -- combined ? )
145
146 M: object combine-or replace-if-= ;
147
148 M: t combine-or
149     nip t ;
150
151 M: f combine-or
152     drop t ;
153
154 M: not-class combine-or
155     class>> = [ t t ] [ f f ] if ;
156
157 M: integer combine-or
158     2dup swap class-member? [ drop t ] [ 2drop f f ] if ;
159
160 : flatten ( seq class -- newseq )
161     '[ dup _ instance? [ seq>> ] [ 1array ] if ] map concat ; inline
162
163 : try-combine ( elt1 elt2 quot -- combined/f ? )
164     3dup call [ [ 3drop ] dip t ] [ drop swapd call ] if ; inline
165
166 DEFER: answer
167
168 :: try-cancel ( elt1 elt2 empty -- combined/f ? )
169     [ elt1 elt2 empty answer dup elt1 = not ] try-combine ;
170
171 :: prefix-combining ( seq elt quot: ( elt1 elt2 -- combined/f ? ) -- newseq )
172     f :> combined!
173     seq [ elt quot call swap combined! ] find drop
174     [ seq remove-nth combined prefix ]
175     [ seq elt prefix ] if* ; inline
176
177 : combine-by ( seq quot -- new-seq )
178     { } swap '[ _ prefix-combining ] reduce ; inline
179
180 :: seq>instance ( seq empty class -- instance )
181     seq length {
182         { 0 [ empty ] }
183         { 1 [ seq first ] }
184         [ drop class new seq >>seq ]
185     } case ; inline
186
187 :: combine ( seq quot: ( elt1 elt2 -- combined/f ? ) empty class -- newseq )
188     seq class flatten
189     [ quot try-combine ] combine-by
190     ! [ empty try-cancel ] combine-by ! This makes the algorithm O(n^4)
191     empty class seq>instance ; inline
192
193 : <and-class> ( seq -- class )
194     [ combine-and ] t and-class combine ;
195
196 M: and-class class-member?
197     seq>> [ class-member? ] with all? ;
198
199 : <or-class> ( seq -- class )
200     [ combine-or ] f or-class combine ;
201
202 M: or-class class-member?
203     seq>> [ class-member? ] with any? ;
204
205 GENERIC: <not-class> ( class -- inverse )
206
207 M: object <not-class>
208     not-class boa ;
209
210 M: not-class <not-class>
211     class>> ;
212
213 M: and-class <not-class>
214     seq>> [ <not-class> ] map <or-class> ;
215
216 M: or-class <not-class>
217     seq>> [ <not-class> ] map <and-class> ;
218
219 M: t <not-class> drop f ;
220 M: f <not-class> drop t ;
221
222 M: not-class class-member?
223     class>> class-member? not ;
224
225 M: primitive-class class-member?
226     class>> class-member? ;
227
228 UNION: class primitive-class not-class or-class and-class range ;
229
230 TUPLE: condition question yes no ;
231 C: <condition> condition
232
233 GENERIC# answer 2 ( class from to -- new-class )
234
235 M:: object answer ( class from to -- new-class )
236     class from = to class ? ;
237
238 : replace-compound ( class from to -- seq )
239     [ seq>> ] 2dip '[ _ _ answer ] map ;
240
241 M: and-class answer
242     replace-compound <and-class> ;
243
244 M: or-class answer
245     replace-compound <or-class> ;
246
247 M: not-class answer
248     [ class>> ] 2dip answer <not-class> ;
249
250 : assoc-answer ( table question answer -- new-table )
251     '[ _ _ answer ] assoc-map
252     [ nip ] assoc-filter ;
253
254 : assoc-answers ( table questions answer -- new-table )
255     '[ _ assoc-answer ] each ;
256
257 DEFER: make-condition
258
259 : (make-condition) ( table questions question -- condition )
260     [ 2nip ]
261     [ swap [ t assoc-answer ] dip make-condition ]
262     [ swap [ f assoc-answer ] dip make-condition ] 3tri
263     2dup = [ 2nip ] [ <condition> ] if ;
264
265 : make-condition ( table questions -- condition )
266     [ keys ] [ unclip (make-condition) ] if-empty ;
267
268 GENERIC: class>questions ( class -- questions )
269 : compound-questions ( class -- questions ) seq>> [ class>questions ] gather ;
270 M: or-class class>questions compound-questions ;
271 M: and-class class>questions compound-questions ;
272 M: not-class class>questions class>> class>questions ;
273 M: object class>questions 1array ;
274
275 : table>questions ( table -- questions )
276     values [ class>questions ] gather >array t swap remove ;
277
278 : table>condition ( table -- condition )
279     ! input table is state => class
280     >alist dup table>questions make-condition ;
281
282 : condition-map ( condition quot: ( obj -- obj' ) -- new-condition ) 
283     over condition? [
284         [ [ question>> ] [ yes>> ] [ no>> ] tri ] dip
285         '[ _ condition-map ] bi@ <condition>
286     ] [ call ] if ; inline recursive
287
288 : condition-states ( condition -- states )
289     dup condition? [
290         [ yes>> ] [ no>> ] bi
291         [ condition-states ] bi@ append prune
292     ] [ 1array ] if ;
293
294 : condition-at ( condition assoc -- new-condition )
295     '[ _ at ] condition-map ;