@abaplint/core
Version:
abaplint - Core API
55 lines • 3.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CDSArithmetics = void 0;
const _1 = require(".");
const combi_1 = require("../../abap/2_statements/combi");
const cds_integer_1 = require("./cds_integer");
/**
* Arithmetic expression: one or more values joined by +, -, *, /.
*
* Parenthesized sub-expressions of any nesting depth are handled via the
* separate CDSArithParen singleton (which wraps back to CDSArithmetics).
* The mutual reference between two distinct singletons enables true n-level
* nesting with no fixed limit and no infinite recursion.
*
* Grammar (simplified):
* CDSArithmetics → operand (op operand)+ -- with-operator form
* | unary val -- unary form
* | unary val (op operand)+ -- unary + continuation
* | unary CDSArithParen -- unary applied to paren, e.g. -(field)
* | unary CDSArithParen (op op)+ -- unary paren + continuation
* operand → CDSArithParen | val
* CDSArithParen → "(" CDSArithmetics ")" | "(" CDSArithParen ")" | "(" val ")"
*/
class CDSArithmetics extends combi_1.Expression {
getRunnable() {
const val = (0, combi_1.altPrio)(cds_integer_1.CDSInteger, _1.CDSFunction, _1.CDSCase, _1.CDSCast, _1.CDSString, _1.CDSAggregate, _1.CDSPrefixedName);
const operator = (0, combi_1.altPrio)("+", "-", "*", "/");
// Unary operator prefix, e.g. -field, +field
const unary = (0, combi_1.altPrio)("-", "+");
const unaryExpression = (0, combi_1.seq)(unary, val);
// An operand is a paren, unary-prefixed value, or bare value.
// Including unaryExpression allows "A + + B" and "A + -B" patterns.
// CDSArithParen = "(" altPrio(CDSArithmetics, CDSArithParen, val) ")" — separate singleton that
// can recursively contain itself, enabling deeply nested parentheses without infinite recursion.
const operand = (0, combi_1.altPrio)(_1.CDSArithParen, unaryExpression, val);
const operatorValue = (0, combi_1.seq)(operator, operand);
// Main form: operand op operand op ... (leading term may itself be a paren)
const withOperators = (0, combi_1.seq)(operand, (0, combi_1.plusPrio)(operatorValue));
// Unary followed by optional continuation operators: -1 * field, -field + 1
const unaryWithOps = (0, combi_1.seq)(unaryExpression, (0, combi_1.plusPrio)(operatorValue));
// Top-level parenthesized expression as a standalone field value: (A + B) * C
const parenExpr = (0, combi_1.altPrio)(withOperators, unaryExpression);
const paren = (0, combi_1.seq)("(", parenExpr, ")");
// Unary applied to a parenthesized group, e.g. -(TaxAmount)
const unaryParen = (0, combi_1.seq)(unary, _1.CDSArithParen);
return (0, combi_1.altPrio)((0, combi_1.seq)(paren, (0, combi_1.starPrio)(operatorValue)), // (expr) op ...
(0, combi_1.seq)(unaryParen, (0, combi_1.starPrio)(operatorValue)), // -(paren) op ...
unaryWithOps, // -val op ...
unaryExpression, // -val
unaryParen, // -(paren)
withOperators);
}
}
exports.CDSArithmetics = CDSArithmetics;
//# sourceMappingURL=cds_arithmetics.js.map