[PATCH] Added LanguageStandard::LS_JavaScript to gate all JS-specific parsing.

Daniel Jasper djasper at google.com
Thu Nov 21 09:56:57 PST 2013


I think I'd rather have a different style and let clang-format's caller
(E.g. the editor integration) decide. But it is just a gut feeling..

On Nov 21, 2013 4:10 PM, "Alexander Kornienko" <alexfh at google.com> wrote:
>
> 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 --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20131121/ee5739e4/attachment.html>


More information about the cfe-commits mailing list