Coverage Report

Created: 2026-09-03 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/parser/parser.cc
Line
Count
Source
1
// Copyright 2021 Google LLC
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include "parser/parser.h"
16
17
#include <memory>
18
#include <string>
19
#include <utility>
20
#include <vector>
21
22
#include "cel/expr/syntax.pb.h"
23
#include "absl/status/statusor.h"
24
#include "absl/strings/string_view.h"
25
#include "common/ast.h"
26
#include "common/ast/expr_proto.h"
27
#include "common/ast/source_info_proto.h"
28
#include "common/source.h"
29
#include "internal/status_macros.h"
30
#ifndef EXCLUDE_CEL_ANTLR_PARSER
31
#include "parser/internal/antlr_parser.h"
32
#endif
33
#include "parser/internal/pratt_parser.h"
34
#include "parser/macro.h"
35
#include "parser/macro_registry.h"
36
#include "parser/options.h"
37
#include "parser/parser_interface.h"
38
#include "parser/source_factory.h"
39
40
namespace google::api::expr::parser {
41
42
using ::cel::Macro;
43
using ::cel::ParserOptions;
44
using ::cel::expr::ParsedExpr;
45
using ::google::api::expr::parser::EnrichedSourceInfo;
46
using ::google::api::expr::parser::VerboseParsedExpr;
47
48
absl::StatusOr<VerboseParsedExpr> EnrichedParse(
49
    const cel::Source& source, const cel::MacroRegistry& registry,
50
28.8k
    const ParserOptions& options) {
51
28.8k
  ParsedExpr parsed_expr;
52
28.8k
  EnrichedSourceInfo enriched_source_info;
53
28.8k
  std::unique_ptr<cel::Ast> ast;
54
#ifdef EXCLUDE_CEL_ANTLR_PARSER
55
  CEL_ASSIGN_OR_RETURN(
56
      ast, cel::parser_internal::PrattParseImpl(source, registry, options,
57
                                                /*parse_issues=*/nullptr,
58
                                                &enriched_source_info));
59
#else
60
28.8k
  if (options.enable_pratt_parser) {
61
0
    CEL_ASSIGN_OR_RETURN(
62
0
        ast, cel::parser_internal::PrattParseImpl(source, registry, options,
63
0
                                                  /*parse_issues=*/nullptr,
64
0
                                                  &enriched_source_info));
65
28.8k
  } else {
66
28.8k
    CEL_ASSIGN_OR_RETURN(
67
19.2k
        ast, cel::parser_internal::AntlrParseImpl(source, registry, options,
68
19.2k
                                                  /*parse_issues=*/nullptr,
69
19.2k
                                                  &enriched_source_info));
70
19.2k
  }
71
19.2k
#endif
72
73
19.2k
  CEL_RETURN_IF_ERROR(cel::ast_internal::ExprToProto(
74
19.2k
      ast->root_expr(), parsed_expr.mutable_expr()));
75
19.2k
  CEL_RETURN_IF_ERROR(cel::ast_internal::SourceInfoToProto(
76
19.2k
      ast->source_info(), parsed_expr.mutable_source_info()));
77
19.2k
  return VerboseParsedExpr(std::move(parsed_expr),
78
19.2k
                           std::move(enriched_source_info));
79
19.2k
}
80
81
absl::StatusOr<VerboseParsedExpr> EnrichedParse(
82
    absl::string_view expression, const std::vector<Macro>& macros,
83
29.0k
    absl::string_view description, const ParserOptions& options) {
84
29.0k
  CEL_ASSIGN_OR_RETURN(auto source,
85
28.8k
                       cel::NewSource(expression, std::string(description)));
86
28.8k
  cel::MacroRegistry macro_registry;
87
28.8k
  CEL_RETURN_IF_ERROR(macro_registry.RegisterMacros(macros));
88
28.8k
  return EnrichedParse(*source, macro_registry, options);
89
28.8k
}
90
91
absl::StatusOr<ParsedExpr> ParseWithMacros(absl::string_view expression,
92
                                           const std::vector<Macro>& macros,
93
                                           absl::string_view description,
94
29.0k
                                           const ParserOptions& options) {
95
29.0k
  CEL_ASSIGN_OR_RETURN(auto verbose_parsed_expr,
96
19.2k
                       EnrichedParse(expression, macros, description, options));
97
19.2k
  return verbose_parsed_expr.parsed_expr();
98
29.0k
}
99
100
absl::StatusOr<ParsedExpr> Parse(absl::string_view expression,
101
                                 absl::string_view description,
102
29.0k
                                 const ParserOptions& options) {
103
29.0k
  std::vector<Macro> macros;
104
29.0k
  if (!options.disable_standard_macros) {
105
29.0k
    macros = Macro::AllMacros();
106
29.0k
  }
107
29.0k
  if (options.enable_optional_syntax) {
108
0
    macros.push_back(cel::OptMapMacro());
109
0
    macros.push_back(cel::OptFlatMapMacro());
110
0
  }
111
29.0k
  return ParseWithMacros(expression, macros, description, options);
112
29.0k
}
113
114
absl::StatusOr<::cel::expr::ParsedExpr> Parse(
115
    const cel::Source& source, const cel::MacroRegistry& registry,
116
0
    const ParserOptions& options) {
117
0
  CEL_ASSIGN_OR_RETURN(auto verbose_expr,
118
0
                       EnrichedParse(source, registry, options));
119
0
  return verbose_expr.parsed_expr();
120
0
}
121
122
}  // namespace google::api::expr::parser
123
124
namespace cel {
125
126
0
std::unique_ptr<ParserBuilder> NewParserBuilder(const ParserOptions& options) {
127
#ifdef EXCLUDE_CEL_ANTLR_PARSER
128
  return parser_internal::NewPrattParserBuilder(options);
129
#else
130
0
  if (options.enable_pratt_parser) {
131
0
    return parser_internal::NewPrattParserBuilder(options);
132
0
  }
133
0
  return parser_internal::NewAntlrParserBuilder(options);
134
0
#endif
135
0
}
136
137
}  // namespace cel