Skip to content

Commit eb9e777

Browse files
jnthntatumcopybara-github
authored andcommitted
Refactor parser implementation to build non-proto version of SourceInfo then convert to proto.
PiperOrigin-RevId: 691903333
1 parent eebc4e9 commit eb9e777

File tree

7 files changed

+180
-124
lines changed

7 files changed

+180
-124
lines changed

extensions/protobuf/ast_converters.cc

Lines changed: 63 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,58 @@ absl::StatusOr<Reference> ConvertProtoReferenceToNative(
355355
return ret_val;
356356
}
357357

358+
absl::StatusOr<cel::expr::SourceInfo> ConvertSourceInfoToProto(
359+
const ast_internal::SourceInfo& source_info) {
360+
cel::expr::SourceInfo result;
361+
result.set_syntax_version(source_info.syntax_version());
362+
result.set_location(source_info.location());
363+
364+
for (int32_t line_offset : source_info.line_offsets()) {
365+
result.add_line_offsets(line_offset);
366+
}
367+
368+
for (auto pos_iter = source_info.positions().begin();
369+
pos_iter != source_info.positions().end(); ++pos_iter) {
370+
(*result.mutable_positions())[pos_iter->first] = pos_iter->second;
371+
}
372+
373+
for (auto macro_iter = source_info.macro_calls().begin();
374+
macro_iter != source_info.macro_calls().end(); ++macro_iter) {
375+
ExprPb& dest_macro = (*result.mutable_macro_calls())[macro_iter->first];
376+
CEL_RETURN_IF_ERROR(
377+
protobuf_internal::ExprToProto(macro_iter->second, &dest_macro));
378+
}
379+
380+
for (const auto& extension : source_info.extensions()) {
381+
auto* extension_pb = result.add_extensions();
382+
extension_pb->set_id(extension.id());
383+
auto* version_pb = extension_pb->mutable_version();
384+
version_pb->set_major(extension.version().major());
385+
version_pb->set_minor(extension.version().minor());
386+
387+
for (auto component : extension.affected_components()) {
388+
switch (component) {
389+
case Extension::Component::kParser:
390+
extension_pb->add_affected_components(ExtensionPb::COMPONENT_PARSER);
391+
break;
392+
case Extension::Component::kTypeChecker:
393+
extension_pb->add_affected_components(
394+
ExtensionPb::COMPONENT_TYPE_CHECKER);
395+
break;
396+
case Extension::Component::kRuntime:
397+
extension_pb->add_affected_components(ExtensionPb::COMPONENT_RUNTIME);
398+
break;
399+
default:
400+
extension_pb->add_affected_components(
401+
ExtensionPb::COMPONENT_UNSPECIFIED);
402+
break;
403+
}
404+
}
405+
}
406+
407+
return result;
408+
}
409+
358410
} // namespace internal
359411

360412
namespace {
@@ -370,7 +422,6 @@ using ::cel::ast_internal::CreateStruct;
370422
using ::cel::ast_internal::DynamicType;
371423
using ::cel::ast_internal::ErrorType;
372424
using ::cel::ast_internal::Expr;
373-
using ::cel::ast_internal::Extension;
374425
using ::cel::ast_internal::FunctionType;
375426
using ::cel::ast_internal::Ident;
376427
using ::cel::ast_internal::ListType;
@@ -386,6 +437,7 @@ using ::cel::ast_internal::SourceInfo;
386437
using ::cel::ast_internal::Type;
387438
using ::cel::ast_internal::UnspecifiedType;
388439
using ::cel::ast_internal::WellKnownType;
440+
using ::cel::extensions::protobuf_internal::ExprToProto;
389441

390442
using ExprPb = cel::expr::Expr;
391443
using ParsedExprPb = cel::expr::ParsedExpr;
@@ -446,62 +498,6 @@ absl::Status ConstantToProto(const ast_internal::Constant& source,
446498
source.constant_kind());
447499
}
448500

449-
absl::StatusOr<ExprPb> ExprToProto(const Expr& expr) {
450-
ExprPb proto_expr;
451-
CEL_RETURN_IF_ERROR(protobuf_internal::ExprToProto(expr, &proto_expr));
452-
return proto_expr;
453-
}
454-
455-
absl::StatusOr<SourceInfoPb> SourceInfoToProto(const SourceInfo& source_info) {
456-
SourceInfoPb result;
457-
result.set_syntax_version(source_info.syntax_version());
458-
result.set_location(source_info.location());
459-
460-
for (int32_t line_offset : source_info.line_offsets()) {
461-
result.add_line_offsets(line_offset);
462-
}
463-
464-
for (auto pos_iter = source_info.positions().begin();
465-
pos_iter != source_info.positions().end(); ++pos_iter) {
466-
(*result.mutable_positions())[pos_iter->first] = pos_iter->second;
467-
}
468-
469-
for (auto macro_iter = source_info.macro_calls().begin();
470-
macro_iter != source_info.macro_calls().end(); ++macro_iter) {
471-
ExprPb& dest_macro = (*result.mutable_macro_calls())[macro_iter->first];
472-
CEL_ASSIGN_OR_RETURN(dest_macro, ExprToProto(macro_iter->second));
473-
}
474-
475-
for (const auto& extension : source_info.extensions()) {
476-
auto* extension_pb = result.add_extensions();
477-
extension_pb->set_id(extension.id());
478-
auto* version_pb = extension_pb->mutable_version();
479-
version_pb->set_major(extension.version().major());
480-
version_pb->set_minor(extension.version().minor());
481-
482-
for (auto component : extension.affected_components()) {
483-
switch (component) {
484-
case Extension::Component::kParser:
485-
extension_pb->add_affected_components(ExtensionPb::COMPONENT_PARSER);
486-
break;
487-
case Extension::Component::kTypeChecker:
488-
extension_pb->add_affected_components(
489-
ExtensionPb::COMPONENT_TYPE_CHECKER);
490-
break;
491-
case Extension::Component::kRuntime:
492-
extension_pb->add_affected_components(ExtensionPb::COMPONENT_RUNTIME);
493-
break;
494-
default:
495-
extension_pb->add_affected_components(
496-
ExtensionPb::COMPONENT_UNSPECIFIED);
497-
break;
498-
}
499-
}
500-
}
501-
502-
return result;
503-
}
504-
505501
absl::StatusOr<ReferencePb> ReferenceToProto(const Reference& reference) {
506502
ReferencePb result;
507503

@@ -681,10 +677,11 @@ absl::StatusOr<std::unique_ptr<Ast>> CreateAstFromParsedExpr(
681677
absl::StatusOr<ParsedExprPb> CreateParsedExprFromAst(const Ast& ast) {
682678
const auto& ast_impl = ast_internal::AstImpl::CastFromPublicAst(ast);
683679
ParsedExprPb parsed_expr;
684-
CEL_ASSIGN_OR_RETURN(*parsed_expr.mutable_expr(),
685-
ExprToProto(ast_impl.root_expr()));
686-
CEL_ASSIGN_OR_RETURN(*parsed_expr.mutable_source_info(),
687-
SourceInfoToProto(ast_impl.source_info()));
680+
CEL_RETURN_IF_ERROR(
681+
ExprToProto(ast_impl.root_expr(), parsed_expr.mutable_expr()));
682+
CEL_ASSIGN_OR_RETURN(
683+
*parsed_expr.mutable_source_info(),
684+
internal::ConvertSourceInfoToProto(ast_impl.source_info()));
688685

689686
return parsed_expr;
690687
}
@@ -728,10 +725,11 @@ absl::StatusOr<cel::expr::CheckedExpr> CreateCheckedExprFromAst(
728725
const auto& ast_impl = ast_internal::AstImpl::CastFromPublicAst(ast);
729726
CheckedExprPb checked_expr;
730727
checked_expr.set_expr_version(ast_impl.expr_version());
731-
CEL_ASSIGN_OR_RETURN(*checked_expr.mutable_expr(),
732-
ExprToProto(ast_impl.root_expr()));
733-
CEL_ASSIGN_OR_RETURN(*checked_expr.mutable_source_info(),
734-
SourceInfoToProto(ast_impl.source_info()));
728+
CEL_RETURN_IF_ERROR(
729+
ExprToProto(ast_impl.root_expr(), checked_expr.mutable_expr()));
730+
CEL_ASSIGN_OR_RETURN(
731+
*checked_expr.mutable_source_info(),
732+
internal::ConvertSourceInfoToProto(ast_impl.source_info()));
735733
for (auto it = ast_impl.reference_map().begin();
736734
it != ast_impl.reference_map().end(); ++it) {
737735
ReferencePb& dest_reference =

extensions/protobuf/ast_converters.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ absl::StatusOr<ast_internal::Reference> ConvertProtoReferenceToNative(
4040
absl::StatusOr<ast_internal::Constant> ConvertConstant(
4141
const cel::expr::Constant& constant);
4242

43+
// Conversion utility for the CEL source info representation to the protobuf
44+
// representation.
45+
absl::StatusOr<cel::expr::SourceInfo> ConvertSourceInfoToProto(
46+
const ast_internal::SourceInfo& source_info);
47+
4348
} // namespace internal
4449

4550
// Creates a runtime AST from a parsed-only protobuf AST.

parser/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@ cc_library(
3333
":macro_registry",
3434
":options",
3535
":source_factory",
36+
"//base/ast_internal:expr",
3637
"//common:ast",
3738
"//common:constant",
3839
"//common:expr_factory",
3940
"//common:operators",
4041
"//common:source",
42+
"//extensions/protobuf:ast_converters",
4143
"//extensions/protobuf/internal:ast",
4244
"//internal:lexis",
4345
"//internal:status_macros",

parser/options.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ struct ParserOptions final {
4747

4848
// Enable support for optional syntax.
4949
bool enable_optional_syntax = false;
50+
51+
// Disable standard macros (has, all, exists, exists_one, filter, map).
52+
bool disable_standard_macros = false;
5053
};
5154

5255
} // namespace cel

0 commit comments

Comments
 (0)