Coverage Report

Created: 2025-11-29 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/parser/parser.h
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
// CEL does not support calling the parser during C++ static initialization.
16
// Callers must ensure the parser is only invoked after C++ static initializers
17
// are run. Failing to do so is undefined behavior. The current reason for this
18
// is the parser uses ANTLRv4, which also makes no guarantees about being safe
19
// with regard to C++ static initialization. As such, neither do we.
20
21
#ifndef THIRD_PARTY_CEL_CPP_PARSER_PARSER_H_
22
#define THIRD_PARTY_CEL_CPP_PARSER_PARSER_H_
23
24
#include <memory>
25
#include <utility>
26
#include <vector>
27
28
#include "cel/expr/syntax.pb.h"
29
#include "absl/status/statusor.h"
30
#include "absl/strings/string_view.h"
31
#include "common/source.h"
32
#include "parser/macro.h"
33
#include "parser/macro_registry.h"
34
#include "parser/options.h"
35
#include "parser/parser_interface.h"
36
#include "parser/source_factory.h"
37
38
namespace google::api::expr::parser {
39
40
class VerboseParsedExpr {
41
 public:
42
  VerboseParsedExpr(cel::expr::ParsedExpr parsed_expr,
43
                    EnrichedSourceInfo enriched_source_info)
44
1.32k
      : parsed_expr_(std::move(parsed_expr)),
45
1.32k
        enriched_source_info_(std::move(enriched_source_info)) {}
46
47
1.32k
  const cel::expr::ParsedExpr& parsed_expr() const {
48
1.32k
    return parsed_expr_;
49
1.32k
  }
50
0
  const EnrichedSourceInfo& enriched_source_info() const {
51
0
    return enriched_source_info_;
52
0
  }
53
54
 private:
55
  cel::expr::ParsedExpr parsed_expr_;
56
  EnrichedSourceInfo enriched_source_info_;
57
};
58
59
// See comments at the top of the file for information about usage during C++
60
// static initialization.
61
absl::StatusOr<VerboseParsedExpr> EnrichedParse(
62
    absl::string_view expression, const std::vector<Macro>& macros,
63
    absl::string_view description = "<input>",
64
    const ParserOptions& options = ParserOptions());
65
66
// See comments at the top of the file for information about usage during C++
67
// static initialization.
68
absl::StatusOr<cel::expr::ParsedExpr> Parse(
69
    absl::string_view expression, absl::string_view description = "<input>",
70
    const ParserOptions& options = ParserOptions());
71
72
// See comments at the top of the file for information about usage during C++
73
// static initialization.
74
absl::StatusOr<cel::expr::ParsedExpr> ParseWithMacros(
75
    absl::string_view expression, const std::vector<Macro>& macros,
76
    absl::string_view description = "<input>",
77
    const ParserOptions& options = ParserOptions());
78
79
// See comments at the top of the file for information about usage during C++
80
// static initialization.
81
absl::StatusOr<VerboseParsedExpr> EnrichedParse(
82
    const cel::Source& source, const cel::MacroRegistry& registry,
83
    const ParserOptions& options = ParserOptions());
84
85
// See comments at the top of the file for information about usage during C++
86
// static initialization.
87
absl::StatusOr<cel::expr::ParsedExpr> Parse(
88
    const cel::Source& source, const cel::MacroRegistry& registry,
89
    const ParserOptions& options = ParserOptions());
90
91
}  // namespace google::api::expr::parser
92
93
namespace cel {
94
// Creates a new parser builder.
95
//
96
// Intended for use with the Compiler class, most users should prefer the free
97
// functions above for independent parsing of expressions.
98
std::unique_ptr<ParserBuilder> NewParserBuilder(
99
    const ParserOptions& options = {});
100
}  // namespace cel
101
102
#endif  // THIRD_PARTY_CEL_CPP_PARSER_PARSER_H_