From 90348b889f397f2753473764c2d7a02986beddeb Mon Sep 17 00:00:00 2001 From: Pascal Kuthe Date: Mon, 13 Mar 2023 19:27:54 +0100 Subject: 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. --- helix-parsec/src/lib.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'helix-parsec') 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> 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> 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)) + } + } +} -- cgit v1.2.3-70-g09d2