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

parser: add context information for unknown tags #167

Merged
merged 2 commits into from
Sep 10, 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
2 changes: 1 addition & 1 deletion book/src/template_syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ You can also match against multiple alternative patterns at once:
```

For better interoperability with linters and auto-formatters like [djLint],
you can also use a optional `{% endwhen %}` node to close a `{% when %}` case:
you can also use an optional `{% endwhen %}` node to close a `{% when %}` case:

```jinja
{% match number %}
Expand Down
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