From d09041bd0acc8d8fcd77e9cda14a8ab8a3b4dbc8 Mon Sep 17 00:00:00 2001 From: René Kijewski Date: Wed, 2 Aug 2023 10:18:21 +0200 Subject: parser: better error message for unknown nodes --- askama_parser/src/lib.rs | 15 +++++++-------- askama_parser/src/tests.rs | 2 +- 2 files changed, 8 insertions(+), 9 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 { - 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!( diff --git a/askama_parser/src/tests.rs b/askama_parser/src/tests.rs index 9cafaf0..ce300d3 100644 --- a/askama_parser/src/tests.rs +++ b/askama_parser/src/tests.rs @@ -702,7 +702,7 @@ fn test_missing_space_after_kw() { let err = Ast::from_str("{%leta=b%}", &syntax).unwrap_err(); assert!(matches!( &*err.to_string(), - "unable to parse template:\n\n\"{%leta=b%}\"" + "problems parsing template source at row 1, column 0 near:\n\"{%leta=b%}\"", )); } -- cgit