-
Notifications
You must be signed in to change notification settings - Fork 200
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
Automatic creation of a Concrete Syntax Tree. #215
Comments
I came across the same problem and did something like below to overcome this.
So by using an IIFE you can create an AST. Maybe better is to have the MANY and others have the same implementation as the OR function.
}); |
Thanks @Zumbala. I'm not sure what is actually happening in the example you provided. I've done something similar by building a very simple "ParseTree" structure and deferring the full AST building to a later stage. but this is just a mitigation of the problem. I'm interested in creating a completely pure grammar and defining the "actions" outside of it (listener/visitor/...). That would be a full solution. I believe this is possible with Chevrotain by overriding CONSUME/SUBRULE/RULE. |
Possibly, however:
I'm not sure I can implement this without causing issues... You could easily override MANY1-5 and "manyInternal" |
If this idea is what I think it is, it would be really nice. I have a grammar that already has actions, which I use to parse and compile a simple language. I'd like to re-use the same grammar for syntax highlighting. It would be neat if I could toggle a "syntax tree only" mode that ignored the actions and returned a syntax tree instead. Awesome project, by the way! |
Thanks @tlrobinson. 😄 I've renamed the issue to be clearer. You will have a pure grammar without any actions (example)
So there will be a stronger separation of concerns between the syntax definition You could still write grammar actions if you prefer. $.RULE("atomicExpression", function() {
return $.OR([
{ALT: function(){ return $.SUBRULE($.parenthesisExpression)}},
// The grammar action (parseInt) can't be separated from the grammar (CONSUME)
{ALT: function(){ return parseInt($.CONSUME(NumberLiteral).image, 10)}}
]);
}); |
Sounds good. In the meantime, how would you recommend re-using a grammar for both purposes? Perhaps I could have an abstract class that has the grammar with actions that call abstract methods that are implemented by two different subclasses.
Doesn't the content assist mode bypass the actions? |
moved discussion related to metabase grammar here: #327 |
Benchmarked using a JSON grammar with a large 1,000 lines input. This is a little slower than expected, but it does make sense as there is a quite a bit of additional overhead |
The Semantic Actions on the Concrete Syntax Tree output will be handled in a separate issue |
Allows writing "pure" grammars and perform semantic actions afterwards. In the (near?) future will add CST visitors to make it easy to traverse the CST structure. fixes #215
Allows writing "pure" grammars and perform semantic actions afterwards. In the (near?) future will add CST visitors to make it easy to traverse the CST structure. fixes #215
Just missing Docs now. |
It is too complicate from the perspective of and end user to try and figure out the type of the children dictionary value (mandatory/optional/collection) As it depends on (potentially) traversing multiple paths in the same grammar rule. Related to #215.
Left overs related to docs & benchmark will be handled separately. |
Thus allowing the creation of "pure" grammars without any actions.
It may be possible as the Parser always knows the current "position in the grammar"
so perhaps the SUBRULE and CONSUME methods may be overridden with pre/post
hooks to assemble the required data structures.
CST
Docs & Examples
The text was updated successfully, but these errors were encountered: