Lines Matching refs:env

142 node(Cenv_t* env, int type, int lo, int hi, size_t extra)
146 if (e = (Rex_t*)alloc(env->disc, 0, sizeof(Rex_t) + extra))
153 e->flags = env->flags;
154 e->map = (e->flags & REG_ICASE) ? env->MAP : env->map;
155 e->explicit = env->explicit;
262 serialize(Cenv_t* env, Rex_t* e, int n)
272 n = serialize(env, e->re.group.expr.binary.left, n);
275 n = serialize(env, e->re.group.expr.binary.right, n);
278 n = serialize(env, e->re.group.expr.binary.left, n);
279 n = serialize(env, e->re.group.expr.binary.right, n);
289 n = serialize(env, e->re.group.expr.rex, n);
301 cat(Cenv_t* env, Rex_t* e, Rex_t* f)
307 drop(env->disc, e);
312 drop(env->disc, e);
319 drop(env->disc, f);
341 drop(env->disc, f);
355 stats(register Cenv_t* env, register Rex_t* e)
377 x = env->stats.x;
378 l = env->stats.l;
379 y = env->stats.y;
380 k = env->stats.k;
381 t = env->stats.t;
382 if (++env->stats.a <= 0)
384 cm = env->stats.m;
385 env->stats.m = 0;
386 cn = env->stats.n;
387 env->stats.n = 0;
388 if (stats(env, e->re.group.expr.binary.left))
390 m = env->stats.m;
391 env->stats.m = 0;
392 n = env->stats.n;
393 env->stats.n = 0;
394 if (e->re.group.expr.binary.right && stats(env, e->re.group.expr.binary.right))
396 if (env->stats.m > m)
397 env->stats.m = m;
399 m = env->stats.m;
400 if ((env->stats.m += cm) < m)
402 if (env->stats.n < n)
403 env->stats.n = n;
405 n = env->stats.n;
406 if ((env->stats.n += cn) < n)
408 env->stats.x = x;
409 env->stats.l = l;
410 env->stats.y = y;
411 env->stats.k = k;
412 env->stats.t = t;
415 if (++env->stats.b <= 0)
422 n = env->stats.m;
423 if ((env->stats.m += e->lo) < n)
427 n = env->stats.n;
428 if ((env->stats.n += e->hi) < n)
433 if (++env->stats.c <= 0)
435 if (++env->stats.s <= 0)
440 cm = env->stats.m;
441 env->stats.m = 0;
442 cn = env->stats.n;
443 env->stats.n = 0;
444 if (stats(env, e->re.group.expr.binary.left))
446 nm = env->stats.m;
447 env->stats.m = 0;
448 nn = env->stats.n;
449 env->stats.n = 0;
450 if (stats(env, e->re.group.expr.binary.right))
452 if (env->stats.m < nm)
453 env->stats.m = nm;
455 nm = env->stats.m;
456 if ((env->stats.m += cm) < nm)
458 if (env->stats.n < nn)
459 env->stats.n = nn;
461 nn = env->stats.n;
462 if ((env->stats.n += cn) < nn)
466 env->stats.e = 1;
469 if (e->re.group.number && ++env->stats.p <= 0 || !e->re.group.number && ++env->stats.u <= 0)
471 if (stats(env, e->re.group.expr.rex))
478 m = env->stats.m;
479 n = env->stats.n;
480 x = env->stats.x;
481 y = env->stats.y;
482 if (stats(env, e->re.group.expr.rex))
484 env->stats.m = m;
485 env->stats.n = n;
486 env->stats.x = x;
487 env->stats.y = y;
492 if (++env->stats.u <= 0)
498 if (++env->stats.u <= 0)
500 m = env->stats.m;
501 n = env->stats.n;
502 x = env->stats.x;
503 y = env->stats.y;
504 if (e->re.group.size > 0 && ++env->stats.b <= 0)
506 if (e->re.group.expr.binary.left && stats(env, e->re.group.expr.binary.left))
510 if (q->re.group.expr.binary.left && stats(env, q->re.group.expr.binary.left))
512 if (q->re.group.expr.binary.right && stats(env, q->re.group.expr.binary.right))
515 env->stats.m = m;
516 env->stats.n = n;
517 env->stats.x = x;
518 env->stats.y = y;
521 if (++env->stats.u <= 0)
523 m = env->stats.m;
524 n = env->stats.n;
525 x = env->stats.x;
526 y = env->stats.y;
527 if (stats(env, e->re.group.expr.rex))
529 env->stats.m = m;
530 env->stats.n = n;
531 env->stats.x = x;
532 env->stats.y = y;
535 env->stats.i++;
536 x = env->stats.x;
537 l = env->stats.l;
538 y = env->stats.y;
539 k = env->stats.k;
540 t = env->stats.t;
541 cm = env->stats.m;
542 env->stats.m = 0;
543 if (stats(env, e->re.group.expr.rex))
545 env->stats.m = !env->stats.m;
546 if ((env->stats.m += cm) < cm)
548 env->stats.x = x;
549 env->stats.l = l;
550 env->stats.y = y;
551 env->stats.k = k;
552 env->stats.t = t;
555 x = env->stats.x;
556 l = env->stats.l;
557 y = env->stats.y;
558 k = env->stats.k;
559 t = env->stats.t;
560 if (++env->stats.c <= 0)
562 b = env->stats.b;
563 c = env->stats.c;
564 cm = env->stats.m;
565 env->stats.m = 0;
566 if (stats(env, e->re.group.expr.rex))
568 if (env->stats.m == 1 && b == env->stats.b && c == env->stats.c && ++env->stats.s <= 0)
572 env->stats.x = x;
573 env->stats.l = l;
574 env->stats.y = y;
575 env->stats.k = k;
576 env->stats.t = t;
577 env->stats.m = cm;
581 m = env->stats.m;
582 if ((env->stats.m *= e->lo) > 0 && env->stats.m < m)
584 m = env->stats.m;
585 if ((env->stats.m += cm) < m)
587 if (env->stats.x != x)
588 env->stats.l = cm;
589 if (env->stats.y != y)
590 env->stats.k = cm;
596 cm = env->stats.m;
597 if ((env->stats.m += e->re.string.size) < cm)
599 cn = env->stats.n;
600 if ((env->stats.n += e->re.string.size) < cn)
602 if (!env->stats.x || env->stats.x->re.string.size < e->re.string.size)
604 env->stats.x = e;
605 env->stats.l = cm;
610 if (++env->stats.s <= 0)
612 cm = env->stats.m;
613 if ((env->stats.m += e->re.trie.min) < cm)
615 cn = env->stats.n;
616 if ((env->stats.n += e->re.trie.max) < cn)
618 env->stats.t++;
619 if (!env->stats.y || env->stats.y->re.trie.min < e->re.trie.min)
621 env->stats.y = e;
622 env->stats.k = cm;
633 magic(register Cenv_t* env, register int c, int escaped)
638 int e = env->error;
639 int l = env->token.len;
645 c = mp[env->type+escaped];
648 sp = (char*)env->cursor + env->token.len;
658 env->error = REG_BADBR;
665 if (env->type < SRE || *sp != ',')
667 env->error = *sp ? REG_BADBR : REG_EBRACE;
673 env->error = REG_BADBR;
676 env->token.min = n;
685 env->error = REG_BADBR;
692 else if (n < env->token.min)
694 env->error = REG_BADBR;
698 env->token.max = n;
702 env->error = REG_EBRACE;
707 env->error = REG_BADBR;
715 env->error = REG_BADBR;
723 env->error = REG_EBRACE;
728 env->error = REG_BADBR;
731 env->token.len = sp - (char*)env->cursor;
734 env->error = REG_EBRACE;
737 if (env->type < SRE && *sp == '?')
739 env->token.len++;
740 env->token.lex = 0;
748 env->token.len += ep - sp;
751 env->token.lex = c;
766 env->token.len += ep - sp;
772 if (env->type == SRE || c == T_BACK && !(env->flags & REG_LENIENT))
774 env->error = REG_BADESC;
777 if ((env->flags & REG_MULTIREF) && isdigit(*sp))
780 if (c > 0 && c <= env->parno && env->paren[c])
784 env->token.len++;
790 if (escaped == 1 && (env->flags & REG_LENIENT) && (c = mp[env->type+escaped+2]) >= T_META)
794 if (env->type >= SRE)
800 if (env->type == KRE && *(env->cursor + env->token.len) == '-' && *(env->cursor + env->token.len + 1) == '(')
802 env->token.len++;
803 env->token.att = 1;
805 if (env->type == KRE && *(env->cursor + env->token.len) == '(')
807 env->token.len++;
813 env->token.lex = c;
816 env->token.lex = 0;
819 env->token.lex = c;
831 env->token.len = l;
837 c = (c > env->parno || !env->paren[c]) ? o : T_BACK + c;
839 else if (env->type == KRE && !env->parnest && (env->flags & REG_SHELL_GROUP))
853 if (env->type >= SRE && !(env->flags & REG_SHELL_ESCAPED) || (env->flags & REG_ESCAPE) && (c == '[' || c == '-' || c == ']' || env->delimiter && c == env->delimiter))
857 env->error = REG_BADESC;
861 else if (escaped && !(env->flags & REG_LENIENT) && c != ']')
863 env->error = REG_BADESC;
868 sp = (char*)env->cursor + env->token.len;
879 env->error = REG_EPAREN;
892 env->cursor = (unsigned char*)sp;
893 return token(env);
896 env->error = e;
897 else if (env->flags & REG_LENIENT)
899 else if (escaped == 1 && !env->error)
903 env->error = REG_BADESC;
909 token(register Cenv_t* env)
914 if (env->token.push)
915 return env->token.lex;
916 env->token.att = env->token.esc = 0;
917 if ((env->token.len = MBSIZE(env->cursor)) > 1)
918 return env->token.lex = C_MB;
919 env->token.lex = 0;
922 c = *env->cursor;
923 if (c == 0 || c == env->delimiter || c == env->terminator)
925 if (!(env->flags & REG_COMMENT))
931 c = *++env->cursor;
932 if (c == 0 || c == env->delimiter)
938 env->cursor++;
940 if (c == '\n' && (env->flags & REG_MULTIPLE) && !env->delimiter)
942 if (env->parnest)
944 env->error = REG_EPAREN;
947 env->parno = 0;
948 env->pattern = env->cursor + 1;
951 if (env->flags & REG_LITERAL)
953 if (posixkludge = env->posixkludge)
955 env->posixkludge = 0;
961 if (env->flags & REG_SHELL_ESCAPED)
963 if (!(c = *(env->cursor + 1)) || c == env->terminator)
965 if (env->flags & REG_LENIENT)
969 env->token.esc = env->token.len;
970 env->token.len += MBSIZE(env->cursor + 1);
975 env->error = REG_EESCAPE;
978 env->token.esc = env->token.len;
979 env->token.len += MBSIZE(env->cursor + 1);
980 if (env->delimiter && c == 'n')
982 else if (c == env->delimiter)
983 return magic(env, c, 0);
984 else if (c == '(' && env->type == BRE)
985 env->posixkludge = 1;
986 else if (c == ')' && env->type == BRE && env->parnest <= 0)
988 env->error = REG_EPAREN;
991 else if (isspace(c) && (env->flags & REG_COMMENT))
993 return magic(env, c, 1);
997 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')
1002 if (env->type == BRE && (env->cursor == env->pattern || posixkludge == 1))
1004 env->posixkludge = 2;
1010 if (env->type != BRE && env->parnest <= 0)
1013 else if (c == '/' && env->explicit == env->mappedslash)
1015 while (*(env->cursor + env->token.len) == c)
1016 env->token.len++;
1019 return magic(env, c, 0);
1160 bra(Cenv_t* env)
1183 if (!(e = node(env, REX_CLASS, 1, 1, sizeof(Set_t))))
1186 if (*env->cursor == '^' || env->type >= SRE && *env->cursor == '!')
1188 env->cursor++;
1193 first = env->cursor;
1195 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))
1197 begin = env->cursor + MBSIZE(env->cursor);
1206 if (!(c = *env->cursor) || c == env->terminator || c == env->delimiter && (env->flags & REG_ESCAPE))
1208 env->cursor += (w = MBSIZE(env->cursor));
1209 if (c == '\\' && ((env->flags & REG_CLASS_ESCAPE) || env->type >= SRE && env->parnest || *env->cursor == env->delimiter && (env->flags & REG_ESCAPE)))
1211 if (*env->cursor)
1213 if (*env->cursor == 'n')
1215 env->cursor++;
1218 else if (env->type < SRE || !(env->flags & REG_SHELL_ESCAPED))
1220 env->token.len = 1;
1221 w = magic(env, *env->cursor, 2);
1222 if (env->token.len > 1 || w != T_BAD)
1224 if (env->token.len == 1 && (f = classfun(w)))
1228 if (env->type < SRE && !(env->flags & REG_LENIENT))
1232 env->cursor++;
1240 if (env->token.len > 1 || w >= 0 && w < T_META)
1245 if (env->type < SRE && !(env->flags & REG_LENIENT) && !mbwide())
1249 env->cursor += env->token.len;
1257 if (env->cursor == begin)
1277 if (!inrange && env->cursor != begin && *env->cursor != ']')
1279 if (env->type < SRE && !(env->flags & REG_LENIENT))
1292 switch (*env->cursor)
1297 if (env->regexp)
1304 if (!(f = regclass((char*)env->cursor, (char**)&env->cursor)))
1306 if (env->cursor == start && (c = *(env->cursor + 1)))
1308 s = start = env->cursor + 1;
1328 env->cursor = s + 3;
1329 drop(env->disc, e);
1330 return node(env, i, 0, 0, 0);
1335 env->error = REG_ECTYPE;
1346 if (env->regexp)
1355 if ((c = regcollate((char*)env->cursor, (char**)&env->cursor, (char*)buf, sizeof(buf))) < 0)
1367 if (env->regexp)
1369 if ((c = regcollate((char*)env->cursor, (char**)&env->cursor, (char*)buf, sizeof(buf))) < 0)
1378 if (*env->cursor == env->terminator || *env->cursor == env->delimiter && (env->flags & REG_ESCAPE))
1391 inrange = env->type >= SRE || (env->flags & REG_LENIENT);
1394 else if (env->type >= SRE)
1401 else if (!(env->flags & REG_LENIENT))
1456 drop(env->disc, e);
1462 drop(env->disc, e);
1463 if (ic = env->flags & REG_ICASE)
1465 if (!(e = node(env, REX_COLL_CLASS, 1, 1, (elements + 2) * sizeof(Celt_t))))
1470 env->cursor = first;
1474 if ((c = *env->cursor) == 0 || c == env->terminator || (env->flags & REG_ESCAPE) && c == env->delimiter)
1476 pp = env->cursor;
1477 env->cursor += (w = MBSIZE(env->cursor));
1478 if (c == '\\' && ((env->flags & REG_CLASS_ESCAPE) || env->type >= SRE && env->parnest || *env->cursor == env->delimiter && (env->flags & REG_ESCAPE)))
1480 if (*env->cursor)
1482 if (*env->cursor == 'n')
1484 pp = env->cursor++;
1487 else if (env->type < SRE || !(env->flags & REG_SHELL_ESCAPED))
1489 env->token.len = 1;
1490 w = magic(env, *env->cursor, 2);
1491 if (env->token.len > 1 || w != T_BAD)
1493 if (env->token.len == 1 && (f = classfun(w)))
1497 if (env->type < SRE && !(env->flags & REG_LENIENT))
1501 env->cursor++;
1507 if (env->token.len > 1 || w >= 0 && w < T_META)
1512 env->cursor += env->token.len;
1520 if (env->cursor == begin)
1537 if (!inrange && env->cursor != begin && *env->cursor != ']')
1539 if (env->type < SRE && !(env->flags & REG_LENIENT))
1551 switch (*env->cursor)
1556 if (env->regexp)
1560 if (!(f = regclass((char*)env->cursor, (char**)&env->cursor)))
1562 if (env->cursor == start && (c = *(env->cursor + 1)) && *(env->cursor + 2) == ':' && *(env->cursor + 3) == ']' && *(env->cursor + 4) == ']')
1578 env->cursor += 5;
1579 drop(env->disc, e);
1580 return node(env, i, 0, 0, 0);
1583 env->error = REG_ECTYPE;
1592 if (env->regexp)
1599 rp = env->cursor + 1;
1600 if ((rw = regcollate((char*)env->cursor, (char**)&env->cursor, (char*)pp, COLL_KEY_MAX)) < 0)
1654 if (env->regexp)
1657 if ((w = regcollate((char*)env->cursor, (char**)&env->cursor, (char*)pp, COLL_KEY_MAX)) < 0)
1663 if (*env->cursor == env->terminator || *env->cursor == env->delimiter && (env->flags & REG_ESCAPE))
1673 if (env->type < SRE && !(env->flags & REG_LENIENT))
1680 inrange = env->type >= SRE || (env->flags & REG_LENIENT);
1697 if (env->flags & REG_ICASE)
1713 if (env->explicit >= 0)
1714 setclr(e->re.charclass, env->explicit);
1718 env->error = REG_ECOLLATE;
1721 env->error = REG_ERANGE;
1723 drop(env->disc, e);
1724 if (!env->error)
1725 env->error = REG_EBRACK;
1730 ccl(Cenv_t* env, int type)
1739 env->error = REG_BADESC;
1744 if (!(e = node(env, REX_CLASS, 1, 1, sizeof(Set_t))))
1749 if (env->explicit >= 0)
1750 setclr(e->re.charclass, env->explicit);
1754 if (!(e = node(env, REX_COLL_CLASS, 1, 1, 2 * sizeof(Celt_t))))
1768 rep(Cenv_t* env, Rex_t* e, int number, int last)
1777 switch (token(env))
1780 eat(env);
1781 if (!(f = node(env, REX_NEG, m, n, 0)))
1783 drop(env->disc, e);
1789 eat(env);
1793 eat(env);
1796 eat(env);
1800 eat(env);
1801 m = env->token.min;
1802 n = env->token.max;
1807 if (env->token.att)
1809 else if (env->type < SRE)
1810 switch (token(env))
1813 eat(env);
1814 minimal = !(env->flags & REG_MINIMAL);
1817 eat(env);
1818 minimal = !!(env->flags & REG_MINIMAL);
1842 env->error = REG_BADRPT;
1843 drop(env->disc, e);
1852 if (!(f = node(env, REX_REP, m, n, 0)))
1854 drop(env->disc, e);
1885 trienode(Cenv_t* env, int c)
1889 if (t = (Trie_node_t*)alloc(env->disc, 0, sizeof(Trie_node_t)))
1898 insert(Cenv_t* env, Rex_t* f, Rex_t* g)
1919 if (!(t = g->re.trie.root[*s]) && !(t = g->re.trie.root[*s] = trienode(env, *s)))
1927 if (!t->son && !(t->son = trienode(env, *s)))
1934 if (!t->sib && !(t->sib = trienode(env, *s)))
1954 trie(Cenv_t* env, Rex_t* e, Rex_t* f)
1962 if (!(g = node(env, REX_TRIE, 0, 0, (UCHAR_MAX + 1) * sizeof(Trie_node_t*))))
1965 if (insert(env, f, g))
1967 drop(env->disc, f);
1973 if (insert(env, e, g))
1975 drop(env->disc, e);
1979 drop(env->disc, g);
1986 chr(register Cenv_t* env, int* escaped)
1992 if (!(c = *env->cursor))
1994 env->cursor++;
1997 if (env->flags & REG_SHELL_ESCAPED)
1999 if (!(c = *(env->cursor + 1)) || c == env->terminator)
2001 if (env->flags & REG_LENIENT)
2003 env->error = REG_EESCAPE;
2006 p = env->cursor;
2007 c = chresc((char*)env->cursor - 1, (char**)&env->cursor);
2008 *escaped = env->cursor - p;
2018 grp(Cenv_t* env, int parno)
2031 beg = env->pattern == env->cursor - env->token.len;
2032 if (!(c = env->token.lex) && (c = *env->cursor))
2033 env->cursor++;
2034 env->token.len = 0;
2035 env->parnest++;
2068 env->token.push = 1;
2074 if (!(env->flags & REG_LITERAL))
2076 env->error = REG_BADRPT;
2085 eat(env);
2086 if (token(env) == T_CLOSE)
2097 env->flags |= (REG_LEFT|REG_RIGHT);
2099 env->flags &= ~(REG_LEFT|REG_RIGHT);
2103 env->flags &= ~REG_MINIMAL;
2105 env->flags |= REG_MINIMAL;
2109 env->flags |= REG_ICASE;
2111 env->flags &= ~REG_ICASE;
2115 env->flags |= REG_LEFT;
2117 env->flags &= ~REG_LEFT;
2121 env->flags |= REG_NEWLINE;
2123 env->flags &= ~REG_NEWLINE;
2124 env->explicit = (env->flags & (REG_NEWLINE|REG_SPAN)) == REG_NEWLINE ? env->mappednewline : -1;
2128 env->flags &= ~REG_LENIENT;
2130 env->flags |= REG_LENIENT;
2134 env->flags |= REG_RIGHT;
2136 env->flags &= ~REG_RIGHT;
2140 env->flags |= REG_SPAN;
2142 env->flags &= ~REG_SPAN;
2143 env->explicit = (env->flags & (REG_NEWLINE|REG_SPAN)) == REG_NEWLINE ? env->mappednewline : -1;
2147 env->flags |= REG_COMMENT;
2149 env->flags &= ~REG_COMMENT;
2152 env->flags &= ~(REG_AUGMENTED|REG_EXTENDED|REG_LITERAL|REG_SHELL|REG_LEFT|REG_RIGHT|REG_CLASS_ESCAPE);
2153 env->flags |= REG_AUGMENTED|REG_EXTENDED;
2158 env->flags &= ~(REG_AUGMENTED|REG_EXTENDED|REG_LITERAL|REG_SHELL|REG_LEFT|REG_RIGHT|REG_CLASS_ESCAPE);
2162 env->flags &= ~(REG_AUGMENTED|REG_EXTENDED|REG_LITERAL|REG_SHELL|REG_LEFT|REG_RIGHT|REG_CLASS_ESCAPE);
2163 env->flags |= REG_EXTENDED;
2168 env->flags &= ~(REG_AUGMENTED|REG_EXTENDED|REG_LITERAL|REG_SHELL|REG_LEFT|REG_RIGHT|REG_CLASS_ESCAPE);
2169 env->flags |= REG_LITERAL;
2173 env->flags &= ~(REG_AUGMENTED|REG_EXTENDED|REG_LITERAL|REG_SHELL|REG_LEFT|REG_RIGHT|REG_CLASS_ESCAPE);
2174 env->flags |= REG_AUGMENTED|REG_SHELL|REG_LEFT|REG_RIGHT;
2187 env->flags &= ~(REG_AUGMENTED|REG_EXTENDED|REG_LITERAL|REG_SHELL|REG_LEFT|REG_RIGHT|REG_CLASS_ESCAPE);
2188 env->flags |= REG_EXTENDED|REG_CLASS_ESCAPE;
2192 env->flags &= ~(REG_AUGMENTED|REG_EXTENDED|REG_LITERAL|REG_SHELL|REG_LEFT|REG_RIGHT|REG_CLASS_ESCAPE);
2193 env->flags |= REG_SHELL|REG_LEFT|REG_RIGHT;
2198 env->flags |= REG_MINIMAL;
2200 env->flags &= ~REG_MINIMAL;
2205 env->error = REG_BADRPT;
2208 eat(env);
2209 c = token(env);
2223 switch (token(env))
2233 env->error = REG_BADRPT;
2236 eat(env);
2243 e = node(env, REX_NEST, 0, 0, (UCHAR_MAX + 1) * sizeof(unsigned short));
2244 e->re.nest.primary = isalnum(*env->cursor) ? -1 : *env->cursor;
2248 switch (i = chr(env, &esc))
2253 env->cursor -= esc + 1;
2254 env->error = REG_EPAREN;
2260 if ((i = chr(env, &esc)) < 0)
2273 switch (i = chr(env, &esc))
2286 if ((i = chr(env, &esc)) < 0)
2316 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)))
2331 env->parnest--;
2335 parno = ++env->parno;
2336 if (!(f = node(env, REX_GROUP, 0, 0, 0)))
2338 drop(env->disc, e);
2341 if (parno < elementsof(env->paren))
2342 env->paren[parno] = f;
2351 if (isdigit(*env->cursor))
2358 env->error = REG_BADRPT;
2361 c = c * 10 + (*env->cursor++ - '0');
2362 } while (isdigit(*env->cursor));
2363 if (*env->cursor++ != ')')
2365 env->error = REG_BADRPT;
2368 if (c && env->type >= SRE)
2370 if (!c || c > env->parno || !env->paren[c])
2372 if (!(env->flags & REG_LENIENT))
2374 env->error = REG_ESUBREG;
2383 if (env->type < SRE && *env->cursor++ != '?')
2385 env->error = REG_BADRPT;
2388 if (!(f = grp(env, parno + 1)) && env->error)
2391 if (!(e = node(env, REX_GROUP_COND, 0, 0, 0)))
2393 drop(env->disc, f);
2398 if (!(e->re.group.expr.binary.right = alt(env, parno, 1)))
2400 drop(env->disc, e);
2403 if (token(env) != T_CLOSE)
2405 env->error = REG_EPAREN;
2408 eat(env);
2409 env->parnest--;
2410 return rep(env, e, parno, parno);
2412 p = env->cursor;
2414 while (c = *env->cursor)
2416 if (c == '\\' && *(env->cursor + 1))
2417 env->cursor++;
2422 else if (c == env->delimiter || c == env->terminator)
2424 env->cursor++;
2428 env->error = REG_EBRACE;
2431 if (*++env->cursor != ')')
2433 env->error = REG_EPAREN;
2436 env->cursor++;
2437 env->parnest--;
2438 if (env->disc->re_version < REG_VERSION_EXEC)
2440 env->error = REG_BADRPT;
2443 if (!env->disc->re_execf)
2445 if (!(e = node(env, REX_EXEC, 0, 0, 0)))
2448 e->re.exec.size = env->cursor - p - 2;
2449 if (!env->disc->re_compf)
2452 e->re.exec.data = (*env->disc->re_compf)(env->regex, e->re.exec.text, e->re.exec.size, env->disc);
2457 while (isdigit(*env->cursor))
2461 env->error = REG_ESUBREG;
2464 c = c * 10 + *env->cursor++ - '0';
2466 if (*env->cursor == ')')
2468 env->cursor++;
2469 env->parnest--;
2470 env->token.len = 1;
2471 if (c > env->parno || !env->paren[c])
2473 env->error = REG_ESUBREG;
2476 env->paren[c]->re.group.back = 1;
2477 return rep(env, node(env, REX_BACK, c, 0, 0), 0, 0);
2481 env->error = REG_BADRPT;
2484 if (x && !(e = alt(env, parno, 0)))
2486 c = token(env);
2487 env->parnest--;
2488 if (c != T_CLOSE && (!(env->flags & REG_LITERAL) || c != ')'))
2490 env->error = REG_EPAREN;
2493 eat(env);
2497 env->pattern = env->cursor;
2498 env->type = typ;
2502 if (!(f = node(env, x, 0, 0, 0)))
2504 drop(env->disc, e);
2510 if (stats(env, e))
2512 drop(env->disc, f);
2513 if (!env->error)
2514 env->error = REG_ECOUNT;
2517 f->re.group.size = env->stats.m;
2518 memset(&env->stats, 0, sizeof(env->stats));
2524 f = rep(env, f, parno, env->parno);
2531 seq(Cenv_t* env)
2552 while ((c = token(env)) < T_META && s < &buf[sizeof(buf) - env->token.len])
2555 p = env->cursor;
2559 *s++ = (env->flags & REG_ICASE) ? toupper(c) : c;
2561 else if (c == C_ESC || (env->flags & REG_ICASE))
2563 c = (c == C_ESC) ? env->token.lex : mbchar(p);
2564 if (env->flags & REG_ICASE)
2580 n = env->token.len - env->token.esc;
2583 eat(env);
2600 if (!(e = node(env, REX_STRING, 0, 0, i)))
2607 if (!(f = node(env, REX_ONECHAR, 1, 1, 0)))
2609 drop(env->disc, e);
2612 f->re.onechar = (env->flags & REG_ICASE) ? toupper(x) : x;
2616 if (!(f = node(env, REX_STRING, 0, 0, n)))
2621 if (!(f = rep(env, f, 0, 0)) || !(f = cat(env, f, seq(env))))
2623 drop(env->disc, e);
2627 f = cat(env, e, f);
2631 if (!(e = node(env, REX_STRING, 0, 0, c)))
2635 return cat(env, e, seq(env));
2639 eat(env);
2641 if (c > env->parno || !env->paren[c])
2643 env->error = REG_ESUBREG;
2646 env->paren[c]->re.group.back = 1;
2647 e = rep(env, node(env, REX_BACK, c, 0, 0), 0, 0);
2656 return node(env, REX_NULL, 0, 0, 0);
2658 eat(env);
2659 e = rep(env, node(env, REX_END, 0, 0, 0), 0, 0);
2662 eat(env);
2663 if ((e = node(env, REX_BEG, 0, 0, 0)) && (env->flags & REG_EXTENDED))
2664 e = rep(env, e, 0, 0);
2667 tok = env->token;
2668 eat(env);
2669 flags = env->flags;
2670 type = env->type;
2671 if (env->token.att)
2672 env->flags |= REG_MINIMAL;
2673 env->parnest++;
2674 if (env->type == KRE)
2675 ++env->parno;
2676 parno = ++env->parno;
2677 if (!(e = alt(env, parno + 1, 0)))
2679 if (e->type == REX_NULL && env->type == ERE && !(env->flags & REG_NULL))
2681 drop(env->disc, e);
2682 env->error = (*env->cursor == 0 || *env->cursor == env->delimiter || *env->cursor == env->terminator) ? REG_EPAREN : REG_ENULL;
2685 if (token(env) != T_CLOSE)
2687 drop(env->disc, e);
2688 env->error = REG_EPAREN;
2691 env->parnest--;
2692 eat(env);
2693 if (!(f = node(env, REX_GROUP, 0, 0, 0)))
2695 drop(env->disc, e);
2698 if (parno < elementsof(env->paren))
2699 env->paren[parno] = f;
2706 env->token = tok;
2708 if (!(e = rep(env, f, parno, env->parno)))
2710 if (env->type == KRE)
2712 if (!(f = node(env, REX_GROUP, 0, 0, 0)))
2714 drop(env->disc, e);
2717 if (--parno < elementsof(env->paren))
2718 env->paren[parno] = f;
2724 env->flags = flags;
2725 env->type = type;
2728 p = env->cursor;
2729 eat(env);
2730 flags = env->flags;
2731 type = env->type;
2732 if (!(e = grp(env, env->parno + 1)))
2734 if (env->error)
2736 if (env->literal == env->pattern && env->literal == p)
2737 env->literal = env->cursor;
2740 env->flags = flags;
2741 env->type = type;
2744 eat(env);
2745 if (e = bra(env))
2746 e = rep(env, e, 0, 0);
2754 eat(env);
2755 if (e = ccl(env, c))
2756 e = rep(env, e, 0, 0);
2759 eat(env);
2760 e = rep(env, node(env, REX_WBEG, 0, 0, 0), 0, 0);
2763 eat(env);
2764 e = rep(env, node(env, REX_WEND, 0, 0, 0), 0, 0);
2767 eat(env);
2768 e = rep(env, node(env, REX_DOT, 1, 1, 0), 0, 0);
2771 eat(env);
2772 env->token.lex = T_STAR;
2773 env->token.push = 1;
2774 e = rep(env, node(env, REX_DOT, 1, 1, 0), 0, 0);
2777 eat(env);
2778 env->token.lex = T_PLUS;
2779 env->token.push = 1;
2780 if (e = node(env, REX_ONECHAR, 1, 1, 0))
2783 e = rep(env, e, 0, 0);
2787 eat(env);
2788 e = rep(env, node(env, REX_WORD, 0, 0, 0), 0, 0);
2791 eat(env);
2792 e = rep(env, node(env, REX_WORD_NOT, 0, 0, 0), 0, 0);
2795 eat(env);
2796 e = rep(env, node(env, REX_BEG_STR, 0, 0, 0), 0, 0);
2799 eat(env);
2800 e = rep(env, node(env, REX_END_STR, 0, 0, 0), 0, 0);
2803 eat(env);
2804 e = rep(env, node(env, REX_FIN_STR, 0, 0, 0), 0, 0);
2807 env->error = REG_BADRPT;
2810 if (e && *env->cursor != 0 && *env->cursor != env->delimiter && *env->cursor != env->terminator)
2811 e = cat(env, e, seq(env));
2817 con(Cenv_t* env)
2823 if (!(e = seq(env)) || !(env->flags & REG_AUGMENTED) || token(env) != T_AND)
2825 eat(env);
2826 if (!(f = con(env)))
2828 drop(env->disc, e);
2831 if (!(g = node(env, REX_CONJ, 0, 0, 0)))
2833 drop(env->disc, e);
2834 drop(env->disc, f);
2843 alt(Cenv_t* env, int number, int cond)
2849 if (!(e = con(env)))
2851 else if (token(env) != T_BAR)
2861 eat(env);
2862 if (!(f = alt(env, number, 0)))
2864 drop(env->disc, e);
2867 if ((e->type == REX_NULL || f->type == REX_NULL) && !(env->flags & REG_NULL))
2869 if (!cond && (g = trie(env, e, f)))
2872 if (!(g = node(env, REX_ALT, 0, 0, 0)))
2874 env->error = REG_ESPACE;
2878 g->re.group.last = env->parno;
2883 drop(env->disc, e);
2884 drop(env->disc, f);
2885 if (!env->error)
2886 env->error = REG_ENULL;
2895 bmstr(Cenv_t* env, register Rex_t* a, unsigned char* v, int n, Bm_mask_t b)
2929 bmtrie(Cenv_t* env, Rex_t* a, unsigned char* v, Trie_node_t* x, int n, int m, Bm_mask_t b)
2936 bmstr(env, a, v, n, b);
2946 b = bmtrie(env, a, v, x->son, n, m + 1, b);
2960 special(Cenv_t* env, regex_t* p)
2974 if (e = p->env->rex)
2976 if ((x = env->stats.x) && x->re.string.size < 3)
2978 if ((t = env->stats.y) && t->re.trie.min < 3)
3001 l = env->stats.l;
3008 l = env->stats.k;
3010 if (!(q = (size_t*)alloc(env->disc, 0, (n + 1) * sizeof(size_t))))
3012 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))))
3014 alloc(env->disc, q, 0);
3022 a->re.bm.right = env->stats.m - l - n;
3023 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;
3029 a->re.bm.skip[0] = a->re.bm.skip[env->mappednewline] = (y->next && y->next->type == REX_END) ? HIT : (n + a->re.bm.left);
3039 bmstr(env, a, x->re.string.base, n, 1);
3047 m = bmtrie(env, a, v, t->re.trie.root[i], n, 0, m);
3117 alloc(env->disc, q, 0);
3119 p->env->rex = a;
3125 if (env->flags & REG_NEWLINE)
3129 if (env->stats.b)
3140 if (env->flags & REG_NULL)
3142 env->error = REG_ENULL;
3145 if ((env->flags & (REG_LEFT|REG_LITERAL|REG_RIGHT)) || e->map)
3149 if (!(a = node(env, REX_KMP, 0, 0, n * (sizeof(int*) + 1))))
3167 p->env->rex = a;
3169 drop(env->disc, e);
3175 p->env->once = 1;
3187 Cenv_t env;
3200 p->env = 0;
3218 if (!(p->env = (Env_t*)alloc(disc, 0, sizeof(Env_t))))
3220 memset(p->env, 0, sizeof(*p->env));
3221 memset(&env, 0, sizeof(env));
3222 env.regex = p;
3223 env.flags = flags;
3224 env.disc = p->env->disc = disc;
3225 if (env.flags & REG_AUGMENTED)
3226 env.flags |= REG_EXTENDED;
3227 env.mappeddot = '.';
3228 env.mappednewline = '\n';
3229 env.mappedslash = '/';
3232 env.map = disc->re_map;
3233 env.MAP = p->env->fold;
3236 env.MAP[i] = fold[env.map[i]];
3237 if (env.map[i] == '.')
3238 env.mappeddot = i;
3239 if (env.map[i] == '\n')
3240 env.mappednewline = i;
3241 if (env.map[i] == '/')
3242 env.mappedslash = i;
3246 env.MAP = fold;
3247 env.type = (env.flags & REG_AUGMENTED) ? ARE : (env.flags & REG_EXTENDED) ? ERE : BRE;
3248 env.explicit = -1;
3249 if (env.flags & REG_SHELL)
3251 if (env.flags & REG_SHELL_PATH)
3252 env.explicit = env.mappedslash;
3253 env.flags |= REG_LENIENT|REG_NULL;
3254 env.type = env.type == BRE ? SRE : KRE;
3256 if ((env.flags & (REG_NEWLINE|REG_SPAN)) == REG_NEWLINE)
3257 env.explicit = env.mappednewline;
3258 p->env->leading = (env.flags & REG_SHELL_DOT) ? env.mappeddot : -1;
3259 env.posixkludge = !(env.flags & (REG_EXTENDED|REG_SHELL));
3260 env.regexp = !!(env.flags & REG_REGEXP);
3261 env.token.lex = 0;
3262 env.token.push = 0;
3263 if (env.flags & REG_DELIMITED)
3265 switch (env.delimiter = *pattern++)
3271 env.error = REG_EDELIM;
3274 env.terminator = '\n';
3276 env.literal = env.pattern = env.cursor = (unsigned char*)pattern;
3277 if (!(p->env->rex = alt(&env, 1, 0)))
3279 if (env.parnest)
3281 env.error = REG_EPAREN;
3284 p->env->stats.re_flags = env.flags & (REG_EXTENDED|REG_AUGMENTED|REG_SHELL);
3285 if (env.flags & REG_LEFT)
3287 if (p->env->rex->type != REX_BEG)
3289 if (p->env->rex->type == REX_ALT)
3290 env.flags &= ~REG_FIRST;
3291 if (!(e = node(&env, REX_BEG, 0, 0, 0)))
3296 e->next = p->env->rex;
3297 p->env->rex = e;
3298 p->env->once = 1;
3300 p->env->stats.re_flags |= REG_LEFT;
3302 for (e = p->env->rex; e->next; e = e->next);
3303 p->env->done.type = REX_DONE;
3304 p->env->done.flags = e->flags;
3305 if (env.flags & REG_RIGHT)
3309 if (p->env->rex->type == REX_ALT)
3310 env.flags &= ~REG_FIRST;
3311 if (!(f = node(&env, REX_END, 0, 0, 0)))
3320 p->env->stats.re_flags |= REG_RIGHT;
3322 if (stats(&env, p->env->rex))
3324 if (!env.error)
3325 env.error = REG_ECOUNT;
3328 if (env.stats.b)
3329 p->env->hard = p->env->separate = 1;
3330 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)))
3331 p->env->hard = 1;
3332 if (p->env->hard || env.stats.c || env.stats.i)
3333 p->env->stats.re_min = p->env->stats.re_max = -1;
3336 if (!(p->env->stats.re_min = env.stats.m))
3337 p->env->stats.re_min = -1;
3338 if (!(p->env->stats.re_max = env.stats.n))
3339 p->env->stats.re_max = -1;
3341 if (special(&env, p))
3343 serialize(&env, p->env->rex, 1);
3344 p->re_nsub = env.stats.p;
3345 if (env.type == KRE)
3347 if (env.flags & REG_DELIMITED)
3349 p->re_npat = env.cursor - env.pattern + 1;
3350 if (*env.cursor == env.delimiter)
3352 else if (env.flags & REG_MUSTDELIM)
3354 env.error = REG_EDELIM;
3358 env.flags &= ~REG_DELIMITED;
3360 p->env->explicit = env.explicit;
3361 p->env->flags = env.flags & REG_COMP;
3362 p->env->min = env.stats.m;
3363 p->env->nsub = env.stats.p + env.stats.u;
3364 p->env->refs = 1;
3368 if (!env.error)
3369 env.error = REG_ESPACE;
3370 if (env.type >= SRE && env.error != REG_ESPACE && !(flags & REG_LITERAL))
3373 pattern = (const char*)env.literal;
3376 return fatal(disc, env.error, pattern);
3381 * the lazy way around adding and checking env.end
3410 Rex_t* e = p->env->rex;
3411 Rex_t* f = q->env->rex;
3414 Cenv_t env;
3417 return fatal(p->env->disc, REG_BADPAT, NiL);
3418 if (p->env->separate || q->env->separate)
3420 memset(&env, 0, sizeof(env));
3421 env.disc = p->env->disc;
3424 p->env->rex = e->next;
3426 drop(env.disc, e);
3427 e = p->env->rex;
3431 q->env->rex = f->next;
3433 drop(env.disc, f);
3434 f = q->env->rex;
3438 p->env->flags |= REG_LEFT;
3439 p->env->rex = e->next;
3441 drop(env.disc, e);
3442 e = p->env->rex;
3443 q->env->rex = f->next;
3445 drop(env.disc, f);
3446 f = q->env->rex;
3452 p->env->flags |= REG_RIGHT;
3453 drop(env.disc, g->next);
3455 drop(env.disc, h->next);
3458 if (!(g = trie(&env, f, e)))
3459 return fatal(p->env->disc, REG_BADPAT, NiL);
3460 p->env->rex = g;
3461 if (!q->env->once)
3462 p->env->once = 0;
3463 q->env->rex = 0;
3464 if (p->env->flags & REG_LEFT)
3466 if (!(e = node(&env, REX_BEG, 0, 0, 0)))
3469 return fatal(p->env->disc, REG_ESPACE, NiL);
3471 e->next = p->env->rex;
3472 p->env->rex = e;
3473 p->env->once = 1;
3475 if (p->env->flags & REG_RIGHT)
3477 for (f = p->env->rex; f->next; f = f->next);
3480 if (!(e = node(&env, REX_END, 0, 0, 0)))
3483 return fatal(p->env->disc, REG_ESPACE, NiL);
3488 env.explicit = p->env->explicit;
3489 env.flags = p->env->flags;
3490 env.disc = p->env->disc;
3491 if (stats(&env, p->env->rex))
3494 return fatal(p->env->disc, env.error ? env.error : REG_ECOUNT, NiL);
3496 if (special(&env, p))
3499 return fatal(p->env->disc, env.error ? env.error : REG_ESPACE, NiL);
3501 p->env->min = g->re.trie.min;
3516 p->env->refs++;