Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions parser/internal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,56 @@ cc_library(
],
)

cc_library(
name = "pratt_parser_worker",
srcs = ["pratt_parser_worker.cc"],
hdrs = ["pratt_parser_worker.h"],
deps = [
":ast_factory_interface",
":lexer",
"//common:operators",
"//common:source",
"//internal:lexis",
"//internal:strings",
"//parser:options",
"//parser:parser_interface",
"@com_google_absl//absl/base:nullability",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
"@com_google_absl//absl/types:optional",
"@com_google_absl//absl/types:span",
],
)

cc_library(
name = "pratt_parser",
srcs = ["pratt_parser.cc"],
hdrs = ["pratt_parser.h"],
deps = [
":ast_factory",
":pratt_parser_worker",
"//common:ast",
"//common:expr",
"//common:source",
"//internal:status_macros",
"//parser:macro",
"//parser:macro_registry",
"//parser:options",
"//parser:parser_interface",
"@com_google_absl//absl/base:nullability",
"@com_google_absl//absl/cleanup",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
"@com_google_absl//absl/types:span",
],
)

cc_test(
name = "ast_factory_test",
srcs = ["ast_factory_test.cc"],
Expand All @@ -84,3 +134,29 @@ cc_test(
"//internal:testing",
],
)

cc_test(
name = "pratt_parser_test",
srcs = ["pratt_parser_test.cc"],
deps = [
":lexer",
":pratt_parser",
":pratt_parser_worker",
"//common:ast",
"//common:constant",
"//common:expr",
"//common:source",
"//internal:status_macros",
"//internal:testing",
"//parser:options",
"//parser:parser_interface",
"//testutil:expr_printer",
"@com_google_absl//absl/algorithm:container",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:status_matchers",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
"@com_google_absl//absl/strings:string_view",
],
)
217 changes: 217 additions & 0 deletions parser/internal/pratt_parser.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "parser/internal/pratt_parser.h"

#include <cstdint>
#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

#include "absl/base/nullability.h"
#include "absl/cleanup/cleanup.h"
#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/strings/str_join.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "common/ast.h"
#include "common/expr.h"
#include "common/source.h"
#include "internal/status_macros.h"
#include "parser/internal/ast_factory.h" // IWYU pragma: keep
#include "parser/internal/pratt_parser_worker.h"
#include "parser/macro.h"
#include "parser/macro_registry.h"
#include "parser/options.h"
#include "parser/parser_interface.h"

namespace cel::parser_internal {

namespace {

std::string DisplayParserError(const cel::Source& source,
SourceLocation location,
std::string_view message) {
return absl::StrCat(
absl::StrFormat("ERROR: %s:%zu:%zu: %s", source.description(),
location.line, location.column + 1, message),
source.DisplayErrorLocation(location));
}

std::string FormatIssues(const cel::Source& source,
absl::Span<const cel::ParseIssue> issues) {
return absl::StrJoin(
issues, "\n", [&source](std::string* out, const cel::ParseIssue& issue) {
absl::StrAppend(
out, DisplayParserError(source, issue.location(), issue.message()));
});
}

class PrattParserBuilderImpl final : public cel::ParserBuilder {
public:
explicit PrattParserBuilderImpl(const cel::ParserOptions& options)
: options_(options) {}

cel::ParserOptions& GetOptions() override { return options_; }

absl::Status AddMacro(const cel::Macro& macro) override {
for (const cel::Macro& existing_macro : macros_) {
if (existing_macro.key() == macro.key()) {
return absl::AlreadyExistsError(
absl::StrCat("macro already exists: ", macro.key()));
}
}
macros_.push_back(macro);
return absl::OkStatus();
}

absl::Status AddLibrary(cel::ParserLibrary library) override {
if (!library.id.empty()) {
auto [it, inserted] = library_ids_.insert(library.id);
if (!inserted) {
return absl::AlreadyExistsError(
absl::StrCat("parser library already exists: ", library.id));
}
}
libraries_.push_back(std::move(library));
return absl::OkStatus();
}

absl::Status AddLibrarySubset(cel::ParserLibrarySubset subset) override {
if (subset.library_id.empty()) {
return absl::InvalidArgumentError("subset must have a library id");
}
std::string library_id = subset.library_id;
auto [it, inserted] =
library_subsets_.insert({library_id, std::move(subset)});
if (!inserted) {
return absl::AlreadyExistsError(
absl::StrCat("parser library subset already exists: ", library_id));
}
return absl::OkStatus();
}

absl::StatusOr<std::unique_ptr<cel::Parser>> Build() override {
using std::swap;
std::vector<cel::Macro> individual_macros;
swap(individual_macros, macros_);
absl::Cleanup cleanup([&] { swap(macros_, individual_macros); });

cel::MacroRegistry macro_registry;

for (const cel::ParserLibrary& library : libraries_) {
CEL_RETURN_IF_ERROR(library.configure(*this));
if (!library.id.empty()) {
auto it = library_subsets_.find(library.id);
if (it != library_subsets_.end()) {
const cel::ParserLibrarySubset& subset = it->second;
for (const cel::Macro& macro : macros_) {
if (subset.should_include_macro(macro)) {
CEL_RETURN_IF_ERROR(macro_registry.RegisterMacro(macro));
}
}
macros_.clear();
continue;
}
}

CEL_RETURN_IF_ERROR(macro_registry.RegisterMacros(macros_));
macros_.clear();
}

absl::flat_hash_set<std::string> library_ids(library_ids_);

if (!options_.disable_standard_macros && !library_ids_.contains("stdlib")) {
CEL_RETURN_IF_ERROR(macro_registry.RegisterMacros(Macro::AllMacros()));
library_ids.insert("stdlib");
}

if (options_.enable_optional_syntax && !library_ids_.contains("optional")) {
CEL_RETURN_IF_ERROR(macro_registry.RegisterMacro(cel::OptMapMacro()));
CEL_RETURN_IF_ERROR(macro_registry.RegisterMacro(cel::OptFlatMapMacro()));
library_ids.insert("optional");
}

CEL_RETURN_IF_ERROR(macro_registry.RegisterMacros(individual_macros));
return std::make_unique<PrattParserImpl>(
options_, std::move(macro_registry), std::move(library_ids));
}

cel::ParserOptions options_;
std::vector<cel::Macro> macros_;
absl::flat_hash_set<std::string> library_ids_;
std::vector<cel::ParserLibrary> libraries_;
absl::flat_hash_map<std::string, cel::ParserLibrarySubset> library_subsets_;
};

} // namespace

template class PrattParserWorker<cel::Expr>;

absl::StatusOr<std::unique_ptr<cel::Ast>> PrattParserImpl::ParseImpl(
const cel::Source& source,
std::vector<cel::ParseIssue>* absl_nullable parse_issues) const {
if (source.content().size() > options_.expression_size_codepoint_limit) {
return absl::InvalidArgumentError(absl::StrFormat(
"expression size exceeds codepoint limit: %zu > %d",
source.content().size(), options_.expression_size_codepoint_limit));
}
std::vector<cel::ParseIssue> issues;
PrattParserWorker<cel::Expr> worker(source, options_, &issues);
Expr expr = worker.Parse();
if (worker.is_recursion_limit_exceeded()) {
return absl::CancelledError(
absl::StrFormat("Expression recursion limit exceeded. limit: %d",
options_.max_recursion_depth));
}
if (worker.has_errors()) {
std::string err_msg = FormatIssues(source, issues);
if (parse_issues != nullptr) {
parse_issues->swap(issues);
}
return absl::InvalidArgumentError(err_msg);
}

cel::SourceInfo source_info;
source_info.set_location(std::string(source.description()));
for (const auto& [id, pos] : worker.GetNodePositions()) {
source_info.mutable_positions().insert({id, pos});
}
source_info.mutable_line_offsets().reserve(worker.GetLineOffsets().size());
for (int32_t offset : worker.GetLineOffsets()) {
source_info.mutable_line_offsets().push_back(offset);
}
return std::make_unique<cel::Ast>(std::move(expr), std::move(source_info));
}

std::unique_ptr<cel::ParserBuilder> PrattParserImpl::ToBuilder() const {
auto ins = std::make_unique<PrattParserBuilderImpl>(options_);
ins->library_ids_ = library_ids_;
ins->macros_ = macro_registry_.ListMacros();
return ins;
}

std::unique_ptr<cel::ParserBuilder> NewPrattParserBuilder(
const cel::ParserOptions& options) {
return std::make_unique<PrattParserBuilderImpl>(options);
}

} // namespace cel::parser_internal
72 changes: 72 additions & 0 deletions parser/internal/pratt_parser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef THIRD_PARTY_CEL_CPP_PARSER_INTERNAL_PRATT_PARSER_H_
#define THIRD_PARTY_CEL_CPP_PARSER_INTERNAL_PRATT_PARSER_H_

#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "absl/base/nullability.h"
#include "absl/container/flat_hash_set.h"
#include "absl/status/statusor.h"
#include "common/ast.h"
#include "common/source.h"
#include "parser/macro_registry.h"
#include "parser/options.h"
#include "parser/parser_interface.h"

namespace cel::parser_internal {

// PrattParserImpl implements the Pratt parsing algorithm for CEL expressions.
//
// WARNING: Since this implementation uses recursive descent to parse
// expressions, its stack consumption depends on expression nesting and
// recursion limits (`ParserOptions::max_recursion_depth`). Note that:
// In production builds (e.g., inside fibers with small default stacks such as
// 64KB), the available stack space may be much tighter than in default thread
// stacks.
// Consequently, `ParserOptions::max_recursion_depth` may need to be tuned
// depending on the caller environment to prevent stack overflow.
class PrattParserImpl final : public cel::Parser {
public:
explicit PrattParserImpl(const cel::ParserOptions& options,
cel::MacroRegistry macro_registry,
absl::flat_hash_set<std::string> library_ids)
: options_(options),
macro_registry_(std::move(macro_registry)),
library_ids_(std::move(library_ids)) {}

~PrattParserImpl() override = default;

absl::StatusOr<std::unique_ptr<cel::Ast>> ParseImpl(
const cel::Source& source,
std::vector<cel::ParseIssue>* absl_nullable parse_issues) const override;

std::unique_ptr<cel::ParserBuilder> ToBuilder() const override;

private:
cel::ParserOptions options_;
cel::MacroRegistry macro_registry_;
absl::flat_hash_set<std::string> library_ids_;
};

std::unique_ptr<cel::ParserBuilder> NewPrattParserBuilder(
const cel::ParserOptions& options = cel::ParserOptions());

} // namespace cel::parser_internal

#endif // THIRD_PARTY_CEL_CPP_PARSER_INTERNAL_PRATT_PARSER_H_
Loading
Loading