r208539 - clang-format: [JS] Basic support for escape sequences in regex literals.
Daniel Jasper
djasper at google.com
Mon May 12 04:29:51 PDT 2014
Author: djasper
Date: Mon May 12 06:29:50 2014
New Revision: 208539
URL: http://llvm.org/viewvc/llvm-project?rev=208539&view=rev
Log:
clang-format: [JS] Basic support for escape sequences in regex literals.
Before:
var regex = /\\/ g; // This isn't even recognized as regex.
After:
var regex = /\\/g; // It now is.
Modified:
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/unittests/Format/FormatTestJS.cpp
Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=208539&r1=208538&r2=208539&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Mon May 12 06:29:50 2014
@@ -1216,6 +1216,8 @@ private:
return;
if (Style.Language == FormatStyle::LK_JavaScript) {
+ if (tryMergeEscapeSequence())
+ return;
if (tryMergeJSRegexLiteral())
return;
@@ -1256,6 +1258,23 @@ private:
return true;
}
+ // Tries to merge an escape sequence, i.e. a "\\" and the following
+ // charachter. Use e.g. inside JavaScript regex literals.
+ bool tryMergeEscapeSequence() {
+ if (Tokens.size() < 2)
+ return false;
+ FormatToken *Previous = Tokens[Tokens.size() - 2];
+ if (Previous->isNot(tok::unknown) || Previous->TokenText != "\\" ||
+ Tokens.back()->NewlinesBefore != 0)
+ return false;
+ Previous->ColumnWidth += Tokens.back()->ColumnWidth;
+ StringRef Text = Previous->TokenText;
+ Previous->TokenText =
+ StringRef(Text.data(), Text.size() + Tokens.back()->TokenText.size());
+ Tokens.resize(Tokens.size() - 1);
+ return true;
+ }
+
// Try to determine whether the current token ends a JavaScript regex literal.
// We heuristically assume that this is a regex literal if we find two
// unescaped slashes on a line and the token before the first slash is one of
@@ -1263,7 +1282,8 @@ private:
// a division.
bool tryMergeJSRegexLiteral() {
if (Tokens.size() < 2 || Tokens.back()->isNot(tok::slash) ||
- Tokens[Tokens.size() - 2]->is(tok::unknown))
+ (Tokens[Tokens.size() - 2]->is(tok::unknown) &&
+ Tokens[Tokens.size() - 2]->TokenText == "\\"))
return false;
unsigned TokenCount = 0;
unsigned LastColumn = Tokens.back()->OriginalColumn;
Modified: cfe/trunk/unittests/Format/FormatTestJS.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJS.cpp?rev=208539&r1=208538&r2=208539&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp Mon May 12 06:29:50 2014
@@ -180,6 +180,9 @@ TEST_F(FormatTestJS, RegexLiteralSpecial
verifyFormat("var regex = /\\W/;");
verifyFormat("var regex = /a(a)\\1/;");
verifyFormat("var regex = /\\0/;");
+ verifyFormat("var regex = /\\\\/g;");
+ verifyFormat("var regex = /\\a\\\\/g;");
+ verifyFormat("var regex = /\a\\//g;");
}
TEST_F(FormatTestJS, RegexLiteralModifiers) {
More information about the cfe-commits
mailing list