| 1641 | }; |
| 1642 | |
| 1643 | absl::StatusOr<ParseResult> ParseImpl(const cel::Source& source, |
| 1644 | const cel::MacroRegistry& registry, |
| 1645 | const ParserOptions& options) { |
| 1646 | try { |
| 1647 | CodePointStream input(source.content(), source.description()); |
| 1648 | if (input.size() > options.expression_size_codepoint_limit) { |
| 1649 | return absl::InvalidArgumentError(absl::StrCat( |
| 1650 | "expression size exceeds codepoint limit.", " input size: ", |
| 1651 | input.size(), ", limit: ", options.expression_size_codepoint_limit)); |
| 1652 | } |
| 1653 | CelLexer lexer(&input); |
| 1654 | CommonTokenStream tokens(&lexer); |
| 1655 | CelParser parser(&tokens); |
| 1656 | ExprRecursionListener listener(options.max_recursion_depth); |
| 1657 | absl::string_view accu_var = cel::kAccumulatorVariableName; |
| 1658 | if (options.enable_hidden_accumulator_var) { |
| 1659 | accu_var = cel::kHiddenAccumulatorVariableName; |
| 1660 | } |
| 1661 | ParserVisitor visitor(source, options.max_recursion_depth, accu_var, |
| 1662 | registry, options.add_macro_calls, |
| 1663 | options.enable_optional_syntax, |
| 1664 | options.enable_quoted_identifiers); |
| 1665 | |
| 1666 | lexer.removeErrorListeners(); |
| 1667 | parser.removeErrorListeners(); |
| 1668 | lexer.addErrorListener(&visitor); |
| 1669 | parser.addErrorListener(&visitor); |
| 1670 | parser.addParseListener(&listener); |
| 1671 | |
| 1672 | // Limit the number of error recovery attempts to prevent bad expressions |
| 1673 | // from consuming lots of cpu / memory. |
| 1674 | parser.setErrorHandler(std::make_shared<RecoveryLimitErrorStrategy>( |
| 1675 | options.error_recovery_limit, |
| 1676 | options.error_recovery_token_lookahead_limit)); |
| 1677 | |
| 1678 | Expr expr; |
| 1679 | try { |
| 1680 | expr = ExprFromAny(visitor.visit(parser.start())); |
| 1681 | } catch (const ParseCancellationException& e) { |
| 1682 | if (visitor.HasErrored()) { |
| 1683 | return absl::InvalidArgumentError(visitor.ErrorMessage()); |
| 1684 | } |
| 1685 | return absl::CancelledError(e.what()); |
| 1686 | } |
| 1687 | |
| 1688 | if (visitor.HasErrored()) { |
| 1689 | return absl::InvalidArgumentError(visitor.ErrorMessage()); |
| 1690 | } |
| 1691 | |
| 1692 | return { |
| 1693 | ParseResult{.expr = std::move(expr), |
| 1694 | .source_info = visitor.GetSourceInfo(), |
| 1695 | .enriched_source_info = visitor.enriched_source_info()}}; |
| 1696 | } catch (const std::exception& e) { |
| 1697 | return absl::AbortedError(e.what()); |
| 1698 | } catch (const char* what) { |
| 1699 | // ANTLRv4 has historically thrown C string literals. |
| 1700 | return absl::AbortedError(what); |
nothing calls this directly
no test coverage detected