/proc/self/cwd/parser/parser.cc
Line | Count | Source |
1 | | // Copyright 2021 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 | | #include "parser/parser.h" |
16 | | |
17 | | #include <memory> |
18 | | #include <string> |
19 | | #include <utility> |
20 | | #include <vector> |
21 | | |
22 | | #include "cel/expr/syntax.pb.h" |
23 | | #include "absl/status/statusor.h" |
24 | | #include "absl/strings/string_view.h" |
25 | | #include "common/ast.h" |
26 | | #include "common/ast/expr_proto.h" |
27 | | #include "common/ast/source_info_proto.h" |
28 | | #include "common/source.h" |
29 | | #include "internal/status_macros.h" |
30 | | #ifndef EXCLUDE_CEL_ANTLR_PARSER |
31 | | #include "parser/internal/antlr_parser.h" |
32 | | #endif |
33 | | #include "parser/internal/pratt_parser.h" |
34 | | #include "parser/macro.h" |
35 | | #include "parser/macro_registry.h" |
36 | | #include "parser/options.h" |
37 | | #include "parser/parser_interface.h" |
38 | | #include "parser/source_factory.h" |
39 | | |
40 | | namespace google::api::expr::parser { |
41 | | |
42 | | using ::cel::Macro; |
43 | | using ::cel::ParserOptions; |
44 | | using ::cel::expr::ParsedExpr; |
45 | | using ::google::api::expr::parser::EnrichedSourceInfo; |
46 | | using ::google::api::expr::parser::VerboseParsedExpr; |
47 | | |
48 | | absl::StatusOr<VerboseParsedExpr> EnrichedParse( |
49 | | const cel::Source& source, const cel::MacroRegistry& registry, |
50 | 35.3k | const ParserOptions& options) { |
51 | 35.3k | ParsedExpr parsed_expr; |
52 | 35.3k | EnrichedSourceInfo enriched_source_info; |
53 | 35.3k | std::unique_ptr<cel::Ast> ast; |
54 | | #ifdef EXCLUDE_CEL_ANTLR_PARSER |
55 | | CEL_ASSIGN_OR_RETURN( |
56 | | ast, cel::parser_internal::PrattParseImpl(source, registry, options, |
57 | | /*parse_issues=*/nullptr, |
58 | | &enriched_source_info)); |
59 | | #else |
60 | 35.3k | if (options.enable_pratt_parser) { |
61 | 0 | CEL_ASSIGN_OR_RETURN( |
62 | 0 | ast, cel::parser_internal::PrattParseImpl(source, registry, options, |
63 | 0 | /*parse_issues=*/nullptr, |
64 | 0 | &enriched_source_info)); |
65 | 35.3k | } else { |
66 | 35.3k | CEL_ASSIGN_OR_RETURN( |
67 | 22.4k | ast, cel::parser_internal::AntlrParseImpl(source, registry, options, |
68 | 22.4k | /*parse_issues=*/nullptr, |
69 | 22.4k | &enriched_source_info)); |
70 | 22.4k | } |
71 | 22.4k | #endif |
72 | | |
73 | 22.4k | CEL_RETURN_IF_ERROR(cel::ast_internal::ExprToProto( |
74 | 22.4k | ast->root_expr(), parsed_expr.mutable_expr())); |
75 | 22.4k | CEL_RETURN_IF_ERROR(cel::ast_internal::SourceInfoToProto( |
76 | 22.4k | ast->source_info(), parsed_expr.mutable_source_info())); |
77 | 22.4k | return VerboseParsedExpr(std::move(parsed_expr), |
78 | 22.4k | std::move(enriched_source_info)); |
79 | 22.4k | } |
80 | | |
81 | | absl::StatusOr<VerboseParsedExpr> EnrichedParse( |
82 | | absl::string_view expression, const std::vector<Macro>& macros, |
83 | 35.6k | absl::string_view description, const ParserOptions& options) { |
84 | 35.6k | CEL_ASSIGN_OR_RETURN(auto source, |
85 | 35.3k | cel::NewSource(expression, std::string(description))); |
86 | 35.3k | cel::MacroRegistry macro_registry; |
87 | 35.3k | CEL_RETURN_IF_ERROR(macro_registry.RegisterMacros(macros)); |
88 | 35.3k | return EnrichedParse(*source, macro_registry, options); |
89 | 35.3k | } |
90 | | |
91 | | absl::StatusOr<ParsedExpr> ParseWithMacros(absl::string_view expression, |
92 | | const std::vector<Macro>& macros, |
93 | | absl::string_view description, |
94 | 35.6k | const ParserOptions& options) { |
95 | 35.6k | CEL_ASSIGN_OR_RETURN(auto verbose_parsed_expr, |
96 | 22.4k | EnrichedParse(expression, macros, description, options)); |
97 | 22.4k | return verbose_parsed_expr.parsed_expr(); |
98 | 35.6k | } |
99 | | |
100 | | absl::StatusOr<ParsedExpr> Parse(absl::string_view expression, |
101 | | absl::string_view description, |
102 | 35.6k | const ParserOptions& options) { |
103 | 35.6k | std::vector<Macro> macros; |
104 | 35.6k | if (!options.disable_standard_macros) { |
105 | 35.6k | macros = Macro::AllMacros(); |
106 | 35.6k | } |
107 | 35.6k | if (options.enable_optional_syntax) { |
108 | 0 | macros.push_back(cel::OptMapMacro()); |
109 | 0 | macros.push_back(cel::OptFlatMapMacro()); |
110 | 0 | } |
111 | 35.6k | return ParseWithMacros(expression, macros, description, options); |
112 | 35.6k | } |
113 | | |
114 | | absl::StatusOr<::cel::expr::ParsedExpr> Parse( |
115 | | const cel::Source& source, const cel::MacroRegistry& registry, |
116 | 0 | const ParserOptions& options) { |
117 | 0 | CEL_ASSIGN_OR_RETURN(auto verbose_expr, |
118 | 0 | EnrichedParse(source, registry, options)); |
119 | 0 | return verbose_expr.parsed_expr(); |
120 | 0 | } |
121 | | |
122 | | } // namespace google::api::expr::parser |
123 | | |
124 | | namespace cel { |
125 | | |
126 | 0 | std::unique_ptr<ParserBuilder> NewParserBuilder(const ParserOptions& options) { |
127 | | #ifdef EXCLUDE_CEL_ANTLR_PARSER |
128 | | return parser_internal::NewPrattParserBuilder(options); |
129 | | #else |
130 | 0 | if (options.enable_pratt_parser) { |
131 | 0 | return parser_internal::NewPrattParserBuilder(options); |
132 | 0 | } |
133 | 0 | return parser_internal::NewAntlrParserBuilder(options); |
134 | 0 | #endif |
135 | 0 | } |
136 | | |
137 | | } // namespace cel |