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

Parse error when using macro with expanded module path #836

Closed
wrapperup opened this issue Jul 5, 2023 · 3 comments · Fixed by #837
Closed

Parse error when using macro with expanded module path #836

wrapperup opened this issue Jul 5, 2023 · 3 comments · Fixed by #837

Comments

@wrapperup
Copy link
Contributor

wrapperup commented Jul 5, 2023

I am making a crate that allows people to embed icons from Iconify at compile time, but I ran into a little snag.
Tried on main and 0.12. Only happens when using a macro, normal functions work fine.

The intended usage is:

<body>
    <h1>Hello</h1>
    {{ iconify::svg!("mdi:home")|safe }}
</body>

But it gives me a parse error:

error: problems parsing template source at row 5, column 19 near:
       "!(\"mdi:home\") }}\r\n</body>\r\n</html>\r"

I can work around it by importing the macro (below compiles fine), but I'd prefer being able to keep the module path so I don't need to.

{{ svg!("mdi:home")|safe }}
@djc
Copy link
Owner

djc commented Jul 5, 2023

Huh, I guess there's some subtlety in the parser relating to the combination of a path, the macro call and the filter. Care to submit a PR?

@wrapperup
Copy link
Contributor Author

Sure, I'll take a jab at it.

@Kijewski
Copy link
Contributor

Kijewski commented Jul 5, 2023

Macro invocations are parsed specially, but they should actually be simply suffix expressions, and be handled like normal function calls:

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)?;
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::Try) => expr = Expr::Try(expr.into()),
None => break,
}
}
Ok((i, expr))
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants