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