formula comments fix

This commit is contained in:
Yuri Kuznetsov
2020-06-22 21:26:51 +03:00
parent c11fd843d0
commit 0f2a26b744
3 changed files with 162 additions and 30 deletions
@@ -58,6 +58,8 @@ class SetPassword extends Base
$password = $this->ask();
$password = trim($password);
if (!$password) {
$this->out("Password can not be empty.\n");
die;
+64 -30
View File
@@ -109,6 +109,8 @@ class Parser
{
$isString = false;
$isSingleQuote = false;
$isComment = false;
$isLineComment = false;
$modifiedString = $string;
@@ -116,27 +118,31 @@ class Parser
for ($i = 0; $i < strlen($string); $i++) {
$isStringStart = false;
if ($string[$i] === "'" && ($i === 0 || $string[$i - 1] !== "\\")) {
if (!$isString) {
$isString = true;
$isSingleQuote = true;
$isStringStart = true;
} else {
if ($isSingleQuote) {
$isString = false;
if (!$isLineComment && !$isComment) {
if ($string[$i] === "'" && ($i === 0 || $string[$i - 1] !== "\\")) {
if (!$isString) {
$isString = true;
$isSingleQuote = true;
$isStringStart = true;
} else {
if ($isSingleQuote) {
$isString = false;
}
}
}
} else if ($string[$i] === "\"" && ($i === 0 || $string[$i - 1] !== "\\")) {
if (!$isString) {
$isString = true;
$isStringStart = true;
$isSingleQuote = false;
} else {
if (!$isSingleQuote) {
$isString = false;
} else if ($string[$i] === "\"" && ($i === 0 || $string[$i - 1] !== "\\")) {
if (!$isString) {
$isString = true;
$isStringStart = true;
$isSingleQuote = false;
} else {
if (!$isSingleQuote) {
$isString = false;
}
}
}
}
if ($isString) {
if ($string[$i] === '(' || $string[$i] === ')') {
$modifiedString[$i] = '_';
@@ -144,24 +150,51 @@ class Parser
$modifiedString[$i] = ' ';
}
} else {
if ($string[$i] === '(') {
$braceCounter++;
}
if ($string[$i] === ')') {
$braceCounter--;
}
if (!$isLineComment && !$isComment) {
if ($braceCounter === 0) {
if (!is_null($splitterIndexList)) {
if ($string[$i] === ';') {
$splitterIndexList[] = $i;
if (!$isComment) {
if ($i && $string[$i] === '/' && $string[$i - 1] === '/') {
$isLineComment = true;
}
}
if ($intoOneLine) {
if ($string[$i] === "\r" || $string[$i] === "\n" || $string[$i] === "\t") {
$string[$i] = ' ';
if (!$isLineComment) {
if ($i && $string[$i] === '*' && $string[$i - 1] === '/') {
$isComment = true;
}
}
if ($string[$i] === '(') {
$braceCounter++;
}
if ($string[$i] === ')') {
$braceCounter--;
}
if ($braceCounter === 0) {
if (!is_null($splitterIndexList)) {
if ($string[$i] === ';') {
$splitterIndexList[] = $i;
}
}
if ($intoOneLine) {
if ($string[$i] === "\r" || $string[$i] === "\n" || $string[$i] === "\t") {
$string[$i] = ' ';
}
}
}
}
if ($isLineComment) {
if ($string[$i] === "\n") {
$isLineComment = false;
}
}
if ($isComment) {
if ($string[$i - 1] === "*" && $string[$i] === "/") {
$isComment = false;
}
}
}
}
@@ -181,6 +214,7 @@ class Parser
$this->processStrings($expression, $modifiedExpression, $splitterIndexList, true);
//echo $modifiedExpression;
$this->stripComments($expression, $modifiedExpression);
foreach ($splitterIndexList as $i => $index) {
@@ -188,4 +188,100 @@ class EvaluatorTest extends \PHPUnit\Framework\TestCase
$this->assertEquals([0, 1, 2], $vars->target);
}
public function testComment1()
{
$expression = "
// test
\$test = '1';
";
$vars = (object) [];
$this->evaluator->process($expression, null, $vars);
$this->assertEquals('1', $vars->test);
}
public function testComment2()
{
$expression = "
// test'test
\$test = '1';
";
$vars = (object) [];
$this->evaluator->process($expression, null, $vars);
$this->assertEquals('1', $vars->test);
}
public function testComment3()
{
$expression = "
// test\"test
\$test = '1';
";
$vars = (object) [];
$this->evaluator->process($expression, null, $vars);
$this->assertEquals('1', $vars->test);
}
public function testComment4()
{
$expression = "
// test)(test
\$test = '1';
";
$vars = (object) [];
$this->evaluator->process($expression, null, $vars);
$this->assertEquals('1', $vars->test);
}
public function testComment5()
{
$expression = "
/* test'test
*/
\$test = '1';
";
$vars = (object) [];
$this->evaluator->process($expression, null, $vars);
$this->assertEquals('1', $vars->test);
}
public function testComment6()
{
$expression = "
/* test(test
*/
\$test = '1';
";
$vars = (object) [];
$this->evaluator->process($expression, null, $vars);
$this->assertEquals('1', $vars->test);
}
public function testComment7()
{
$expression = "
\$test = '/* 1 */';
";
$vars = (object) [];
$this->evaluator->process($expression, null, $vars);
$this->assertEquals('/* 1 */', $vars->test);
}
public function testComment8()
{
$expression = "
\$test = '// 1 */';
";
$vars = (object) [];
$this->evaluator->process($expression, null, $vars);
$this->assertEquals('// 1 */', $vars->test);
}
}