[f40a309] | 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 | int QQsw; /* parser result switch */
|
---|
| 19 | int QQanch; /* parser anchored match switch */
|
---|
| 20 |
|
---|
| 21 | char *QQin; /* parser initial input pointer */
|
---|
| 22 | char *QQip; /* parser current input pointer */
|
---|
| 23 | char *QQop; /* parser string output pointer */
|
---|
| 24 |
|
---|
| 25 | long QQnum; /* parser numeric result */
|
---|
| 26 | int QQlnum; /* parser list element number result */
|
---|
| 27 | char QQdig; /* parser digit result */
|
---|
| 28 | char QQchr; /* parser character result */
|
---|
| 29 |
|
---|
| 30 | char 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 | */
|
---|
[0580615] | 41 |
|
---|
[f40a309] | 42 | void CMinit(char *ip)
|
---|
| 43 | {
|
---|
| 44 | register int i;
|
---|
| 45 | register char *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 | =============================================================================
|
---|
[0580615] | 69 | */
|
---|
[f40a309] | 70 |
|
---|
| 71 | int CMchr(char 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.
|
---|
[0580615] | 89 | =============================================================================
|
---|
[f40a309] | 90 | */
|
---|
| 91 |
|
---|
| 92 | int CMuchr(char c)
|
---|
| 93 | {
|
---|
| 94 | register char 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 | =============================================================================
|
---|
[0580615] | 121 | CMstr(s) -- attempt to match string at 's' in the input.
|
---|
[f40a309] | 122 | =============================================================================
|
---|
| 123 | */
|
---|
| 124 |
|
---|
| 125 | int CMstr(char *s)
|
---|
| 126 | {
|
---|
| 127 | register char *t;
|
---|
| 128 | char *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 | /*
|
---|
[0580615] | 154 | =============================================================================
|
---|
[f40a309] | 155 | CMustr(s) -- attempt to match string 's' in the input, ignoring case.
|
---|
| 156 | =============================================================================
|
---|
| 157 | */
|
---|
| 158 |
|
---|
| 159 | int CMustr(char *s)
|
---|
| 160 | {
|
---|
| 161 | register char *t, t1, t2;
|
---|
| 162 | char *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 |
|
---|
[0580615] | 198 | /*
|
---|
[f40a309] | 199 | =============================================================================
|
---|
| 200 | CMlong() -- attempt to parse a digit string in the input as a long.
|
---|
| 201 | =============================================================================
|
---|
| 202 | */
|
---|
| 203 |
|
---|
| 204 | int CMlong(void)
|
---|
| 205 | {
|
---|
| 206 | register char *p;
|
---|
| 207 | register long n;
|
---|
| 208 | register char 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 | */
|
---|
[0580615] | 244 |
|
---|
[f40a309] | 245 | /*
|
---|
| 246 | =============================================================================
|
---|
| 247 | CMdig() -- attempt to match a digit in the input string.
|
---|
| 248 | =============================================================================
|
---|
| 249 | */
|
---|
| 250 |
|
---|
| 251 | int CMdig(void)
|
---|
| 252 | {
|
---|
| 253 | register char 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 |
|
---|
[0580615] | 272 | */
|
---|
[f40a309] | 273 |
|
---|
| 274 | /*
|
---|
| 275 | =============================================================================
|
---|
| 276 | CMlist(l) -- attempt to match a string from the list 'l' in the input.
|
---|
| 277 | =============================================================================
|
---|
| 278 | */
|
---|
| 279 |
|
---|
| 280 | int CMlist(char *l[])
|
---|
| 281 | {
|
---|
| 282 | register int n;
|
---|
| 283 | register char *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 |
|
---|
[0580615] | 311 | */
|
---|
[f40a309] | 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 | int CMulist(char *l[])
|
---|
| 321 | {
|
---|
| 322 | register int n;
|
---|
| 323 | register char *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 |
|
---|
[0580615] | 349 | /* |
---|
[f40a309] | 350 |
|
---|
| 351 | */
|
---|
| 352 |
|
---|
| 353 | /*
|
---|
| 354 | =============================================================================
|
---|
| 355 | CMstat(msg) -- output 'msg and dump parser status. Returns QQsw.
|
---|
| 356 | =============================================================================
|
---|
| 357 | */
|
---|
| 358 |
|
---|
| 359 | int CMstat(char *msg)
|
---|
| 360 | {
|
---|
| 361 | register char *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 | }
|
---|