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