summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaxime Coste <mawww@kakoune.org>2024-07-15 22:02:43 +1000
committerMaxime Coste <mawww@kakoune.org>2024-07-15 22:02:43 +1000
commit840f4ec3ed14281cea6dd17c4c38d302963ae16d (patch)
treeae9ac5e0913af36b0c3861d06a6607c24ccd7859 /src
parentc0013dcab2564385bdd8235e67d2d964b7b30849 (diff)
Improve error message for alternations in lookarounds and add tests
Fixes #5203
Diffstat (limited to 'src')
-rw-r--r--src/regex_impl.cc23
1 files changed, 21 insertions, 2 deletions
diff --git a/src/regex_impl.cc b/src/regex_impl.cc
index a6298dc7..9b307f23 100644
--- a/src/regex_impl.cc
+++ b/src/regex_impl.cc
@@ -268,9 +268,12 @@ private:
}
m_pos = Iterator{it, m_regex};
NodeIndex lookaround = alternative(op);
- if (at_end() or *m_pos++ != ')')
+ if (auto end_pos = m_pos; at_end() or *m_pos++ != ')')
+ {
+ if (*end_pos == '|')
+ parse_error("Alternations cannot be used in lookarounds");
parse_error("unclosed parenthesis");
-
+ }
validate_lookaround(lookaround);
return lookaround;
}
@@ -1608,6 +1611,22 @@ auto test_regex = UnitTest{[]{
kak_assert(eq(vm.named_captures[1], {"month", 2}));
kak_assert(eq(vm.named_captures[2], {"day", 3}));
}
+
+ auto check_parse_error = [](StringView re, StringView expected_error) {
+ try
+ {
+ TestVM<>{re};
+ kak_assert(false);
+ }
+ catch (const regex_error& err)
+ {
+ kak_assert(err.what() == String{"regex parse error: "} + expected_error);
+ }
+ };
+
+ check_parse_error("(?=a*)", "Quantifiers cannot be used in lookarounds at '(?=a*)<<<HERE>>>'");
+ check_parse_error("(?=(a))", "Lookaround can only contain literals, any chars or character classes at '(?=(a))<<<HERE>>>'");
+ check_parse_error("(?=a|b)", "Alternations cannot be used in lookarounds at '(?=a|<<<HERE>>>b)'");
}};
}