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 43 ArrayRef<FormatToken *> lex(); 44 45 const AdditionalKeywords &getKeywords() { return Keywords; } 46 47 private: 48 void tryMergePreviousTokens(); 49 50 bool tryMergeLessLess(); 51 bool tryMergeNSStringLiteral(); 52 bool tryMergeJSPrivateIdentifier(); 53 bool tryMergeCSharpStringLiteral(); 54 bool tryMergeCSharpKeywordVariables(); 55 bool tryMergeCSharpNullConditionals(); 56 bool tryMergeCSharpDoubleQuestion(); 57 bool tryTransformCSharpForEach(); 58 bool tryMergeCSharpAttributeAndTarget(); 59 60 bool tryMergeTokens(ArrayRef<tok::TokenKind> Kinds, TokenType NewType); 61 62 // Returns \c true if \p Tok can only be followed by an operand in JavaScript. 63 bool precedesOperand(FormatToken *Tok); 64 65 bool canPrecedeRegexLiteral(FormatToken *Prev); 66 67 // Tries to parse a JavaScript Regex literal starting at the current token, 68 // if that begins with a slash and is in a location where JavaScript allows 69 // regex literals. Changes the current token to a regex literal and updates 70 // its text if successful. 71 void tryParseJSRegexLiteral(); 72 73 // Handles JavaScript template strings. 74 // 75 // JavaScript template strings use backticks ('`') as delimiters, and allow 76 // embedding expressions nested in ${expr-here}. Template strings can be 77 // nested recursively, i.e. expressions can contain template strings in turn. 78 // 79 // The code below parses starting from a backtick, up to a closing backtick or 80 // an opening ${. It also maintains a stack of lexing contexts to handle 81 // nested template parts by balancing curly braces. 82 void handleTemplateStrings(); 83 84 void handleCSharpVerbatimAndInterpolatedStrings(); 85 86 void tryParsePythonComment(); 87 88 bool tryMerge_TMacro(); 89 90 bool tryMergeConflictMarkers(); 91 92 FormatToken *getStashedToken(); 93 94 FormatToken *getNextToken(); 95 96 FormatToken *FormatTok; 97 bool IsFirstToken; 98 std::stack<LexerState> StateStack; 99 unsigned Column; 100 unsigned TrailingWhitespace; 101 std::unique_ptr<Lexer> Lex; 102 const SourceManager &SourceMgr; 103 FileID ID; 104 const FormatStyle &Style; 105 IdentifierTable IdentTable; 106 AdditionalKeywords Keywords; 107 encoding::Encoding Encoding; 108 llvm::SpecificBumpPtrAllocator<FormatToken> Allocator; 109 // Index (in 'Tokens') of the last token that starts a new line. 110 unsigned FirstInLineIndex; 111 SmallVector<FormatToken *, 16> Tokens; 112 113 llvm::SmallMapVector<IdentifierInfo *, TokenType, 8> Macros; 114 115 bool FormattingDisabled; 116 117 llvm::Regex MacroBlockBeginRegex; 118 llvm::Regex MacroBlockEndRegex; 119 120 // Targets that may appear inside a C# attribute. 121 static const llvm::StringSet<> CSharpAttributeTargets; 122 123 void readRawToken(FormatToken &Tok); 124 125 void resetLexer(unsigned Offset); 126 }; 127 128 } // namespace format 129 } // namespace clang 130 131 #endif 132