1 | /*
|
---|
2 | =============================================================================
|
---|
3 | cminit.c -- C-Meta parser functions
|
---|
4 | Version 5 -- 1987-07-10 -- D.N. Lynx Crowe
|
---|
5 |
|
---|
6 | Some parser functions in C, modelled after HyperMeta(tm).
|
---|
7 | Designed for, but not dedicated to, LR(k), top-down, recursive parsing.
|
---|
8 | =============================================================================
|
---|
9 | */
|
---|
10 |
|
---|
11 | #include <stddefs.h>
|
---|
12 | #include <ctype.h>
|
---|
13 |
|
---|
14 | #define CM_MXSTR 257 /* maximum parser string result length */
|
---|
15 |
|
---|
16 | #define CM_DBLK if (!QQanch) while (*QQip EQ ' ') ++QQip
|
---|
17 |
|
---|
18 | int16_t QQsw; /* parser result switch */
|
---|
19 | int16_t QQanch; /* parser anchored match switch */
|
---|
20 |
|
---|
21 | int8_t *QQin; /* parser initial input pointer */
|
---|
22 | int8_t *QQip; /* parser current input pointer */
|
---|
23 | int8_t *QQop; /* parser string output pointer */
|
---|
24 |
|
---|
25 | int32_t QQnum; /* parser numeric result */
|
---|
26 | int16_t QQlnum; /* parser list element number result */
|
---|
27 | int8_t QQdig; /* parser digit result */
|
---|
28 | int8_t QQchr; /* parser character result */
|
---|
29 |
|
---|
30 | int8_t QQstr[CM_MXSTR]; /* parser string result */
|
---|
31 |
|
---|
32 | /* |
---|
33 |
|
---|
34 | */
|
---|
35 |
|
---|
36 | /*
|
---|
37 | =============================================================================
|
---|
38 | CMinit(ip) -- initialize the parser to work on the string at 'ip'.
|
---|
39 | =============================================================================
|
---|
40 | */
|
---|
41 |
|
---|
42 | void CMinit(int8_t *ip)
|
---|
43 | {
|
---|
44 | register int16_t i;
|
---|
45 | register int8_t *t;
|
---|
46 |
|
---|
47 | QQip = ip;
|
---|
48 | QQin = ip;
|
---|
49 | QQsw = TRUE;
|
---|
50 | QQanch = FALSE;
|
---|
51 | QQdig = '?';
|
---|
52 | QQchr = '?';
|
---|
53 | QQnum = 0;
|
---|
54 | QQop = QQstr;
|
---|
55 | t = QQstr;
|
---|
56 |
|
---|
57 | for (i = 0; i < CM_MXSTR; i++)
|
---|
58 | *t++ = '\0';
|
---|
59 | }
|
---|
60 |
|
---|
61 | /* |
---|
62 |
|
---|
63 | */
|
---|
64 |
|
---|
65 | /*
|
---|
66 | =============================================================================
|
---|
67 | CMchr(c) -- attempt to match character 'c' in the input.
|
---|
68 | =============================================================================
|
---|
69 | */
|
---|
70 |
|
---|
71 | int16_t CMchr(int8_t c)
|
---|
72 | {
|
---|
73 | CM_DBLK;
|
---|
74 |
|
---|
75 | if (c NE *QQip)
|
---|
76 | return(QQsw = FALSE);
|
---|
77 |
|
---|
78 | QQchr = *QQip++;
|
---|
79 | return(QQsw = TRUE);
|
---|
80 | }
|
---|
81 |
|
---|
82 | /* |
---|
83 |
|
---|
84 | */
|
---|
85 |
|
---|
86 | /*
|
---|
87 | =============================================================================
|
---|
88 | CMuchr(c) -- attempt to match character 'c' in the input, ignoring case.
|
---|
89 | =============================================================================
|
---|
90 | */
|
---|
91 |
|
---|
92 | int16_t CMuchr(int8_t c)
|
---|
93 | {
|
---|
94 | register int8_t t;
|
---|
95 |
|
---|
96 | CM_DBLK;
|
---|
97 |
|
---|
98 | t = *QQip;
|
---|
99 |
|
---|
100 | if (isascii(c))
|
---|
101 | if (isupper(c))
|
---|
102 | c = _tolower(c);
|
---|
103 |
|
---|
104 | if (isascii(t))
|
---|
105 | if (isupper(t))
|
---|
106 | t = _tolower(t);
|
---|
107 |
|
---|
108 | if (c NE t)
|
---|
109 | return(QQsw = FALSE);
|
---|
110 |
|
---|
111 | QQchr = *QQip++;
|
---|
112 | return(QQsw = TRUE);
|
---|
113 | }
|
---|
114 |
|
---|
115 | /* |
---|
116 |
|
---|
117 | */
|
---|
118 |
|
---|
119 | /*
|
---|
120 | =============================================================================
|
---|
121 | CMstr(s) -- attempt to match string at 's' in the input.
|
---|
122 | =============================================================================
|
---|
123 | */
|
---|
124 |
|
---|
125 | int16_t CMstr(int8_t *s)
|
---|
126 | {
|
---|
127 | register int8_t *t;
|
---|
128 | int8_t *q;
|
---|
129 |
|
---|
130 | CM_DBLK;
|
---|
131 |
|
---|
132 | t = QQip;
|
---|
133 | q = s;
|
---|
134 |
|
---|
135 | while (*s) {
|
---|
136 |
|
---|
137 | if (*t++ NE *s++)
|
---|
138 | return(QQsw = FALSE);
|
---|
139 | }
|
---|
140 |
|
---|
141 | QQop = QQstr;
|
---|
142 |
|
---|
143 | while (*QQop++ = *q++) ;
|
---|
144 |
|
---|
145 | QQip = t;
|
---|
146 | return(QQsw = TRUE);
|
---|
147 | }
|
---|
148 |
|
---|
149 | /* |
---|
150 |
|
---|
151 | */
|
---|
152 |
|
---|
153 | /*
|
---|
154 | =============================================================================
|
---|
155 | CMustr(s) -- attempt to match string 's' in the input, ignoring case.
|
---|
156 | =============================================================================
|
---|
157 | */
|
---|
158 |
|
---|
159 | int16_t CMustr(int8_t *s)
|
---|
160 | {
|
---|
161 | register int8_t *t, t1, t2;
|
---|
162 | int8_t *q;
|
---|
163 |
|
---|
164 | CM_DBLK;
|
---|
165 |
|
---|
166 | t = QQip;
|
---|
167 | q = s;
|
---|
168 |
|
---|
169 | while (*s) {
|
---|
170 |
|
---|
171 | t1 = *t++;
|
---|
172 | t2 = *s++;
|
---|
173 |
|
---|
174 | if (isascii(t1))
|
---|
175 | if (isupper(t1))
|
---|
176 | t1 = _tolower(t1);
|
---|
177 |
|
---|
178 | if (isascii(t2))
|
---|
179 | if (isupper(t2))
|
---|
180 | t2 = _tolower(t2);
|
---|
181 |
|
---|
182 | if (t1 NE t2)
|
---|
183 | return(QQsw = FALSE);
|
---|
184 | }
|
---|
185 |
|
---|
186 | QQop = QQstr;
|
---|
187 |
|
---|
188 | while (*QQop++ = *q++) ;
|
---|
189 |
|
---|
190 | QQip = t;
|
---|
191 | return(QQsw = TRUE);
|
---|
192 | }
|
---|
193 |
|
---|
194 | /* |
---|
195 |
|
---|
196 | */
|
---|
197 |
|
---|
198 | /*
|
---|
199 | =============================================================================
|
---|
200 | CMlong() -- attempt to parse a digit string in the input as a long.
|
---|
201 | =============================================================================
|
---|
202 | */
|
---|
203 |
|
---|
204 | int16_t CMlong(void)
|
---|
205 | {
|
---|
206 | register int8_t *p;
|
---|
207 | register int32_t n;
|
---|
208 | register int8_t c;
|
---|
209 |
|
---|
210 | CM_DBLK;
|
---|
211 |
|
---|
212 | p = QQip;
|
---|
213 | n = 0L;
|
---|
214 | c = *p++;
|
---|
215 |
|
---|
216 | if (!isascii(c))
|
---|
217 | return(QQsw = FALSE);
|
---|
218 |
|
---|
219 | if (!isdigit(c))
|
---|
220 | return(QQsw = FALSE);
|
---|
221 |
|
---|
222 | n = c - '0';
|
---|
223 |
|
---|
224 | while (c = *p) {
|
---|
225 |
|
---|
226 | if (!isascii(c))
|
---|
227 | break;
|
---|
228 |
|
---|
229 | if (!isdigit(c))
|
---|
230 | break;
|
---|
231 |
|
---|
232 | n = (n * 10) + (c - '0');
|
---|
233 | ++p;
|
---|
234 | }
|
---|
235 |
|
---|
236 | QQip = p;
|
---|
237 | QQnum = n;
|
---|
238 | return(QQsw = TRUE);
|
---|
239 | }
|
---|
240 |
|
---|
241 | /* |
---|
242 |
|
---|
243 | */
|
---|
244 |
|
---|
245 | /*
|
---|
246 | =============================================================================
|
---|
247 | CMdig() -- attempt to match a digit in the input string.
|
---|
248 | =============================================================================
|
---|
249 | */
|
---|
250 |
|
---|
251 | int16_t CMdig(void)
|
---|
252 | {
|
---|
253 | register int8_t c;
|
---|
254 |
|
---|
255 | CM_DBLK;
|
---|
256 |
|
---|
257 | c = *QQip;
|
---|
258 |
|
---|
259 | if (!isascii(c))
|
---|
260 | return(QQsw = FALSE);
|
---|
261 |
|
---|
262 | if (!isdigit(c))
|
---|
263 | return(QQsw = FALSE);
|
---|
264 |
|
---|
265 | QQdig = c;
|
---|
266 | ++QQip;
|
---|
267 | return(QQsw = TRUE);
|
---|
268 | }
|
---|
269 |
|
---|
270 | /* |
---|
271 |
|
---|
272 | */
|
---|
273 |
|
---|
274 | /*
|
---|
275 | =============================================================================
|
---|
276 | CMlist(l) -- attempt to match a string from the list 'l' in the input.
|
---|
277 | =============================================================================
|
---|
278 | */
|
---|
279 |
|
---|
280 | int16_t CMlist(int8_t *l[])
|
---|
281 | {
|
---|
282 | register int16_t n;
|
---|
283 | register int8_t *p, *q;
|
---|
284 |
|
---|
285 | CM_DBLK;
|
---|
286 |
|
---|
287 | n = 0;
|
---|
288 |
|
---|
289 | while (p = *l++) {
|
---|
290 |
|
---|
291 | q = p;
|
---|
292 |
|
---|
293 | if (CMstr(p)) {
|
---|
294 |
|
---|
295 | QQop = QQstr;
|
---|
296 |
|
---|
297 | while (*QQop++ = *q++) ;
|
---|
298 |
|
---|
299 | QQlnum = n;
|
---|
300 | return(QQsw = TRUE);
|
---|
301 | }
|
---|
302 |
|
---|
303 | ++n;
|
---|
304 | }
|
---|
305 |
|
---|
306 | return(QQsw = FALSE);
|
---|
307 | }
|
---|
308 |
|
---|
309 | /* |
---|
310 |
|
---|
311 | */
|
---|
312 |
|
---|
313 | /*
|
---|
314 | =============================================================================
|
---|
315 | CMulist(l) -- attempt to match a string from the list 'l' in the input
|
---|
316 | ignoring case in both the list and the input string.
|
---|
317 | =============================================================================
|
---|
318 | */
|
---|
319 |
|
---|
320 | int16_t CMulist(int8_t *l[])
|
---|
321 | {
|
---|
322 | register int16_t n;
|
---|
323 | register int8_t *p, *q;
|
---|
324 |
|
---|
325 | CM_DBLK;
|
---|
326 |
|
---|
327 | n = 0;
|
---|
328 |
|
---|
329 | while (p = *l++) {
|
---|
330 |
|
---|
331 | q = p;
|
---|
332 |
|
---|
333 | if (CMustr(p)) {
|
---|
334 |
|
---|
335 | QQop = QQstr;
|
---|
336 |
|
---|
337 | while (*QQop++ = *q++) ;
|
---|
338 |
|
---|
339 | QQlnum = n;
|
---|
340 | return(QQsw = TRUE);
|
---|
341 | }
|
---|
342 |
|
---|
343 | ++n;
|
---|
344 | }
|
---|
345 |
|
---|
346 | return(QQsw = FALSE);
|
---|
347 | }
|
---|
348 |
|
---|
349 | /* |
---|
350 |
|
---|
351 | */
|
---|
352 |
|
---|
353 | /*
|
---|
354 | =============================================================================
|
---|
355 | CMstat(msg) -- output 'msg and dump parser status. Returns QQsw.
|
---|
356 | =============================================================================
|
---|
357 | */
|
---|
358 |
|
---|
359 | int16_t CMstat(int8_t *msg)
|
---|
360 | {
|
---|
361 | register int8_t *tp;
|
---|
362 |
|
---|
363 | tp = QQin;
|
---|
364 | printf("%s\r\n", msg);
|
---|
365 | printf(" QQsw: %s, QQanch: %s, QQchr: 0x%02x <%c>, QQdig: %c\r\n",
|
---|
366 | (QQsw ? "OK" : "NOGO"), (QQanch ? "anchored" : "deblanked"),
|
---|
367 | QQchr, (isascii(QQchr) ? (isprint(QQchr) ? QQchr : ' ') : ' '),
|
---|
368 | QQdig);
|
---|
369 | printf(" QQnum: %ld, QQlnum: %d\r\n", QQnum, QQlnum);
|
---|
370 | printf(" QQstr: %s\r\n", QQstr);
|
---|
371 | printf(" {%s}\r\n", QQin);
|
---|
372 | printf(" ");
|
---|
373 |
|
---|
374 | while (tp++ NE QQip)
|
---|
375 | printf(" ");
|
---|
376 |
|
---|
377 | printf("^\r\n");
|
---|
378 | return(QQsw);
|
---|
379 | }
|
---|