Replies: 6 comments 10 replies
-
Looking more at the parse-tree angle, it might be okay that the parse-tree is untyped. By default, that's what tree-sitter and rust-analyser do. rust-analyser even has a special generator ( Maybe we could add integration with this by default or something? To quote the blog post:
|
Beta Was this translation helpful? Give feedback.
-
Hi Techcable, Thank you very much for your elaborate contribution to our discussion. First I would like to state that I'm convinced that this loose coupling is one of the strengths of Nevertheless I totally agree with you in the sense that the problem of constructing ASTs is solely left to the user and he or she is a bit left alone here. No support from This is really a downside of So I'm glad that you pointed me to ANTLR. Actually some fundamentals of But there are some crucial differences between ANTLR and ANTLR doesn't need to deal with grammar transformation and due to the similarity between the grammar description and the generated parser's source it quasi entails the ability to provide the Visitor pattern. The downside of ANTRL's code generation is that the user could write files that the parser would crash with stack overflows 😉. Anyway I think we should carefully think over some kind of improvement here. Several things are imaginable:
|
Beta Was this translation helpful? Give feedback.
-
I thought about how to provide "user level semantic actions". The auto-implementation must include an inner Grammar struct that auto-implements the expanded grammar's actions and calls the user level semantic actions at appropriate points. You can think of this Grammar implementation as an adapter between the expanded grammar and the original grammar. The advantage is that it seems possible to keep the parol-runtime library untouched. This is not exactly what you suggested. But it goes into the right direction, I think. I will start a test implementation for this soon where the auto-generated parts are still manually created by me, I think. |
Beta Was this translation helpful? Give feedback.
-
I see the auto-AST feature just landed. 🎉 Thank you for working on this. I will have to find time to play around with it :) |
Beta Was this translation helpful? Give feedback.
-
Your For syntax:
The generated AST generator: trait AstGenerator {
type List;
type Num;
fn gen_list(num: Self::Num, list: Vec<(Token, Self::Num)>) -> Self::List;
fn gen_num(text: Token) -> Self::Num;
} Is it viable to have option to generate this in |
Beta Was this translation helpful? Give feedback.
-
Hi ryo33, first, I really appreciate your positive feedback. 😎 As far as your proposal is concerned, I think I need some more detailed information. Please correct me if I got something wrong! 😉 I think this should be feasible with the information What I think is that you're giving up (deliberately?) the possibility to generate a functioning parser when working in this mode (this is what So, in my opinion I would be glad to accept a PR from you. You should add a new argument switch that enables this special generation mode for |
Beta Was this translation helpful? Give feedback.
-
Hi! It seems very awkward to construct an AST from parol's semantic actions trait :(
As a particular example, take a look at the json example. We have the AST node that's all nice and strongly-typed.
However, converting from the parse tree -> AST require you manually
push
/pop
from an untyped stack, which seems extremely awkward and error-prone too.Other parser generators in rust seem to have similar problems. This is especially the case for ones that enforce a separation between grammar and semantic actions (in particular pest)
Parser generators that do not enforce this separation between actions/grammars can get around this problem, by building up the AST directly in the grammar file. This is what lalrpop does to build ASTs
I am not suggesting we include actions in the grammar. That seems ugly. I am also not suggesting we provide a way to automatically map between parse trees and ASTs. That is a landmine of complexit.
What would be really nice is if we had some sort of macro that could help map users build up their AST. Maybe it could auto-implement the grammar actions trait?
The Problem of Grammar Transformations
Part of what makes implementing this sort of macro difficult is the grammar transformations. Right now, the actions trait is based on the expanded grammar, not the original grammar.
This means that the originally specified
Object
item in the json grammar, expands to four sub-items in the expanded grammar.While the user would intuitively expect to write one semantic action for an
Object
, when in fact they have to write four actions based around the expanded grammar :(I'm not a parser expert by any means, and I totally understand grammar transformations are necessary to deal with left-recursion and all that stuff.
However, I still think it would be amazing if we could have semantic actions more in line with the original grammar/parse tree, and not based on the expanded/transformer grammar.
This would make going from parol -> AST much easier, regardless of whether they do it by hand or by some sort of macro.
Antlr
Again, I would like to reiterate that I am not a parser expert. I barely understand the basics of DFA transitions, let alone this whole
LL(*)
magic that Antlr is doing 😉However, I think the antlr could be maybe (a little bit) of an API inspiration for how to design the visitor traits.
Here's a gist of ANTLR's json visitor/parser.
Notice two things:
enum
sAs far as I can tell, parol's parse tree is dynamically typed. Maybe in addition to hiding internal transformations, we can (somehow) make the parse tree statically typed?
This is kind of me just babbling/complaining about usability (and comparison to ANTLR), I'm not sure how actionable this is :)
Beta Was this translation helpful? Give feedback.
All reactions