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.
94 lines
1.5 KiB
94 lines
1.5 KiB
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Engine\Cfnpp\Expression;
|
|
|
|
/**
|
|
* Token representing a string literal.
|
|
*
|
|
* @author Adam Pippin <hello@adampippin.ca>
|
|
*/
|
|
class TokenStringLiteral extends Token
|
|
{
|
|
/**
|
|
* Value of the string literal.
|
|
* @var string
|
|
*/
|
|
protected $value;
|
|
|
|
/**
|
|
* Create a new string literal token.
|
|
*
|
|
* @param string $value
|
|
*/
|
|
public function __construct(string $value)
|
|
{
|
|
$this->value = $value;
|
|
}
|
|
|
|
/**
|
|
* Get the value of the string literal.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getValue(): string
|
|
{
|
|
return $this->value;
|
|
}
|
|
|
|
/**
|
|
* Determine whether a string token can be parsed from a stream.
|
|
*
|
|
* @param string $stream
|
|
* @return bool
|
|
*/
|
|
public static function isToken(string $stream): bool
|
|
{
|
|
return $stream[0] == '"';
|
|
}
|
|
|
|
/**
|
|
* Parse a string literal 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 = '';
|
|
$in_string = false;
|
|
$escaped = false;
|
|
|
|
for ($i = 0; $i < strlen($stream); $i++)
|
|
{
|
|
if ($escaped)
|
|
{
|
|
$buffer .= $stream[$i];
|
|
}
|
|
elseif ($stream[$i] == '"')
|
|
{
|
|
if ($in_string)
|
|
{
|
|
break;
|
|
}
|
|
|
|
$in_string = true;
|
|
}
|
|
elseif ($stream[$i] == '\\')
|
|
{
|
|
$escaped = true;
|
|
}
|
|
else
|
|
{
|
|
$buffer .= $stream[$i];
|
|
}
|
|
}
|
|
|
|
$stream = substr($stream, $i + 1);
|
|
|
|
return new TokenStringLiteral($buffer);
|
|
}
|
|
}
|
|
|