BAM
Abstract Machine for Bottom-Up Evaluation with the Push Method
ch.h
Go to the documentation of this file.
1 // ============================================================================
2 // Project: Deductive Database
3 // Filename: ch.h
4 // Purpose: Character Classes
5 // Last Change: 04.08.2017
6 // Language: C++
7 // EMail: brass@informatik.uni-halle.de
8 // WWW: http://www.informatik.uni-halle.de/~brass/
9 // Address: Feldschloesschen 15, D-06120 Halle (Saale), GERMANY
10 // Copyright: (c) 1998-2017 by Stefan Brass
11 // License: See file "LICENSE" for copying conditions.
12 // Note: There is no warranty at all - this code may contain bugs.
13 // ============================================================================
14 
15 
22 //=============================================================================
23 // Include File Frame:
24 //=============================================================================
25 
26 #ifndef CH_INCLUDED
27 #define CH_INCLUDED
28 
29 //=============================================================================
30 // Used Types and Macros:
31 //=============================================================================
32 
33 #ifndef CHECK_INCLUDED
34 #include "../base/check.h"
35 #endif
36 
37 //=============================================================================
38 // Private Constants:
39 //=============================================================================
40 
41 //-----------------------------------------------------------------------------
42 // CH_TABSIZE: Size of the character table, i.e. number of char values:
43 //-----------------------------------------------------------------------------
44 
45 static const int CH_TABSIZE = 256;
46 
47 //=============================================================================
48 // Exported Types:
49 //=============================================================================
50 
51 //-----------------------------------------------------------------------------
52 // ch_t: Character classes.
53 //-----------------------------------------------------------------------------
54 
55 typedef enum ch_enum {
56  CH_NULL,
57  CH_EOF,
58  CH_ATOM,
59  CH_VAR,
60  CH_MINUS,
61  CH_DIGIT,
62  CH_QUOTE,
63  CH_FULLSTOP,
64  CH_COMMA,
65  CH_LPAREN,
66  CH_RPAREN,
67  CH_LESS,
68  CH_GREATER,
69  CH_COLON,
70  CH_NOT,
71  CH_OR,
72  CH_SEMICOLON,
73  CH_AND,
74  CH_QUEST,
75  CH_UNUSED,
76  CH_UNDEFINED
77 } ch_t;
78 
79 //=============================================================================
80 // Auxillary Macros:
81 //=============================================================================
82 
83 //-----------------------------------------------------------------------------
84 // CH_CHARCAST: How to get from characters to indices.
85 //-----------------------------------------------------------------------------
86 
87 #define CH_CHARCAST(c) ((int) ((unsigned char)(c)))
88 
89 //=============================================================================
90 // Class for Classifying Characters:
91 //=============================================================================
92 
93 class ch_c {
94  public:
95 
96 //-----------------------------------------------------------------------------
97 // init: Initialization of classification tables:
98 //-----------------------------------------------------------------------------
99 
100  static void init();
101 
102 //-----------------------------------------------------------------------------
103 // classify: Return the character class of a character.
104 //-----------------------------------------------------------------------------
105 
106  static inline ch_t classify(char c)
107  { CHECK(Initialized,
108  "ch_c::classify(not initialized)");
109  CHECK(CH_CHARCAST(c)<Tabsize,
110  "ch_c::classify(index out of range)");
111  return(Classify[CH_CHARCAST(c)]); }
112 
113 //-----------------------------------------------------------------------------
114 // is_alnum: Is this an alphanumeric character?
115 //-----------------------------------------------------------------------------
116 
117  static inline bool is_alnum(char c)
118  { CHECK(Initialized,
119  "ch_c::is_alnum(not initialized)");
120  CHECK(CH_CHARCAST(c)<Tabsize,
121  "ch_c::is_alnum(index out of range)");
122  return(IsAlnum[CH_CHARCAST(c)]); }
123 
124 //-----------------------------------------------------------------------------
125 // is_space: Is this a space character (excluding newline)?
126 //-----------------------------------------------------------------------------
127 
128  static inline bool is_space(char c)
129  { return(c==' ' || c=='\t' || c=='\r'); }
130 
131 //-----------------------------------------------------------------------------
132 // is_comment: Is this the comment character?
133 //-----------------------------------------------------------------------------
134 
135  static inline bool is_comment(char c)
136  { return(c=='%'); }
137 
138 //-----------------------------------------------------------------------------
139 // is_newline: Is this the newline character?
140 //-----------------------------------------------------------------------------
141 
142  static inline bool is_newline(char c)
143  { return(c=='\n'); }
144 
145 //-----------------------------------------------------------------------------
146 // is_quote: Is this the quote character?
147 //-----------------------------------------------------------------------------
148 
149  static inline bool is_quote(char c)
150  { return(c=='\''); }
151 
152 //-----------------------------------------------------------------------------
153 // is_backslash: Is this the backslash character?
154 //-----------------------------------------------------------------------------
155 
156  static inline bool is_backslash(char c)
157  { return(c=='\\'); }
158 
159 //-----------------------------------------------------------------------------
160 // is_escape_seq: Is this a valid character after the backslash character?
161 //-----------------------------------------------------------------------------
162 
163  static inline bool is_escape_seq(char c)
164  { return(c=='\\' || c=='\'' || c=='\"' || c=='n' || c=='r' ||
165  c=='t'); }
166 
167 //-----------------------------------------------------------------------------
168 // is_undersc: Is this the underscore character?
169 //-----------------------------------------------------------------------------
170 
171  static inline bool is_undersc(char c)
172  { return(c=='_'); }
173 
174 //-----------------------------------------------------------------------------
175 // digit_val: Return int-value of digit.
176 //-----------------------------------------------------------------------------
177 
178  static inline int digit_val(char c) {
179  switch(c) {
180  case '0': return 0;
181  case '1': return 1;
182  case '2': return 2;
183  case '3': return 3;
184  case '4': return 4;
185  case '5': return 5;
186  case '6': return 6;
187  case '7': return 7;
188  case '8': return 8;
189  case '9': return 9;
190  default: return -1;
191  }
192  }
193 
194 //-----------------------------------------------------------------------------
195 // ord: Return position in "alphabet" (for sorting purposes).
196 //-----------------------------------------------------------------------------
197 
198  static inline int ord(char c)
199  { CHECK(Initialized,
200  "ch_c::ord(not initialized)");
201  CHECK(CH_CHARCAST(c)<Tabsize,
202  "ch_c::ord(index out of range)");
203  return(Ord[CH_CHARCAST(c)]); }
204 
205 //-----------------------------------------------------------------------------
206 // xord: Position of second character equivalent to national character.
207 //-----------------------------------------------------------------------------
208 
209  static inline int xord(char c)
210  { CHECK(Initialized,
211  "ch_c::xord(not initialized)");
212  CHECK(CH_CHARCAST(c)<Tabsize,
213  "ch_c::xord(index out of range)");
214  return(Xord[CH_CHARCAST(c)]); }
215 
216 //=============================================================================
217 // This class has no objects, only static methods.
218 //=============================================================================
219 
220 //-----------------------------------------------------------------------------
221 // Constructor, Destructor:
222 //-----------------------------------------------------------------------------
223 
224  private:
225 
226  // Constructor:
227  ch_c(); // Not implemented
228 
229  // Destructor:
230  ~ch_c(); // Not implemented
231 
232 
233 //-----------------------------------------------------------------------------
234 // Copy-constructor and assignment operator are not supported for this class:
235 //-----------------------------------------------------------------------------
236 
237  private:
238 
239  ch_c(const ch_c& obj); // Not implemented
240  ch_c& operator=(const ch_c& obj); // Not implemented
241 
242 //=============================================================================
243 // Private Auxillary Functions:
244 //=============================================================================
245 
246  private:
247 
248 //-----------------------------------------------------------------------------
249 // enter: Enter character-class translation into table.
250 //-----------------------------------------------------------------------------
251 
252  static inline void enter(char c, ch_t cl);
253 
254 //=============================================================================
255 // Private Class Members:
256 //=============================================================================
257 
258 //-----------------------------------------------------------------------------
259 // Initialized: This module was initialized.
260 //-----------------------------------------------------------------------------
261 
262  static bool Initialized;
263 
264 //-----------------------------------------------------------------------------
265 // Classify: Character class of each character.
266 //-----------------------------------------------------------------------------
267 
268  static ch_t Classify[CH_TABSIZE];
269 
270 //-----------------------------------------------------------------------------
271 // IsAlnum: Which characters are alphanumeric?
272 //-----------------------------------------------------------------------------
273 
274  static bool IsAlnum[CH_TABSIZE];
275 
276 //-----------------------------------------------------------------------------
277 // Ord: Position in "alphabet" (for sorting).
278 //-----------------------------------------------------------------------------
279 
280  static int Ord[CH_TABSIZE];
281 
282 //-----------------------------------------------------------------------------
283 // Xord: Position of second character equivalent to national character.
284 //-----------------------------------------------------------------------------
285 
286  static int Xord[CH_TABSIZE];
287 
288 //-----------------------------------------------------------------------------
289 // Xupper: Extended list of uppercase characters (including national ones).
290 //-----------------------------------------------------------------------------
291 
292  static str_t Xupper;
293 
294 //-----------------------------------------------------------------------------
295 // Xlower: Extended list of lowercase characters (including national ones).
296 //-----------------------------------------------------------------------------
297 
298  static str_t Xlower;
299 
300 //-----------------------------------------------------------------------------
301 // SzLig: The german "ss" character (special for alphabetic orders).
302 //-----------------------------------------------------------------------------
303 
304  static char SzLig;
305 
306 //-----------------------------------------------------------------------------
307 // AeUpper: The nordic "Ae" character (special for alphabetic orders).
308 //-----------------------------------------------------------------------------
309 
310  static char AeUpper;
311 
312 //-----------------------------------------------------------------------------
313 // AeLower: The nordic "ae" character (special for alphabetic orders).
314 //-----------------------------------------------------------------------------
315 
316  static char AeLower;
317 
318 //-----------------------------------------------------------------------------
319 // Tabsize: To avoid warning "comparison is always true" in CECKs.
320 //-----------------------------------------------------------------------------
321 
322 #if VER_DEBUG
323  static int Tabsize; // Will be CH_TABSIZE
324 #endif
325 
326 //=============================================================================
327 // End of Class:
328 //=============================================================================
329 
330 };
331 
332 //=============================================================================
333 // End of File:
334 //=============================================================================
335 
336 #endif
337 
Definition: ch.h:93
const char * str_t
Definition: str.h:41
#define CHECK(EX, MSG)
Definition: check.h:69