[PATCH] Added LanguageStandard::LS_JavaScript to gate all JS-specific parsing.
Alexander Kornienko
alexfh at google.com
Thu Nov 21 07:11:20 PST 2013
Hi djasper, klimek,
Use LS_JavaScript for files ending with ".js". Added support for ">>>="
operator.
http://llvm-reviews.chandlerc.com/D2242
Files:
include/clang/Format/Format.h
lib/Format/Format.cpp
unittests/Format/FormatTest.cpp
Index: include/clang/Format/Format.h
===================================================================
--- include/clang/Format/Format.h
+++ include/clang/Format/Format.h
@@ -71,6 +71,8 @@
/// Use features of C++11 (e.g. \c A<A<int>> instead of
/// <tt>A<A<int> ></tt>).
LS_Cpp11,
+ /// Allow JavaScript-specific syntax constructs.
+ LS_JavaScript,
/// Automatic detection based on the input.
LS_Auto
};
Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -1038,24 +1038,40 @@
private:
void tryMergePreviousTokens() {
- tryMerge_TMacro() || tryMergeJavaScriptIdentityOperators();
+ if (tryMerge_TMacro())
+ return;
+
+ if (Style.Standard == FormatStyle::LS_JavaScript) {
+ static tok::TokenKind JSIdentity[] = { tok::equalequal, tok::equal };
+ static tok::TokenKind JSNotIdentity[] = { tok::exclaimequal, tok::equal };
+ static tok::TokenKind JSShiftEqual[] = { tok::greater, tok::greater,
+ tok::greaterequal };
+ // FIXME: We probably need to change token type to mimic operator with the
+ // correct priority.
+ tryMergeTokens(JSIdentity) || tryMergeTokens(JSNotIdentity) ||
+ tryMergeTokens(JSShiftEqual);
+ }
}
- bool tryMergeJavaScriptIdentityOperators() {
- if (Tokens.size() < 2)
- return false;
- FormatToken &First = *Tokens[Tokens.size() - 2];
- if (!First.isOneOf(tok::exclaimequal, tok::equalequal))
- return false;
- FormatToken &Second = *Tokens.back();
- if (!Second.is(tok::equal))
+ bool tryMergeTokens(ArrayRef<tok::TokenKind> Kinds) {
+ if (Tokens.size() < Kinds.size())
return false;
- if (Second.WhitespaceRange.getBegin() != Second.WhitespaceRange.getEnd())
+
+ SmallVectorImpl<FormatToken *>::const_iterator First =
+ Tokens.end() - Kinds.size();
+ if (!First[0]->is(Kinds[0]))
return false;
- First.TokenText =
- StringRef(First.TokenText.data(), First.TokenText.size() + 1);
- First.ColumnWidth += 1;
- Tokens.pop_back();
+ unsigned AddLength = 0;
+ for (unsigned i = 1; i < Kinds.size(); ++i) {
+ if (!First[i]->is(Kinds[i]) || First[i]->WhitespaceRange.getBegin() !=
+ First[i]->WhitespaceRange.getEnd())
+ return false;
+ AddLength += First[i]->TokenText.size();
+ }
+ Tokens.resize(Tokens.size() - Kinds.size() + 1);
+ First[0]->TokenText = StringRef(First[0]->TokenText.data(),
+ First[0]->TokenText.size() + AddLength);
+ First[0]->ColumnWidth += AddLength;
return true;
}
@@ -1265,6 +1281,12 @@
<< (Encoding == encoding::Encoding_UTF8 ? "UTF8"
: "unknown")
<< "\n");
+ if (SourceMgr.getFilename(Lex.getFileLoc()).endswith(".js"))
+ this->Style.Standard = FormatStyle::LS_JavaScript;
+ // Should be in sync with the LanguageStandard enum.
+ static const char *Standards[] = { "C++03", "C++11", "JavaScript", "Auto" };
+ DEBUG(llvm::dbgs() << "Language standard: "
+ << Standards[this->Style.Standard] << "\n");
}
tooling::Replacements format() {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2242.1.patch
Type: text/x-patch
Size: 3383 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20131121/d5b703a1/attachment.bin>
More information about the cfe-commits
mailing list