1 //===--- FormatTokenLexer.h - Format C++ code ----------------*- C++ ----*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// 9 /// \file 10 /// This file contains FormatTokenLexer, which tokenizes a source file 11 /// into a token stream suitable for ClangFormat. 12 /// 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_CLANG_LIB_FORMAT_FORMATTOKENLEXER_H 16 #define LLVM_CLANG_LIB_FORMAT_FORMATTOKENLEXER_H 17 18 #include "Encoding.h" 19 #include "FormatToken.h" 20 #include "clang/Basic/SourceLocation.h" 21 #include "clang/Basic/SourceManager.h" 22 #include "clang/Format/Format.h" 23 #include "llvm/ADT/MapVector.h" 24 #include "llvm/ADT/StringSet.h" 25 #include "llvm/Support/Regex.h" 26 27 #include <stack> 28 29 namespace clang { 30 namespace format { 31 32 enum LexerState { 33 NORMAL, 34 TEMPLATE_STRING, 35 TOKEN_STASHED, 36 }; 37 38 class FormatTokenLexer { 39 public: 40 FormatTokenLexer(const SourceManager &SourceMgr, FileID ID, unsigned Column, 41 const FormatStyle &Style, encoding::Encoding Encoding, 42 llvm::SpecificBumpPtrAllocator<FormatToken> &Allocator, 43 IdentifierTable &IdentTable); 44 45 ArrayRef<FormatToken *> lex(); 46 47 const AdditionalKeywords &getKeywords() { return Keywords; } 48 49 private: 50 void tryMergePreviousTokens(); 51 52 bool tryMergeLessLess(); 53 bool tryMergeNSStringLiteral(); 54 bool tryMergeJSPrivateIdentifier(); 55 bool tryMergeCSharpStringLiteral(); 56 bool tryMergeCSharpKeywordVariables(); 57 bool tryMergeCSharpDoubleQuestion(); 58 bool tryMergeCSharpNullConditional(); 59 bool tryTransformCSharpForEach(); 60 bool tryMergeForEach(); 61 bool tryTransformTryUsageForC(); 62 63 bool tryMergeTokens(ArrayRef<tok::TokenKind> Kinds, TokenType NewType); 64 65 // Returns \c true if \p Tok can only be followed by an operand in JavaScript. 66 bool precedesOperand(FormatToken *Tok); 67 68 bool canPrecedeRegexLiteral(FormatToken *Prev); 69 70 // Tries to parse a JavaScript Regex literal starting at the current token, 71 // if that begins with a slash and is in a location where JavaScript allows 72 // regex literals. Changes the current token to a regex literal and updates 73 // its text if successful. 74 void tryParseJSRegexLiteral(); 75 76 // Handles JavaScript template strings. 77 // 78 // JavaScript template strings use backticks ('`') as delimiters, and allow 79 // embedding expressions nested in ${expr-here}. Template strings can be 80 // nested recursively, i.e. expressions can contain template strings in turn. 81 // 82 // The code below parses starting from a backtick, up to a closing backtick or 83 // an opening ${. It also maintains a stack of lexing contexts to handle 84 // nested template parts by balancing curly braces. 85 void handleTemplateStrings(); 86 87 void handleCSharpVerbatimAndInterpolatedStrings(); 88 89 void tryParsePythonComment(); 90 91 bool tryMerge_TMacro(); 92 93 bool tryMergeConflictMarkers(); 94 95 FormatToken *getStashedToken(); 96 97 FormatToken *getNextToken(); 98 99 FormatToken *FormatTok; 100 bool IsFirstToken; 101 std::stack<LexerState> StateStack; 102 unsigned Column; 103 unsigned TrailingWhitespace; 104 std::unique_ptr<Lexer> Lex; 105 const SourceManager &SourceMgr; 106 FileID ID; 107 const FormatStyle &Style; 108 IdentifierTable &IdentTable; 109 AdditionalKeywords Keywords; 110 encoding::Encoding Encoding; 111 llvm::SpecificBumpPtrAllocator<FormatToken> &Allocator; 112 // Index (in 'Tokens') of the last token that starts a new line. 113 unsigned FirstInLineIndex; 114 SmallVector<FormatToken *, 16> Tokens; 115 116 llvm::SmallMapVector<IdentifierInfo *, TokenType, 8> Macros; 117 118 bool FormattingDisabled; 119 120 llvm::Regex MacroBlockBeginRegex; 121 llvm::Regex MacroBlockEndRegex; 122 123 // Targets that may appear inside a C# attribute. 124 static const llvm::StringSet<> CSharpAttributeTargets; 125 126 void readRawToken(FormatToken &Tok); 127 128 void resetLexer(unsigned Offset); 129 }; 130 131 } // namespace format 132 } // namespace clang 133 134 #endif 135