Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweaks from improved unclosed tag errors #1053

Merged
merged 3 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions askama_parser/src/expr.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::borrow::Cow;
use std::collections::HashSet;
use std::str;

Expand Down Expand Up @@ -111,12 +110,10 @@ impl<'a> Expr<'a> {
move |i| Self::parse(i, level),
))(i)?;
if has_named_arguments && !matches!(expr, Self::NamedArgument(_, _)) {
Err(nom::Err::Failure(ErrorContext {
input: start,
message: Some(Cow::Borrowed(
"named arguments must always be passed last",
)),
}))
Err(nom::Err::Failure(ErrorContext::new(
"named arguments must always be passed last",
start,
)))
} else {
Ok((i, expr))
}
Expand Down Expand Up @@ -146,12 +143,10 @@ impl<'a> Expr<'a> {
if named_arguments.insert(argument) {
Ok((i, Self::NamedArgument(argument, Box::new(value))))
} else {
Err(nom::Err::Failure(ErrorContext {
input: start,
message: Some(Cow::Owned(format!(
"named argument `{argument}` was passed more than once"
))),
}))
Err(nom::Err::Failure(ErrorContext::new(
format!("named argument `{argument}` was passed more than once"),
start,
)))
}
}

Expand Down
73 changes: 43 additions & 30 deletions askama_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,33 @@ pub(crate) struct ErrorContext<'a> {
pub(crate) message: Option<Cow<'static, str>>,
}

impl<'a> ErrorContext<'a> {
fn unclosed(kind: &str, tag: &str, i: &'a str) -> Self {
Self::new(format!("unclosed {kind}, missing {tag:?}"), i)
}

fn new(message: impl Into<Cow<'static, str>>, input: &'a str) -> Self {
Self {
input,
message: Some(message.into()),
}
}

pub(crate) fn from_err(error: nom::Err<Error<&'a str>>) -> nom::Err<Self> {
match error {
nom::Err::Incomplete(i) => nom::Err::Incomplete(i),
nom::Err::Failure(Error { input, .. }) => nom::Err::Failure(Self {
input,
message: None,
}),
nom::Err::Error(Error { input, .. }) => nom::Err::Error(Self {
input,
message: None,
}),
}
}
}

impl<'a> nom::error::ParseError<&'a str> for ErrorContext<'a> {
fn from_error_kind(input: &'a str, _code: ErrorKind) -> Self {
Self {
Expand All @@ -188,19 +215,9 @@ impl<'a, E: std::fmt::Display> FromExternalError<&'a str, E> for ErrorContext<'a
}
}

impl<'a> ErrorContext<'a> {
pub(crate) fn from_err(error: nom::Err<Error<&'a str>>) -> nom::Err<Self> {
match error {
nom::Err::Incomplete(i) => nom::Err::Incomplete(i),
nom::Err::Failure(Error { input, .. }) => nom::Err::Failure(Self {
input,
message: None,
}),
nom::Err::Error(Error { input, .. }) => nom::Err::Error(Self {
input,
message: None,
}),
}
impl<'a> From<ErrorContext<'a>> for nom::Err<ErrorContext<'a>> {
fn from(cx: ErrorContext<'a>) -> Self {
Self::Failure(cx)
}
}

Expand Down Expand Up @@ -360,19 +377,20 @@ fn char_lit(i: &str) -> ParseResult<'_> {
opt(escaped(is_not("\\\'"), '\\', anychar)),
char('\''),
)(i)?;

let Some(s) = s else {
return Err(nom::Err::Failure(ErrorContext {
input: start,
// Same error as rustc.
message: Some(Cow::Borrowed("empty character literal")),
}));
return Err(nom::Err::Failure(ErrorContext::new(
"empty character literal",
start,
)));
};
let Ok(("", c)) = Char::parse(s) else {
return Err(nom::Err::Failure(ErrorContext {
input: start,
message: Some(Cow::Borrowed("invalid character")),
}));
return Err(nom::Err::Failure(ErrorContext::new(
"invalid character",
start,
)));
};

let (nb, max_value, err1, err2) = match c {
Char::Literal | Char::Escaped => return Ok((i, s)),
Char::AsciiEscape(nb) => (
Expand All @@ -392,17 +410,12 @@ fn char_lit(i: &str) -> ParseResult<'_> {
};

let Ok(nb) = u32::from_str_radix(nb, 16) else {
return Err(nom::Err::Failure(ErrorContext {
input: start,
message: Some(Cow::Borrowed(err1)),
}));
return Err(nom::Err::Failure(ErrorContext::new(err1, start)));
};
if nb > max_value {
return Err(nom::Err::Failure(ErrorContext {
input: start,
message: Some(Cow::Borrowed(err2)),
}));
return Err(nom::Err::Failure(ErrorContext::new(err2, start)));
}

Ok((i, s))
}

Expand Down
91 changes: 39 additions & 52 deletions askama_parser/src/node.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::borrow::Cow;
use std::str;

use nom::branch::alt;
Expand All @@ -12,7 +11,7 @@ use nom::error_position;
use nom::multi::{fold_many0, many0, many1, separated_list0, separated_list1};
use nom::sequence::{delimited, pair, preceded, terminated, tuple};

use crate::{not_ws, ErrorContext, ParseErr, ParseResult};
use crate::{not_ws, ErrorContext, ParseResult};

use super::{
bool_lit, char_lit, filter, identifier, is_ws, keyword, num_lit, path_or_identifier, skip_till,
Expand Down Expand Up @@ -101,7 +100,7 @@ impl<'a> Node<'a> {
)))(i)?;
match closed {
true => Ok((i, node)),
false => Err(fail_unclosed("block", s.syntax.block_end, i)),
false => Err(ErrorContext::unclosed("block", s.syntax.block_end, i).into()),
}
}

Expand All @@ -113,10 +112,10 @@ impl<'a> Node<'a> {
));
let (j, (pws, _, nws)) = p(i)?;
if !s.is_in_loop() {
return Err(nom::Err::Failure(ErrorContext {
input: i,
message: Some(Cow::Borrowed("you can only `break` inside a `for` loop")),
}));
return Err(nom::Err::Failure(ErrorContext::new(
"you can only `break` inside a `for` loop",
i,
)));
}
Ok((j, Self::Break(Ws(pws, nws))))
}
Expand All @@ -129,10 +128,10 @@ impl<'a> Node<'a> {
));
let (j, (pws, _, nws)) = p(i)?;
if !s.is_in_loop() {
return Err(nom::Err::Failure(ErrorContext {
input: i,
message: Some(Cow::Borrowed("you can only `continue` inside a `for` loop")),
}));
return Err(nom::Err::Failure(ErrorContext::new(
"you can only `continue` inside a `for` loop",
i,
)));
}
Ok((j, Self::Continue(Ws(pws, nws))))
}
Expand All @@ -152,7 +151,7 @@ impl<'a> Node<'a> {
))(i)?;
match closed {
true => Ok((i, Self::Expr(Ws(pws, nws), expr))),
false => Err(fail_unclosed("expression", s.syntax.expr_end, i)),
false => Err(ErrorContext::unclosed("expression", s.syntax.expr_end, i).into()),
}
}
}
Expand Down Expand Up @@ -297,10 +296,10 @@ impl<'a> Target<'a> {

fn verify_name(input: &'a str, name: &'a str) -> Result<Self, nom::Err<ErrorContext<'a>>> {
match name {
"self" | "writer" => Err(nom::Err::Failure(ErrorContext {
"self" | "writer" => Err(nom::Err::Failure(ErrorContext::new(
format!("cannot use `{name}` as a name"),
input,
message: Some(Cow::Owned(format!("Cannot use `{name}` as a name"))),
})),
))),
_ => Ok(Self::Name(name)),
}
}
Expand Down Expand Up @@ -375,12 +374,10 @@ impl<'a> Cond<'a> {
opt(Whitespace::parse),
ws(alt((keyword("else"), |i| {
let _ = keyword("elif")(i)?;
Err(nom::Err::Failure(ErrorContext {
input: i,
message: Some(Cow::Borrowed(
"unknown `elif` keyword; did you mean `else if`?",
)),
}))
Err(nom::Err::Failure(ErrorContext::new(
"unknown `elif` keyword; did you mean `else if`?",
i,
)))
}))),
cut(tuple((
opt(|i| CondTest::parse(i, s)),
Expand Down Expand Up @@ -559,10 +556,10 @@ impl<'a> Macro<'a> {
));
let (j, (pws1, _, (name, params, nws1, _))) = start(i)?;
if name == "super" {
return Err(nom::Err::Failure(ErrorContext {
input: i,
message: Some(Cow::Borrowed("'super' is not a valid name for a macro")),
}));
return Err(nom::Err::Failure(ErrorContext::new(
"'super' is not a valid name for a macro",
i,
)));
}

let mut end = cut(tuple((
Expand Down Expand Up @@ -831,15 +828,14 @@ fn check_end_name<'a>(
if name == end_name {
return Ok((after, end_name));
}
let message = if name.is_empty() && !end_name.is_empty() {
format!("unexpected name `{end_name}` in `end{kind}` tag for unnamed `{kind}`")
} else {
format!("expected name `{name}` in `end{kind}` tag, found `{end_name}`")
};
Err(nom::Err::Failure(ErrorContext {
input: before,
message: Some(Cow::Owned(message)),
}))

Err(nom::Err::Failure(ErrorContext::new(
match name.is_empty() && !end_name.is_empty() {
true => format!("unexpected name `{end_name}` in `end{kind}` tag for unnamed `{kind}`"),
false => format!("expected name `{name}` in `end{kind}` tag, found `{end_name}`"),
},
before,
)))
}

#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -1036,12 +1032,10 @@ impl<'a> Extends<'a> {
))(i)?;
match (pws, nws) {
(None, None) => Ok((i, Self { path })),
(_, _) => Err(nom::Err::Failure(ErrorContext {
input: start,
message: Some(Cow::Borrowed(
"whitespace control is not allowed on `extends`",
)),
})),
(_, _) => Err(nom::Err::Failure(ErrorContext::new(
"whitespace control is not allowed on `extends`",
start,
))),
}
}
}
Expand Down Expand Up @@ -1072,16 +1066,16 @@ impl<'a> Comment<'a> {
loop {
let (_, tag) = opt(skip_till(|i| tag(i, s)))(i)?;
let Some((j, tag)) = tag else {
return Err(fail_unclosed("comment", s.syntax.comment_end, i));
return Err(ErrorContext::unclosed("comment", s.syntax.comment_end, i).into());
};
match tag {
Tag::Open => match depth.checked_add(1) {
Some(new_depth) => depth = new_depth,
None => {
return Err(nom::Err::Failure(ErrorContext {
input: i,
message: Some(Cow::Borrowed("too deeply nested comments")),
}))
return Err(nom::Err::Failure(ErrorContext::new(
"too deeply nested comments",
i,
)));
}
},
Tag::Close => match depth.checked_sub(1) {
Expand Down Expand Up @@ -1123,10 +1117,3 @@ impl<'a> Comment<'a> {
/// Second field is "minus/plus sign was used on the right part of the item".
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Ws(pub Option<Whitespace>, pub Option<Whitespace>);

fn fail_unclosed<'a>(kind: &str, tag: &str, i: &'a str) -> ParseErr<'a> {
nom::Err::Failure(ErrorContext {
input: i,
message: Some(Cow::Owned(format!("unclosed {kind}, missing {tag:?}"))),
})
}
Loading