Skip to content

Reasonable way to less greedily parse an Expr? #1950

@helixbass

Description

@helixbass

Hi, I'm writing a proc-macro with haml-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 is what I did fwiw)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions