packcc icon indicating copy to clipboard operation
packcc copied to clipboard

Add tree parsing utilities

Open MinecraftPublisher opened this issue 2 years ago • 6 comments

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.

MinecraftPublisher avatar Jan 10 '24 09:01 MinecraftPublisher

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.

MinecraftPublisher avatar Jan 10 '24 11:01 MinecraftPublisher

For example:

name <- [a-z]+ %auto

becomes:

name <- [a-z]+ { $$ = CREATE_NODE("__name", $0); }

MinecraftPublisher avatar Jan 10 '24 11:01 MinecraftPublisher

Commit also adds support for rulename = pattern and rulename : pattern

MinecraftPublisher avatar Jan 11 '24 08:01 MinecraftPublisher

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.

arithy avatar Apr 14 '24 14:04 arithy

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.

MinecraftPublisher avatar Apr 16 '24 17:04 MinecraftPublisher

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.

arithy avatar Apr 25 '24 01:04 arithy

@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 avatar May 20 '24 11:05 arithy

@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.

MinecraftPublisher avatar May 20 '24 11:05 MinecraftPublisher

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.

arithy avatar May 23 '24 19:05 arithy

@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.

arithy avatar Jun 01 '24 01:06 arithy