From f58db0a5d2e0fcb74aea081f50c1c5d1b8d88a11 Mon Sep 17 00:00:00 2001 From: Wilhelm Behncke Date: Sun, 6 Nov 2022 16:43:26 +0100 Subject: [PATCH] Add Boolean Literals --- src/Definition/Precedence.php | 3 +- src/Parser/Ast/BooleanLiteralNode.php | 60 +++++++++++++++++++++++++++ src/Parser/Ast/ExpressionNode.php | 6 ++- src/Parser/Tokenizer/TokenType.php | 4 ++ 4 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 src/Parser/Ast/BooleanLiteralNode.php diff --git a/src/Definition/Precedence.php b/src/Definition/Precedence.php index 1c28d393..18ed2598 100644 --- a/src/Definition/Precedence.php +++ b/src/Definition/Precedence.php @@ -66,7 +66,8 @@ public static function forTokenType(TokenType $tokenType): self TokenType::COMPARATOR_LESS_THAN, TokenType::COMPARATOR_LESS_THAN_OR_EQUAL => self::COMPARISON, - TokenType::COMPARATOR_EQUAL => self::EQUALITY, + TokenType::COMPARATOR_EQUAL, + TokenType::COMPARATOR_NOT_EQUAL => self::EQUALITY, TokenType::OPERATOR_BOOLEAN_AND => self::LOGICAL_AND, diff --git a/src/Parser/Ast/BooleanLiteralNode.php b/src/Parser/Ast/BooleanLiteralNode.php new file mode 100644 index 00000000..0af52a8d --- /dev/null +++ b/src/Parser/Ast/BooleanLiteralNode.php @@ -0,0 +1,60 @@ +. + */ + +declare(strict_types=1); + +namespace PackageFactory\ComponentEngine\Parser\Ast; + +use PackageFactory\ComponentEngine\Parser\Tokenizer\Scanner; +use PackageFactory\ComponentEngine\Parser\Tokenizer\Token; +use PackageFactory\ComponentEngine\Parser\Tokenizer\TokenType; + +final class BooleanLiteralNode implements \JsonSerializable +{ + private function __construct( + public readonly bool $value + ) { + } + + /** + * @param \Iterator $tokens + * @return self + */ + public static function fromTokens(\Iterator $tokens): self + { + Scanner::assertType($tokens, TokenType::KEYWORD_TRUE, TokenType::KEYWORD_FALSE); + + $value = Scanner::type($tokens) === TokenType::KEYWORD_TRUE; + + Scanner::skipOne($tokens); + + return new self( + value: $value + ); + } + + public function jsonSerialize(): mixed + { + return [ + 'type' => 'BooleanLiteralNode', + 'payload' => $this->value + ]; + } +} diff --git a/src/Parser/Ast/ExpressionNode.php b/src/Parser/Ast/ExpressionNode.php index 03e5b336..127800c6 100644 --- a/src/Parser/Ast/ExpressionNode.php +++ b/src/Parser/Ast/ExpressionNode.php @@ -31,7 +31,7 @@ final class ExpressionNode implements \JsonSerializable { private function __construct( - public readonly IdentifierNode | NumberLiteralNode | BinaryOperationNode | AccessNode | TernaryOperationNode | TagNode | StringLiteralNode | MatchNode | TemplateLiteralNode $root + public readonly IdentifierNode | NumberLiteralNode | BinaryOperationNode | AccessNode | TernaryOperationNode | TagNode | StringLiteralNode | MatchNode | TemplateLiteralNode | BooleanLiteralNode $root ) { } @@ -77,6 +77,10 @@ public static function fromTokens(\Iterator $tokens, Precedence $precedence = Pr case TokenType::NUMBER_HEXADECIMAL: $root = NumberLiteralNode::fromTokens($tokens); break; + case TokenType::KEYWORD_TRUE: + case TokenType::KEYWORD_FALSE: + $root = BooleanLiteralNode::fromTokens($tokens); + break; case TokenType::KEYWORD_MATCH: $root = MatchNode::fromTokens($tokens); break; diff --git a/src/Parser/Tokenizer/TokenType.php b/src/Parser/Tokenizer/TokenType.php index 00debe10..f6ff6c44 100644 --- a/src/Parser/Tokenizer/TokenType.php +++ b/src/Parser/Tokenizer/TokenType.php @@ -37,6 +37,8 @@ enum TokenType: string case KEYWORD_MATCH = 'KEYWORD_MATCH'; case KEYWORD_DEFAULT = 'KEYWORD_DEFAULT'; case KEYWORD_RETURN = 'KEYWORD_RETURN'; + case KEYWORD_TRUE = 'KEYWORD_TRUE'; + case KEYWORD_FALSE = 'KEYWORD_FALSE'; case CONSTANT = 'CONSTANT'; @@ -110,6 +112,8 @@ public static function fromBuffer(Buffer $buffer): TokenType $value === 'match' => self::KEYWORD_MATCH, $value === 'default' => self::KEYWORD_DEFAULT, $value === 'return' => self::KEYWORD_RETURN, + $value === 'true' => self::KEYWORD_TRUE, + $value === 'false' => self::KEYWORD_FALSE, (bool) preg_match( '/^0[bB][0-1]+$/',