Mercurial > ~dholland > hg > ag > index.cgi
comparison anagram/agcore/csexp.h @ 0:13d2b8934445
Import AnaGram (near-)release tree into Mercurial.
author | David A. Holland |
---|---|
date | Sat, 22 Dec 2007 17:52:45 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:13d2b8934445 |
---|---|
1 /* | |
2 * AnaGram, A System for Syntax Directed Programming | |
3 * Copyright 1993-2002 Parsifal Software. All Rights Reserved. | |
4 * See the file COPYING for license and usage terms. | |
5 * | |
6 * csexp.h | |
7 */ | |
8 | |
9 #ifndef CSEXP_H | |
10 #define CSEXP_H | |
11 | |
12 #include <stdlib.h> | |
13 #include "port.h" | |
14 | |
15 class AgString; // from agstring.h | |
16 #include "bitmap.h" | |
17 #include "minmax.h" | |
18 | |
19 //#define INCLUDE_LOGGING | |
20 //#include "log.h" | |
21 | |
22 | |
23 unsigned char agToUpper(unsigned char); | |
24 | |
25 class CharSetExpression; | |
26 class CharRange; | |
27 class CodeRange; | |
28 class IndividualChar; | |
29 class IndividualCode; | |
30 class NamedCharSet; | |
31 class CharSetUnion; | |
32 class CharSetIntersection; | |
33 class CharSetDifference; | |
34 class CharSetComplement; | |
35 | |
36 class CharSetExpression { | |
37 protected: | |
38 enum Precedence { | |
39 additive=1, | |
40 multiplicative, | |
41 unary, | |
42 atomic | |
43 }; | |
44 | |
45 Precedence precedence; | |
46 | |
47 class TranslateTable { | |
48 private: | |
49 int *table; | |
50 public: | |
51 TranslateTable() : table(new int[256]) { | |
52 for (int i = 0; i < 256; i++) { | |
53 table[i] = i; | |
54 } | |
55 } | |
56 ~TranslateTable() { | |
57 delete [] table; | |
58 } | |
59 int &operator [] (unsigned const x) { | |
60 return table[x]; | |
61 } | |
62 }; | |
63 | |
64 static TranslateTable translateTable; | |
65 | |
66 public: | |
67 enum Type { | |
68 notDefined = 1, | |
69 charRange, | |
70 codeRange, | |
71 individualChar, | |
72 individualCode, | |
73 namedCharSet, | |
74 charSetUnion, | |
75 charSetIntersection, | |
76 charSetDifference, | |
77 charSetComplement, | |
78 }; | |
79 | |
80 Type type; | |
81 | |
82 void *operator new (size_t n) { return malloc(n); } | |
83 void operator delete(void *p) { free(p); } | |
84 | |
85 static void checkMinimum(int f); | |
86 | |
87 CharSetExpression(Precedence p, Type t) : precedence(p), type(t) {} | |
88 CharSetExpression(Type t) : precedence(atomic), type(t) {} | |
89 CharSetExpression() : precedence(atomic), type(notDefined) {} | |
90 virtual ~CharSetExpression() {} | |
91 | |
92 virtual int operator == (const CharSetExpression &) const { | |
93 //LOGSECTION("CharSetExpression::operator==(CharSetExpression &)"); | |
94 return 0; | |
95 } | |
96 | |
97 virtual int operator != (const CharSetExpression &x) const { | |
98 return !(*this == x); | |
99 } | |
100 | |
101 //virtual int operator > (const CharSetExpression &x) const = 0; | |
102 | |
103 virtual int operator < (const CharSetExpression &x) const { | |
104 return type < x.type; | |
105 } | |
106 | |
107 virtual AgString asString() const = 0; | |
108 AgString asString(Precedence p) const; | |
109 | |
110 virtual int recursive(int) { return 0; } | |
111 virtual int nameNode() { return 0; } | |
112 virtual int char_node() { return 0; } | |
113 | |
114 virtual CharBitmap bitmap() const = 0; | |
115 virtual void checkRange() {} | |
116 virtual int sameType(const CharSetExpression &x) const { | |
117 return type == x.type; | |
118 } | |
119 }; | |
120 | |
121 class CharRange : public CharSetExpression { | |
122 private: | |
123 int first; | |
124 int last; | |
125 CharRange(const CharRange &) : CharSetExpression() {} | |
126 public: | |
127 CharRange(int f, int l) | |
128 : CharSetExpression(charRange), first(min(f, l)), last(max(f, l)) | |
129 { | |
130 //LOGSECTION("CharRange"); | |
131 //LOGV(first) LCV(last); | |
132 checkRange(); | |
133 } | |
134 ~CharRange() {} | |
135 | |
136 //virtual int operator == (const CharRange &x) const; | |
137 virtual int operator == (const CharSetExpression &x) const; | |
138 virtual int operator < (const CharSetExpression &x) const; | |
139 virtual AgString asString() const; | |
140 virtual CharBitmap bitmap() const; | |
141 virtual void checkRange(); | |
142 }; | |
143 | |
144 class CodeRange : public CharSetExpression { | |
145 private: | |
146 int first; | |
147 int last; | |
148 CodeRange(const CharRange &) : CharSetExpression() {} | |
149 public: | |
150 CodeRange(int f, int l) | |
151 : CharSetExpression(codeRange), first(min(f, l)), last(max(f, l)) | |
152 { | |
153 //LOGSECTION("CodeRange"); | |
154 //LOGV(first) LCV(last); | |
155 checkRange(); | |
156 checkMinimum(f); | |
157 } | |
158 ~CodeRange() {} | |
159 | |
160 //virtual int operator == (const CodeRange &x) const; | |
161 virtual int operator == (const CharSetExpression &x) const; | |
162 virtual int operator < (const CharSetExpression &x) const; | |
163 virtual AgString asString() const; | |
164 virtual CharBitmap bitmap() const; | |
165 virtual void checkRange(); | |
166 }; | |
167 | |
168 class IndividualChar : public CharSetExpression { | |
169 private: | |
170 IndividualChar(const IndividualChar &) : CharSetExpression() {} | |
171 public: | |
172 int asciiValue; | |
173 | |
174 IndividualChar(int c) : CharSetExpression(individualChar), asciiValue(c) { | |
175 //LOGSECTION("IndividualChar"); | |
176 //LOGV(asciiValue); | |
177 checkRange(); | |
178 } | |
179 ~IndividualChar() {} | |
180 | |
181 //virtual int operator == (const IndividualChar &x) const; | |
182 virtual int operator == (const CharSetExpression &x) const; | |
183 virtual int operator < (const CharSetExpression &x) const; | |
184 virtual AgString asString() const; | |
185 virtual CharBitmap bitmap() const; | |
186 virtual void checkRange(); | |
187 }; | |
188 | |
189 class IndividualCode : public CharSetExpression { | |
190 private: | |
191 IndividualCode(const IndividualCode &) : CharSetExpression() {} | |
192 public: | |
193 int value; | |
194 | |
195 IndividualCode(int c) : CharSetExpression(individualCode), value(c) { | |
196 //LOGSECTION("IndividualCode"); | |
197 //LOGV(value); | |
198 checkRange(); | |
199 checkMinimum(value); | |
200 } | |
201 ~IndividualCode() {} | |
202 | |
203 virtual int operator == (const CharSetExpression &x) const; | |
204 virtual int operator < (const CharSetExpression &x) const; | |
205 virtual AgString asString() const; | |
206 virtual int char_node() { return 1; } | |
207 virtual CharBitmap bitmap() const; | |
208 virtual void checkRange(); | |
209 }; | |
210 | |
211 class NamedCharSet : public CharSetExpression { | |
212 private: | |
213 NamedCharSet(const NamedCharSet &) : CharSetExpression() {} | |
214 public: | |
215 int name; | |
216 | |
217 NamedCharSet(int n) : CharSetExpression(namedCharSet), name(n) { | |
218 //LOGSECTION("NamedCharSet"); | |
219 //LOGV(dict_str(tkn_dict, name)); | |
220 } | |
221 ~NamedCharSet() {} | |
222 | |
223 virtual int operator == (const CharSetExpression &x) const; | |
224 virtual int operator < (const CharSetExpression &x) const; | |
225 virtual AgString asString() const; | |
226 virtual int recursive(int n) { return n == name; } | |
227 virtual int nameNode() { return 1; } | |
228 virtual CharBitmap bitmap() const; | |
229 }; | |
230 | |
231 class CharSetUnion : public CharSetExpression { | |
232 private: | |
233 CharSetExpression *left, *right; | |
234 CharSetUnion(const CharSetUnion &) : CharSetExpression() {} | |
235 public: | |
236 CharSetUnion(CharSetExpression *l, CharSetExpression *r) | |
237 : CharSetExpression(additive, charSetUnion), left(l), right(r) | |
238 { | |
239 //LOGSECTION("CharSetUnion"); | |
240 } | |
241 ~CharSetUnion() { delete left; delete right; } | |
242 | |
243 virtual int operator == (const CharSetExpression &x) const; | |
244 virtual int operator < (const CharSetExpression &x) const; | |
245 virtual AgString asString() const; | |
246 virtual int recursive(int n) { | |
247 return left->recursive(n) || right->recursive(n); | |
248 } | |
249 virtual CharBitmap bitmap() const; | |
250 virtual void checkRange(); | |
251 }; | |
252 | |
253 class CharSetIntersection : public CharSetExpression { | |
254 private: | |
255 CharSetExpression *left, *right; | |
256 CharSetIntersection(const CharSetIntersection &) : CharSetExpression() {} | |
257 public: | |
258 CharSetIntersection(CharSetExpression *l, CharSetExpression *r) | |
259 : CharSetExpression(multiplicative, charSetIntersection), left(l), right(r) | |
260 { | |
261 //LOGSECTION("CharSetIntersection"); | |
262 } | |
263 ~CharSetIntersection() {delete left; delete right;} | |
264 | |
265 virtual int operator == (const CharSetExpression &x) const; | |
266 virtual int operator < (const CharSetExpression &x) const; | |
267 virtual AgString asString() const; | |
268 virtual CharBitmap bitmap() const; | |
269 virtual int recursive(int n) { | |
270 return left->recursive(n) || right->recursive(n); | |
271 } | |
272 virtual void checkRange(); | |
273 }; | |
274 | |
275 class CharSetDifference : public CharSetExpression { | |
276 private: | |
277 CharSetExpression *left, *right; | |
278 CharSetDifference(const CharSetDifference &) : CharSetExpression() {} | |
279 public: | |
280 CharSetDifference(CharSetExpression *l, CharSetExpression *r) | |
281 : CharSetExpression(additive, charSetDifference), left(l), right(r) | |
282 { | |
283 //LOGSECTION("CharSetDifference"); | |
284 } | |
285 ~CharSetDifference() {delete left; delete right;} | |
286 | |
287 virtual int operator == (const CharSetExpression &x) const; | |
288 virtual int operator < (const CharSetExpression &x) const; | |
289 virtual AgString asString() const; | |
290 virtual CharBitmap bitmap() const; | |
291 virtual int recursive(int n) { | |
292 return left->recursive(n) || right->recursive(n); | |
293 } | |
294 virtual void checkRange(); | |
295 }; | |
296 | |
297 class CharSetComplement : public CharSetExpression { | |
298 private: | |
299 CharSetExpression *operand; | |
300 CharSetComplement(const CharSetComplement &) : CharSetExpression() {} | |
301 public: | |
302 CharSetComplement(CharSetExpression *o) | |
303 : CharSetExpression(unary, charSetComplement), operand(o) | |
304 { | |
305 //LOGSECTION("CharSetComplement"); | |
306 } | |
307 ~CharSetComplement() {delete operand;} | |
308 | |
309 virtual int operator == (const CharSetExpression &x) const; | |
310 virtual int operator < (const CharSetExpression &x) const; | |
311 virtual AgString asString() const; | |
312 virtual CharBitmap bitmap() const; | |
313 virtual int recursive(int n) { return operand->recursive(n); } | |
314 virtual void checkRange(); | |
315 }; | |
316 | |
317 | |
318 #endif /* CSEXP_H */ |