You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
1.3 KiB
79 lines
1.3 KiB
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Cfnpp\Expression;
|
|
|
|
/**
|
|
* Expression token referencing a variable.
|
|
*
|
|
* @author Adam Pippin <hello@adampippin.ca>
|
|
*/
|
|
class TokenVariable extends Token
|
|
{
|
|
/**
|
|
* Name of the variable this token references.
|
|
* @var string
|
|
*/
|
|
protected $name;
|
|
|
|
/**
|
|
* Create a new variable token.
|
|
*
|
|
* @param string $name
|
|
*/
|
|
public function __construct(string $name)
|
|
{
|
|
$this->name = $name;
|
|
}
|
|
|
|
/**
|
|
* Get the name of the variable this token references.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getName(): string
|
|
{
|
|
return $this->name;
|
|
}
|
|
|
|
/**
|
|
* Determine whether a variable name token can be parsed from a stream.
|
|
*
|
|
* @param string $stream
|
|
* @return bool
|
|
*/
|
|
public static function isToken(string $stream): bool
|
|
{
|
|
return (bool)preg_match('/^[A-Za-z]$/', $stream[0]);
|
|
}
|
|
|
|
/**
|
|
* Parse a variable token from a stream.
|
|
*
|
|
* Returns token, and modifies stream to remove all consumed characters.
|
|
*
|
|
* @param string $stream
|
|
* @return Token
|
|
*/
|
|
public static function getToken(string &$stream): Token
|
|
{
|
|
$buffer = '';
|
|
$buffer = $stream[0];
|
|
for ($i = 1; $i < strlen($stream); $i++)
|
|
{
|
|
if (preg_match('/^[A-Za-z0-9]$/', $stream[$i]))
|
|
{
|
|
$buffer .= $stream[$i];
|
|
}
|
|
else
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
$stream = substr($stream, strlen($buffer));
|
|
|
|
return new TokenVariable($buffer);
|
|
}
|
|
}
|
|
|