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 ;
8 SINGLETONS: dot letter-class LETTER-class Letter-class digit-class
9 alpha-class non-newline-blank-class
10 ascii-class punctuation-class java-printable-class blank-class
11 control-character-class hex-digit-class java-blank-class c-identifier-class
12 unmatchable-class terminator-class word-boundary-class ;
14 SINGLETONS: beginning-of-input ^ end-of-input $ end-of-file
15 ^unix $unix word-break ;
17 TUPLE: range from to ;
20 TUPLE: primitive-class class ;
21 C: <primitive-class> primitive-class
23 TUPLE: category-class category ;
24 C: <category-class> category-class
26 TUPLE: category-range-class category ;
27 C: <category-range-class> category-range-class
29 TUPLE: script-class script ;
30 C: <script-class> script-class
32 GENERIC: class-member? ( obj class -- ? )
34 M: t class-member? ( obj class -- ? ) 2drop t ;
36 M: integer class-member? ( obj class -- ? ) = ;
38 M: range class-member? ( obj class -- ? )
39 [ from>> ] [ to>> ] bi between? ;
41 M: letter-class class-member? ( obj class -- ? )
44 M: LETTER-class class-member? ( obj class -- ? )
47 M: Letter-class class-member? ( obj class -- ? )
50 M: ascii-class class-member? ( obj class -- ? )
53 M: digit-class class-member? ( obj class -- ? )
56 : c-identifier-char? ( ch -- ? )
57 { [ alpha? ] [ CHAR: _ = ] } 1|| ;
59 M: c-identifier-class class-member? ( obj class -- ? )
60 drop c-identifier-char? ;
62 M: alpha-class class-member? ( obj class -- ? )
66 "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" member? ;
68 M: punctuation-class class-member? ( obj class -- ? )
71 : java-printable? ( ch -- ? )
72 { [ alpha? ] [ punct? ] } 1|| ;
74 M: java-printable-class class-member? ( obj class -- ? )
75 drop java-printable? ;
77 M: non-newline-blank-class class-member? ( obj class -- ? )
78 drop { [ blank? ] [ CHAR: \n = not ] } 1&& ;
80 M: control-character-class class-member? ( obj class -- ? )
83 : hex-digit? ( ch -- ? )
85 [ CHAR: A CHAR: F between? ]
86 [ CHAR: a CHAR: f between? ]
87 [ CHAR: 0 CHAR: 9 between? ]
90 M: hex-digit-class class-member? ( obj class -- ? )
93 : java-blank? ( ch -- ? )
95 CHAR: \s CHAR: \t CHAR: \n
96 HEX: b HEX: 7 CHAR: \r
99 M: java-blank-class class-member? ( obj class -- ? )
102 M: unmatchable-class class-member? ( obj class -- ? )
105 M: terminator-class class-member? ( obj class -- ? )
106 drop "\r\n\u000085\u002029\u002028" member? ;
108 M: f class-member? 2drop f ;
110 M: script-class class-member?
111 [ script-of ] [ script>> ] bi* = ;
113 M: category-class class-member?
114 [ category# ] [ category>> ] bi* = ;
116 M: category-range-class class-member?
117 [ category first ] [ category>> ] bi* = ;
119 TUPLE: not-class class ;
121 PREDICATE: not-integer < not-class class>> integer? ;
122 PREDICATE: not-primitive < not-class class>> primitive-class? ;
124 M: not-class class-member?
125 class>> class-member? not ;
127 TUPLE: or-class seq ;
129 M: or-class class-member?
130 seq>> [ class-member? ] with any? ;
132 TUPLE: and-class seq ;
134 M: and-class class-member?
135 seq>> [ class-member? ] with all? ;
139 : flatten ( seq class -- newseq )
140 '[ dup _ instance? [ seq>> ] [ 1array ] if ] map concat ; inline
142 :: seq>instance ( seq empty class -- instance )
146 [ drop class new seq { } like >>seq ]
149 TUPLE: class-partition integers not-integers primitives not-primitives and or other ;
151 : partition-classes ( seq -- class-partition )
153 [ integer? ] partition
154 [ not-integer? ] partition
155 [ primitive-class? ] partition ! extend primitive-class to epsilon tags
156 [ not-primitive? ] partition
157 [ and-class? ] partition
158 [ or-class? ] partition
159 class-partition boa ;
161 : class-partition>seq ( class-partition -- seq )
162 make-mirror values concat ;
164 : repartition ( partition -- partition' )
165 ! This could be made more efficient; only and and or are effected
166 class-partition>seq partition-classes ;
168 : filter-not-integers ( partition -- partition' )
170 [ primitives>> ] [ not-primitives>> ] [ or>> ] tri
171 3append and-class boa
172 '[ [ class>> _ class-member? ] filter ] change-not-integers ;
174 : answer-ors ( partition -- partition' )
175 dup [ not-integers>> ] [ not-primitives>> ] [ primitives>> ] tri 3append
176 '[ [ _ [ t substitute ] each ] map ] change-or ;
178 : contradiction? ( partition -- ? )
180 [ [ primitives>> ] [ not-primitives>> ] bi intersects? ]
181 [ other>> f swap member? ]
184 : make-and-class ( partition -- and-class )
185 answer-ors repartition
186 [ t swap remove ] change-other
189 [ filter-not-integers class-partition>seq prune t and-class seq>instance ] if ;
191 : <and-class> ( seq -- class )
192 dup and-class flatten partition-classes
193 dup integers>> length {
194 { 0 [ nip make-and-class ] }
195 { 1 [ integers>> first [ '[ _ swap class-member? ] all? ] keep and ] }
199 : filter-integers ( partition -- partition' )
201 [ primitives>> ] [ not-primitives>> ] [ and>> ] tri
203 '[ [ _ class-member? not ] filter ] change-integers ;
205 : answer-ands ( partition -- partition' )
206 dup [ integers>> ] [ not-primitives>> ] [ primitives>> ] tri 3append
207 '[ [ _ [ f substitute ] each ] map ] change-and ;
209 : tautology? ( partition -- ? )
211 [ [ primitives>> ] [ not-primitives>> ] bi intersects? ]
212 [ other>> t swap member? ]
215 : make-or-class ( partition -- and-class )
216 answer-ands repartition
217 [ f swap remove ] change-other
220 [ filter-integers class-partition>seq prune f or-class seq>instance ] if ;
222 : <or-class> ( seq -- class )
223 dup or-class flatten partition-classes
224 dup not-integers>> length {
225 { 0 [ nip make-or-class ] }
226 { 1 [ not-integers>> first [ class>> '[ _ swap class-member? ] any? ] keep or ] }
230 GENERIC: <not-class> ( class -- inverse )
232 M: object <not-class>
235 M: not-class <not-class>
238 M: and-class <not-class>
239 seq>> [ <not-class> ] map <or-class> ;
241 M: or-class <not-class>
242 seq>> [ <not-class> ] map <and-class> ;
244 M: t <not-class> drop f ;
245 M: f <not-class> drop t ;
247 M: primitive-class class-member?
248 class>> class-member? ;
250 UNION: class primitive-class not-class or-class and-class range ;
252 TUPLE: condition question yes no ;
253 C: <condition> condition
255 GENERIC# answer 2 ( class from to -- new-class )
257 M:: object answer ( class from to -- new-class )
258 class from = to class ? ;
260 : replace-compound ( class from to -- seq )
261 [ seq>> ] 2dip '[ _ _ answer ] map ;
264 replace-compound <and-class> ;
267 replace-compound <or-class> ;
270 [ class>> ] 2dip answer <not-class> ;
272 GENERIC# substitute 1 ( class from to -- new-class )
273 M: object substitute answer ;
274 M: not-class substitute [ <not-class> ] bi@ answer ;
276 : assoc-answer ( table question answer -- new-table )
277 '[ _ _ substitute ] assoc-map
278 [ nip ] assoc-filter ;
280 : assoc-answers ( table questions answer -- new-table )
281 '[ _ assoc-answer ] each ;
283 DEFER: make-condition
285 : (make-condition) ( table questions question -- condition )
287 [ swap [ t assoc-answer ] dip make-condition ]
288 [ swap [ f assoc-answer ] dip make-condition ] 3tri
289 2dup = [ 2nip ] [ <condition> ] if ;
291 : make-condition ( table questions -- condition )
292 [ keys ] [ unclip (make-condition) ] if-empty ;
294 GENERIC: class>questions ( class -- questions )
295 : compound-questions ( class -- questions ) seq>> [ class>questions ] gather ;
296 M: or-class class>questions compound-questions ;
297 M: and-class class>questions compound-questions ;
298 M: not-class class>questions class>> class>questions ;
299 M: object class>questions 1array ;
301 : table>questions ( table -- questions )
302 values [ class>questions ] gather >array t swap remove ;
304 : table>condition ( table -- condition )
305 ! input table is state => class
306 >alist dup table>questions make-condition ;
308 : condition-map ( condition quot: ( obj -- obj' ) -- new-condition )
310 [ [ question>> ] [ yes>> ] [ no>> ] tri ] dip
311 '[ _ condition-map ] bi@ <condition>
312 ] [ call ] if ; inline recursive
314 : condition-states ( condition -- states )
316 [ yes>> ] [ no>> ] bi
317 [ condition-states ] bi@ append prune
320 : condition-at ( condition assoc -- new-condition )
321 '[ _ at ] condition-map ;