Skip to content

Commit

Permalink
Use abseil-version of RE2. (#60)
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 361105743

Co-authored-by: PINS Team <[email protected]>
  • Loading branch information
smolkaj and PINS Team authored Mar 5, 2021
1 parent 920b6ab commit 3c02ca5
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
4 changes: 2 additions & 2 deletions gutils/protocol_buffer_matchers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class IgnoreFieldPathCriteria
};

namespace {
bool Consume(re2::StringPiece* s, re2::StringPiece x) {
bool Consume(absl::string_view* s, absl::string_view x) {
// We use the implementation of ABSL's StartsWith here until we can pick up a
// dependency on Abseil.
if (x.empty() ||
Expand Down Expand Up @@ -209,7 +209,7 @@ ParseFieldPathOrDie(const std::string& relative_field_path,
const RE2 field_subscript_regex(R"(([^.()[\]]+)\[(\d+)\])");
const RE2 extension_regex(R"(\(([^)]+)\))");

re2::StringPiece input(relative_field_path);
absl::string_view input(relative_field_path);
while (!input.empty()) {
// Consume a dot, except on the first iteration.
if (input.size() < relative_field_path.size() && !Consume(&input, ".")) {
Expand Down
3 changes: 1 addition & 2 deletions p4_constraints/backend/constraint_info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
#include "p4_constraints/frontend/lexer.h"
#include "p4_constraints/frontend/parser.h"
#include "re2/re2.h"
#include "re2/stringpiece.h"

namespace p4_constraints {

Expand Down Expand Up @@ -62,7 +61,7 @@ absl::StatusOr<absl::optional<ast::Expression>> ParseTableConstraint(
ast::SourceLocation location;
int line = 0;
absl::string_view constraint = "";
for (re2::StringPiece annotation : table.preamble().annotations()) {
for (absl::string_view annotation : table.preamble().annotations()) {
if (RE2::Consume(&annotation, file_annotation,
location.mutable_file_path()))
continue;
Expand Down
19 changes: 10 additions & 9 deletions p4_constraints/frontend/lexer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@
#include <string>
#include <vector>

#include "absl/strings/string_view.h"
#include "glog/logging.h"
#include "p4_constraints/ast.h"
#include "p4_constraints/ast.pb.h"
#include "p4_constraints/frontend/token.h"
#include "re2/re2.h"
#include "re2/stringpiece.h"

namespace p4_constraints {

namespace {

// Consumes characters from the input until the string "*/" appears or the input
// is exhausted, updating the given SourceLocation accordingly.
void SwallowMultiLineComment(re2::StringPiece* input,
void SwallowMultiLineComment(absl::string_view* input,
ast::SourceLocation* location) {
static const LazyRE2 kCommentRegexp{
// Important: the ordering matters, since the first matching case applies!
Expand All @@ -40,9 +40,9 @@ void SwallowMultiLineComment(re2::StringPiece* input,
// (first case) did not match.
"|((?:\\r\\n?)|\\n)" // Newline.
};
re2::StringPiece end_of_comment = "";
re2::StringPiece comment = "";
re2::StringPiece newline = "";
absl::string_view end_of_comment = "";
absl::string_view comment = "";
absl::string_view newline = "";
while (RE2::Consume(input, *kCommentRegexp, &end_of_comment, &comment,
&newline)) {
if (!end_of_comment.empty()) {
Expand Down Expand Up @@ -93,8 +93,9 @@ const LazyRE2 kTokenPattern{
};

// Access capture group in kTokenPattern by name.
re2::StringPiece CaptureByName(const std::string& group_name,
const std::vector<re2::StringPiece>& captures) {
absl::string_view CaptureByName(
const std::string& group_name,
const std::vector<absl::string_view>& captures) {
auto iter = kTokenPattern->NamedCapturingGroups().find(group_name);
if (iter != kTokenPattern->NamedCapturingGroups().end()) {
return captures[iter->second];
Expand All @@ -106,7 +107,7 @@ re2::StringPiece CaptureByName(const std::string& group_name,

} // namespace

std::vector<Token> Tokenize(re2::StringPiece input,
std::vector<Token> Tokenize(absl::string_view input,
ast::SourceLocation start_location) {
// Output.
std::vector<Token> tokens;
Expand All @@ -119,7 +120,7 @@ std::vector<Token> Tokenize(re2::StringPiece input,
const int capture_count = kTokenPattern->NumberOfCapturingGroups() + 1;

// We will have RE2 store the strings matched by each capturing group here.
std::vector<re2::StringPiece> captures(capture_count);
std::vector<absl::string_view> captures(capture_count);

while (kTokenPattern->Match(input, 0, input.size(), RE2::Anchor::ANCHOR_START,
captures.data(), capture_count)) {
Expand Down
4 changes: 2 additions & 2 deletions p4_constraints/frontend/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@

#include <vector>

#include "absl/strings/string_view.h"
#include "p4_constraints/ast.pb.h"
#include "p4_constraints/frontend/token.h"
#include "re2/stringpiece.h"

namespace p4_constraints {

// Turns the input string into a sequence of tokens. Note that this function
// is total; syntax errors are signaled by emitting an UNEXPECTED_CHAR token.
// The final token in the returned token sequence is guaranteed to be an
// END_OF_INPUT or UNEXPECTED_CHAR token.
std::vector<Token> Tokenize(re2::StringPiece input,
std::vector<Token> Tokenize(absl::string_view input,
ast::SourceLocation start_location);

} // namespace p4_constraints
Expand Down
8 changes: 4 additions & 4 deletions p4_constraints_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ def p4_constraints_deps():
sha256 = "6dd0f6b20094910fbb7f1f7908688df01af2d4f6c5c21331b9f636048674aebf",
)
if not native.existing_rule("com_googlesource_code_re2"):
http_archive(
git_repository(
name = "com_googlesource_code_re2",
url = "https://github.com/google/re2/archive/2020-07-06.tar.gz",
strip_prefix = "re2-2020-07-06",
sha256 = "2e9489a31ae007c81e90e8ec8a15d62d58a9c18d4fd1603f6441ef248556b41f",
# Newest commit on `abseil` branch on 2021-03-05.
commit = "72f110e82ccf3a9ae1c9418bfb447c3ba1cf95c2",
remote = "https://github.com/google/re2",
)
if not native.existing_rule("rules_proto"):
http_archive(
Expand Down

0 comments on commit 3c02ca5

Please sign in to comment.