Adam Pippin
3 years ago
2 changed files with 96 additions and 6 deletions
@ -0,0 +1,70 @@ |
|||||
|
<?php |
||||
|
|
||||
|
declare(strict_types=1); |
||||
|
|
||||
|
namespace App\Cfnpp\Expression\Token; |
||||
|
|
||||
|
use App\Cfnpp\Expression\Token; |
||||
|
use App\Cfnpp\Expression\TokenLiteral; |
||||
|
|
||||
|
class StringLiteral extends TokenLiteral |
||||
|
{ |
||||
|
protected $value; |
||||
|
|
||||
|
public function __construct($value) |
||||
|
{ |
||||
|
$this->value = $value; |
||||
|
} |
||||
|
|
||||
|
public function getValue() |
||||
|
{ |
||||
|
return $this->value; |
||||
|
} |
||||
|
|
||||
|
public static function isToken(string $stream): bool |
||||
|
{ |
||||
|
return $stream[0] == '"'; |
||||
|
} |
||||
|
|
||||
|
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]; |
||||
|
$escaped = false; |
||||
|
} |
||||
|
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 StringLiteral($buffer); |
||||
|
} |
||||
|
|
||||
|
public function execute(?array $arguments = null) |
||||
|
{ |
||||
|
return $this->getValue(); |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue