diff --git a/src/Parser/Ast/ExpressionNode.php b/src/Parser/Ast/ExpressionNode.php index 5c09d7a1..fb70b0af 100644 --- a/src/Parser/Ast/ExpressionNode.php +++ b/src/Parser/Ast/ExpressionNode.php @@ -119,15 +119,8 @@ public static function fromTokens(\Iterator $tokens, Precedence $precedence = Pr } Scanner::skipSpaceAndComments($tokens); - if (Scanner::isEnd($tokens) || $precedence->mustStopAt(Scanner::type($tokens))) { - return new self( - root: $root - ); - } - - while ($tokens->valid()) { - Scanner::skipSpaceAndComments($tokens); + while (!Scanner::isEnd($tokens) && !$precedence->mustStopAt(Scanner::type($tokens))) { switch (Scanner::type($tokens)) { case TokenType::OPERATOR_BOOLEAN_AND: case TokenType::OPERATOR_BOOLEAN_OR: @@ -154,6 +147,8 @@ public static function fromTokens(\Iterator $tokens, Precedence $precedence = Pr default: break 2; } + + Scanner::skipSpaceAndComments($tokens); } return new self( diff --git a/test/Unit/Target/Php/Transpiler/TernaryOperation/TernaryOperationTranspilerTest.php b/test/Unit/Target/Php/Transpiler/TernaryOperation/TernaryOperationTranspilerTest.php index 1a44e3d3..dd8a6dcb 100644 --- a/test/Unit/Target/Php/Transpiler/TernaryOperation/TernaryOperationTranspilerTest.php +++ b/test/Unit/Target/Php/Transpiler/TernaryOperation/TernaryOperationTranspilerTest.php @@ -23,9 +23,12 @@ namespace PackageFactory\ComponentEngine\Test\Unit\Target\Php\Transpiler\TernaryOperation; use PackageFactory\ComponentEngine\Parser\Ast\ExpressionNode; +use PackageFactory\ComponentEngine\Parser\Ast\StructDeclarationNode; use PackageFactory\ComponentEngine\Parser\Ast\TernaryOperationNode; use PackageFactory\ComponentEngine\Test\Unit\TypeSystem\Scope\Fixtures\DummyScope; use PackageFactory\ComponentEngine\Target\Php\Transpiler\TernaryOperation\TernaryOperationTranspiler; +use PackageFactory\ComponentEngine\TypeSystem\Type\StringType\StringType; +use PackageFactory\ComponentEngine\TypeSystem\Type\StructType\StructType; use PHPUnit\Framework\TestCase; final class TernaryOperationTranspilerTest extends TestCase @@ -46,8 +49,24 @@ public function ternaryOperationExamples(): array ]; } + /** + * @return array + */ + public function ternaryOperationWithVariablesInConditionExamples(): array + { + return [ + 'true === someString ? "a" : "foo"' => ['true === someString ? "a" : "foo"', '((true === $this->someString) ? \'a\' : \'foo\')'], + 'true === someStruct.foo ? "a" : "foo"' => ['true === someStruct.foo ? "a" : "foo"', '((true === $this->someStruct->foo) ? \'a\' : \'foo\')'], + 'true === someStruct.deep.foo ? "a" : "foo"' => ['true === someStruct.deep.foo ? "a" : "foo"', '((true === $this->someStruct->deep->foo) ? \'a\' : \'foo\')'], + 'someStruct.foo === true ? "a" : "foo"' => ['someStruct.foo === true ? "a" : "foo"', '(($this->someStruct->foo === true) ? \'a\' : \'foo\')'], + 'someStruct.foo === true || false ? "a" : "foo"' => ['someStruct.foo === true || false ? "a" : "foo"', '(($this->someStruct->foo === true || false) ? \'a\' : \'foo\')'], + '1 + 2 + 3 === a || 5 * b || c === true && false ? "a" : "foo"' => ['1 + 2 + 3 === a || 5 * b || c === true && false ? "a" : "foo"', '((1 + 2 + 3 === $this->a || 5 * $this->b || $this->c === true && false) ? \'a\' : \'foo\')'], + ]; + } + /** * @dataProvider ternaryOperationExamples + * @dataProvider ternaryOperationWithVariablesInConditionExamples * @test * @param string $ternaryOperationAsString * @param string $expectedTranspilationResult @@ -56,7 +75,17 @@ public function ternaryOperationExamples(): array public function transpilesTernaryOperationNodes(string $ternaryOperationAsString, string $expectedTranspilationResult): void { $ternaryOperationTranspiler = new TernaryOperationTranspiler( - scope: new DummyScope() + scope: new DummyScope([ + "someString" => StringType::get(), + "someStruct" => StructType::fromStructDeclarationNode( + StructDeclarationNode::fromString(<<<'AFX' + struct SomeStruct { + foo: string + deep: ?SomeStruct + } + AFX) + ) + ]) ); $ternaryOperationNode = ExpressionNode::fromString($ternaryOperationAsString)->root; assert($ternaryOperationNode instanceof TernaryOperationNode); @@ -70,4 +99,4 @@ public function transpilesTernaryOperationNodes(string $ternaryOperationAsString $actualTranspilationResult ); } -} \ No newline at end of file +}