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