summaryrefslogtreecommitdiff
path: root/regex.c
diff options
context:
space:
mode:
Diffstat (limited to 'regex.c')
-rw-r--r--regex.c31
1 files changed, 27 insertions, 4 deletions
diff --git a/regex.c b/regex.c
index 6a362e4..88f926e 100644
--- a/regex.c
+++ b/regex.c
@@ -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) {