Coverage Report

Created: 2026-09-14 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/parser/internal/pratt_parser.h
Line
Count
Source
1
// Copyright 2026 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
#ifndef THIRD_PARTY_CEL_CPP_PARSER_INTERNAL_PRATT_PARSER_H_
16
#define THIRD_PARTY_CEL_CPP_PARSER_INTERNAL_PRATT_PARSER_H_
17
18
#include <memory>
19
#include <string>
20
#include <utility>
21
#include <vector>
22
23
#include "absl/base/nullability.h"
24
#include "absl/container/flat_hash_map.h"
25
#include "absl/container/flat_hash_set.h"
26
#include "absl/status/status.h"
27
#include "absl/status/statusor.h"
28
#include "absl/strings/string_view.h"
29
#include "common/ast.h"
30
#include "common/source.h"
31
#include "parser/macro.h"
32
#include "parser/macro_registry.h"
33
#include "parser/options.h"
34
#include "parser/parser_interface.h"
35
36
namespace cel {
37
class EnrichedSourceInfo;
38
}  // namespace cel
39
40
namespace cel::parser_internal {
41
42
// PrattParserImpl implements the Pratt parsing algorithm for CEL expressions.
43
//
44
// WARNING: Since this implementation uses recursive descent to parse
45
// expressions, its stack consumption depends on expression nesting and
46
// recursion limits (`ParserOptions::max_recursion_depth`). Note that:
47
// In production builds (e.g., inside fibers with small default stacks such as
48
// 64KB), the available stack space may be much tighter than in default thread
49
// stacks.
50
// Consequently, `ParserOptions::max_recursion_depth` may need to be tuned
51
// depending on the caller environment to prevent stack overflow.
52
class PrattParserImpl final : public cel::Parser {
53
 public:
54
  explicit PrattParserImpl(const cel::ParserOptions& options,
55
                           cel::MacroRegistry macro_registry,
56
                           absl::flat_hash_set<std::string> library_ids)
57
0
      : options_(options),
58
0
        macro_registry_(std::move(macro_registry)),
59
0
        library_ids_(std::move(library_ids)) {}
60
61
0
  ~PrattParserImpl() override = default;
62
63
  absl::StatusOr<std::unique_ptr<cel::Ast>> ParseImpl(
64
      const cel::Source& source,
65
      std::vector<cel::ParseIssue>* absl_nullable parse_issues) const override;
66
67
  absl::StatusOr<std::unique_ptr<cel::Source>> PrepareSourceImpl(
68
      absl::string_view input, absl::string_view description) const override;
69
70
  std::unique_ptr<cel::ParserBuilder> ToBuilder() const override;
71
72
 private:
73
  cel::ParserOptions options_;
74
  cel::MacroRegistry macro_registry_;
75
  absl::flat_hash_set<std::string> library_ids_;
76
};
77
78
absl::StatusOr<std::unique_ptr<cel::Ast>> PrattParseImpl(
79
    const cel::Source& source, const cel::MacroRegistry& registry,
80
    const ParserOptions& options,
81
    std::vector<cel::ParseIssue>* parse_issues = nullptr,
82
    cel::EnrichedSourceInfo* enriched_source_info = nullptr);
83
84
class PrattParserBuilderImpl final : public cel::ParserBuilder {
85
 public:
86
  explicit PrattParserBuilderImpl(const cel::ParserOptions& options)
87
0
      : options_(options) {}
88
89
0
  cel::ParserOptions& GetOptions() override { return options_; }
90
91
  absl::Status AddMacro(const cel::Macro& macro) override;
92
93
  absl::Status AddLibrary(cel::ParserLibrary library) override;
94
95
  absl::Status AddLibrarySubset(cel::ParserLibrarySubset subset) override;
96
97
  absl::StatusOr<std::unique_ptr<cel::Parser>> Build() override;
98
99
 private:
100
  friend class PrattParserImpl;
101
102
  cel::ParserOptions options_;
103
  std::vector<cel::Macro> macros_;
104
  absl::flat_hash_set<std::string> library_ids_;
105
  std::vector<cel::ParserLibrary> libraries_;
106
  absl::flat_hash_map<std::string, cel::ParserLibrarySubset> library_subsets_;
107
};
108
109
inline std::unique_ptr<cel::ParserBuilder> NewPrattParserBuilder(
110
0
    const cel::ParserOptions& options = cel::ParserOptions()) {
111
0
  return std::make_unique<PrattParserBuilderImpl>(options);
112
0
}
113
114
}  // namespace cel::parser_internal
115
116
#endif  // THIRD_PARTY_CEL_CPP_PARSER_INTERNAL_PRATT_PARSER_H_