Skip to content

Commit

Permalink
parser: add context information for unknown tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Kijewski committed Sep 10, 2024
1 parent b6a1595 commit 1948d63
Show file tree
Hide file tree
Showing 6 changed files with 481 additions and 202 deletions.
25 changes: 11 additions & 14 deletions rinja_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ 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, consumed, cut, eof, fail, map, not, opt, recognize, value};
use nom::combinator::{consumed, cut, fail, map, not, opt, recognize, value};
use nom::error::{ErrorKind, FromExternalError};
use nom::multi::{many0_count, many1};
use nom::sequence::{delimited, pair, preceded, terminated, tuple};
use nom::sequence::{delimited, pair, preceded, tuple};
use nom::{AsChar, InputTakeAtPosition};

pub mod expr;
Expand Down Expand Up @@ -110,21 +110,18 @@ impl<'a> Ast<'a> {
file_path: Option<Arc<Path>>,
syntax: &Syntax<'_>,
) -> Result<Self, ParseError> {
let parse = |i: &'a str| Node::many(i, &State::new(syntax));
let (input, message) = match complete(terminated(parse, cut(eof)))(src) {
Ok(("", nodes)) => return Ok(Self { nodes }),
Ok(_) => unreachable!("eof() is not eof?"),
Err(nom::Err::Incomplete(_)) => unreachable!("complete() is not complete?"),
match Node::parse_template(src, &State::new(syntax)) {
Ok(("", nodes)) => Ok(Self { nodes }),
Ok(_) | Err(nom::Err::Incomplete(_)) => unreachable!(),
Err(
nom::Err::Error(ErrorContext { input, message, .. })
| nom::Err::Failure(ErrorContext { input, message, .. }),
) => (input, message),
};
Err(ParseError {
message,
offset: src.len() - input.len(),
file_path,
})
) => Err(ParseError {
message,
offset: src.len() - input.len(),
file_path,
}),
}
}

pub fn nodes(&self) -> &[Node<'a>] {
Expand Down
Loading

0 comments on commit 1948d63

Please sign in to comment.