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

Allow paths to start with :: #393

Merged
merged 5 commits into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 16 additions & 6 deletions askama_shared/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1342,10 +1342,15 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
}

fn visit_path(&mut self, buf: &mut Buffer, path: &[&str]) -> DisplayWrap {
for (i, part) in path.iter().enumerate() {
if i > 0 {
buf.write("::");
let mut it = path.iter();
match it.next() {
Some(&part) if part != "::" => {
buf.write(part);
}
_ => {}
}
for part in it {
buf.write("::");
buf.write(part);
}
DisplayWrap::Unwrapped
Expand All @@ -1357,10 +1362,15 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
path: &[&str],
args: &[Expr],
) -> Result<DisplayWrap, CompileError> {
for (i, part) in path.iter().enumerate() {
if i > 0 {
buf.write("::");
let mut it = path.iter();
match it.next() {
Some(&part) if part != "::" => {
buf.write(part);
}
_ => {}
}
for part in it {
buf.write("::");
buf.write(part);
}
buf.write("(");
Expand Down
10 changes: 6 additions & 4 deletions askama_shared/src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use nom::branch::alt;
use nom::bytes::complete::{escaped, is_not, tag, take_until};
use nom::character::complete::{anychar, char, digit1};
use nom::combinator::{complete, map, opt};
use nom::combinator::{complete, map, opt, value};
use nom::error::ParseError;
use nom::multi::{many0, many1, separated_list0, separated_list1};
use nom::sequence::{delimited, pair, tuple};
Expand Down Expand Up @@ -312,10 +312,12 @@ fn expr_var_call(i: &[u8]) -> IResult<&[u8], Expr> {
}

fn path(i: &[u8]) -> IResult<&[u8], Vec<&str>> {
let root = opt(value("::", ws(tag("::"))));
vallentin marked this conversation as resolved.
Show resolved Hide resolved
let tail = separated_list1(ws(tag("::")), identifier);
let (i, (start, _, rest)) = tuple((identifier, ws(tag("::")), tail))(i)?;

let mut path = vec![start];
let (i, (root, start, _, rest)) = tuple((root, identifier, ws(tag("::")), tail))(i)?;
let mut path = Vec::new();
path.extend(root);
path.push(start);
path.extend(rest);
Ok((i, path))
}
Expand Down