diff options
author | René Kijewski <rene.kijewski@fu-berlin.de> | 2023-08-02 10:18:21 +0200 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2023-08-02 11:09:05 +0200 |
commit | d09041bd0acc8d8fcd77e9cda14a8ab8a3b4dbc8 (patch) | |
tree | 21067c681c781415cdd315c83fe9ef1680f47a1e /askama_parser/src/lib.rs | |
parent | 04e585b9d26ff5976854176bce0ba2f6cb42c76c (diff) | |
download | askama-d09041bd0acc8d8fcd77e9cda14a8ab8a3b4dbc8.tar.gz askama-d09041bd0acc8d8fcd77e9cda14a8ab8a3b4dbc8.tar.bz2 askama-d09041bd0acc8d8fcd77e9cda14a8ab8a3b4dbc8.zip |
parser: better error message for unknown nodes
Diffstat (limited to 'askama_parser/src/lib.rs')
-rw-r--r-- | askama_parser/src/lib.rs | 15 |
1 files changed, 7 insertions, 8 deletions
diff --git a/askama_parser/src/lib.rs b/askama_parser/src/lib.rs index 25d164f..65d2eff 100644 --- a/askama_parser/src/lib.rs +++ b/askama_parser/src/lib.rs @@ -8,10 +8,10 @@ use nom::branch::alt; use nom::bytes::complete::{escaped, is_not, tag, take_till}; use nom::character::complete::char; use nom::character::complete::{anychar, digit1}; -use nom::combinator::{map, opt, recognize, value}; +use nom::combinator::{cut, eof, map, opt, recognize, value}; use nom::error::ErrorKind; use nom::multi::separated_list1; -use nom::sequence::{delimited, pair, tuple}; +use nom::sequence::{delimited, pair, terminated, tuple}; use nom::{error_position, AsChar, IResult, InputTakeAtPosition}; pub mod expr; @@ -75,11 +75,10 @@ pub struct Ast<'a> { impl<'a> Ast<'a> { pub fn from_str(src: &'a str, syntax: &Syntax<'_>) -> Result<Self, ParseError> { - let err = match Node::many(src, &State::new(syntax)) { - Ok((left, nodes)) => match left.is_empty() { - true => return Ok(Self { nodes }), - false => return Err(ParseError(format!("unable to parse template:\n\n{left:?}"))), - }, + let parse = |i: &'a str| Node::many(i, &State::new(syntax)); + let err = match terminated(parse, cut(eof))(src) { + Ok(("", nodes)) => return Ok(Self { nodes }), + Ok(_) => unreachable!("eof() is not eof?"), Err(nom::Err::Error(err)) | Err(nom::Err::Failure(err)) => err, Err(nom::Err::Incomplete(_)) => return Err(ParseError("parsing incomplete".into())), }; @@ -93,7 +92,7 @@ impl<'a> Ast<'a> { _ => format!("{source_after:?}"), }; - let (row, last_line) = source_before.lines().enumerate().last().unwrap(); + let (row, last_line) = source_before.lines().enumerate().last().unwrap_or_default(); let column = last_line.chars().count(); let msg = format!( |