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 /// \brief 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"
37 class FormatTokenLexer {
39 FormatTokenLexer(const SourceManager &SourceMgr, FileID ID,
40 const FormatStyle &Style, encoding::Encoding Encoding);
42 ArrayRef<FormatToken *> lex();
44 const AdditionalKeywords &getKeywords() { return Keywords; }
47 void tryMergePreviousTokens();
49 bool tryMergeLessLess();
50 bool tryMergeNSStringLiteral();
52 bool tryMergeTokens(ArrayRef<tok::TokenKind> Kinds, TokenType NewType);
54 // Returns \c true if \p Tok can only be followed by an operand in JavaScript.
55 bool precedesOperand(FormatToken *Tok);
57 bool canPrecedeRegexLiteral(FormatToken *Prev);
59 // Tries to parse a JavaScript Regex literal starting at the current token,
60 // if that begins with a slash and is in a location where JavaScript allows
61 // regex literals. Changes the current token to a regex literal and updates
62 // its text if successful.
63 void tryParseJSRegexLiteral();
65 // Handles JavaScript template strings.
67 // JavaScript template strings use backticks ('`') as delimiters, and allow
68 // embedding expressions nested in ${expr-here}. Template strings can be
69 // nested recursively, i.e. expressions can contain template strings in turn.
71 // The code below parses starting from a backtick, up to a closing backtick or
72 // an opening ${. It also maintains a stack of lexing contexts to handle
73 // nested template parts by balancing curly braces.
74 void handleTemplateStrings();
76 bool tryMerge_TMacro();
78 bool tryMergeConflictMarkers();
80 FormatToken *getStashedToken();
82 FormatToken *getNextToken();
84 FormatToken *FormatTok;
86 std::stack<LexerState> StateStack;
88 unsigned TrailingWhitespace;
89 std::unique_ptr<Lexer> Lex;
90 const SourceManager &SourceMgr;
92 const FormatStyle &Style;
93 IdentifierTable IdentTable;
94 AdditionalKeywords Keywords;
95 encoding::Encoding Encoding;
96 llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
97 // Index (in 'Tokens') of the last token that starts a new line.
98 unsigned FirstInLineIndex;
99 SmallVector<FormatToken *, 16> Tokens;
100 SmallVector<IdentifierInfo *, 8> ForEachMacros;
102 bool FormattingDisabled;
104 llvm::Regex MacroBlockBeginRegex;
105 llvm::Regex MacroBlockEndRegex;
107 void readRawToken(FormatToken &Tok);
109 void resetLexer(unsigned Offset);
112 } // namespace format