dtolnay / dtolnay/syn

Reasonable way to less greedily parse an Expr?

Open
#1,950 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
3.4k
Forks
374
Avg merge
1d 5h
Merged PRs (30d)
2

Description

Hi, I'm writing a proc-macro with [`haml`](https://haml.info/)-esque syntax. So I want to be able to say things like eg:
```
renderer.render(soft! {
%FlexColumn
children => [
%FlexColumn
children => [
%Text
text => "Top area"
cursor => %Cursor.Relative
x => 0
y => 0
]
flex_grow => 1
%StatusBar::new(current_percent)
%Text "This looks great. Hit q to quit"
]
})?;
```
What I ran into is that I want eg in the above example the `StatusBar::new(current_percent)` to be allowed to be/get parsed as a Rust/`syn` `Expr` basically (so eg one could also say `%MyComponentBuilder::default().current_percent(current_percent).build().unwrap()` there or some other reasonable Rust expression after the `%` sign)

But it got overly greedy when I parsed things as a normal `syn::Expr` and interpreted the following `%` (ie the `%` at the beginning of the following `%Text`) as part of a `%` binary expression I assume

What I did temporarily was to fork `syn` and expose a `LessThanBinaryExpr` parse-able type (by inspecting `syn`'s source code and seeing that perhaps approximately what I wanted was the `unary_expr()` function in `syn`) ([here](https://github.com/dtolnay/syn/compare/master...helixbass:syn:less-than-binary-expr) is what I did fwiw)

But so basically I'm wondering if there's a more canonical/sane way to achieve something similar? Thanks

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.