Skip to content

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions src/Tree/ParseTreeProperty.php
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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not private?

Copy link
Author

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.


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)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank line before

$value = $this->storage->offsetGet($node);
}
return $value;
Copy link
Collaborator

Choose a reason for hiding this comment

The 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);
Copy link
Collaborator

Choose a reason for hiding this comment

The 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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put a line before the return statement

}

}