Skip to content

Commit

Permalink
[P4 Constraints Network Address Notation] Change from double quotes t…
Browse files Browse the repository at this point in the history
…o single quotes for the lexer. (#131)

PiperOrigin-RevId: 607368066

Co-authored-by: PINS Team <[email protected]>
  • Loading branch information
matthewtlam and PINS Team authored Feb 16, 2024
1 parent 6d017e4 commit ceacb6a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
3 changes: 1 addition & 2 deletions p4_constraints/frontend/lexer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const LazyRE2 kTokenPattern{
"|(?:0[xX])" "(?P<hexadec>[0-9a-fA-F]+)"
"|(?:0[dD])?" "(?P<decimal>[0-9]+)"
// Strings.
"|(?P<string>\"([^\"]*)\")"
"|(?P<string>'([^']*)')"
// clang-format on
};

Expand Down Expand Up @@ -182,7 +182,6 @@ std::vector<Token> Tokenize(const ConstraintSource& constraint) {
kind = Token::HEXADEC;
} else if (!CaptureByName("string", captures).empty()) {
lexeme = std::string(CaptureByName("string", captures));
// Update lexeme to exclude double quotation marks
lexeme = lexeme.substr(1, lexeme.size() - 2);
kind = Token::STRING;
} else {
Expand Down
26 changes: 13 additions & 13 deletions p4_constraints/frontend/lexer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ TEST(LexerTest, CommentsAreLexedCorrectly) {

TEST(LexerTest, TokenizeAnEmptyString) {
std::vector<Token> empty_str_tokens = Tokenize({
.constraint_string = "\"\"",
.constraint_string = "''",
.constraint_location = ast::SourceLocation(),
});
EXPECT_THAT(GetTokenKinds(empty_str_tokens),
Expand All @@ -311,7 +311,7 @@ TEST(LexerTest, TokenizeAnEmptyString) {

TEST(LexerTest, TokenizeAnEmptyStringWithADoubleQuoteInMiddle) {
std::vector<Token> double_quote_in_middle_str_tokens = Tokenize({
.constraint_string = "\"\"\"",
.constraint_string = "'''",
.constraint_location = ast::SourceLocation(),
});
EXPECT_THAT(GetTokenKinds(double_quote_in_middle_str_tokens),
Expand All @@ -320,37 +320,37 @@ TEST(LexerTest, TokenizeAnEmptyStringWithADoubleQuoteInMiddle) {

TEST(LexerTest, TokenizeString) {
auto str_tokens = Tokenize({
.constraint_string = "\"192.168.2.1\"",
.constraint_string = "'192.168.2.1'",
.constraint_location = ast::SourceLocation(),
});
EXPECT_THAT(GetTokenKinds(str_tokens),
testing::ElementsAre(Token::STRING, Token::END_OF_INPUT));
EXPECT_THAT(str_tokens[0].text, "192.168.2.1");
}

TEST(LexerTest, TokenizeIPv4String) {
auto double_quote_str_tokens = Tokenize({
.constraint_string = "ipv4(\"192.168.2.1\")",
TEST(LexerTest, TokenizeIPv4SingleQuoteString) {
auto single_quote_str_tokens = Tokenize({
.constraint_string = "ipv4('192.168.2.1')",
.constraint_location = ast::SourceLocation(),
});
EXPECT_THAT(GetTokenKinds(double_quote_str_tokens),
EXPECT_THAT(GetTokenKinds(single_quote_str_tokens),
testing::ElementsAre(Token::ID, Token::LPAR, Token::STRING,
Token::RPAR, Token::END_OF_INPUT));
}

TEST(LexerTest, TokenizeIPv4SingleQuoteString) {
auto single_quote_str_tokens = Tokenize({
.constraint_string = "ipv4(\'192.168.2.1\')",
TEST(LexerTest, TokenizeIPv4DoubleQuoteString) {
auto double_quote_str_tokens = Tokenize({
.constraint_string = "ipv4(\"192.168.2.1\")",
.constraint_location = ast::SourceLocation(),
});
EXPECT_THAT(
GetTokenKinds(single_quote_str_tokens),
GetTokenKinds(double_quote_str_tokens),
testing::ElementsAre(Token::ID, Token::LPAR, Token::UNEXPECTED_CHAR));
}

TEST(LexerTest, TokenizeIPv4MixedQuoteString) {
auto mixed_quote_str_tokens = Tokenize({
.constraint_string = "ipv4(\"192.168.2.1\')",
.constraint_string = "ipv4(\"192.168.2.1')",
.constraint_location = ast::SourceLocation(),
});
EXPECT_THAT(
Expand All @@ -360,7 +360,7 @@ TEST(LexerTest, TokenizeIPv4MixedQuoteString) {

TEST(LexerTest, TokenizeIPv4ReverseMixedQuoteString) {
auto reverse_mixed_quote_str_tokens = Tokenize({
.constraint_string = "ipv4(\'192.168.2.1\")",
.constraint_string = "ipv4('192.168.2.1\")",
.constraint_location = ast::SourceLocation(),
});
EXPECT_THAT(
Expand Down
2 changes: 1 addition & 1 deletion p4_constraints/frontend/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ struct Token {
DECIMAL, // decimal numeral: 0..9+ or 0[dD](0..9)+
HEXADEC, // hexadecimal numeral: 0[xX](0..9|[a-fA-F])+
END_OF_INPUT, // indicates that the end of the input was reached
STRING, // double quote string: \"[^\"]*\"|
STRING, // single quote string: '[^']*'
UNEXPECTED_CHAR // indicates that an unexpected character was encountered
// Invariant: UNEXPECTED_CHAR must always be the last (maximum) kind.
};
Expand Down

0 comments on commit ceacb6a

Please sign in to comment.