/proc/self/cwd/parser/internal/antlr_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_ANTLR_PARSER_H_ |
16 | | #define THIRD_PARTY_CEL_CPP_PARSER_INTERNAL_ANTLR_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 | | class AntlrParserImpl final : public cel::Parser { |
43 | | public: |
44 | | explicit AntlrParserImpl(const cel::ParserOptions& options, |
45 | | cel::MacroRegistry macro_registry, |
46 | | absl::flat_hash_set<std::string> library_ids) |
47 | 0 | : options_(options), |
48 | 0 | macro_registry_(std::move(macro_registry)), |
49 | 0 | library_ids_(std::move(library_ids)) {} |
50 | | |
51 | 0 | ~AntlrParserImpl() override = default; |
52 | | |
53 | | absl::StatusOr<std::unique_ptr<cel::Ast>> ParseImpl( |
54 | | const cel::Source& source, |
55 | | std::vector<cel::ParseIssue>* absl_nullable parse_issues) const override; |
56 | | |
57 | | absl::StatusOr<std::unique_ptr<cel::Source>> PrepareSourceImpl( |
58 | | absl::string_view input, absl::string_view description) const override; |
59 | | |
60 | | std::unique_ptr<cel::ParserBuilder> ToBuilder() const override; |
61 | | |
62 | | private: |
63 | | cel::ParserOptions options_; |
64 | | cel::MacroRegistry macro_registry_; |
65 | | absl::flat_hash_set<std::string> library_ids_; |
66 | | }; |
67 | | |
68 | | absl::StatusOr<std::unique_ptr<cel::Ast>> AntlrParseImpl( |
69 | | const cel::Source& source, const cel::MacroRegistry& registry, |
70 | | const ParserOptions& options, |
71 | | std::vector<cel::ParseIssue>* parse_issues = nullptr, |
72 | | cel::EnrichedSourceInfo* enriched_source_info = nullptr); |
73 | | |
74 | | class AntlrParserBuilderImpl final : public cel::ParserBuilder { |
75 | | public: |
76 | | explicit AntlrParserBuilderImpl(const cel::ParserOptions& options) |
77 | 0 | : options_(options) {} |
78 | | |
79 | 0 | cel::ParserOptions& GetOptions() override { return options_; } |
80 | | |
81 | | absl::Status AddMacro(const cel::Macro& macro) override; |
82 | | |
83 | | absl::Status AddLibrary(cel::ParserLibrary library) override; |
84 | | |
85 | | absl::Status AddLibrarySubset(cel::ParserLibrarySubset subset) override; |
86 | | |
87 | | absl::StatusOr<std::unique_ptr<cel::Parser>> Build() override; |
88 | | |
89 | | private: |
90 | | friend class AntlrParserImpl; |
91 | | |
92 | | cel::ParserOptions options_; |
93 | | std::vector<cel::Macro> macros_; |
94 | | absl::flat_hash_set<std::string> library_ids_; |
95 | | std::vector<cel::ParserLibrary> libraries_; |
96 | | absl::flat_hash_map<std::string, cel::ParserLibrarySubset> library_subsets_; |
97 | | }; |
98 | | |
99 | | inline std::unique_ptr<cel::ParserBuilder> NewAntlrParserBuilder( |
100 | 0 | const cel::ParserOptions& options = cel::ParserOptions()) { |
101 | 0 | return std::make_unique<AntlrParserBuilderImpl>(options); |
102 | 0 | } |
103 | | |
104 | | } // namespace cel::parser_internal |
105 | | |
106 | | #endif // THIRD_PARTY_CEL_CPP_PARSER_INTERNAL_ANTLR_PARSER_H_ |