]> gitweb.factorcode.org Git - factor.git/blob - basis/regexp/classes/classes.factor
Reorganizing things in regexp, mostly
[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 mirrors unicode.script
6 unicode.data ;
7 IN: regexp.classes
8
9 SINGLETONS: dot 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 ^unix $unix word-break ;
17
18 TUPLE: range-class from to ;
19 C: <range-class> range-class
20
21 TUPLE: primitive-class class ;
22 C: <primitive-class> primitive-class
23
24 TUPLE: category-class category ;
25 C: <category-class> category-class
26
27 TUPLE: category-range-class category ;
28 C: <category-range-class> category-range-class
29
30 TUPLE: script-class script ;
31 C: <script-class> script-class
32
33 GENERIC: class-member? ( obj class -- ? )
34
35 M: t class-member? ( obj class -- ? ) 2drop t ;
36
37 M: integer class-member? ( obj class -- ? ) = ;
38
39 M: range-class class-member? ( obj class -- ? )
40     [ from>> ] [ to>> ] bi between? ;
41
42 M: letter-class class-member? ( obj class -- ? )
43     drop letter? ;
44             
45 M: LETTER-class class-member? ( obj class -- ? )
46     drop LETTER? ;
47
48 M: Letter-class class-member? ( obj class -- ? )
49     drop Letter? ;
50
51 M: ascii-class class-member? ( obj class -- ? )
52     drop ascii? ;
53
54 M: digit-class class-member? ( obj class -- ? )
55     drop digit? ;
56
57 : c-identifier-char? ( ch -- ? )
58     { [ alpha? ] [ CHAR: _ = ] } 1|| ;
59
60 M: c-identifier-class class-member? ( obj class -- ? )
61     drop c-identifier-char? ;
62
63 M: alpha-class class-member? ( obj class -- ? )
64     drop alpha? ;
65
66 : punct? ( ch -- ? )
67     "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" member? ;
68
69 M: punctuation-class class-member? ( obj class -- ? )
70     drop punct? ;
71
72 : java-printable? ( ch -- ? )
73     { [ alpha? ] [ punct? ] } 1|| ;
74
75 M: java-printable-class class-member? ( obj class -- ? )
76     drop java-printable? ;
77
78 M: non-newline-blank-class class-member? ( obj class -- ? )
79     drop { [ blank? ] [ CHAR: \n = not ] } 1&& ;
80
81 M: control-character-class class-member? ( obj class -- ? )
82     drop control? ;
83
84 : hex-digit? ( ch -- ? )
85     {
86         [ CHAR: A CHAR: F between? ]
87         [ CHAR: a CHAR: f between? ]
88         [ CHAR: 0 CHAR: 9 between? ]
89     } 1|| ;
90
91 M: hex-digit-class class-member? ( obj class -- ? )
92     drop hex-digit? ;
93
94 : java-blank? ( ch -- ? )
95     {
96         CHAR: \s CHAR: \t CHAR: \n
97         HEX: b HEX: 7 CHAR: \r
98     } member? ;
99
100 M: java-blank-class class-member? ( obj class -- ? )
101     drop java-blank? ;
102
103 M: unmatchable-class class-member? ( obj class -- ? )
104     2drop f ;
105
106 M: terminator-class class-member? ( obj class -- ? )
107     drop "\r\n\u000085\u002029\u002028" member? ;
108
109 M: f class-member? 2drop f ;
110
111 M: script-class class-member?
112     [ script-of ] [ script>> ] bi* = ;
113
114 M: category-class class-member?
115     [ category# ] [ category>> ] bi* = ;
116
117 M: category-range-class class-member?
118     [ category first ] [ category>> ] bi* = ;
119
120 TUPLE: not-class class ;
121
122 PREDICATE: not-integer < not-class class>> integer? ;
123
124 UNION: simple-class
125     primitive-class range-class category-class category-range-class dot ;
126 PREDICATE: not-simple < not-class class>> simple-class? ;
127
128 M: not-class class-member?
129     class>> class-member? not ;
130
131 TUPLE: or-class seq ;
132
133 M: or-class class-member?
134     seq>> [ class-member? ] with any? ;
135
136 TUPLE: and-class seq ;
137
138 M: and-class class-member?
139     seq>> [ class-member? ] with all? ;
140
141 DEFER: substitute
142
143 : flatten ( seq class -- newseq )
144     '[ dup _ instance? [ seq>> ] [ 1array ] if ] map concat ; inline
145
146 :: seq>instance ( seq empty class -- instance )
147     seq length {
148         { 0 [ empty ] }
149         { 1 [ seq first ] }
150         [ drop class new seq { } like >>seq ]
151     } case ; inline
152
153 TUPLE: class-partition integers not-integers simples not-simples and or other ;
154
155 : partition-classes ( seq -- class-partition )
156     prune
157     [ integer? ] partition
158     [ not-integer? ] partition
159     [ simple-class? ] partition
160     [ not-simple? ] partition
161     [ and-class? ] partition
162     [ or-class? ] partition
163     class-partition boa ;
164
165 : class-partition>seq ( class-partition -- seq )
166     make-mirror values concat ;
167
168 : repartition ( partition -- partition' )
169     ! This could be made more efficient; only and and or are effected
170     class-partition>seq partition-classes ;
171
172 : filter-not-integers ( partition -- partition' )
173     dup
174     [ simples>> ] [ not-simples>> ] [ or>> ] tri
175     3append and-class boa
176     '[ [ class>> _ class-member? ] filter ] change-not-integers ;
177
178 : answer-ors ( partition -- partition' )
179     dup [ not-integers>> ] [ not-simples>> ] [ simples>> ] tri 3append
180     '[ [ _ [ t substitute ] each ] map ] change-or ;
181
182 : contradiction? ( partition -- ? )
183     {
184         [ [ simples>> ] [ not-simples>> ] bi intersects? ]
185         [ other>> f swap member? ]
186     } 1|| ;
187
188 : make-and-class ( partition -- and-class )
189     answer-ors repartition
190     [ t swap remove ] change-other
191     dup contradiction?
192     [ drop f ]
193     [ filter-not-integers class-partition>seq prune t and-class seq>instance ] if ;
194
195 : <and-class> ( seq -- class )
196     dup and-class flatten partition-classes
197     dup integers>> length {
198         { 0 [ nip make-and-class ] }
199         { 1 [ integers>> first [ '[ _ swap class-member? ] all? ] keep and ] }
200         [ 3drop f ]
201     } case ;
202
203 : filter-integers ( partition -- partition' )
204     dup
205     [ simples>> ] [ not-simples>> ] [ and>> ] tri
206     3append or-class boa
207     '[ [ _ class-member? not ] filter ] change-integers ;
208
209 : answer-ands ( partition -- partition' )
210     dup [ integers>> ] [ not-simples>> ] [ simples>> ] tri 3append
211     '[ [ _ [ f substitute ] each ] map ] change-and ;
212
213 : tautology? ( partition -- ? )
214     {
215         [ [ simples>> ] [ not-simples>> ] bi intersects? ]
216         [ other>> t swap member? ]
217     } 1|| ;
218
219 : make-or-class ( partition -- and-class )
220     answer-ands repartition
221     [ f swap remove ] change-other
222     dup tautology?
223     [ drop t ]
224     [ filter-integers class-partition>seq prune f or-class seq>instance ] if ;
225
226 : <or-class> ( seq -- class )
227     dup or-class flatten partition-classes
228     dup not-integers>> length {
229         { 0 [ nip make-or-class ] }
230         { 1 [ not-integers>> first [ class>> '[ _ swap class-member? ] any? ] keep or ] }
231         [ 3drop t ]
232     } case ;
233
234 GENERIC: <not-class> ( class -- inverse )
235
236 M: object <not-class>
237     not-class boa ;
238
239 M: not-class <not-class>
240     class>> ;
241
242 M: and-class <not-class>
243     seq>> [ <not-class> ] map <or-class> ;
244
245 M: or-class <not-class>
246     seq>> [ <not-class> ] map <and-class> ;
247
248 M: t <not-class> drop f ;
249 M: f <not-class> drop t ;
250
251 M: primitive-class class-member?
252     class>> class-member? ;
253
254 TUPLE: condition question yes no ;
255 C: <condition> condition
256
257 GENERIC# answer 2 ( class from to -- new-class )
258
259 M:: object answer ( class from to -- new-class )
260     class from = to class ? ;
261
262 : replace-compound ( class from to -- seq )
263     [ seq>> ] 2dip '[ _ _ answer ] map ;
264
265 M: and-class answer
266     replace-compound <and-class> ;
267
268 M: or-class answer
269     replace-compound <or-class> ;
270
271 M: not-class answer
272     [ class>> ] 2dip answer <not-class> ;
273
274 GENERIC# substitute 1 ( class from to -- new-class )
275 M: object substitute answer ;
276 M: not-class substitute [ <not-class> ] bi@ answer ;
277
278 : assoc-answer ( table question answer -- new-table )
279     '[ _ _ substitute ] assoc-map
280     [ nip ] assoc-filter ;
281
282 : assoc-answers ( table questions answer -- new-table )
283     '[ _ assoc-answer ] each ;
284
285 DEFER: make-condition
286
287 : (make-condition) ( table questions question -- condition )
288     [ 2nip ]
289     [ swap [ t assoc-answer ] dip make-condition ]
290     [ swap [ f assoc-answer ] dip make-condition ] 3tri
291     2dup = [ 2nip ] [ <condition> ] if ;
292
293 : make-condition ( table questions -- condition )
294     [ keys ] [ unclip (make-condition) ] if-empty ;
295
296 GENERIC: class>questions ( class -- questions )
297 : compound-questions ( class -- questions ) seq>> [ class>questions ] gather ;
298 M: or-class class>questions compound-questions ;
299 M: and-class class>questions compound-questions ;
300 M: not-class class>questions class>> class>questions ;
301 M: object class>questions 1array ;
302
303 : table>questions ( table -- questions )
304     values [ class>questions ] gather >array t swap remove ;
305
306 : table>condition ( table -- condition )
307     ! input table is state => class
308     >alist dup table>questions make-condition ;
309
310 : condition-map ( condition quot: ( obj -- obj' ) -- new-condition ) 
311     over condition? [
312         [ [ question>> ] [ yes>> ] [ no>> ] tri ] dip
313         '[ _ condition-map ] bi@ <condition>
314     ] [ call ] if ; inline recursive
315
316 : condition-states ( condition -- states )
317     dup condition? [
318         [ yes>> ] [ no>> ] bi
319         [ condition-states ] bi@ append prune
320     ] [ 1array ] if ;
321
322 : condition-at ( condition assoc -- new-condition )
323     '[ _ at ] condition-map ;