use of org.projectnessie.cel.common.Errors in project cel-java by projectnessie.
the class Parser method parse.
ParseResult parse(Source source) {
StringCharStream charStream = new StringCharStream(source.content(), source.description());
CELLexer lexer = new CELLexer(charStream);
CELParser parser = new CELParser(new CommonTokenStream(lexer, 0));
RecursionListener parserListener = new RecursionListener(options.getMaxRecursionDepth());
parser.addParseListener(parserListener);
parser.setErrorHandler(new RecoveryLimitErrorStrategy(options.getErrorRecoveryLimit()));
Helper helper = new Helper(source);
Errors errors = new Errors(source);
InnerParser inner = new InnerParser(helper, errors);
lexer.addErrorListener(inner);
parser.addErrorListener(inner);
Expr expr = null;
try {
if (charStream.size() > options.getExpressionSizeCodePointLimit()) {
errors.reportError(Location.NoLocation, "expression code point size exceeds limit: size: %d, limit %d", charStream.size(), options.getExpressionSizeCodePointLimit());
} else {
expr = inner.exprVisit(parser.start());
}
} catch (RecoveryLimitError | RecursionError e) {
errors.reportError(Location.NoLocation, "%s", e.getMessage());
}
if (errors.hasErrors()) {
expr = null;
}
return new ParseResult(expr, errors, helper.getSourceInfo());
}
use of org.projectnessie.cel.common.Errors in project cel-java by projectnessie.
the class Env method check.
/**
* Check performs type-checking on the input Ast and yields a checked Ast and/or set of Issues.
*
* <p>Checking has failed if the returned Issues value and its Issues.Err() value are non-nil.
* Issues should be inspected if they are non-nil, but may not represent a fatal error.
*
* <p>It is possible to have both non-nil Ast and Issues values returned from this call: however,
* the mere presence of an Ast does not imply that it is valid for use.
*/
public AstIssuesTuple check(Ast ast) {
// Note, errors aren't currently possible on the Ast to ParsedExpr conversion.
ParsedExpr pe = astToParsedExpr(ast);
// Construct the internal checker env, erroring if there is an issue adding the declarations.
synchronized (once) {
if (chk == null && chkErr == null) {
CheckerEnv ce = CheckerEnv.newCheckerEnv(container, provider);
ce.enableDynamicAggregateLiterals(true);
if (hasFeature(FeatureDisableDynamicAggregateLiterals)) {
ce.enableDynamicAggregateLiterals(false);
}
try {
ce.add(declarations);
chk = ce;
} catch (RuntimeException e) {
chkErr = e;
} catch (Exception e) {
chkErr = new RuntimeException(e);
}
}
}
// The once call will ensure that this value is set or nil for all invocations.
if (chkErr != null) {
Errors errs = new Errors(ast.getSource());
errs.reportError(NoLocation, "%s", chkErr.toString());
return new AstIssuesTuple(null, newIssues(errs));
}
ParseResult pr = new ParseResult(pe.getExpr(), new Errors(ast.getSource()), pe.getSourceInfo());
CheckResult checkRes = Checker.Check(pr, ast.getSource(), chk);
if (checkRes.hasErrors()) {
return new AstIssuesTuple(null, newIssues(checkRes.getErrors()));
}
// Manually create the Ast to ensure that the Ast source information (which may be more
// detailed than the information provided by Check), is returned to the caller.
CheckedExpr ce = checkRes.getCheckedExpr();
ast = new Ast(ce.getExpr(), ce.getSourceInfo(), ast.getSource(), ce.getReferenceMapMap(), ce.getTypeMapMap());
return new AstIssuesTuple(ast, Issues.noIssues(ast.getSource()));
}
Aggregations