antlr / antlr-php-runtime

PHP Runtime for ANTLR4
BSD 3-Clause "New" or "Revised" License
81 stars 19 forks source link

Serialization of the Parser Tree #27

Open Goutte opened 2 years ago

Goutte commented 2 years ago

✨ 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

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 commented 2 years ago

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

There are two issues:

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

Goutte commented 2 years ago

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