]> gitweb.factorcode.org Git - factor.git/blob - basis/regexp/classes/classes.factor
Redoing class algebra so conjunction works
[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 ;
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 word-break ;
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: not-class class ;
114
115 PREDICATE: not-integer < not-class class>> integer? ;
116 PREDICATE: not-primitive < not-class class>> primitive-class? ;
117
118 M: not-class class-member?
119     class>> class-member? not ;
120
121 TUPLE: or-class seq ;
122
123 M: or-class class-member?
124     seq>> [ class-member? ] with any? ;
125
126 TUPLE: and-class seq ;
127
128 M: and-class class-member?
129     seq>> [ class-member? ] with all? ;
130
131 DEFER: substitute
132
133 : flatten ( seq class -- newseq )
134     '[ dup _ instance? [ seq>> ] [ 1array ] if ] map concat ; inline
135
136 :: seq>instance ( seq empty class -- instance )
137     seq length {
138         { 0 [ empty ] }
139         { 1 [ seq first ] }
140         [ drop class new seq { } like >>seq ]
141     } case ; inline
142
143 TUPLE: class-partition integers not-integers primitives not-primitives and or other ;
144
145 : partition-classes ( seq -- class-partition )
146     prune
147     [ integer? ] partition
148     [ not-integer? ] partition
149     [ primitive-class? ] partition ! extend primitive-class to epsilon tags
150     [ not-primitive? ] partition
151     [ and-class? ] partition
152     [ or-class? ] partition
153     class-partition boa ;
154
155 : class-partition>seq ( class-partition -- seq )
156     make-mirror values concat ;
157
158 : repartition ( partition -- partition' )
159     ! This could be made more efficient; only and and or are effected
160     class-partition>seq partition-classes ;
161
162 : filter-not-integers ( partition -- partition' )
163     dup
164     [ primitives>> ] [ not-primitives>> ] [ or>> ] tri
165     3append and-class boa
166     '[ [ class>> _ class-member? ] filter ] change-not-integers ;
167
168 : answer-ors ( partition -- partition' )
169     dup [ not-integers>> ] [ not-primitives>> ] [ primitives>> ] tri 3append
170     '[ [ _ [ t substitute ] each ] map ] change-or ;
171
172 : contradiction? ( partition -- ? )
173     {
174         [ [ primitives>> ] [ not-primitives>> ] bi intersects? ]
175         [ other>> f swap member? ]
176     } 1|| ;
177
178 : make-and-class ( partition -- and-class )
179     answer-ors repartition
180     [ t swap remove ] change-other
181     dup contradiction?
182     [ drop f ]
183     [ filter-not-integers class-partition>seq prune t and-class seq>instance ] if ;
184
185 : <and-class> ( seq -- class )
186     dup and-class flatten partition-classes
187     dup integers>> length {
188         { 0 [ nip make-and-class ] }
189         { 1 [ integers>> first [ '[ _ swap class-member? ] all? ] keep and ] }
190         [ 3drop f ]
191     } case ;
192
193 : filter-integers ( partition -- partition' )
194     dup
195     [ primitives>> ] [ not-primitives>> ] [ and>> ] tri
196     3append or-class boa
197     '[ [ _ class-member? not ] filter ] change-integers ;
198
199 : answer-ands ( partition -- partition' )
200     dup [ integers>> ] [ not-primitives>> ] [ primitives>> ] tri 3append
201     '[ [ _ [ f substitute ] each ] map ] change-and ;
202
203 : tautology? ( partition -- ? )
204     {
205         [ [ primitives>> ] [ not-primitives>> ] bi intersects? ]
206         [ other>> t swap member? ]
207     } 1|| ;
208
209 : make-or-class ( partition -- and-class )
210     answer-ands repartition
211     [ f swap remove ] change-other
212     dup tautology?
213     [ drop t ]
214     [ filter-integers class-partition>seq prune f or-class seq>instance ] if ;
215
216 : <or-class> ( seq -- class )
217     dup or-class flatten partition-classes
218     dup not-integers>> length {
219         { 0 [ nip make-or-class ] }
220         { 1 [ not-integers>> first [ class>> '[ _ swap class-member? ] any? ] keep or ] }
221         [ 3drop t ]
222     } case ;
223
224 GENERIC: <not-class> ( class -- inverse )
225
226 M: object <not-class>
227     not-class boa ;
228
229 M: not-class <not-class>
230     class>> ;
231
232 M: and-class <not-class>
233     seq>> [ <not-class> ] map <or-class> ;
234
235 M: or-class <not-class>
236     seq>> [ <not-class> ] map <and-class> ;
237
238 M: t <not-class> drop f ;
239 M: f <not-class> drop t ;
240
241 M: primitive-class class-member?
242     class>> class-member? ;
243
244 UNION: class primitive-class not-class or-class and-class range ;
245
246 TUPLE: condition question yes no ;
247 C: <condition> condition
248
249 GENERIC# answer 2 ( class from to -- new-class )
250
251 M:: object answer ( class from to -- new-class )
252     class from = to class ? ;
253
254 : replace-compound ( class from to -- seq )
255     [ seq>> ] 2dip '[ _ _ answer ] map ;
256
257 M: and-class answer
258     replace-compound <and-class> ;
259
260 M: or-class answer
261     replace-compound <or-class> ;
262
263 M: not-class answer
264     [ class>> ] 2dip answer <not-class> ;
265
266 GENERIC# substitute 1 ( class from to -- new-class )
267 M: object substitute answer ;
268 M: not-class substitute [ <not-class> ] bi@ answer ;
269
270 : assoc-answer ( table question answer -- new-table )
271     '[ _ _ substitute ] assoc-map
272     [ nip ] assoc-filter ;
273
274 : assoc-answers ( table questions answer -- new-table )
275     '[ _ assoc-answer ] each ;
276
277 DEFER: make-condition
278
279 : (make-condition) ( table questions question -- condition )
280     [ 2nip ]
281     [ swap [ t assoc-answer ] dip make-condition ]
282     [ swap [ f assoc-answer ] dip make-condition ] 3tri
283     2dup = [ 2nip ] [ <condition> ] if ;
284
285 : make-condition ( table questions -- condition )
286     [ keys ] [ unclip (make-condition) ] if-empty ;
287
288 GENERIC: class>questions ( class -- questions )
289 : compound-questions ( class -- questions ) seq>> [ class>questions ] gather ;
290 M: or-class class>questions compound-questions ;
291 M: and-class class>questions compound-questions ;
292 M: not-class class>questions class>> class>questions ;
293 M: object class>questions 1array ;
294
295 : table>questions ( table -- questions )
296     values [ class>questions ] gather >array t swap remove ;
297
298 : table>condition ( table -- condition )
299     ! input table is state => class
300     >alist dup table>questions make-condition ;
301
302 : condition-map ( condition quot: ( obj -- obj' ) -- new-condition ) 
303     over condition? [
304         [ [ question>> ] [ yes>> ] [ no>> ] tri ] dip
305         '[ _ condition-map ] bi@ <condition>
306     ] [ call ] if ; inline recursive
307
308 : condition-states ( condition -- states )
309     dup condition? [
310         [ yes>> ] [ no>> ] bi
311         [ condition-states ] bi@ append prune
312     ] [ 1array ] if ;
313
314 : condition-at ( condition assoc -- new-condition )
315     '[ _ at ] condition-map ;