Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions src/Parser/Ast/ExpressionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -154,6 +147,8 @@ public static function fromTokens(\Iterator $tokens, Precedence $precedence = Pr
default:
break 2;
}

Scanner::skipSpaceAndComments($tokens);
}

return new self(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -46,8 +49,24 @@ public function ternaryOperationExamples(): array
];
}

/**
* @return array<string,mixed>
*/
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL :) I did not know this was possible :D

* @test
* @param string $ternaryOperationAsString
* @param string $expectedTranspilationResult
Expand All @@ -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);
Expand All @@ -70,4 +99,4 @@ public function transpilesTernaryOperationNodes(string $ternaryOperationAsString
$actualTranspilationResult
);
}
}
}