From 4bec49f625fafb98d6f9969655727203866e150c Mon Sep 17 00:00:00 2001 From: Herman Peeren Date: Wed, 9 Sep 2020 10:01:10 +0200 Subject: [PATCH 1/5] Add ParseTreeProperty class --- src/Tree/ParseTreeProperty.php | 84 ++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/Tree/ParseTreeProperty.php diff --git a/src/Tree/ParseTreeProperty.php b/src/Tree/ParseTreeProperty.php new file mode 100644 index 0000000..73b2899 --- /dev/null +++ b/src/Tree/ParseTreeProperty.php @@ -0,0 +1,84 @@ + + * 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)) + { + $value = $this->storage->offsetGet($node); + } + return $value; + } + + /** + * 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) + { + $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); + $this->storage->detach($node); + return $value; + } + +} From 230022a54c31e15a7ba73a38a6327148751fa160 Mon Sep 17 00:00:00 2001 From: Herman Peeren Date: Thu, 10 Sep 2020 11:18:12 +0200 Subject: [PATCH 2/5] minor change: * added void return type to put() * deleted code-tag in description in docblock --- src/Tree/ParseTreeProperty.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Tree/ParseTreeProperty.php b/src/Tree/ParseTreeProperty.php index 73b2899..016bd93 100644 --- a/src/Tree/ParseTreeProperty.php +++ b/src/Tree/ParseTreeProperty.php @@ -13,7 +13,7 @@ * 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 @@ -27,7 +27,7 @@ * $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 */ @@ -63,7 +63,7 @@ public function get(ParseTree $node) * @param mixed $value Any value * @return void */ - public function put(ParseTree $node, $value) + public function put(ParseTree $node, $value): void { $this->storage->attach($node, $value); } From e8964b77a0b33afafb5b624c075a62ae699245c0 Mon Sep 17 00:00:00 2001 From: Herman Peeren Date: Thu, 10 Sep 2020 16:50:35 +0200 Subject: [PATCH 3/5] Code style adjusted, PSR-12 --- src/Tree/ParseTreeProperty.php | 99 +++++++++++++++++----------------- 1 file changed, 50 insertions(+), 49 deletions(-) diff --git a/src/Tree/ParseTreeProperty.php b/src/Tree/ParseTreeProperty.php index 016bd93..28088c7 100644 --- a/src/Tree/ParseTreeProperty.php +++ b/src/Tree/ParseTreeProperty.php @@ -4,6 +4,8 @@ namespace Antlr\Antlr4\Runtime\Tree; +use \SplObjectStorage; + /** * Associate a property with a parse tree node. * @@ -16,69 +18,68 @@ * * use Antlr\Antlr4\Runtime\Tree\ParseTreeProperty; * - * // Declare a ParseTreeProperty-container for values in your listener + * // Declare a ParseTreeProperty-container for values in your listener. * private $values; * - * // Make a new container in the constructor of the listener + * // 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 + * // 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 + * This class is implemented in PHP as a wrapper around \SplObjectStorage. */ -Class ParseTreeProperty +class ParseTreeProperty { - protected $storage; + protected $storage; - public function __construct() - { - $this->storage = new \SplObjectStorage(); - } + 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)) - { - $value = $this->storage->offsetGet($node); - } - return $value; - } + /** + * 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)) { + $value = $this->storage->offsetGet($node); + } + return $value; + } - /** - * 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); - } + /** + * 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); - $this->storage->detach($node); - return $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); + $this->storage->detach($node); + return $value; + } } From 5c8c8eadffc9a60ec018e9b5a9e0c064cd6b5e18 Mon Sep 17 00:00:00 2001 From: Herman Peeren Date: Thu, 10 Sep 2020 17:02:35 +0200 Subject: [PATCH 4/5] Some blank lines inserted and changed the $storage to private, as result from code review by @marcospassos. --- src/Tree/ParseTreeProperty.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Tree/ParseTreeProperty.php b/src/Tree/ParseTreeProperty.php index 28088c7..100b56a 100644 --- a/src/Tree/ParseTreeProperty.php +++ b/src/Tree/ParseTreeProperty.php @@ -35,7 +35,7 @@ */ class ParseTreeProperty { - protected $storage; + private $storage; public function __construct() { @@ -51,9 +51,11 @@ public function __construct() public function get(ParseTree $node) { $value = null; + if ($this->storage->contains($node)) { $value = $this->storage->offsetGet($node); } + return $value; } @@ -79,6 +81,7 @@ public function removeFrom(ParseTree $node) { $value = $this->get($node); $this->storage->detach($node); + return $value; } From 02f31f21d2693079bddb1004f0b2e9715dcf1672 Mon Sep 17 00:00:00 2001 From: Herman Peeren Date: Thu, 10 Sep 2020 17:10:52 +0200 Subject: [PATCH 5/5] tabs indents replaced by 4 spaces --- src/Tree/ParseTreeProperty.php | 84 +++++++++++++++++----------------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/src/Tree/ParseTreeProperty.php b/src/Tree/ParseTreeProperty.php index 100b56a..c49d513 100644 --- a/src/Tree/ParseTreeProperty.php +++ b/src/Tree/ParseTreeProperty.php @@ -35,54 +35,54 @@ */ class ParseTreeProperty { - private $storage; + private $storage; - public function __construct() - { - $this->storage = new SplObjectStorage(); - } + 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; + /** + * 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)) { - $value = $this->storage->offsetGet($node); - } + if ($this->storage->contains($node)) { + $value = $this->storage->offsetGet($node); + } - return $value; - } + return $value; + } - /** - * 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); - } + /** + * 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); - $this->storage->detach($node); + /** + * 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); + $this->storage->detach($node); - return $value; - } + return $value; + } }