antlr / antlr-php-runtime

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

Class ParseTreeProperty #7

Open HermanPeeren opened 3 years ago

HermanPeeren commented 3 years ago

In the API for the runtime, as documented for Java in https://www.antlr.org/api/Java, we have a Class ParseTreeProperty in the Tree-package. It is to easily attach a value to a node in the parse tree. Java uses an IdentityHashMap for that, with the node / parse tree as key. In PHP we don't have such a datastructure, where we can use an object as key. That is probably why you left this class out in the PHP runtime?

My thoughts:

Maybe I'm overlooking something here and is this a non-issue... Please let me know your thoughts.

marcospassos commented 3 years ago

I left this implementation out because it's just a utility map class, and PHP provides native support for that. I believe that SplObjectStorate has a perfect fit for this use case, or I am missing something?

The only benefit I see in shipping it as part of the runtime is making it easier for people coming from other targets to reproduce the tutorials out there (a new section in the docs may work as well). In that sense, we can implement the PHP version of ParseTreeProperty on top of the SplObjectStorate.

What your thoughts?

HermanPeeren commented 3 years ago

O, perfect, didn't know SplObjectStorage ::blushblush::, but that is exactly it. I'd prefer to wrap that in a ParseTreeProperty-class, so people can easily use it when reading material about ANTLR that is not specifically for PHP, like section 7.5 inTerrence Parr's book (about Sharing Information Among Event Methods).

marcospassos commented 3 years ago

Contributions are welcome! Feel free to open a PR :)

HermanPeeren commented 3 years ago

When no value for a node is stored, null is returned. Standard SplObjectStorage::offsetGet() would throw an Exception there.