From 27774c84c6b4a8340ae9eb0bcedbec97bb0a1a13 Mon Sep 17 00:00:00 2001 From: Adam Pippin Date: Wed, 17 Feb 2021 00:27:35 -0800 Subject: [PATCH] Expression bug fix replacing variables; add string literal --- app/Cfnpp/Expression/Expression.php | 32 +++++++-- app/Cfnpp/Expression/Token/StringLiteral.php | 70 ++++++++++++++++++++ 2 files changed, 96 insertions(+), 6 deletions(-) create mode 100644 app/Cfnpp/Expression/Token/StringLiteral.php diff --git a/app/Cfnpp/Expression/Expression.php b/app/Cfnpp/Expression/Expression.php index 19e7f87..b0c4d97 100644 --- a/app/Cfnpp/Expression/Expression.php +++ b/app/Cfnpp/Expression/Expression.php @@ -13,6 +13,7 @@ class Expression Token\NumericLiteral::class, Token\OperatorUnary::class, Token\OperatorBinary::class, + Token\StringLiteral::class, Token\Variable::class ]; @@ -48,6 +49,16 @@ class Expression return $complete; } + public function count() + { + return sizeof($this->nodes); + } + + public function getValue() + { + return $this->nodes[0]->getValue(); + } + public function toArray(): array { return static::unwrap($this->nodes); @@ -111,11 +122,16 @@ class Expression return $stack->get(); } - protected static function solve(array $nodes, array $variables = [], array $parameters = []) + protected static function solve(array $nodes, array $variables = [], array $parameters = []): array { - static::fillVariables($nodes, $variables, $parameters); - static::collapse($nodes); - return $nodes; + $root = new GraphNode(); + foreach ($nodes as $node) + { + $root->appendChild($node); + } + $nodes = static::fillVariables($root->getChildren(), $variables, $parameters); + $nodes = static::collapse($root->getChildren()); + return $root->getChildren(); } protected static function execute(array $nodes, array $variables = [], array $parameters = []) @@ -128,6 +144,10 @@ class Expression foreach ($nodes as $node) { $node->walk(static function($node) { + if (is_scalar($node->getValue())) + { + return; + } if (!($node->getValue() instanceof ICloudformationNative)) { throw new \Exception('Token '.basename(get_class($node->getValue())).' is not natively supported by CloudFormation.'); @@ -144,7 +164,7 @@ class Expression return $nodes[0]->getValue(); } - protected static function fillVariables(array $nodes, array $variables, array $parameters) + protected static function fillVariables(array $nodes, array $variables, array $parameters): void { foreach ($nodes as $node) { @@ -169,7 +189,7 @@ class Expression } } - protected static function collapse(array $nodes) + protected static function collapse(array $nodes): void { foreach ($nodes as $node) { diff --git a/app/Cfnpp/Expression/Token/StringLiteral.php b/app/Cfnpp/Expression/Token/StringLiteral.php new file mode 100644 index 0000000..9114e3d --- /dev/null +++ b/app/Cfnpp/Expression/Token/StringLiteral.php @@ -0,0 +1,70 @@ +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(); + } +}