Search in sources :

Example 1 with CELParser

use of org.projectnessie.cel.parser.gen.CELParser 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)

Aggregations

Expr (com.google.api.expr.v1alpha1.Expr)1 Errors (org.projectnessie.cel.common.Errors)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