1 //===--- FormatTokenLexer.h - Format C++ code ----------------*- C++ ----*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// This file contains FormatTokenLexer, which tokenizes a source file
11 /// into a token stream suitable for ClangFormat.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_LIB_FORMAT_FORMATTOKENLEXER_H
16 #define LLVM_CLANG_LIB_FORMAT_FORMATTOKENLEXER_H
19 #include "FormatToken.h"
20 #include "clang/Basic/LangOptions.h"
21 #include "clang/Basic/SourceLocation.h"
22 #include "clang/Basic/SourceManager.h"
23 #include "clang/Format/Format.h"
24 #include "llvm/ADT/MapVector.h"
25 #include "llvm/ADT/StringSet.h"
26 #include "llvm/Support/Regex.h"
39 class FormatTokenLexer
{
41 FormatTokenLexer(const SourceManager
&SourceMgr
, FileID ID
, unsigned Column
,
42 const FormatStyle
&Style
, encoding::Encoding Encoding
,
43 llvm::SpecificBumpPtrAllocator
<FormatToken
> &Allocator
,
44 IdentifierTable
&IdentTable
);
46 ArrayRef
<FormatToken
*> lex();
48 const AdditionalKeywords
&getKeywords() { return Keywords
; }
51 void tryMergePreviousTokens();
53 bool tryMergeLessLess();
54 bool tryMergeGreaterGreater();
55 bool tryMergeNSStringLiteral();
56 bool tryMergeJSPrivateIdentifier();
57 bool tryMergeCSharpStringLiteral();
58 bool tryMergeCSharpKeywordVariables();
59 bool tryMergeNullishCoalescingEqual();
60 bool tryTransformCSharpForEach();
61 bool tryMergeForEach();
62 bool tryTransformTryUsageForC();
64 // Merge the most recently lexed tokens into a single token if their kinds are
66 bool tryMergeTokens(ArrayRef
<tok::TokenKind
> Kinds
, TokenType NewType
);
67 // Merge without checking their kinds.
68 bool tryMergeTokens(size_t Count
, TokenType NewType
);
69 // Merge if their kinds match any one of Kinds.
70 bool tryMergeTokensAny(ArrayRef
<ArrayRef
<tok::TokenKind
>> Kinds
,
73 // Returns \c true if \p Tok can only be followed by an operand in JavaScript.
74 bool precedesOperand(FormatToken
*Tok
);
76 bool canPrecedeRegexLiteral(FormatToken
*Prev
);
78 // Tries to parse a JavaScript Regex literal starting at the current token,
79 // if that begins with a slash and is in a location where JavaScript allows
80 // regex literals. Changes the current token to a regex literal and updates
81 // its text if successful.
82 void tryParseJSRegexLiteral();
84 // Handles JavaScript template strings.
86 // JavaScript template strings use backticks ('`') as delimiters, and allow
87 // embedding expressions nested in ${expr-here}. Template strings can be
88 // nested recursively, i.e. expressions can contain template strings in turn.
90 // The code below parses starting from a backtick, up to a closing backtick or
91 // an opening ${. It also maintains a stack of lexing contexts to handle
92 // nested template parts by balancing curly braces.
93 void handleTemplateStrings();
95 void handleCSharpVerbatimAndInterpolatedStrings();
97 void tryParsePythonComment();
99 bool tryMerge_TMacro();
101 bool tryMergeConflictMarkers();
103 void truncateToken(size_t NewLen
);
105 FormatToken
*getStashedToken();
107 FormatToken
*getNextToken();
109 FormatToken
*FormatTok
;
111 std::stack
<LexerState
> StateStack
;
113 unsigned TrailingWhitespace
;
114 std::unique_ptr
<Lexer
> Lex
;
115 LangOptions LangOpts
;
116 const SourceManager
&SourceMgr
;
118 const FormatStyle
&Style
;
119 IdentifierTable
&IdentTable
;
120 AdditionalKeywords Keywords
;
121 encoding::Encoding Encoding
;
122 llvm::SpecificBumpPtrAllocator
<FormatToken
> &Allocator
;
123 // Index (in 'Tokens') of the last token that starts a new line.
124 unsigned FirstInLineIndex
;
125 SmallVector
<FormatToken
*, 16> Tokens
;
127 llvm::SmallMapVector
<IdentifierInfo
*, TokenType
, 8> Macros
;
129 bool FormattingDisabled
;
131 llvm::Regex MacroBlockBeginRegex
;
132 llvm::Regex MacroBlockEndRegex
;
134 // Targets that may appear inside a C# attribute.
135 static const llvm::StringSet
<> CSharpAttributeTargets
;
137 /// Handle Verilog-specific tokens.
138 bool readRawTokenVerilogSpecific(Token
&Tok
);
140 void readRawToken(FormatToken
&Tok
);
142 void resetLexer(unsigned Offset
);
145 } // namespace format