|
27 | 27 | #include "checker/type_checker.h" |
28 | 28 | #include "checker/type_checker_builder.h" |
29 | 29 | #include "checker/validation_result.h" |
| 30 | +#include "common/source.h" |
30 | 31 | #include "parser/options.h" |
31 | 32 | #include "parser/parser_interface.h" |
32 | 33 | #include "validator/validator.h" |
@@ -129,9 +130,18 @@ class Compiler { |
129 | 130 | public: |
130 | 131 | virtual ~Compiler() = default; |
131 | 132 |
|
132 | | - virtual absl::StatusOr<ValidationResult> Compile( |
| 133 | + absl::StatusOr<ValidationResult> Compile( |
| 134 | + const Source& source, google::protobuf::Arena* absl_nullable arena) const { |
| 135 | + return CompileImpl(source, arena); |
| 136 | + } |
| 137 | + |
| 138 | + absl::StatusOr<ValidationResult> Compile(const Source& source) const { |
| 139 | + return CompileImpl(source, nullptr); |
| 140 | + } |
| 141 | + |
| 142 | + absl::StatusOr<ValidationResult> Compile( |
133 | 143 | absl::string_view source, absl::string_view description, |
134 | | - google::protobuf::Arena* absl_nullable arena) const = 0; |
| 144 | + google::protobuf::Arena* absl_nullable arena) const; |
135 | 145 |
|
136 | 146 | absl::StatusOr<ValidationResult> Compile(absl::string_view source) const { |
137 | 147 | return Compile(source, "<input>", nullptr); |
@@ -159,8 +169,27 @@ class Compiler { |
159 | 169 | // The returned builder does not share state with the compiler and may be |
160 | 170 | // modified independently. |
161 | 171 | virtual std::unique_ptr<CompilerBuilder> ToBuilder() const = 0; |
| 172 | + |
| 173 | + protected: |
| 174 | + virtual absl::StatusOr<ValidationResult> CompileImpl( |
| 175 | + const Source& source, google::protobuf::Arena* absl_nullable arena) const = 0; |
162 | 176 | }; |
163 | 177 |
|
| 178 | +inline absl::StatusOr<ValidationResult> Compiler::Compile( |
| 179 | + absl::string_view source, absl::string_view description, |
| 180 | + google::protobuf::Arena* absl_nullable arena) const { |
| 181 | + absl::StatusOr<SourcePtr> source_obj = |
| 182 | + NewSource(source, std::string(description)); |
| 183 | + if (!source_obj.ok()) { |
| 184 | + return source_obj.status(); |
| 185 | + } |
| 186 | + absl::StatusOr<ValidationResult> result = CompileImpl(**source_obj, arena); |
| 187 | + if (result.ok()) { |
| 188 | + result->SetSource(std::move(*source_obj)); |
| 189 | + } |
| 190 | + return result; |
| 191 | +} |
| 192 | + |
164 | 193 | } // namespace cel |
165 | 194 |
|
166 | 195 | #endif // THIRD_PARTY_CEL_CPP_COMPILER_COMPILER_INTERFACE_H_ |
0 commit comments