/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 | | |
20 | | #include "absl/functional/any_invocable.h" |
21 | | #include "absl/status/status.h" |
22 | | #include "absl/status/statusor.h" |
23 | | #include "common/ast.h" |
24 | | #include "common/source.h" |
25 | | #include "parser/macro.h" |
26 | | #include "parser/options.h" |
27 | | |
28 | | namespace cel { |
29 | | |
30 | | class Parser; |
31 | | class ParserBuilder; |
32 | | |
33 | | // Callable for configuring a ParserBuilder. |
34 | | using ParserBuilderConfigurer = |
35 | | absl::AnyInvocable<absl::Status(ParserBuilder&) const>; |
36 | | |
37 | | struct ParserLibrary { |
38 | | // Optional identifier to avoid collisions re-adding the same macros. If |
39 | | // empty, it is not considered for collision detection. |
40 | | std::string id; |
41 | | ParserBuilderConfigurer configure; |
42 | | }; |
43 | | |
44 | | // Declares a subset of a parser library. |
45 | | struct ParserLibrarySubset { |
46 | | // The id of the library to subset. Only one subset can be applied per |
47 | | // library id. |
48 | | // |
49 | | // Must be non-empty. |
50 | | std::string library_id; |
51 | | |
52 | | using MacroPredicate = absl::AnyInvocable<bool(const Macro&) const>; |
53 | | MacroPredicate should_include_macro; |
54 | | }; |
55 | | |
56 | | // Interface for building a CEL parser, see comments on `Parser` below. |
57 | | class ParserBuilder { |
58 | | public: |
59 | 0 | virtual ~ParserBuilder() = default; |
60 | | |
61 | | // Returns the (mutable) current parser options. |
62 | | virtual ParserOptions& GetOptions() = 0; |
63 | | |
64 | | // Adds a macro to the parser. |
65 | | // Standard macros should be automatically added based on parser options. |
66 | | virtual absl::Status AddMacro(const cel::Macro& macro) = 0; |
67 | | |
68 | | virtual absl::Status AddLibrary(ParserLibrary library) = 0; |
69 | | |
70 | | virtual absl::Status AddLibrarySubset(ParserLibrarySubset subset) = 0; |
71 | | |
72 | | // Builds a new parser instance, may error if incompatible macros are added. |
73 | | virtual absl::StatusOr<std::unique_ptr<Parser>> Build() = 0; |
74 | | }; |
75 | | |
76 | | // Interface for stateful CEL parser objects for use with a `Compiler` |
77 | | // (bundled parse and type check). This is not needed for most users: |
78 | | // prefer using the free functions in `parser.h` for more flexibility. |
79 | | class Parser { |
80 | | public: |
81 | 0 | virtual ~Parser() = default; |
82 | | |
83 | | // Parses the given source into a CEL AST. |
84 | | virtual absl::StatusOr<std::unique_ptr<cel::Ast>> Parse( |
85 | | const cel::Source& source) const = 0; |
86 | | }; |
87 | | |
88 | | } // namespace cel |
89 | | |
90 | | #endif // THIRD_PARTY_CEL_CPP_PARSER_PARSER_INTERFACE_H_ |