always parse the entire snippet

Previously any remaining text of the snippet that could not be parsed
was ignored. This commit adds the `parse_all` function which reports
an error if any text was not consumed by the parser
pull/2883/head
Pascal Kuthe 2 years ago committed by Blaž Hrastnik
parent 7bf168dce0
commit bbf480007d

@ -384,9 +384,14 @@ mod parser {
} }
pub fn parse(s: &str) -> Result<Snippet, &str> { pub fn parse(s: &str) -> Result<Snippet, &str> {
snippet().parse(s).map(|(_input, elements)| elements) snippet().parse(s).and_then(|(remainder, snippet)| {
if remainder.is_empty() {
Ok(snippet)
} else {
Err(remainder)
}
})
} }
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::SnippetElement::*; use super::SnippetElement::*;
@ -414,6 +419,28 @@ mod parser {
) )
} }
#[test]
fn parse_unterminated_placeholder_error() {
assert_eq!(Err("${1:)"), parse("match(${1:)"))
}
#[test]
fn parse_empty_placeholder() {
assert_eq!(
Ok(Snippet {
elements: vec![
Text("match(".into()),
Placeholder {
tabstop: 1,
value: vec![],
},
Text(")".into())
]
}),
parse("match(${1:})")
)
}
#[test] #[test]
fn parse_placeholders_in_statement() { fn parse_placeholders_in_statement() {
assert_eq!( assert_eq!(

Loading…
Cancel
Save