- reg - regular expression, i.e. main body or parenthesized thing * * Caller must absorb opening parenthesis. * * Combining parenthesis handling with the base level of regular expression * is a trifle forced, but the need to tie the tails of the branches to what * follows makes it hard to avoid. */
| 303 | * follows makes it hard to avoid. |
| 304 | */ |
| 305 | static char * |
| 306 | reg( |
| 307 | int paren, /* Parenthesized? */ |
| 308 | int *flagp ) |
| 309 | { |
| 310 | register char *ret; |
| 311 | register char *br; |
| 312 | register char *ender; |
| 313 | register int parno = 0; |
| 314 | int flags; |
| 315 | |
| 316 | *flagp = HASWIDTH; /* Tentatively. */ |
| 317 | |
| 318 | /* Make an OPEN node, if parenthesized. */ |
| 319 | if (paren) { |
| 320 | if (regnpar >= NSUBEXP) |
| 321 | FAIL("too many ()"); |
| 322 | parno = regnpar; |
| 323 | regnpar++; |
| 324 | ret = regnode(OPEN+parno); |
| 325 | } else |
| 326 | ret = NULL; |
| 327 | |
| 328 | /* Pick up the branches, linking them together. */ |
| 329 | br = regbranch(&flags); |
| 330 | if (br == NULL) |
| 331 | return(NULL); |
| 332 | if (ret != NULL) |
| 333 | regtail(ret, br); /* OPEN -> first. */ |
| 334 | else |
| 335 | ret = br; |
| 336 | if (!(flags&HASWIDTH)) |
| 337 | *flagp &= ~HASWIDTH; |
| 338 | *flagp |= flags&SPSTART; |
| 339 | while (*regparse == '|' || *regparse == '\n') { |
| 340 | regparse++; |
| 341 | br = regbranch(&flags); |
| 342 | if (br == NULL) |
| 343 | return(NULL); |
| 344 | regtail(ret, br); /* BRANCH -> BRANCH. */ |
| 345 | if (!(flags&HASWIDTH)) |
| 346 | *flagp &= ~HASWIDTH; |
| 347 | *flagp |= flags&SPSTART; |
| 348 | } |
| 349 | |
| 350 | /* Make a closing node, and hook it on the end. */ |
| 351 | ender = regnode((paren) ? CLOSE+parno : END); |
| 352 | regtail(ret, ender); |
| 353 | |
| 354 | /* Hook the tails of the branches to the closing node. */ |
| 355 | for (br = ret; br != NULL; br = regnext(br)) |
| 356 | regoptail(br, ender); |
| 357 | |
| 358 | /* Check for proper termination. */ |
| 359 | if (paren && *regparse++ != ')') { |
| 360 | FAIL("unmatched ()"); |
| 361 | } else if (!paren && *regparse != '\0') { |
| 362 | if (*regparse == ')') { |