Skip to content

Commit

Permalink
libnixf/Parse: handle indented strings ''\ escape sequence (#642)
Browse files Browse the repository at this point in the history
Fixes: #641
  • Loading branch information
inclyc authored Jan 8, 2025
1 parent 17b7dfd commit 6e58141
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
10 changes: 8 additions & 2 deletions libnixf/src/Parse/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,9 +409,15 @@ Token Lexer::lexIndString() {
return finishToken();
}
if (consumePrefix("''")) {
Tok = tok_quote2;
if (consumePrefix("$") || consumePrefix("\\") || consumePrefix("'"))
if (consumePrefix("$") || consumePrefix("'")) {
Tok = tok_string_escape;
} else if (consumePrefix("\\")) {
// ''\ escapes any character
consume();
Tok = tok_string_escape;
} else {
Tok = tok_quote2;
}
return finishToken();
}

Expand Down
13 changes: 13 additions & 0 deletions libnixf/test/Parse/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,17 @@ TEST_F(LexerTest, lexURI) {
ASSERT_EQ(Tokens.size(), sizeof(Match) / sizeof(TokenKind));
}

TEST_F(LexerTest, IndStringEscape) {
Lexer Lexer(R"(''\$${)", Diags);
const TokenKind Match[] = {
tok_string_escape, // ''\ escapes $
tok_dollar_curly, // ${
};
auto Tokens = collect(Lexer, &Lexer::lexIndString);
for (size_t I = 0; I < Tokens.size(); I++) {
ASSERT_EQ(Tokens[I].kind(), Match[I]);
}
ASSERT_EQ(Tokens.size(), sizeof(Match) / sizeof(TokenKind));
}

} // namespace

0 comments on commit 6e58141

Please sign in to comment.