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