aboutsummaryrefslogtreecommitdiff
path: root/helix-parsec
diff options
context:
space:
mode:
authorPascal Kuthe2023-03-13 18:27:54 +0000
committerBlaž Hrastnik2023-03-16 06:46:08 +0000
commit90348b889f397f2753473764c2d7a02986beddeb (patch)
treeb4da76ddd1a9daf0ff8ca0d572f6a9926cc99d06 /helix-parsec
parentbbf480007d87631ef7b7f93cef170ec1af961406 (diff)
revamped snippet text element parsing
Snippet text elements can contain escape sequences that must be treated properly. Furthermore snippets must always escape certain characters (like `}` or `\`). The function has been updated to account for that. `text` is now also included with `anything` to match the grammar and can also match empty text. To avoid infinite loops the `non-empty` combinator has been added which is automatically used in the `one_or_more` and `zero_or more` combinator where the problemn would occur.
Diffstat (limited to 'helix-parsec')
-rw-r--r--helix-parsec/src/lib.rs13
1 files changed, 13 insertions, 0 deletions
diff --git a/helix-parsec/src/lib.rs b/helix-parsec/src/lib.rs
index e09814b8..846d02d6 100644
--- a/helix-parsec/src/lib.rs
+++ b/helix-parsec/src/lib.rs
@@ -459,6 +459,7 @@ pub fn zero_or_more<'a, P, T>(parser: P) -> impl Parser<'a, Output = Vec<T>>
where
P: Parser<'a, Output = T>,
{
+ let parser = non_empty(parser);
move |mut input| {
let mut values = Vec::new();
@@ -491,6 +492,7 @@ pub fn one_or_more<'a, P, T>(parser: P) -> impl Parser<'a, Output = Vec<T>>
where
P: Parser<'a, Output = T>,
{
+ let parser = non_empty(parser);
move |mut input| {
let mut values = Vec::new();
@@ -559,3 +561,14 @@ where
Ok((input, values))
}
}
+
+pub fn non_empty<'a, T>(p: impl Parser<'a, Output = T>) -> impl Parser<'a, Output = T> {
+ move |input| {
+ let (new_input, res) = p.parse(input)?;
+ if new_input.len() == input.len() {
+ Err(input)
+ } else {
+ Ok((new_input, res))
+ }
+ }
+}