source: buchla-68k/ram/cminit.c@ 6262b5c

Last change on this file since 6262b5c was 6262b5c, checked in by Thomas Lopatic <thomas@…>, 7 years ago

Added include files for global functions and variables.

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