Skip to content
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

Serialization of the Parser Tree #27

Open
Goutte opened this issue May 19, 2022 · 2 comments · May be fixed by #28
Open

Serialization of the Parser Tree #27

Goutte opened this issue May 19, 2022 · 2 comments · May be fixed by #28
Labels
enhancement New feature or request

Comments

@Goutte
Copy link

Goutte commented May 19, 2022

✨ Feature request

Make the parser tree serializable with the \serialize() function.

Motivation

Making the parser tree takes most of the total computation time.
If I could "cache" it somehow, it would be great !

Example

$tree = $parser->program();
print(\serialize($tree));

yields

Serialization of 'Antlr\Antlr4\Runtime\Comparison\Equivalence@anonymous' is not allowed

Alternatives

  • Make the grammar more efficient (there's room, I'm a noob)
  • Use a visitor to transpile to target language (but code gen is always tricky)
  • Make my own AST from the parser tree, and somehow ensure it is serializable (contexts?)
  • How do YOU do it ?

Additional context

The vt time is the visitor. The rest could be cached, perhaps.
On more complex scripts, with nested flow statements, it goes up to seconds.

image

Cyprak is the name of our budding DSL for an online role playing game (and is a pun on sprak == language)

@Goutte Goutte added the enhancement New feature or request label May 19, 2022
@Goutte
Copy link
Author

Goutte commented May 30, 2022

I managed to (un)serialize the parse tree, after some tweaking.

There are two issues:

  • The anonymous class (easily solved by naming it)
  • Trouble when unserializing because a single parser file (the generated parser) declares two namespaces (and one of them is not PSR-4). This can be hotfixed using unserialize-callback-func, but something could perhaps also be done upstream.

I can make a MR for the first part, if you're open.

@Goutte Goutte linked a pull request May 31, 2022 that will close this issue
9 tasks
@Goutte
Copy link
Author

Goutte commented May 31, 2022

I went ahead and pushed the changes, so I can use my own fork.

image

Caching is worth it, this script takes about 60ms without it.
You can see the ca (cache) and the vt (visitor) now eating up all the time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant