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