diff options
| author | WormHeamer | 2026-01-01 04:20:20 -0500 |
|---|---|---|
| committer | WormHeamer | 2026-01-01 04:20:20 -0500 |
| commit | fafa797d33d1e6fd0fa0cee9a8cd7eb90ba85782 (patch) | |
| tree | 547c072101f9c511232b82d7867216c162100a2f /regex.c | |
| parent | d862b833292133948e67bfd39a20d825bc80733e (diff) | |
regex error reporting
Diffstat (limited to 'regex.c')
| -rw-r--r-- | regex.c | 31 |
1 files changed, 27 insertions, 4 deletions
@@ -33,6 +33,7 @@ typedef struct { Arena *perm, *scratch; DYNARR(u32) lbl; ReCompFlags flags; + ReCompErr err; Str s; int i; } ReParser; @@ -90,7 +91,10 @@ ReNode *re_parse_group(ReParser *s) { /* TODO */ skip(s, 1); ReNode *n = re_new_node(s, N_GROUP, re_parse_expr(s), NULL); - if (ch(s) != ')') FAIL_WITH_MSG("expected )"); + if (ch(s) != ')') { + s->err = RE_COMP_ENORPAREN; + return NULL; + } skip(s, 1); return n; } @@ -177,7 +181,7 @@ ReNode *re_parse_atom(ReParser *s) { } break; case 0: /* should never happen? */ - FAIL_WITH_MSG("atom parsed at end"); + s->err = RE_COMP_EEOF; return NULL; default: { ReNode *n = re_new_node_char(s, ch(s)); @@ -586,14 +590,33 @@ int re_comp_ex(RegEx *re, Str src, Arena *perm, Arena *scratch, ReCompFlags flag .perm = perm, .scratch = scratch, .s = src, - .flags = flags + .flags = flags, + .err = RE_COMP_ENONE }; ReNode *n = re_parse_expr(&s); + if (!s.err) { + if (s.s.n > 0 && s.s.s[s.s.n - 1] == ')') { + s.err = RE_COMP_ENOLPAREN; + } + } if (re_comp_node(&s, n)) return -1; if (re_comp_fin(&s)) return -1; - return 0; + return s.err; +} + +const char *re_comp_strerror(ReCompErr err) { + switch (err) { + case RE_COMP_ENONE: + return "success"; + case RE_COMP_ENORPAREN: + return "no closing )"; + case RE_COMP_ENOLPAREN: + return "no opening ("; + case RE_COMP_EEOF: + return "unexpected end of pattern"; + } } int re_comp(RegEx *re, Str src, Arena *perm, Arena *scratch) { |
