Lines Matching defs:env

145 node(Cenv_t* env, int type, int lo, int hi, size_t extra)
150 if (e = (Rex_t*)alloc(env->disc, 0, sizeof(Rex_t) + extra))
157 e->flags = env->flags;
158 e->map = (e->flags & REG_ICASE) ? env->MAP : env->map;
159 e->explicit = env->explicit;
271 serialize(Cenv_t* env, Rex_t* e, int n)
281 n = serialize(env, e->re.group.expr.binary.left, n);
284 n = serialize(env, e->re.group.expr.binary.right, n);
287 n = serialize(env, e->re.group.expr.binary.left, n);
288 n = serialize(env, e->re.group.expr.binary.right, n);
298 n = serialize(env, e->re.group.expr.rex, n);
310 cat(Cenv_t* env, Rex_t* e, Rex_t* f)
316 drop(env->disc, e);
321 drop(env->disc, e);
328 drop(env->disc, f);
350 drop(env->disc, f);
364 stats(register Cenv_t* env, register Rex_t* e)
386 x = env->stats.x;
387 l = env->stats.l;
388 y = env->stats.y;
389 k = env->stats.k;
390 t = env->stats.t;
391 if (++env->stats.a <= 0)
393 cm = env->stats.m;
394 env->stats.m = 0;
395 cn = env->stats.n;
396 env->stats.n = 0;
397 if (stats(env, e->re.group.expr.binary.left))
399 m = env->stats.m;
400 env->stats.m = 0;
401 n = env->stats.n;
402 env->stats.n = 0;
403 if (e->re.group.expr.binary.right && stats(env, e->re.group.expr.binary.right))
405 if (env->stats.m > m)
406 env->stats.m = m;
408 m = env->stats.m;
409 if ((env->stats.m += cm) < m)
411 if (env->stats.n < n)
412 env->stats.n = n;
414 n = env->stats.n;
415 if ((env->stats.n += cn) < n)
417 env->stats.x = x;
418 env->stats.l = l;
419 env->stats.y = y;
420 env->stats.k = k;
421 env->stats.t = t;
424 if (++env->stats.b <= 0)
431 n = env->stats.m;
432 if ((env->stats.m += e->lo) < n)
436 n = env->stats.n;
437 if ((env->stats.n += e->hi) < n)
442 if (++env->stats.c <= 0)
444 if (++env->stats.s <= 0)
449 cm = env->stats.m;
450 env->stats.m = 0;
451 cn = env->stats.n;
452 env->stats.n = 0;
453 if (stats(env, e->re.group.expr.binary.left))
455 nm = env->stats.m;
456 env->stats.m = 0;
457 nn = env->stats.n;
458 env->stats.n = 0;
459 if (stats(env, e->re.group.expr.binary.right))
461 if (env->stats.m < nm)
462 env->stats.m = nm;
464 nm = env->stats.m;
465 if ((env->stats.m += cm) < nm)
467 if (env->stats.n < nn)
468 env->stats.n = nn;
470 nn = env->stats.n;
471 if ((env->stats.n += cn) < nn)
475 env->stats.e = 1;
478 if (e->re.group.number && ++env->stats.p <= 0 || !e->re.group.number && ++env->stats.u <= 0)
480 if (stats(env, e->re.group.expr.rex))
487 m = env->stats.m;
488 n = env->stats.n;
489 x = env->stats.x;
490 y = env->stats.y;
491 if (stats(env, e->re.group.expr.rex))
493 env->stats.m = m;
494 env->stats.n = n;
495 env->stats.x = x;
496 env->stats.y = y;
501 if (++env->stats.u <= 0)
507 if (++env->stats.u <= 0)
509 m = env->stats.m;
510 n = env->stats.n;
511 x = env->stats.x;
512 y = env->stats.y;
513 if (e->re.group.size > 0 && ++env->stats.b <= 0)
515 if (e->re.group.expr.binary.left && stats(env, e->re.group.expr.binary.left))
519 if (q->re.group.expr.binary.left && stats(env, q->re.group.expr.binary.left))
521 if (q->re.group.expr.binary.right && stats(env, q->re.group.expr.binary.right))
524 env->stats.m = m;
525 env->stats.n = n;
526 env->stats.x = x;
527 env->stats.y = y;
530 if (++env->stats.u <= 0)
532 m = env->stats.m;
533 n = env->stats.n;
534 x = env->stats.x;
535 y = env->stats.y;
536 if (stats(env, e->re.group.expr.rex))
538 env->stats.m = m;
539 env->stats.n = n;
540 env->stats.x = x;
541 env->stats.y = y;
544 env->stats.i++;
545 x = env->stats.x;
546 l = env->stats.l;
547 y = env->stats.y;
548 k = env->stats.k;
549 t = env->stats.t;
550 cm = env->stats.m;
551 env->stats.m = 0;
552 if (stats(env, e->re.group.expr.rex))
554 env->stats.m = !env->stats.m;
555 if ((env->stats.m += cm) < cm)
557 env->stats.x = x;
558 env->stats.l = l;
559 env->stats.y = y;
560 env->stats.k = k;
561 env->stats.t = t;
564 x = env->stats.x;
565 l = env->stats.l;
566 y = env->stats.y;
567 k = env->stats.k;
568 t = env->stats.t;
569 if (++env->stats.c <= 0)
571 b = env->stats.b;
572 c = env->stats.c;
573 cm = env->stats.m;
574 env->stats.m = 0;
575 if (stats(env, e->re.group.expr.rex))
577 if (env->stats.m == 1 && b == env->stats.b && c == env->stats.c && ++env->stats.s <= 0)
581 env->stats.x = x;
582 env->stats.l = l;
583 env->stats.y = y;
584 env->stats.k = k;
585 env->stats.t = t;
586 env->stats.m = cm;
590 m = env->stats.m;
591 if ((env->stats.m *= e->lo) > 0 && env->stats.m < m)
593 m = env->stats.m;
594 if ((env->stats.m += cm) < m)
596 if (env->stats.x != x)
597 env->stats.l = cm;
598 if (env->stats.y != y)
599 env->stats.k = cm;
605 cm = env->stats.m;
606 if ((env->stats.m += e->re.string.size) < cm)
608 cn = env->stats.n;
609 if ((env->stats.n += e->re.string.size) < cn)
611 if (!env->stats.x || env->stats.x->re.string.size < e->re.string.size)
613 env->stats.x = e;
614 env->stats.l = cm;
619 if (++env->stats.s <= 0)
621 cm = env->stats.m;
622 if ((env->stats.m += e->re.trie.min) < cm)
624 cn = env->stats.n;
625 if ((env->stats.n += e->re.trie.max) < cn)
627 env->stats.t++;
628 if (!env->stats.y || env->stats.y->re.trie.min < e->re.trie.min)
630 env->stats.y = e;
631 env->stats.k = cm;
642 magic(register Cenv_t* env, register int c, int escaped)
647 int e = env->error;
648 int l = env->token.len;
654 c = mp[env->type+escaped];
657 sp = (char*)env->cursor + env->token.len;
667 env->error = REG_BADBR;
674 if (env->type < SRE || *sp != ',')
676 env->error = *sp ? REG_BADBR : REG_EBRACE;
682 env->error = REG_BADBR;
685 env->token.min = n;
694 env->error = REG_BADBR;
701 else if (n < env->token.min)
703 env->error = REG_BADBR;
707 env->token.max = n;
711 env->error = REG_EBRACE;
716 env->error = REG_BADBR;
724 env->error = REG_BADBR;
732 env->error = REG_EBRACE;
737 env->error = REG_BADBR;
740 env->token.len = sp - (char*)env->cursor;
743 env->error = REG_EBRACE;
746 if (env->type < SRE && *sp == '?')
748 env->token.len++;
749 env->token.lex = 0;
757 env->token.len += ep - sp;
760 env->token.lex = c;
775 env->token.len += ep - sp;
781 if (env->type == SRE || c == T_BACK && !(env->flags & (REG_LENIENT|REG_REGEXP)))
783 env->error = REG_BADESC;
786 if ((env->flags & REG_MULTIREF) && isdigit(*sp))
789 if (c > 0 && c <= env->parno && env->paren[c])
793 env->token.len++;
799 if (escaped == 1 && (env->flags & (REG_LENIENT|REG_REGEXP)) && (c = mp[env->type+escaped+2]) >= T_META)
803 if (env->type >= SRE)
809 if (env->type == KRE && *(env->cursor + env->token.len) == '-' && *(env->cursor + env->token.len + 1) == '(')
811 env->token.len++;
812 env->token.att = 1;
814 if (env->type == KRE && *(env->cursor + env->token.len) == '(')
816 env->token.len++;
822 env->token.lex = c;
825 env->token.lex = 0;
828 env->token.lex = c;
840 env->token.len = l;
846 c = (c > env->parno || !env->paren[c]) ? o : T_BACK + c;
848 else if (env->type == KRE && !env->parnest && (env->flags & REG_SHELL_GROUP))
862 if (env->type >= SRE && !(env->flags & REG_SHELL_ESCAPED) || (env->flags & REG_ESCAPE) && (c == '[' || c == '-' || c == ']' || env->delimiter && c == env->delimiter))
866 env->error = REG_BADESC;
870 else if (escaped && !(env->flags & (REG_LENIENT|REG_REGEXP)) && c != ']')
872 env->error = REG_BADESC;
877 sp = (char*)env->cursor + env->token.len;
888 env->error = REG_EPAREN;
901 env->cursor = (unsigned char*)sp;
902 return token(env);
905 env->error = e;
906 else if (env->flags & (REG_LENIENT|REG_REGEXP))
908 else if (escaped == 1 && !env->error)
912 env->error = REG_BADESC;
918 token(register Cenv_t* env)
923 if (env->token.push)
924 return env->token.lex;
925 env->token.att = env->token.esc = 0;
926 if ((env->token.len = MBSIZE(env->cursor)) > 1)
927 return env->token.lex = C_MB;
928 env->token.lex = 0;
931 c = *env->cursor;
932 if (c == 0 || c == env->delimiter || c == env->terminator)
934 if (!(env->flags & REG_COMMENT))
940 c = *++env->cursor;
941 if (c == 0 || c == env->delimiter)
947 env->cursor++;
949 if (c == '\n' && (env->flags & REG_MULTIPLE) && !env->delimiter)
951 if (env->parnest)
953 env->error = REG_EPAREN;
956 env->parno = 0;
957 env->pattern = env->cursor + 1;
960 if (env->flags & REG_LITERAL)
962 if (posixkludge = env->posixkludge)
964 env->posixkludge = 0;
970 if (env->flags & REG_SHELL_ESCAPED)
972 if (!(c = *(env->cursor + 1)) || c == env->terminator)
974 if (env->flags & (REG_LENIENT|REG_REGEXP))
978 env->token.esc = env->token.len;
979 env->token.len += MBSIZE(env->cursor + 1);
984 env->error = REG_EESCAPE;
987 env->token.esc = env->token.len;
988 env->token.len += MBSIZE(env->cursor + 1);
989 if (env->delimiter && c == 'n')
991 else if (c == env->delimiter)
992 return magic(env, c, 0);
993 else if (c == '(' && env->type == BRE)
994 env->posixkludge = 1;
995 else if (c == ')' && env->type == BRE && env->parnest <= 0)
997 env->error = REG_EPAREN;
1000 else if (isspace(c) && (env->flags & REG_COMMENT))
1002 return magic(env, c, 1);
1006 if (env->type == BRE && (*(env->cursor + 1) == 0 || *(env->cursor + 1) == env->delimiter || *(env->cursor + 1) == env->terminator || *(env->cursor + 1) == '\\' && *(env->cursor + 2) == ')') || (env->flags & REG_MULTIPLE) && *(env->cursor + 1) == '\n')
1011 if (env->type == BRE && (env->cursor == env->pattern || posixkludge == 1))
1013 env->posixkludge = 2;
1019 if (env->type != BRE && env->parnest <= 0)
1022 else if (c == '/' && env->explicit == env->mappedslash)
1024 while (*(env->cursor + env->token.len) == c)
1025 env->token.len++;
1028 return magic(env, c, 0);
1169 bra(Cenv_t* env)
1192 if (!(e = node(env, REX_CLASS, 1, 1, sizeof(Set_t))))
1195 if (*env->cursor == '^' || env->type >= SRE && *env->cursor == '!')
1197 env->cursor++;
1202 first = env->cursor;
1204 if (*env->cursor == 0 || *(env->cursor + 1) == 0 || *env->cursor == env->terminator || *(env->cursor + 1) == env->terminator || (env->flags & REG_ESCAPE) && (*env->cursor == env->delimiter || *env->cursor != '\\' && *(env->cursor + 1) == env->delimiter))
1206 begin = env->cursor + MBSIZE(env->cursor);
1215 if (!(c = *env->cursor) || c == env->terminator || c == env->delimiter && (env->flags & REG_ESCAPE))
1217 env->cursor += (w = MBSIZE(env->cursor));
1218 if (c == '\\' && ((env->flags & REG_CLASS_ESCAPE) || *env->cursor == env->delimiter && (env->flags & REG_ESCAPE)))
1220 if (*env->cursor)
1222 if (*env->cursor == 'n')
1224 env->cursor++;
1227 else if (env->type < SRE || !(env->flags & REG_SHELL_ESCAPED))
1229 env->token.len = 1;
1230 w = magic(env, *env->cursor, 2);
1231 if (env->token.len > 1 || w != T_BAD)
1233 if (env->token.len == 1 && (f = classfun(w)))
1237 if (env->type < SRE && !(env->flags & (REG_LENIENT|REG_REGEXP)))
1241 env->cursor++;
1249 if (env->token.len > 1 || w >= 0 && w < T_META)
1254 if (env->type < SRE && !(env->flags & (REG_LENIENT|REG_REGEXP)) && !mbwide())
1258 env->cursor += env->token.len;
1266 if (env->cursor == begin)
1286 if (!inrange && env->cursor != begin && *env->cursor != ']')
1288 if (env->type < SRE && !(env->flags & (REG_LENIENT|REG_REGEXP)))
1301 switch (*env->cursor)
1306 if (env->flags & REG_REGEXP)
1313 if (!(f = regclass((char*)env->cursor, (char**)&env->cursor)))
1315 if (env->cursor == start && (c = *(env->cursor + 1)))
1317 s = start = env->cursor + 1;
1337 env->cursor = s + 3;
1338 drop(env->disc, e);
1339 return node(env, i, 0, 0, 0);
1344 env->error = REG_ECTYPE;
1355 if (env->flags & REG_REGEXP)
1364 if ((c = regcollate((char*)env->cursor, (char**)&env->cursor, (char*)buf, sizeof(buf), NiL)) < 0)
1376 if (env->flags & REG_REGEXP)
1378 if ((c = regcollate((char*)env->cursor, (char**)&env->cursor, (char*)buf, sizeof(buf), NiL)) < 0)
1387 if (*env->cursor == env->terminator || *env->cursor == env->delimiter && (env->flags & REG_ESCAPE))
1400 inrange = env->type >= SRE || (env->flags & (REG_LENIENT|REG_REGEXP));
1403 else if (env->type >= SRE)
1410 else if (!(env->flags & (REG_LENIENT|REG_REGEXP)))
1464 drop(env->disc, e);
1470 drop(env->disc, e);
1471 if (ic = env->flags & REG_ICASE)
1473 if (!(e = node(env, REX_COLL_CLASS, 1, 1, (elements + 3) * sizeof(Celt_t))))
1478 env->cursor = first;
1482 if ((c = *env->cursor) == 0 || c == env->terminator || (env->flags & REG_ESCAPE) && c == env->delimiter)
1484 pp = env->cursor;
1485 env->cursor += (w = MBSIZE(env->cursor));
1486 if (c == '\\' && ((env->flags & REG_CLASS_ESCAPE) || *env->cursor == env->delimiter && (env->flags & REG_ESCAPE)))
1488 if (*env->cursor)
1490 if (*env->cursor == 'n')
1492 pp = env->cursor++;
1495 else if (env->type < SRE || !(env->flags & REG_SHELL_ESCAPED))
1497 env->token.len = 1;
1498 w = magic(env, *env->cursor, 2);
1499 if (env->token.len > 1 || w != T_BAD)
1501 if (env->token.len == 1 && (f = classfun(w)))
1505 if (env->type < SRE && !(env->flags & (REG_LENIENT|REG_REGEXP)))
1509 env->cursor++;
1515 if (env->token.len > 1 || w >= 0 && w < T_META)
1520 env->cursor += env->token.len;
1528 if (env->cursor == begin)
1545 if (!inrange && env->cursor != begin && *env->cursor != ']')
1547 if (env->type < SRE && !(env->flags & (REG_LENIENT|REG_REGEXP)))
1559 switch (*env->cursor)
1564 if (env->flags & REG_REGEXP)
1568 if (!(f = regclass((char*)env->cursor, (char**)&env->cursor)))
1570 if (env->cursor == start && (c = *(env->cursor + 1)) && *(env->cursor + 2) == ':' && *(env->cursor + 3) == ']' && *(env->cursor + 4) == ']')
1586 env->cursor += 5;
1587 drop(env->disc, e);
1588 return node(env, i, 0, 0, 0);
1591 env->error = REG_ECTYPE;
1600 if (env->flags & REG_REGEXP)
1607 rp = env->cursor + 1;
1608 if ((rw = regcollate((char*)env->cursor, (char**)&env->cursor, (char*)pp, COLL_KEY_MAX, &wc)) < 0)
1676 if (env->flags & REG_REGEXP)
1679 if ((w = regcollate((char*)env->cursor, (char**)&env->cursor, (char*)pp, COLL_KEY_MAX, NiL)) < 0)
1685 if (*env->cursor == env->terminator || *env->cursor == env->delimiter && (env->flags & REG_ESCAPE))
1696 if (env->type < SRE && !(env->flags & (REG_LENIENT|REG_REGEXP)))
1703 inrange = env->type >= SRE || (env->flags & (REG_LENIENT|REG_REGEXP));
1720 if (env->flags & REG_ICASE)
1736 if (env->explicit >= 0)
1737 setclr(e->re.charclass, env->explicit);
1741 env->error = REG_ECOLLATE;
1744 env->error = REG_ERANGE;
1746 drop(env->disc, e);
1747 if (!env->error)
1748 env->error = REG_EBRACK;
1753 ccl(Cenv_t* env, int type)
1762 env->error = REG_BADESC;
1767 if (!(e = node(env, REX_CLASS, 1, 1, sizeof(Set_t))))
1772 if (env->explicit >= 0)
1773 setclr(e->re.charclass, env->explicit);
1777 if (!(e = node(env, REX_COLL_CLASS, 1, 1, 2 * sizeof(Celt_t))))
1791 rep(Cenv_t* env, Rex_t* e, int number, int last)
1800 switch (token(env))
1803 eat(env);
1804 if (!(f = node(env, REX_NEG, m, n, 0)))
1806 drop(env->disc, e);
1812 eat(env);
1816 eat(env);
1819 eat(env);
1823 eat(env);
1824 m = env->token.min;
1825 n = env->token.max;
1830 if (env->token.att)
1832 else if (env->type < SRE)
1833 switch (token(env))
1836 eat(env);
1837 minimal = !(env->flags & REG_MINIMAL);
1840 eat(env);
1841 minimal = !!(env->flags & REG_MINIMAL);
1865 env->error = REG_BADRPT;
1866 drop(env->disc, e);
1875 if (!(f = node(env, REX_REP, m, n, 0)))
1877 drop(env->disc, e);
1908 trienode(Cenv_t* env, int c)
1912 if (t = (Trie_node_t*)alloc(env->disc, 0, sizeof(Trie_node_t)))
1921 insert(Cenv_t* env, Rex_t* f, Rex_t* g)
1942 if (!(t = g->re.trie.root[*s]) && !(t = g->re.trie.root[*s] = trienode(env, *s)))
1950 if (!t->son && !(t->son = trienode(env, *s)))
1957 if (!t->sib && !(t->sib = trienode(env, *s)))
1977 trie(Cenv_t* env, Rex_t* e, Rex_t* f)
1985 if (!(g = node(env, REX_TRIE, 0, 0, (UCHAR_MAX + 1) * sizeof(Trie_node_t*))))
1988 if (insert(env, f, g))
1990 drop(env->disc, f);
1996 if (insert(env, e, g))
1998 drop(env->disc, e);
2002 drop(env->disc, g);
2009 chr(register Cenv_t* env, int* escaped)
2015 if (!(c = *env->cursor))
2017 env->cursor++;
2020 if (env->flags & REG_SHELL_ESCAPED)
2022 if (!(c = *(env->cursor + 1)) || c == env->terminator)
2024 if (env->flags & (REG_LENIENT|REG_REGEXP))
2026 env->error = REG_EESCAPE;
2029 p = env->cursor;
2030 c = chresc((char*)env->cursor - 1, (char**)&env->cursor);
2031 *escaped = env->cursor - p;
2041 grp(Cenv_t* env, int parno)
2055 g = env->flags;
2056 beg = env->pattern == env->cursor - env->token.len;
2057 if (!(c = env->token.lex) && (c = *env->cursor))
2058 env->cursor++;
2059 env->token.len = 0;
2060 env->parnest++;
2094 env->token.push = 1;
2100 if (!(env->flags & REG_LITERAL))
2102 env->error = REG_BADRPT;
2111 eat(env);
2112 if (token(env) == T_CLOSE)
2123 env->flags |= (REG_LEFT|REG_RIGHT);
2125 env->flags &= ~(REG_LEFT|REG_RIGHT);
2129 env->flags &= ~REG_MINIMAL;
2131 env->flags |= REG_MINIMAL;
2135 env->flags |= REG_ICASE;
2137 env->flags &= ~REG_ICASE;
2141 env->flags |= REG_LEFT;
2143 env->flags &= ~REG_LEFT;
2147 env->flags |= REG_NEWLINE;
2149 env->flags &= ~REG_NEWLINE;
2150 env->explicit = (env->flags & (REG_NEWLINE|REG_SPAN)) == REG_NEWLINE ? env->mappednewline : -1;
2154 env->flags &= ~REG_LENIENT;
2156 env->flags |= REG_LENIENT;
2160 env->flags |= REG_RIGHT;
2162 env->flags &= ~REG_RIGHT;
2166 env->flags |= REG_SPAN;
2168 env->flags &= ~REG_SPAN;
2169 env->explicit = (env->flags & (REG_NEWLINE|REG_SPAN)) == REG_NEWLINE ? env->mappednewline : -1;
2173 env->flags |= REG_COMMENT;
2175 env->flags &= ~REG_COMMENT;
2178 if (typ >= 0 || env->type == ERE && (env->flags & REG_CLASS_ESCAPE))
2182 env->flags &= ~(REG_AUGMENTED|REG_EXTENDED|REG_LITERAL|REG_REGEXP|REG_SHELL|REG_LEFT|REG_RIGHT);
2183 env->flags |= REG_AUGMENTED|REG_EXTENDED;
2188 env->flags &= ~(REG_AUGMENTED|REG_EXTENDED|REG_LITERAL|REG_REGEXP|REG_SHELL|REG_LEFT|REG_RIGHT);
2192 env->flags &= ~(REG_AUGMENTED|REG_EXTENDED|REG_LITERAL|REG_REGEXP|REG_SHELL|REG_LEFT|REG_RIGHT);
2193 env->flags |= REG_EXTENDED;
2198 env->flags &= ~(REG_AUGMENTED|REG_EXTENDED|REG_LITERAL|REG_REGEXP|REG_SHELL|REG_LEFT|REG_RIGHT);
2199 env->flags |= REG_LITERAL;
2203 env->flags &= ~(REG_AUGMENTED|REG_EXTENDED|REG_LITERAL|REG_REGEXP|REG_SHELL|REG_LEFT|REG_RIGHT);
2204 env->flags |= REG_AUGMENTED|REG_SHELL|REG_LEFT|REG_RIGHT;
2217 env->flags &= ~(REG_AUGMENTED|REG_EXTENDED|REG_LITERAL|REG_REGEXP|REG_SHELL|REG_LEFT|REG_RIGHT);
2218 env->flags |= REG_EXTENDED|REG_CLASS_ESCAPE;
2222 env->flags &= ~(REG_AUGMENTED|REG_EXTENDED|REG_LITERAL|REG_REGEXP|REG_SHELL|REG_LEFT|REG_RIGHT);
2223 env->flags |= REG_SHELL|REG_LEFT|REG_RIGHT;
2227 if (typ >= 0 || env->type == ERE && (env->flags & REG_CLASS_ESCAPE))
2230 env->flags |= REG_MINIMAL;
2232 env->flags &= ~REG_MINIMAL;
2236 env->flags &= ~(REG_AUGMENTED|REG_EXTENDED|REG_LITERAL|REG_REGEXP|REG_SHELL|REG_LEFT|REG_RIGHT);
2237 env->flags |= REG_REGEXP;
2241 env->error = REG_BADRPT;
2244 eat(env);
2245 c = token(env);
2259 switch (token(env))
2269 env->error = REG_BADRPT;
2272 eat(env);
2279 e = node(env, REX_NEST, 0, 0, (UCHAR_MAX + 1) * sizeof(unsigned short));
2280 e->re.nest.primary = isalnum(*env->cursor) ? -1 : *env->cursor;
2284 switch (i = chr(env, &esc))
2289 env->cursor -= esc + 1;
2290 env->error = REG_EPAREN;
2296 if ((i = chr(env, &esc)) < 0)
2309 switch (i = chr(env, &esc))
2322 if ((i = chr(env, &esc)) < 0)
2352 if ((x = chr(env, &esc)) < 0 || (e->re.nest.type[x] & (REX_NEST_close|REX_NEST_escape|REX_NEST_delimiter|REX_NEST_separator|REX_NEST_terminator)))
2367 env->parnest--;
2371 parno = ++env->parno;
2372 if (!(f = node(env, REX_GROUP, 0, 0, 0)))
2374 drop(env->disc, e);
2377 if (parno < elementsof(env->paren))
2378 env->paren[parno] = f;
2387 if (isdigit(*env->cursor))
2394 env->error = REG_BADRPT;
2397 c = c * 10 + (*env->cursor++ - '0');
2398 } while (isdigit(*env->cursor));
2399 if (*env->cursor++ != ')')
2401 env->error = REG_BADRPT;
2404 if (c && env->type >= SRE)
2406 if (!c || c > env->parno || !env->paren[c])
2408 if (!(env->flags & (REG_LENIENT|REG_REGEXP)))
2410 env->error = REG_ESUBREG;
2419 if (env->type < SRE && *env->cursor++ != '?')
2421 env->error = REG_BADRPT;
2424 if (!(f = grp(env, parno + 1)) && env->error)
2427 if (!(e = node(env, REX_GROUP_COND, 0, 0, 0)))
2429 drop(env->disc, f);
2434 if (!(e->re.group.expr.binary.right = alt(env, parno, 1)))
2436 drop(env->disc, e);
2439 if (token(env) != T_CLOSE)
2441 env->error = REG_EPAREN;
2444 eat(env);
2445 env->parnest--;
2446 return rep(env, e, parno, parno);
2448 p = env->cursor;
2450 while (c = *env->cursor)
2452 if (c == '\\' && *(env->cursor + 1))
2453 env->cursor++;
2458 else if (c == env->delimiter || c == env->terminator)
2460 env->cursor++;
2464 env->error = REG_EBRACE;
2467 if (*++env->cursor != ')')
2469 env->error = REG_EPAREN;
2472 env->cursor++;
2473 env->parnest--;
2474 if (env->disc->re_version < REG_VERSION_EXEC)
2476 env->error = REG_BADRPT;
2479 if (!env->disc->re_execf)
2481 if (!(e = node(env, REX_EXEC, 0, 0, 0)))
2484 e->re.exec.size = env->cursor - p - 2;
2485 if (!env->disc->re_compf)
2488 e->re.exec.data = (*env->disc->re_compf)(env->regex, e->re.exec.text, e->re.exec.size, env->disc);
2493 while (isdigit(*env->cursor))
2497 env->error = REG_ESUBREG;
2500 c = c * 10 + *env->cursor++ - '0';
2502 if (*env->cursor == ')')
2504 env->cursor++;
2505 env->parnest--;
2506 env->token.len = 1;
2507 if (c > env->parno || !env->paren[c])
2509 env->error = REG_ESUBREG;
2512 env->paren[c]->re.group.back = 1;
2513 return rep(env, node(env, REX_BACK, c, 0, 0), 0, 0);
2517 env->error = REG_BADRPT;
2520 p = env->pattern;
2521 i = env->type;
2525 env->type = typ;
2526 if (!(e = alt(env, parno, 0)))
2528 env->flags = g;
2529 env->type = i;
2531 c = token(env);
2532 env->parnest--;
2533 if (c != T_CLOSE && (!(env->flags & REG_LITERAL) || c != ')'))
2535 env->error = REG_EPAREN;
2538 eat(env);
2540 env->pattern = env->cursor;
2544 env->type = typ;
2547 if (!(f = node(env, x, 0, 0, 0)))
2549 drop(env->disc, e);
2555 if (stats(env, e))
2557 drop(env->disc, f);
2558 if (!env->error)
2559 env->error = REG_ECOUNT;
2562 f->re.group.size = env->stats.m;
2563 memset(&env->stats, 0, sizeof(env->stats));
2569 f = rep(env, f, parno, env->parno);
2575 env->flags = g;
2576 env->pattern = p;
2577 env->type = i;
2582 seq(Cenv_t* env)
2603 while ((c = token(env)) < T_META && s < &buf[sizeof(buf) - env->token.len])
2606 p = env->cursor;
2610 *s++ = (env->flags & REG_ICASE) ? toupper(c) : c;
2612 else if (c == C_ESC || (env->flags & REG_ICASE))
2614 c = (c == C_ESC) ? env->token.lex : mbchar(p);
2615 if (env->flags & REG_ICASE)
2631 n = env->token.len - env->token.esc;
2634 eat(env);
2651 if (!(e = node(env, REX_STRING, 0, 0, i)))
2658 if (!(f = node(env, REX_ONECHAR, 1, 1, 0)))
2660 drop(env->disc, e);
2663 f->re.onechar = (env->flags & REG_ICASE) ? toupper(x) : x;
2667 if (!(f = node(env, REX_STRING, 0, 0, n)))
2672 if (!(f = rep(env, f, 0, 0)) || !(f = cat(env, f, seq(env))))
2674 drop(env->disc, e);
2678 f = cat(env, e, f);
2682 if (!(e = node(env, REX_STRING, 0, 0, c)))
2686 return cat(env, e, seq(env));
2690 eat(env);
2692 if (c > env->parno || !env->paren[c])
2694 env->error = REG_ESUBREG;
2697 env->paren[c]->re.group.back = 1;
2698 e = rep(env, node(env, REX_BACK, c, 0, 0), 0, 0);
2707 return node(env, REX_NULL, 0, 0, 0);
2709 eat(env);
2710 e = rep(env, node(env, REX_END, 0, 0, 0), 0, 0);
2713 eat(env);
2714 if ((e = node(env, REX_BEG, 0, 0, 0)) && (env->flags & REG_EXTENDED))
2715 e = rep(env, e, 0, 0);
2718 tok = env->token;
2719 eat(env);
2720 flags = env->flags;
2721 type = env->type;
2722 if (env->token.att)
2723 env->flags |= REG_MINIMAL;
2724 env->parnest++;
2725 if (env->type == KRE)
2726 ++env->parno;
2727 parno = ++env->parno;
2728 if (!(e = alt(env, parno + 1, 0)))
2730 if (e->type == REX_NULL && env->type == ERE && !(env->flags & (REG_NULL|REG_REGEXP)))
2732 drop(env->disc, e);
2733 env->error = (*env->cursor == 0 || *env->cursor == env->delimiter || *env->cursor == env->terminator) ? REG_EPAREN : REG_ENULL;
2736 if (token(env) != T_CLOSE)
2738 drop(env->disc, e);
2739 env->error = REG_EPAREN;
2742 env->parnest--;
2743 eat(env);
2744 if (!(f = node(env, REX_GROUP, 0, 0, 0)))
2746 drop(env->disc, e);
2749 if (parno < elementsof(env->paren))
2750 env->paren[parno] = f;
2757 env->token = tok;
2759 if (!(e = rep(env, f, parno, env->parno)))
2761 if (env->type == KRE)
2763 if (!(f = node(env, REX_GROUP, 0, 0, 0)))
2765 drop(env->disc, e);
2768 if (--parno < elementsof(env->paren))
2769 env->paren[parno] = f;
2775 env->flags = flags;
2776 env->type = type;
2779 p = env->cursor;
2780 eat(env);
2781 flags = env->flags;
2782 type = env->type;
2783 if (!(e = grp(env, env->parno + 1)))
2785 if (env->error)
2787 if (env->literal == env->pattern && env->literal == p)
2788 env->literal = env->cursor;
2791 env->flags = flags;
2792 env->type = type;
2795 eat(env);
2796 if (e = bra(env))
2797 e = rep(env, e, 0, 0);
2805 eat(env);
2806 if (e = ccl(env, c))
2807 e = rep(env, e, 0, 0);
2810 eat(env);
2811 e = rep(env, node(env, REX_WBEG, 0, 0, 0), 0, 0);
2814 eat(env);
2815 e = rep(env, node(env, REX_WEND, 0, 0, 0), 0, 0);
2818 eat(env);
2819 e = rep(env, node(env, REX_DOT, 1, 1, 0), 0, 0);
2822 eat(env);
2823 env->token.lex = T_STAR;
2824 env->token.push = 1;
2825 e = rep(env, node(env, REX_DOT, 1, 1, 0), 0, 0);
2828 eat(env);
2829 env->token.lex = T_PLUS;
2830 env->token.push = 1;
2831 if (e = node(env, REX_ONECHAR, 1, 1, 0))
2834 e = rep(env, e, 0, 0);
2838 eat(env);
2839 e = rep(env, node(env, REX_WORD, 0, 0, 0), 0, 0);
2842 eat(env);
2843 e = rep(env, node(env, REX_WORD_NOT, 0, 0, 0), 0, 0);
2846 eat(env);
2847 e = rep(env, node(env, REX_BEG_STR, 0, 0, 0), 0, 0);
2850 eat(env);
2851 e = rep(env, node(env, REX_END_STR, 0, 0, 0), 0, 0);
2854 eat(env);
2855 e = rep(env, node(env, REX_FIN_STR, 0, 0, 0), 0, 0);
2858 env->error = REG_BADRPT;
2861 if (e && *env->cursor != 0 && *env->cursor != env->delimiter && *env->cursor != env->terminator)
2862 e = cat(env, e, seq(env));
2868 con(Cenv_t* env)
2874 if (!(e = seq(env)) || !(env->flags & REG_AUGMENTED) || token(env) != T_AND)
2876 eat(env);
2877 if (!(f = con(env)))
2879 drop(env->disc, e);
2882 if (!(g = node(env, REX_CONJ, 0, 0, 0)))
2884 drop(env->disc, e);
2885 drop(env->disc, f);
2894 alt(Cenv_t* env, int number, int cond)
2900 if (!(e = con(env)))
2902 else if (token(env) != T_BAR)
2912 eat(env);
2913 if (!(f = alt(env, number, 0)))
2915 drop(env->disc, e);
2918 if ((e->type == REX_NULL || f->type == REX_NULL) && !(env->flags & (REG_NULL|REG_REGEXP)))
2920 if (!cond && (g = trie(env, e, f)))
2923 if (!(g = node(env, REX_ALT, 0, 0, 0)))
2925 env->error = REG_ESPACE;
2929 g->re.group.last = env->parno;
2934 drop(env->disc, e);
2935 drop(env->disc, f);
2936 if (!env->error)
2937 env->error = REG_ENULL;
2946 bmstr(Cenv_t* env, register Rex_t* a, unsigned char* v, int n, Bm_mask_t b)
2980 bmtrie(Cenv_t* env, Rex_t* a, unsigned char* v, Trie_node_t* x, int n, int m, Bm_mask_t b)
2987 bmstr(env, a, v, n, b);
2997 b = bmtrie(env, a, v, x->son, n, m + 1, b);
3011 special(Cenv_t* env, regex_t* p)
3025 if (e = p->env->rex)
3027 if ((x = env->stats.x) && x->re.string.size < 3)
3029 if ((t = env->stats.y) && t->re.trie.min < 3)
3052 l = env->stats.l;
3059 l = env->stats.k;
3061 if (!(q = (size_t*)alloc(env->disc, 0, (n + 1) * sizeof(size_t))))
3063 if (!(a = node(env, REX_BM, 0, 0, n * (sizeof(Bm_mask_t*) + (UCHAR_MAX + 1) * sizeof(Bm_mask_t)) + (UCHAR_MAX + n + 2) * sizeof(size_t))))
3065 alloc(env->disc, q, 0);
3073 a->re.bm.right = env->stats.m - l - n;
3074 a->re.bm.complete = (env->stats.e || y != e && (e->type != REX_GROUP || y != e->re.group.expr.rex) || e->next || ((a->re.bm.left + a->re.bm.right) >= 0)) ? 0 : n;
3080 a->re.bm.skip[0] = a->re.bm.skip[env->mappednewline] = (y->next && y->next->type == REX_END) ? HIT : (n + a->re.bm.left);
3090 bmstr(env, a, x->re.string.base, n, 1);
3098 m = bmtrie(env, a, v, t->re.trie.root[i], n, 0, m);
3168 alloc(env->disc, q, 0);
3170 p->env->rex = a;
3176 if (env->flags & REG_NEWLINE)
3180 if (env->stats.b)
3191 if (env->flags & (REG_NULL|REG_REGEXP))
3193 env->error = REG_ENULL;
3196 if ((env->flags & (REG_LEFT|REG_LITERAL|REG_RIGHT)) || e->map)
3200 if (!(a = node(env, REX_KMP, 0, 0, n * (sizeof(int*) + 1))))
3218 p->env->rex = a;
3220 drop(env->disc, e);
3226 p->env->once = 1;
3238 Cenv_t env;
3251 p->env = 0;
3269 if (!(p->env = (Env_t*)alloc(disc, 0, sizeof(Env_t))))
3271 memset(p->env, 0, sizeof(*p->env));
3272 memset(&env, 0, sizeof(env));
3273 env.regex = p;
3274 env.flags = flags;
3275 env.disc = p->env->disc = disc;
3276 if (env.flags & REG_AUGMENTED)
3277 env.flags |= REG_EXTENDED;
3278 env.mappeddot = '.';
3279 env.mappednewline = '\n';
3280 env.mappedslash = '/';
3283 env.map = disc->re_map;
3284 env.MAP = p->env->fold;
3287 env.MAP[i] = fold[env.map[i]];
3288 if (env.map[i] == '.')
3289 env.mappeddot = i;
3290 if (env.map[i] == '\n')
3291 env.mappednewline = i;
3292 if (env.map[i] == '/')
3293 env.mappedslash = i;
3297 env.MAP = fold;
3298 env.type = (env.flags & REG_AUGMENTED) ? ARE : (env.flags & REG_EXTENDED) ? ERE : BRE;
3299 env.explicit = -1;
3300 if (env.flags & REG_SHELL)
3302 if (env.flags & REG_SHELL_PATH)
3303 env.explicit = env.mappedslash;
3304 if (!(env.flags & REG_SHELL_ESCAPED))
3305 env.flags |= REG_CLASS_ESCAPE;
3306 env.flags |= REG_LENIENT|REG_NULL;
3307 env.type = env.type == BRE ? SRE : KRE;
3310 env.flags &= ~(REG_SHELL_DOT|REG_SHELL_ESCAPED|REG_SHELL_GROUP|REG_SHELL_PATH);
3311 if ((env.flags & (REG_NEWLINE|REG_SPAN)) == REG_NEWLINE)
3312 env.explicit = env.mappednewline;
3313 p->env->leading = (env.flags & REG_SHELL_DOT) ? env.mappeddot : -1;
3314 env.posixkludge = !(env.flags & (REG_EXTENDED|REG_SHELL));
3315 env.token.lex = 0;
3316 env.token.push = 0;
3317 if (env.flags & REG_DELIMITED)
3319 switch (env.delimiter = *pattern++)
3325 env.error = REG_EDELIM;
3328 env.terminator = '\n';
3330 env.literal = env.pattern = env.cursor = (unsigned char*)pattern;
3331 if (!(p->env->rex = alt(&env, 1, 0)))
3333 if (env.parnest)
3335 env.error = REG_EPAREN;
3338 if ((env.flags & REG_LEFT) && p->env->rex->type != REX_BEG)
3340 if (p->env->rex->type == REX_ALT)
3341 env.flags &= ~REG_FIRST;
3342 if (!(e = node(&env, REX_BEG, 0, 0, 0)))
3347 e->next = p->env->rex;
3348 p->env->rex = e;
3349 p->env->once = 1;
3351 for (e = p->env->rex; e->next; e = e->next);
3352 p->env->done.type = REX_DONE;
3353 p->env->done.flags = e->flags;
3354 if ((env.flags & REG_RIGHT) && e->type != REX_END)
3356 if (p->env->rex->type == REX_ALT)
3357 env.flags &= ~REG_FIRST;
3358 if (!(f = node(&env, REX_END, 0, 0, 0)))
3367 if (stats(&env, p->env->rex))
3369 if (!env.error)
3370 env.error = REG_ECOUNT;
3373 if (env.stats.b)
3374 p->env->hard = p->env->separate = 1;
3375 else if (!(env.flags & REG_FIRST) && (env.stats.a || env.stats.c > 1 && env.stats.c != env.stats.s || env.stats.t && (env.stats.t > 1 || env.stats.a || env.stats.c)))
3376 p->env->hard = 1;
3377 if (p->env->hard || env.stats.c || env.stats.i)
3378 p->env->stats.re_min = p->env->stats.re_max = -1;
3381 if (!(p->env->stats.re_min = env.stats.m))
3382 p->env->stats.re_min = -1;
3383 if (!(p->env->stats.re_max = env.stats.n))
3384 p->env->stats.re_max = -1;
3386 if (special(&env, p))
3388 serialize(&env, p->env->rex, 1);
3389 p->re_nsub = env.stats.p;
3390 if (env.type == KRE)
3392 if (env.flags & REG_DELIMITED)
3394 p->re_npat = env.cursor - env.pattern + 1;
3395 if (*env.cursor == env.delimiter)
3397 else if (env.flags & REG_MUSTDELIM)
3399 env.error = REG_EDELIM;
3403 env.flags &= ~REG_DELIMITED;
3405 p->env->explicit = env.explicit;
3406 p->env->flags = env.flags & REG_COMP;
3407 p->env->min = env.stats.m;
3408 p->env->nsub = env.stats.p + env.stats.u;
3409 p->env->refs = 1;
3413 if (!env.error)
3414 env.error = REG_ESPACE;
3415 if (env.type >= SRE && env.error != REG_ESPACE && !(flags & REG_LITERAL))
3418 pattern = (const char*)env.literal;
3421 return fatal(disc, env.error, pattern);
3426 * the lazy way around adding and checking env.end
3455 Rex_t* e = p->env->rex;
3456 Rex_t* f = q->env->rex;
3459 Cenv_t env;
3462 return fatal(p->env->disc, REG_BADPAT, NiL);
3463 if (p->env->separate || q->env->separate)
3465 memset(&env, 0, sizeof(env));
3466 env.disc = p->env->disc;
3469 p->env->rex = e->next;
3471 drop(env.disc, e);
3472 e = p->env->rex;
3476 q->env->rex = f->next;
3478 drop(env.disc, f);
3479 f = q->env->rex;
3483 p->env->flags |= REG_LEFT;
3484 p->env->rex = e->next;
3486 drop(env.disc, e);
3487 e = p->env->rex;
3488 q->env->rex = f->next;
3490 drop(env.disc, f);
3491 f = q->env->rex;
3497 p->env->flags |= REG_RIGHT;
3498 drop(env.disc, g->next);
3500 drop(env.disc, h->next);
3503 if (!(g = trie(&env, f, e)))
3504 return fatal(p->env->disc, REG_BADPAT, NiL);
3505 p->env->rex = g;
3506 if (!q->env->once)
3507 p->env->once = 0;
3508 q->env->rex = 0;
3509 if (p->env->flags & REG_LEFT)
3511 if (!(e = node(&env, REX_BEG, 0, 0, 0)))
3514 return fatal(p->env->disc, REG_ESPACE, NiL);
3516 e->next = p->env->rex;
3517 p->env->rex = e;
3518 p->env->once = 1;
3520 if (p->env->flags & REG_RIGHT)
3522 for (f = p->env->rex; f->next; f = f->next);
3525 if (!(e = node(&env, REX_END, 0, 0, 0)))
3528 return fatal(p->env->disc, REG_ESPACE, NiL);
3533 env.explicit = p->env->explicit;
3534 env.flags = p->env->flags;
3535 env.disc = p->env->disc;
3536 if (stats(&env, p->env->rex))
3539 return fatal(p->env->disc, env.error ? env.error : REG_ECOUNT, NiL);
3541 if (special(&env, p))
3544 return fatal(p->env->disc, env.error ? env.error : REG_ESPACE, NiL);
3546 p->env->min = g->re.trie.min;
3561 p->env->refs++;