Replies: 2 comments
-
You want an AST, not a parse tree for those operations. A parse tree is a lossless representation of the input, but it can have too much detail for further processing. Usually, a compiler will have a pass that converts a parse tree to an AST by throwing away data, e.g. in Rust's compiler: https://rustc-dev-guide.rust-lang.org/lowering.html (although they call the parse tree "AST" and what I call AST "HIR"). As a matter of fact, I am currently working on adding an AST to lexy and support this sort of lowering, as it is quite annoying boilerplate to write by hand. Alternatively, you can add user data to the parse tree using a hash map using the
It's not quite as simple, as you want to capture all tokens. The way to do it is to react to the parse events using your own handler, look at e.g. trace for an implementation that reacts to all events: lexy/include/lexy/action/trace.hpp Lines 305 to 462 in d5041eb But note that this interface is undocumented and subject to change without notice. |
Beta Was this translation helpful? Give feedback.
-
Thanks. |
Beta Was this translation helpful? Give feedback.
-
I'm trying out lexy for a personal cppfront-type project.
I know the parse tree output is designed to be immutable and allow constexpr...
That said, have you thought about adding a mutable templated "userdata" field? In my usage case for instance, I would like to produce a parse tree for an expression, and then iteratively label each resultant node with a pointer to the typedef for the expression result type, if known.
Have you thought about a mutable tree option allowing node re-arrangement/replacement? For instance, in an optimizing language parser, you might want to do constant folding on the resultant tree, and other such operations.
I'm just getting into it, so I'm not sure how hard it would be to just implement my own tree output. Would one do it just be making the "value" output of rules be tree nodes (perhaps w/ children)?
Thanks for the library!
Beta Was this translation helpful? Give feedback.
All reactions