/proc/self/cwd/parser/parser_interface.h
Line | Count | Source |
1 | | // Copyright 2024 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 | | #ifndef THIRD_PARTY_CEL_CPP_PARSER_PARSER_INTERFACE_H_ |
15 | | #define THIRD_PARTY_CEL_CPP_PARSER_PARSER_INTERFACE_H_ |
16 | | |
17 | | #include <memory> |
18 | | #include <string> |
19 | | #include <utility> |
20 | | #include <vector> |
21 | | |
22 | | #include "absl/base/nullability.h" |
23 | | #include "absl/functional/any_invocable.h" |
24 | | #include "absl/status/status.h" |
25 | | #include "absl/status/statusor.h" |
26 | | #include "absl/strings/string_view.h" |
27 | | #include "common/ast.h" |
28 | | #include "common/source.h" |
29 | | #include "parser/macro.h" |
30 | | #include "parser/options.h" |
31 | | |
32 | | namespace cel { |
33 | | |
34 | | class Parser; |
35 | | class ParserBuilder; |
36 | | |
37 | | // Callable for configuring a ParserBuilder. |
38 | | using ParserBuilderConfigurer = |
39 | | absl::AnyInvocable<absl::Status(ParserBuilder&) const>; |
40 | | |
41 | | struct ParserLibrary { |
42 | | // Optional identifier to avoid collisions re-adding the same macros. If |
43 | | // empty, it is not considered for collision detection. |
44 | | std::string id; |
45 | | ParserBuilderConfigurer configure; |
46 | | }; |
47 | | |
48 | | // Declares a subset of a parser library. |
49 | | struct ParserLibrarySubset { |
50 | | // The id of the library to subset. Only one subset can be applied per |
51 | | // library id. |
52 | | // |
53 | | // Must be non-empty. |
54 | | std::string library_id; |
55 | | |
56 | | using MacroPredicate = absl::AnyInvocable<bool(const Macro&) const>; |
57 | | MacroPredicate should_include_macro; |
58 | | }; |
59 | | |
60 | | // Interface for building a CEL parser, see comments on `Parser` below. |
61 | | class ParserBuilder { |
62 | | public: |
63 | 0 | virtual ~ParserBuilder() = default; |
64 | | |
65 | | // Returns the (mutable) current parser options. |
66 | | virtual ParserOptions& GetOptions() = 0; |
67 | | |
68 | | // Adds a macro to the parser. |
69 | | // Standard macros should be automatically added based on parser options. |
70 | | virtual absl::Status AddMacro(const cel::Macro& macro) = 0; |
71 | | |
72 | | virtual absl::Status AddLibrary(ParserLibrary library) = 0; |
73 | | |
74 | | virtual absl::Status AddLibrarySubset(ParserLibrarySubset subset) = 0; |
75 | | |
76 | | // Builds a new parser instance, may error if incompatible macros are added. |
77 | | virtual absl::StatusOr<std::unique_ptr<Parser>> Build() = 0; |
78 | | }; |
79 | | |
80 | | // Information about a parse failure. |
81 | | class ParseIssue { |
82 | | public: |
83 | 786 | explicit ParseIssue(std::string message) : message_(std::move(message)) {} |
84 | | ParseIssue(SourceLocation location, std::string message) |
85 | 191k | : location_(location), message_(std::move(message)) {} |
86 | | |
87 | | ParseIssue(const ParseIssue& other) = default; |
88 | | ParseIssue& operator=(const ParseIssue& other) = default; |
89 | 192k | ParseIssue(ParseIssue&& other) = default; |
90 | | ParseIssue& operator=(ParseIssue&& other) = default; |
91 | | |
92 | 192k | SourceLocation location() const { return location_; } |
93 | 192k | absl::string_view message() const { return message_; } |
94 | | |
95 | | private: |
96 | | SourceLocation location_; |
97 | | std::string message_; |
98 | | }; |
99 | | |
100 | | // Interface for stateful CEL parser objects for use with a `Compiler` |
101 | | // (bundled parse and type check). This is not needed for most users: |
102 | | // prefer using the free functions in `parser.h` for more flexibility. |
103 | | class Parser { |
104 | | public: |
105 | 0 | virtual ~Parser() = default; |
106 | | |
107 | | // Parses the given source into a CEL AST. |
108 | | absl::StatusOr<std::unique_ptr<cel::Ast>> Parse( |
109 | | const cel::Source& source) const; |
110 | | |
111 | | // Parses the given source into a CEL AST, collecting parse errors in |
112 | | // `issues`. If `issues` is non-null, it will be cleared and all parse |
113 | | // issues will be appended to it. |
114 | | absl::StatusOr<std::unique_ptr<cel::Ast>> Parse( |
115 | | const cel::Source& source, std::vector<ParseIssue>* issues) const; |
116 | | |
117 | | // Returns a builder initialized with the configuration of this parser. |
118 | | virtual std::unique_ptr<ParserBuilder> ToBuilder() const = 0; |
119 | | |
120 | | protected: |
121 | | virtual absl::StatusOr<std::unique_ptr<cel::Ast>> ParseImpl( |
122 | | const cel::Source& source, |
123 | | std::vector<ParseIssue>* absl_nullable parse_issues) const = 0; |
124 | | }; |
125 | | |
126 | | inline absl::StatusOr<std::unique_ptr<cel::Ast>> Parser::Parse( |
127 | 0 | const cel::Source& source) const { |
128 | 0 | return ParseImpl(source, nullptr); |
129 | 0 | } |
130 | | |
131 | | inline absl::StatusOr<std::unique_ptr<cel::Ast>> Parser::Parse( |
132 | 0 | const cel::Source& source, std::vector<ParseIssue>* issues) const { |
133 | 0 | if (issues != nullptr) issues->clear(); |
134 | 0 | return ParseImpl(source, issues); |
135 | 0 | } |
136 | | |
137 | | } // namespace cel |
138 | | |
139 | | #endif // THIRD_PARTY_CEL_CPP_PARSER_PARSER_INTERFACE_H_ |