Add tree parsing utilities
This is super helpful for the users so they don't have to implement their own tree system every single time. It is a simple binary tree system with a string and int type.
Let me know if you want me to implement variable node count trees. This is made to be easier to maintain and move through.
I could probably also enable automatic tree generation without much C code required, But that would require deep examination of the parser code.
For example:
name <- [a-z]+ %auto
becomes:
name <- [a-z]+ { $$ = CREATE_NODE("__name", $0); }
Commit also adds support for rulename = pattern and rulename : pattern
Thank you for your suggestion with the interesting code. However, let me close this PR for the following reasons.
- I want to stick to keeping PackCC with a single source file. Sorry, it's just for my preference.
- I don't want to constrain programmers to use a specific AST implementation. I want to leave room for choice.
Sure, But you could simply merge the two files.
Plus, They are not limited to just this AST implementation, It's simply just a type that comes built-in that doesn't require any boilerplate, My design could also use a lot of improvements.
I have realized the similar thing you suggested, by using the import feature. Please see here. It is actually less easier than your suggestion but has much more versatility.
@MinecraftPublisher , how is my solution? I guess it is not exactly what you had in mind. If you come up with a concrete idea on automatic AST building, let me know it. I'll close this PR in a week if no reply. Even after that, feel free to submit an issue or a PR again.
@arithy Sorry, I didn't see your comment. The import feature looks cool, But in my opinion, The AST generation tools should be built into packcc itself, Or at least come with an optional default import. Simple functions like createAST(...nodes), createNode(type, content_ptr), addNode(ast, node), removeNode(ast, node_ptr), freeAST(ast), freeNode(node) should be included.
Yes, of course, the automatic AST generation cannot be realized without implementing it in PackCC itself to generate every action code that creates an appropriate AST node. I recognize the needs of the automatic AST generation like #54 , but I have not been able to determine its specification, because there are many manners to form ASTs. I show some examples below. Example 1: (from the point of view of applications)
term <- l:term _ '+' _ r:factor {
$$ = pcc_ast_node__create_2(l, r); $$->custom.text = strdup("+");
}
Example 2: (from the point of view of syntax rules)
term <- l:term _ '+' _ r:factor {
pcc_ast_node_t *t = pcc_ast_node__create_0(); t->custom.text = strdup("+");
$$ = pcc_ast_node__create_3(l, t, r); $$->custom.text = strdup("term");
}
So, at this time, I introduced only a minimum helper functionality using the import feature.
@MinecraftPublisher , anyway this PR is not acceptable for reasons of the code quality and the specifications. Thank you for your activity to try improving PackCC! I'm very glad if you let me know any other idea you will come up with.