formula parser fixes

This commit is contained in:
Yuri Kuznetsov
2023-03-31 22:42:44 +03:00
parent 828d8da741
commit 28d1c052d2
2 changed files with 109 additions and 1 deletions

View File

@@ -298,6 +298,11 @@ class Parser
!$lastStatement->isEndedWithSemicolon()
) {
array_pop($statementList);
} else if (
$lastStatement instanceof StatementRef &&
!$lastStatement->isEndedWithSemicolon()
) {
$lastStatement->setEnd(strlen($string));
}
}
@@ -395,7 +400,10 @@ class Parser
}
if (
$parenthesisCounter === 0 &&
(
$parenthesisCounter === 0 ||
$parenthesisCounter === 1 && $char === '('
) &&
$braceCounter === 0
) {
if ($isLineComment || $isComment) {

View File

@@ -1196,4 +1196,104 @@ class EvaluatorTest extends \PHPUnit\Framework\TestCase
$this->evaluator->process($expression);
}
public function testNoTrailingSemicolon1(): void
{
$expression = "1;2 ";
$this->evaluator->process($expression);
$this->assertTrue(true);
}
public function testNoTrailingSemicolon2(): void
{
$expression = "1;2 ";
$this->evaluator->process($expression);
$this->assertTrue(true);
}
public function testNoTrailingSemicolon3(): void
{
$expression = "1; 2 ";
$this->evaluator->process($expression);
$this->assertTrue(true);
}
public function testNoTrailingSemicolon4(): void
{
$expression = "ifThen(1, \$a = 1; \$a = \$a + 1)";
$vars = (object) [];
$this->evaluator->process($expression, null, $vars);
$this->assertEquals(2, $vars->a);;
}
public function testSemicolonAndParentheses1(): void
{
$expression = "(2);";
$this->evaluator->process($expression);
$this->assertTrue(true);
}
public function testSemicolonAndParentheses2(): void
{
$expression = "(2 );";
$this->evaluator->process($expression);
$this->assertTrue(true);
}
public function testSemicolonAndParentheses3(): void
{
$expression = "(2);3;";
$this->evaluator->process($expression);
$this->assertTrue(true);
}
public function testSemicolonAndParentheses4(): void
{
$expression = "(2); (2);";
$this->evaluator->process($expression);
$this->assertTrue(true);
}
public function testSemicolonAndParentheses5(): void
{
$expression = "
\$j = 0;
\$a = list(0, 1);
ifThen(
1,
\$i = 0;
while(
\$i < array\length(\$a),
(
\$j = \$j + 1;
);
\$i = \$i + 1;
)
);
";
$vars = (object) [];
$this->evaluator->process($expression, null, $vars);
$this->assertEquals(2, $vars->j);;
}
}