Skip to content

Commit

Permalink
refactor(parser): Update signature for 'Node::parse_template'
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Dec 19, 2024
1 parent c66d679 commit 6b98cb6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
6 changes: 3 additions & 3 deletions rinja_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ impl<'a> Ast<'a> {
/// If `file_path` is `None`, it means the `source` is an inline template. Therefore, if
/// a parsing error occurs, we won't display the path as it wouldn't be useful.
pub fn from_str(
src: &'a str,
mut src: &'a str,
file_path: Option<Arc<Path>>,
syntax: &Syntax<'_>,
) -> Result<Self, ParseError> {
match Node::parse_template(src, &State::new(syntax)) {
Ok(("", nodes)) => Ok(Self { nodes }),
match Node::parse_template(&mut src, &State::new(syntax)) {
Ok(nodes) if src.is_empty() => Ok(Self { nodes }),
Ok(_) | Err(winnow::error::ErrMode::Incomplete(_)) => unreachable!(),
Err(
winnow::error::ErrMode::Backtrack(ErrorContext { span, message, .. })
Expand Down
20 changes: 11 additions & 9 deletions rinja_parser/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,33 +37,35 @@ pub enum Node<'a> {
}

impl<'a> Node<'a> {
pub(super) fn parse_template(i: &'a str, s: &State<'_>) -> InputParseResult<'a, Vec<Self>> {
let (i, result) = match (|i: &mut _| Self::many(i, s)).parse_peek(i) {
Ok((i, result)) => (i, result),
pub(super) fn parse_template(i: &mut &'a str, s: &State<'_>) -> ParseResult<'a, Vec<Self>> {
let start = *i;
let result = match (|i: &mut _| Self::many(i, s)).parse_next(i) {
Ok(result) => result,
Err(err) => {
if let winnow::error::ErrMode::Backtrack(err) | winnow::error::ErrMode::Cut(err) =
&err
{
if err.message.is_none() {
if let Some(span) = err.span.as_suffix_of(i) {
opt(unpeek(|i| unexpected_tag(i, s))).parse_peek(span)?;
*i = start;
if let Some(mut span) = err.span.as_suffix_of(i) {
opt(unpeek(|i| unexpected_tag(i, s))).parse_next(&mut span)?;
}
}
}
return Err(err);
}
};
let (i, _) = opt(unpeek(|i| unexpected_tag(i, s))).parse_peek(i)?;
let (i, is_eof) = opt(eof).parse_peek(i)?;
opt(unpeek(|i| unexpected_tag(i, s))).parse_next(i)?;
let is_eof = opt(eof).parse_next(i)?;
if is_eof.is_none() {
return Err(winnow::error::ErrMode::Cut(ErrorContext::new(
"cannot parse entire template\n\
you should never encounter this error\n\
please report this error to <https://github.com/rinja-rs/rinja/issues>",
i,
*i,
)));
}
Ok((i, result))
Ok(result)
}

fn many(i: &mut &'a str, s: &State<'_>) -> ParseResult<'a, Vec<Self>> {
Expand Down

0 comments on commit 6b98cb6

Please sign in to comment.