Skip to content

EspoCRM – Open Source CRM Application

Formula check syntax printing position

Yurii10d637262e77
diff --git a/application/Espo/Core/Formula/Evaluator.php b/application/Espo/Core/Formula/Evaluator.php
index acf77f500c..4f3cdc9076 100644
--- a/application/Espo/Core/Formula/Evaluator.php
+++ b/application/Espo/Core/Formula/Evaluator.php
@@ -40,6 +40,7 @@ use Espo\Core\Formula\Parser\Ast\Attribute;
use Espo\Core\Formula\Parser\Ast\Node;
use Espo\Core\Formula\Parser\Ast\Value;
use Espo\Core\Formula\Parser\Ast\Variable;
+use Espo\Core\Formula\Utils\PositionUtil;
use Espo\Core\Utils\Log;
use Espo\ORM\Entity;
use Espo\Core\InjectableFactory;
@@ -184,10 +185,7 @@ class Evaluator
$excerpt = substr($expression, $position, self::ERROR_PART_LENGTH);
- $line = substr_count($expression, "\n", 0, $position) + 1;
-
- $lastNewline = strrpos(substr($expression, 0, $position), "\n");
- $column = $position - ($lastNewline === false ? -1 : $lastNewline);
+ [$line, $column] = PositionUtil::getLineAndColumn($expression, $position);
$this->log?->info("Failed formula script; line: {line}, column: {column}. Code:\n{excerpt}", [
'excerpt' => $excerpt,
diff --git a/application/Espo/Core/Formula/Exceptions/SyntaxError.php b/application/Espo/Core/Formula/Exceptions/SyntaxError.php
index 66589926f8..d89b20e391 100644
--- a/application/Espo/Core/Formula/Exceptions/SyntaxError.php
+++ b/application/Espo/Core/Formula/Exceptions/SyntaxError.php
@@ -38,15 +38,18 @@ class SyntaxError extends Error
*/
private $shortMessage = null;
+ private ?int $position = null;
+
final public function __construct(string $message = '', int $code = 0, ?Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
- public static function create(string $message, ?string $shortMessage = null): self
+ public static function create(string $message, ?string $shortMessage = null, ?int $position = null): self
{
$obj = new static($message);
$obj->shortMessage = $shortMessage;
+ $obj->position = $position;
return $obj;
}
@@ -55,4 +58,12 @@ class SyntaxError extends Error
{
return $this->shortMessage ?? $this->getMessage();
}
+
+ /**
+ * @since 10.1.0
+ */
+ public function getPosition(): ?int
+ {
+ return $this->position;
+ }
}
diff --git a/application/Espo/Core/Formula/Parser.php b/application/Espo/Core/Formula/Parser.php
index a277c279e7..64b7740c91 100644
--- a/application/Espo/Core/Formula/Parser.php
+++ b/application/Espo/Core/Formula/Parser.php
@@ -1093,7 +1093,7 @@ class Parser
}
if ($functionName === '' || !preg_match($this->functionNameRegExp, $functionName)) {
- throw new SyntaxError("Bad function name `$functionName`.");
+ throw SyntaxError::create("Bad function name `$functionName`.", position: $position);
}
return new Node(
@@ -1105,15 +1105,18 @@ class Parser
}
if (str_contains($expression, ' ')) {
- throw SyntaxError::create("Could not parse.");
+ throw SyntaxError::create("Could not parse.", position: $position);
}
if (!preg_match($this->attributeNameRegExp, $expression)) {
- throw SyntaxError::create("Attribute name `$expression` contains not allowed characters.");
+ throw SyntaxError::create(
+ message: "Attribute name `$expression` contains not allowed characters.",
+ position: $position,
+ );
}
if (str_ends_with($expression, '.')) {
- throw SyntaxError::create("Attribute ends with dot.");
+ throw SyntaxError::create("Attribute ends with dot.", position: $position);
}
return new Attribute(
@@ -1191,8 +1194,8 @@ class Parser
} else if ($statement instanceof IfRef) {
if (!$isRoot || !$statement->isReady()) {
throw SyntaxError::create(
- 'Incorrect if statement usage in expression ' . $expression . '.',
- 'Incorrect if statement.'
+ message: 'Incorrect if statement usage in expression ' . $expression . '.',
+ shortMessage: 'Incorrect if statement.',
);
}
@@ -1242,8 +1245,8 @@ class Parser
} else if ($statement instanceof WhileRef) {
if (!$isRoot || !$statement->isReady()) {
throw SyntaxError::create(
- 'Incorrect while statement usage in expression ' . $expression . '.',
- 'Incorrect while statement.'
+ message: 'Incorrect while statement usage in expression ' . $expression . '.',
+ shortMessage: 'Incorrect while statement.',
);
}
@@ -1277,8 +1280,9 @@ class Parser
if (!$parsedPart) {
throw SyntaxError::create(
- 'Unknown syntax error in expression ' . $expression . '.',
- 'Unknown syntax error.'
+ message: 'Unknown syntax error in expression ' . $expression . '.',
+ shortMessage: 'Unknown syntax error.',
+ position: $position,
);
}
@@ -1480,7 +1484,7 @@ class Parser
}
if ($variable === '' || !preg_match($this->variableNameRegExp, $variable)) {
- throw new SyntaxError("Bad variable name `$variable`.");
+ throw SyntaxError::create("Bad variable name `$variable`.", position: $firstPosition);
}
$secondNode = $this->split($secondPart, position: $secondPosition);
@@ -1541,7 +1545,7 @@ class Parser
}
if ($value === '' || !preg_match($this->variableNameRegExp, $value)) {
- throw new SyntaxError("Bad variable name `$value`.");
+ throw SyntaxError::create("Bad variable name `$value`.", position: $position);
}
if ($isIncrement) {
diff --git a/application/Espo/Core/Formula/Utils/PositionUtil.php b/application/Espo/Core/Formula/Utils/PositionUtil.php
new file mode 100644
index 0000000000..76a2e10ef0
--- /dev/null
+++ b/application/Espo/Core/Formula/Utils/PositionUtil.php
@@ -0,0 +1,49 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero 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 Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Formula\Utils;
+
+/**
+ * @since 10.1.0
+ */
+class PositionUtil
+{
+ /**
+ * @return array{int, int}
+ */
+ public static function getLineAndColumn(string $expression, int $position): array
+ {
+ $line = substr_count($expression, "\n", 0, $position) + 1;
+
+ $lastNewline = strrpos(substr($expression, 0, $position), "\n");
+ $column = $position - ($lastNewline === false ? -1 : $lastNewline);
+
+ return [$line, $column];
+ }
+}
diff --git a/application/Espo/Resources/i18n/en_US/Formula.json b/application/Espo/Resources/i18n/en_US/Formula.json
index 05605e25ac..0c88ea27db 100644
--- a/application/Espo/Resources/i18n/en_US/Formula.json
+++ b/application/Espo/Resources/i18n/en_US/Formula.json
@@ -15,7 +15,8 @@
"runError": "Error.",
"checkSyntaxSuccess": "Syntax is correct.",
"checkSyntaxError": "Syntax error.",
- "emptyScript": "Script is empty."
+ "emptyScript": "Script is empty.",
+ "checkSyntaxErrorPosition": "Line: `{line}`\nColumn: `{column}`"
},
"tooltips": {
"output": "Print values with the function `output\\printLine`."
diff --git a/application/Espo/Tools/Formula/Service.php b/application/Espo/Tools/Formula/Service.php
index 0b3af4934c..e4d49fb85d 100644
--- a/application/Espo/Tools/Formula/Service.php
+++ b/application/Espo/Tools/Formula/Service.php
@@ -55,7 +55,7 @@ class Service
$result = SyntaxCheckResult::createSuccess();
} catch (SyntaxError $e) {
- return SyntaxCheckResult::createError($e);
+ return SyntaxCheckResult::createError($e, $expression);
}
return $result;
diff --git a/application/Espo/Tools/Formula/SyntaxCheckResult.php b/application/Espo/Tools/Formula/SyntaxCheckResult.php
index f8605686a5..efecc57144 100644
--- a/application/Espo/Tools/Formula/SyntaxCheckResult.php
+++ b/application/Espo/Tools/Formula/SyntaxCheckResult.php
@@ -32,15 +32,16 @@ namespace Espo\Tools\Formula;
use Espo\Core\Formula\Exceptions\SyntaxError;
use Espo\Core\Formula\Exceptions\Error;
+use Espo\Core\Formula\Utils\PositionUtil;
use stdClass;
class SyntaxCheckResult
{
- private bool $isSuccess = false;
-
+ private bool $isSuccess;
private ?string $message = null;
-
private ?Error $exception = null;
+ private ?int $line = null;
+ private ?int $column = null;
private function __construct(bool $isSuccess)
{
@@ -52,13 +53,21 @@ class SyntaxCheckResult
return new self(true);
}
- public static function createError(SyntaxError $exception): self
+ public static function createError(SyntaxError $exception, ?string $expression = null): self
{
$obj = new self(false);
+ if ($expression !== null && $exception->getPosition() !== null) {
+ [$line, $column] = PositionUtil::getLineAndColumn($expression, $exception->getPosition());
+
+ $obj->line = $line;
+ $obj->column = $column;
+ }
+
$obj->message = $exception->getShortMessage();
$obj->exception = $exception;
+
return $obj;
}
@@ -87,6 +96,27 @@ class SyntaxCheckResult
$data->message = $this->message;
}
+ if ($this->line !== null && $this->column !== null) {
+ $data->line = $this->line;
+ $data->column = $this->column;
+ }
+
return $data;
}
+
+ /**
+ * @since 10.1.0
+ */
+ public function getLine(): ?int
+ {
+ return $this->line;
+ }
+
+ /**
+ * @since 10.1.0
+ */
+ public function getColumn(): ?int
+ {
+ return $this->column;
+ }
}
diff --git a/client/src/views/fields/formula.js b/client/src/views/fields/formula.js
index 5bb48eda0f..7086c59265 100644
--- a/client/src/views/fields/formula.js
+++ b/client/src/views/fields/formula.js
@@ -476,6 +476,13 @@ class FormulaFieldView extends TextFieldView {
message += ' ' + response.message;
}
+ if (response.line != null && response.column != null) {
+ message += "\n\n" +
+ this.translate('checkSyntaxErrorPosition', 'messages', 'Formula')
+ .replace('{line}', response.line)
+ .replace('{column}', response.column);
+ }
+
Ui.error(message, {
closeButton: true,
});