Skip to content

Commit

Permalink
parse suffix call instead, change test names
Browse files Browse the repository at this point in the history
  • Loading branch information
wrapperup committed Jul 14, 2023
1 parent 615792f commit 1a1e674
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
39 changes: 24 additions & 15 deletions askama_derive/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ fn expr_single(i: &str) -> IResult<&str, Expr<'_>> {
expr_num_lit,
expr_str_lit,
expr_char_lit,
expr_rust_macro,
expr_path,
expr_array_lit,
expr_var,
Expand All @@ -194,6 +193,7 @@ enum Suffix<'a> {
Attr(&'a str),
Index(Expr<'a>),
Call(Vec<Expr<'a>>),
MacroCall(&'a str),
Try,
}

Expand All @@ -218,6 +218,10 @@ fn expr_call(i: &str) -> IResult<&str, Suffix<'_>> {
map(arguments, Suffix::Call)(i)
}

fn expr_macro_call(i: &str) -> IResult<&str, Suffix<'_>> {
map(macro_arguments, Suffix::MacroCall)(i)
}

fn expr_try(i: &str) -> IResult<&str, Suffix<'_>> {
map(preceded(take_till(not_ws), char('?')), |_| Suffix::Try)(i)
}
Expand Down Expand Up @@ -256,32 +260,30 @@ fn expr_prefix(i: &str) -> IResult<&str, Expr<'_>> {
fn expr_suffix(i: &str) -> IResult<&str, Expr<'_>> {
let (mut i, mut expr) = expr_single(i)?;
loop {
let (j, suffix) = opt(alt((expr_attr, expr_index, expr_call, expr_try)))(i)?;
let (j, suffix) = opt(alt((
expr_attr,
expr_index,
expr_call,
expr_macro_call,
expr_try,
)))(i)?;
i = j;
match suffix {
Some(Suffix::Attr(attr)) => expr = Expr::Attr(expr.into(), attr),
Some(Suffix::Index(index)) => expr = Expr::Index(expr.into(), index.into()),
Some(Suffix::Call(args)) => expr = Expr::Call(expr.into(), args),
Some(Suffix::MacroCall(args)) => match expr {
Expr::Path(path) => expr = Expr::RustMacro(path, args),
Expr::Var(name) => expr = Expr::RustMacro(vec![name], args),
_ => break,
},
Some(Suffix::Try) => expr = Expr::Try(expr.into()),
None => break,
}
}
Ok((i, expr))
}

fn macro_arguments(i: &str) -> IResult<&str, &str> {
delimited(char('('), recognize(nested_parenthesis), char(')'))(i)
}

fn expr_rust_macro(i: &str) -> IResult<&str, Expr<'_>> {
let (i, (path_vec, _, args)) = tuple((
alt((path, map(identifier, |id| vec![id]))),
char('!'),
macro_arguments,
))(i)?;
Ok((i, Expr::RustMacro(path_vec, args)))
}

macro_rules! expr_prec_layer {
( $name:ident, $inner:ident, $op:expr ) => {
fn $name(i: &str) -> IResult<&str, Expr<'_>> {
Expand Down Expand Up @@ -348,3 +350,10 @@ fn arguments(i: &str) -> IResult<&str, Vec<Expr<'_>>> {
ws(char(')')),
)(i)
}

fn macro_arguments(i: &str) -> IResult<&str, &str> {
preceded(
char('!'),
delimited(char('('), recognize(nested_parenthesis), char(')')),
)(i)
}
6 changes: 3 additions & 3 deletions testing/tests/rust_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ macro_rules! hello {
struct RustMacrosTemplate {}

#[test]
fn main() {
fn macro_basic() {
let template = RustMacrosTemplate {};
assert_eq!("Hello, world!", template.render().unwrap());
}
Expand All @@ -31,7 +31,7 @@ mod foo {
struct RustMacrosFullPathTemplate {}

#[test]
fn full_path() {
fn macro_full_path() {
let template = RustMacrosFullPathTemplate {};
assert_eq!("Hello, world!", template.render().unwrap());
}
Expand Down Expand Up @@ -67,7 +67,7 @@ fn day(_: u16, _: &str, d: u8) -> u8 {
struct RustMacrosArgTemplate {}

#[test]
fn args() {
fn macro_with_args() {
let template = RustMacrosArgTemplate {};
assert_eq!("2021\nJuly\n2", template.render().unwrap());
}

0 comments on commit 1a1e674

Please sign in to comment.