]> gitweb.factorcode.org Git - factor.git/blob - basis/regexp/classes/classes.factor
Fixing bug in disambiguation in regexps
[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 ;
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 $ ;
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>> = [ f t ] [ f f ] if ;
134
135 M: integer combine-and
136     swap 2dup class-member? [ drop t ] [ 2drop f t ] if ;
137
138 GENERIC: combine-or ( class1 class2 -- combined ? )
139
140 M: object combine-or replace-if-= ;
141
142 M: t combine-or
143     drop f ;
144
145 M: f combine-or
146     drop t ;
147
148 M: not-class combine-or
149     class>> = [ t t ] [ f f ] if ;
150
151 M: integer combine-or
152     2dup swap class-member? [ drop t ] [ 2drop f f ] if ;
153
154 MACRO: instance? ( class -- ? )
155     "predicate" word-prop ;
156
157 : flatten ( seq class -- newseq )
158     '[ dup _ instance? [ seq>> ] [ 1array ] if ] map concat ; inline
159
160 : try-combine ( elt1 elt2 quot -- combined/f ? )
161     3dup call [ [ 3drop ] dip t ] [ drop swapd call ] if ; inline
162
163 :: prefix-combining ( seq elt quot: ( elt1 elt2 -- combined/f ? ) -- newseq )
164     f :> combined!
165     seq [ elt quot try-combine swap combined! ] find drop
166     [ seq remove-nth combined prefix ]
167     [ seq elt prefix ] if* ; inline
168
169 :: combine ( seq quot: ( elt1 elt2 -- combined/f ? ) empty class -- newseq )
170     seq class flatten
171     { } [ quot prefix-combining ] reduce
172     dup length {
173         { 0 [ drop empty ] }
174         { 1 [ first ] }
175         [ drop class new swap >>seq ]
176     } case ; inline
177
178 : <and-class> ( seq -- class )
179     [ combine-and ] t and-class combine ;
180
181 M: and-class class-member?
182     seq>> [ class-member? ] with all? ;
183
184 : <or-class> ( seq -- class )
185     [ combine-or ] f or-class combine ;
186
187 M: or-class class-member?
188     seq>> [ class-member? ] with any? ;
189
190 GENERIC: <not-class> ( class -- inverse )
191
192 M: object <not-class>
193     not-class boa ;
194
195 M: not-class <not-class>
196     class>> ;
197
198 M: and-class <not-class>
199     seq>> [ <not-class> ] map <or-class> ;
200
201 M: or-class <not-class>
202     seq>> [ <not-class> ] map <and-class> ;
203
204 M: not-class class-member?
205     class>> class-member? not ;
206
207 M: primitive-class class-member?
208     class>> class-member? ;
209
210 UNION: class primitive-class not-class or-class and-class range ;