Search in sources :

Example 1 with Errors

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());
}
Also used : CommonTokenStream(org.projectnessie.cel.shaded.org.antlr.v4.runtime.CommonTokenStream) CELLexer(org.projectnessie.cel.parser.gen.CELLexer) CELParser(org.projectnessie.cel.parser.gen.CELParser) Errors(org.projectnessie.cel.common.Errors) Expr(com.google.api.expr.v1alpha1.Expr)

Example 2 with Errors

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()));
}
Also used : Errors(org.projectnessie.cel.common.Errors) AstPruner.pruneAst(org.projectnessie.cel.interpreter.AstPruner.pruneAst) CEL.parsedExprToAst(org.projectnessie.cel.CEL.parsedExprToAst) ParseResult(org.projectnessie.cel.parser.Parser.ParseResult) CheckResult(org.projectnessie.cel.checker.Checker.CheckResult) CheckedExpr(com.google.api.expr.v1alpha1.CheckedExpr) CheckerEnv(org.projectnessie.cel.checker.CheckerEnv) ParsedExpr(com.google.api.expr.v1alpha1.ParsedExpr) CEL.astToParsedExpr(org.projectnessie.cel.CEL.astToParsedExpr)

Aggregations

Errors (org.projectnessie.cel.common.Errors)2 CheckedExpr (com.google.api.expr.v1alpha1.CheckedExpr)1 Expr (com.google.api.expr.v1alpha1.Expr)1 ParsedExpr (com.google.api.expr.v1alpha1.ParsedExpr)1 CEL.astToParsedExpr (org.projectnessie.cel.CEL.astToParsedExpr)1 CEL.parsedExprToAst (org.projectnessie.cel.CEL.parsedExprToAst)1 CheckResult (org.projectnessie.cel.checker.Checker.CheckResult)1 CheckerEnv (org.projectnessie.cel.checker.CheckerEnv)1 AstPruner.pruneAst (org.projectnessie.cel.interpreter.AstPruner.pruneAst)1 ParseResult (org.projectnessie.cel.parser.Parser.ParseResult)1 CELLexer (org.projectnessie.cel.parser.gen.CELLexer)1 CELParser (org.projectnessie.cel.parser.gen.CELParser)1 CommonTokenStream (org.projectnessie.cel.shaded.org.antlr.v4.runtime.CommonTokenStream)1