parseCel function
Parses one FFX-CEL expression (05 §3.2). Throws CelParseError (message
- fix) on any grammar or limit violation and on every typing violation
decidable without the model schema — the excluded constructs, malformed
constructors, unknown roots and members, list-literal rules, and operand
types known from the grammar (literals,
request.time,user.id,user.accountType,size(), methods, constructors).
Implementation
CelProgram parseCel(String expression) {
if (expression.trim().isEmpty) {
throw const CelParseError(
'Expression is empty.',
'Provide a boolean FFX-CEL expression.',
);
}
if (expression.length > maxRuleExpressionLength) {
throw CelParseError(
'Expression is ${expression.length} chars; the maximum is $maxRuleExpressionLength.',
'Shorten the expression.',
);
}
final tokens = _tokenize(expression);
var nesting = 0;
for (final token in tokens) {
if (token.isOp('(') || token.isOp('[')) {
if (++nesting > _maxDepth) {
throw const CelParseError(
'Expression nesting exceeds $_maxDepth.',
'Reduce nested parentheses and function calls.',
);
}
} else if (token.isOp(')') || token.isOp(']')) {
nesting--;
}
}
final root = _Parser(tokens).parse();
_checkComplexity(root);
final type = _checkOperand(root, true);
if (type.known && type.kind != _Kind.boolean) {
throw CelParseError(
'The expression has type ${type.name}; rules must be boolean.',
'Wrap the expression in a comparison so it yields true/false.',
);
}
return CelProgram._(expression, root);
}