1 //===--- FormatTokenLexer.h - Format C++ code ----------------*- C++ ----*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This file contains FormatTokenLexer, which tokenizes a source file
12 /// into a token stream suitable for ClangFormat.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_CLANG_LIB_FORMAT_FORMATTOKENLEXER_H
17 #define LLVM_CLANG_LIB_FORMAT_FORMATTOKENLEXER_H
20 #include "FormatToken.h"
21 #include "clang/Basic/SourceLocation.h"
22 #include "clang/Basic/SourceManager.h"
23 #include "clang/Format/Format.h"
24 #include "llvm/Support/Regex.h"
25 #include "llvm/ADT/MapVector.h"
38 class FormatTokenLexer {
40 FormatTokenLexer(const SourceManager &SourceMgr, FileID ID, unsigned Column,
41 const FormatStyle &Style, encoding::Encoding Encoding);
43 ArrayRef<FormatToken *> lex();
45 const AdditionalKeywords &getKeywords() { return Keywords; }
48 void tryMergePreviousTokens();
50 bool tryMergeLessLess();
51 bool tryMergeNSStringLiteral();
53 bool tryMergeTokens(ArrayRef<tok::TokenKind> Kinds, TokenType NewType);
55 // Returns \c true if \p Tok can only be followed by an operand in JavaScript.
56 bool precedesOperand(FormatToken *Tok);
58 bool canPrecedeRegexLiteral(FormatToken *Prev);
60 // Tries to parse a JavaScript Regex literal starting at the current token,
61 // if that begins with a slash and is in a location where JavaScript allows
62 // regex literals. Changes the current token to a regex literal and updates
63 // its text if successful.
64 void tryParseJSRegexLiteral();
66 // Handles JavaScript template strings.
68 // JavaScript template strings use backticks ('`') as delimiters, and allow
69 // embedding expressions nested in ${expr-here}. Template strings can be
70 // nested recursively, i.e. expressions can contain template strings in turn.
72 // The code below parses starting from a backtick, up to a closing backtick or
73 // an opening ${. It also maintains a stack of lexing contexts to handle
74 // nested template parts by balancing curly braces.
75 void handleTemplateStrings();
77 void tryParsePythonComment();
79 bool tryMerge_TMacro();
81 bool tryMergeConflictMarkers();
83 FormatToken *getStashedToken();
85 FormatToken *getNextToken();
87 FormatToken *FormatTok;
89 std::stack<LexerState> StateStack;
91 unsigned TrailingWhitespace;
92 std::unique_ptr<Lexer> Lex;
93 const SourceManager &SourceMgr;
95 const FormatStyle &Style;
96 IdentifierTable IdentTable;
97 AdditionalKeywords Keywords;
98 encoding::Encoding Encoding;
99 llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
100 // Index (in 'Tokens') of the last token that starts a new line.
101 unsigned FirstInLineIndex;
102 SmallVector<FormatToken *, 16> Tokens;
104 llvm::SmallMapVector<IdentifierInfo *, TokenType, 8> Macros;
106 bool FormattingDisabled;
108 llvm::Regex MacroBlockBeginRegex;
109 llvm::Regex MacroBlockEndRegex;
111 void readRawToken(FormatToken &Tok);
113 void resetLexer(unsigned Offset);
116 } // namespace format