Skip to content

Commit

Permalink
parser: use fail fail() where appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
Kijewski committed Aug 7, 2024
1 parent dec67c6 commit e0a7d4d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 38 deletions.
9 changes: 3 additions & 6 deletions rinja_parser/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::str;
use nom::branch::alt;
use nom::bytes::complete::{tag, take_till};
use nom::character::complete::char;
use nom::combinator::{cut, map, not, opt, peek, recognize, value};
use nom::combinator::{cut, fail, map, not, opt, peek, recognize, value};
use nom::error::ErrorKind;
use nom::error_position;
use nom::multi::{fold_many0, many0, separated_list0};
Expand Down Expand Up @@ -127,7 +127,7 @@ impl<'a> Expr<'a> {
if !is_template_macro {
// If this is not a template macro, we don't want to parse named arguments so
// we instead return an error which will allow to continue the parsing.
return Err(nom::Err::Error(error_position!(i, ErrorKind::Alt)));
return fail(i);
}

let (_, level) = level.nest(i)?;
Expand Down Expand Up @@ -486,10 +486,7 @@ impl<'a> Suffix<'a> {
if nested == 0 {
Ok((&input[last..], ()))
} else {
Err(nom::Err::Error(error_position!(
input,
ErrorKind::SeparatedNonEmptyList
)))
fail(input)
}
}

Expand Down
28 changes: 5 additions & 23 deletions rinja_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ use std::{fmt, str};
use nom::branch::alt;
use nom::bytes::complete::{escaped, is_not, tag, take_till, take_while_m_n};
use nom::character::complete::{anychar, char, one_of, satisfy};
use nom::combinator::{complete, cut, eof, map, not, opt, recognize};
use nom::error::{Error, ErrorKind, FromExternalError};
use nom::combinator::{complete, cut, eof, fail, map, not, opt, recognize};
use nom::error::{ErrorKind, FromExternalError};
use nom::multi::{many0_count, many1};
use nom::sequence::{delimited, pair, preceded, terminated, tuple};
use nom::{error_position, AsChar, InputTakeAtPosition};
use nom::{AsChar, InputTakeAtPosition};

pub mod expr;
pub use expr::{Expr, Filter};
Expand Down Expand Up @@ -240,20 +240,6 @@ impl<'a> ErrorContext<'a> {
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> {
Expand Down Expand Up @@ -323,11 +309,7 @@ fn skip_till<'a, 'b, O>(
fn keyword<'a>(k: &'a str) -> impl FnMut(&'a str) -> ParseResult<'_> {
move |i: &'a str| -> ParseResult<'a> {
let (j, v) = identifier(i)?;
if k == v {
Ok((j, v))
} else {
Err(nom::Err::Error(error_position!(i, ErrorKind::Tag)))
}
if k == v { Ok((j, v)) } else { fail(i) }
}
}

Expand Down Expand Up @@ -366,7 +348,7 @@ fn num_lit(i: &str) -> ParseResult<'_> {
Ok((i, suffix))
} else if ignore.contains(&suffix) {
// no need for a message, this case only occures in an `opt(…)`
Err(nom::Err::Error(ErrorContext::new("", i)))
fail(i)
} else {
Err(nom::Err::Failure(ErrorContext::new(
format!("unknown {kind} suffix `{suffix}`"),
Expand Down
11 changes: 2 additions & 9 deletions rinja_parser/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use nom::branch::alt;
use nom::bytes::complete::{tag, take_till};
use nom::character::complete::char;
use nom::combinator::{complete, consumed, cut, eof, fail, map, not, opt, peek, recognize, value};
use nom::error::ErrorKind;
use nom::error_position;
use nom::multi::{many0, many1, separated_list0};
use nom::sequence::{delimited, pair, preceded, tuple};

Expand Down Expand Up @@ -79,12 +77,7 @@ impl<'a> Node<'a> {
"break" => |i, s| Self::r#break(i, s),
"continue" => |i, s| Self::r#continue(i, s),
"filter" => |i, s| wrap(Self::FilterBlock, FilterBlock::parse(i, s)),
_ => {
return Err(ErrorContext::from_err(nom::Err::Error(error_position!(
i,
ErrorKind::Tag
))));
}
_ => return fail(i),
};

let (i, node) = s.nest(j, |i| func(i, s))?;
Expand Down Expand Up @@ -773,7 +766,7 @@ impl<'a> Lit<'a> {
let (i, content) = match content {
Some("") => {
// {block,comment,expr}_start follows immediately.
return Err(nom::Err::Error(error_position!(i, ErrorKind::TakeUntil)));
return fail(i);
}
Some(content) => (i, content),
None => ("", i), // there is no {block,comment,expr}_start: take everything
Expand Down

0 comments on commit e0a7d4d

Please sign in to comment.