-
Notifications
You must be signed in to change notification settings - Fork 21
Add ParseTreeProperty class #8
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
4bec49f
230022a
e8964b7
5c8c8ea
02f31f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Antlr\Antlr4\Runtime\Tree; | ||
|
||
use \SplObjectStorage; | ||
|
||
/** | ||
* Associate a property with a parse tree node. | ||
* | ||
* Useful with parse tree listeners that need to associate a value with a | ||
* particular tree node. For instance when specifying a return value for the | ||
* listener event method that visited a particular node. | ||
* You can associate any type of variable with a node. | ||
* | ||
* Example use: | ||
* | ||
* use Antlr\Antlr4\Runtime\Tree\ParseTreeProperty; | ||
* | ||
* // Declare a ParseTreeProperty-container for values in your listener. | ||
* private $values; | ||
* | ||
* // Make a new container in the constructor of the listener. | ||
* $this->values = new ParseTreeProperty(); | ||
* | ||
* // Use it from any method in your listener. | ||
* // Use it to store or retrieve any value, associated to a specific node. | ||
* $this->values->put($node, $someValue); | ||
* $aValue = $this->values->get($node); | ||
* $this->values->removeFrom($node); | ||
* | ||
* | ||
* This class is implemented in PHP as a wrapper around \SplObjectStorage. | ||
*/ | ||
class ParseTreeProperty | ||
{ | ||
protected $storage; | ||
|
||
public function __construct() | ||
{ | ||
$this->storage = new SplObjectStorage(); | ||
} | ||
|
||
/** | ||
* Get the value associated with $node from the storage. | ||
* | ||
* @param ParseTree $node The {@see ParseTree} with which the value is associated. | ||
* @return mixed $value The stored value | null when $node is not in the storage. | ||
*/ | ||
public function get(ParseTree $node) | ||
{ | ||
$value = null; | ||
if ($this->storage->contains($node)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. blank line before |
||
$value = $this->storage->offsetGet($node); | ||
} | ||
return $value; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. blank line before |
||
} | ||
|
||
/** | ||
* Put a value associated with $node in the storage. | ||
* | ||
* @param ParseTree $node The {@see ParseTree} with which the value is associated. | ||
* @param mixed $value Any value | ||
* @return void | ||
*/ | ||
public function put(ParseTree $node, $value): void | ||
{ | ||
$this->storage->attach($node, $value); | ||
} | ||
|
||
/** | ||
* Remove the value associated with $node from the storage. | ||
* | ||
* @param ParseTree $node The {@see ParseTree} with which the value is associated. | ||
* @return mixed $value The removed value | null when $node was not in the storage. | ||
*/ | ||
public function removeFrom(ParseTree $node) | ||
{ | ||
$value = $this->get($node); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The indentation should be 4 chars long |
||
$this->storage->detach($node); | ||
return $value; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please put a line before the return statement |
||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not private?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep. It was private first, but I changed it to protected to be abele to use it in the drived class... which is not a good idea. Change it back. Thanks.