[PATCH] D13641: clang-format: [JS] handle character classes in regexes.
Martin Probst via cfe-commits
cfe-commits at lists.llvm.org
Sun Oct 11 17:04:05 PDT 2015
mprobst created this revision.
mprobst added a reviewer: djasper.
mprobst added subscribers: cfe-commits, klimek.
Slashes in regular expressions do not need to be escaped and do not terminate
the regular expression even without a preceding backslash.
http://reviews.llvm.org/D13641
Files:
lib/Format/Format.cpp
unittests/Format/FormatTestJS.cpp
Index: unittests/Format/FormatTestJS.cpp
===================================================================
--- unittests/Format/FormatTestJS.cpp
+++ unittests/Format/FormatTestJS.cpp
@@ -617,9 +617,15 @@
verifyFormat("var regex = /x|y/;");
verifyFormat("var regex = /a{2}/;");
verifyFormat("var regex = /a{1,3}/;");
+
verifyFormat("var regex = /[abc]/;");
verifyFormat("var regex = /[^abc]/;");
verifyFormat("var regex = /[\\b]/;");
+ verifyFormat("var regex = /[/]/;");
+ verifyFormat("var regex = /[\\/]/;");
+ verifyFormat("var regex = /\\[/;");
+ verifyFormat("var regex = /\\\\[/]/;");
+
verifyFormat("var regex = /\\b/;");
verifyFormat("var regex = /\\B/;");
verifyFormat("var regex = /\\d/;");
Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -876,12 +876,23 @@
return false;
unsigned TokenCount = 0;
+ auto InCharacterClass = false;
for (auto I = Tokens.rbegin() + 1, E = Tokens.rend(); I != E; ++I) {
++TokenCount;
auto Prev = I + 1;
while (Prev != E && Prev[0]->is(tok::comment))
++Prev;
- if (I[0]->isOneOf(tok::slash, tok::slashequal) &&
+ // Slashes in character classes (delimited by [ and ]) do not need
+ // escaping. Escaping of the squares themselves is already handled by
+ // \c tryMergeEscapeSequence(), a plain tok::r_square must be non-escaped.
+ if (I[0]->is(tok::r_square))
+ InCharacterClass = true;
+ if (I[0]->is(tok::l_square)) {
+ if (!InCharacterClass)
+ return false;
+ InCharacterClass = false;
+ }
+ if (!InCharacterClass && I[0]->isOneOf(tok::slash, tok::slashequal) &&
(Prev == E ||
((Prev[0]->isOneOf(tok::l_paren, tok::semi, tok::l_brace,
tok::r_brace, tok::exclaim, tok::l_square,
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D13641.37072.patch
Type: text/x-patch
Size: 1932 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20151012/63456866/attachment.bin>
More information about the cfe-commits
mailing list