Skip to content

Commit

Permalink
Add Boolean Literals
Browse files Browse the repository at this point in the history
  • Loading branch information
grebaldi committed Nov 6, 2022
1 parent a1ac6f7 commit f58db0a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Definition/Precedence.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
60 changes: 60 additions & 0 deletions src/Parser/Ast/BooleanLiteralNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/**
* PackageFactory.ComponentEngine - Universal View Components for PHP
* Copyright (C) 2022 Contributors of PackageFactory.ComponentEngine
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

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<mixed,Token> $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
];
}
}
6 changes: 5 additions & 1 deletion src/Parser/Ast/ExpressionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
}

Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/Parser/Tokenizer/TokenType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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]+$/',
Expand Down

0 comments on commit f58db0a

Please sign in to comment.