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