FormatTokenLexer.h revision 360784
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/Support/Regex.h"
25
26#include <stack>
27
28namespace clang {
29namespace format {
30
31enum LexerState {
32  NORMAL,
33  TEMPLATE_STRING,
34  TOKEN_STASHED,
35};
36
37class FormatTokenLexer {
38public:
39  FormatTokenLexer(const SourceManager &SourceMgr, FileID ID, unsigned Column,
40                   const FormatStyle &Style, encoding::Encoding Encoding);
41
42  ArrayRef<FormatToken *> lex();
43
44  const AdditionalKeywords &getKeywords() { return Keywords; }
45
46private:
47  void tryMergePreviousTokens();
48
49  bool tryMergeLessLess();
50  bool tryMergeNSStringLiteral();
51  bool tryMergeJSPrivateIdentifier();
52  bool tryMergeCSharpVerbatimStringLiteral();
53  bool tryMergeCSharpKeywordVariables();
54  bool tryMergeCSharpNullConditionals();
55  bool tryMergeCSharpDoubleQuestion();
56  bool tryTransformCSharpForEach();
57
58  bool tryMergeTokens(ArrayRef<tok::TokenKind> Kinds, TokenType NewType);
59
60  // Returns \c true if \p Tok can only be followed by an operand in JavaScript.
61  bool precedesOperand(FormatToken *Tok);
62
63  bool canPrecedeRegexLiteral(FormatToken *Prev);
64
65  // Tries to parse a JavaScript Regex literal starting at the current token,
66  // if that begins with a slash and is in a location where JavaScript allows
67  // regex literals. Changes the current token to a regex literal and updates
68  // its text if successful.
69  void tryParseJSRegexLiteral();
70
71  // Handles JavaScript template strings.
72  //
73  // JavaScript template strings use backticks ('`') as delimiters, and allow
74  // embedding expressions nested in ${expr-here}. Template strings can be
75  // nested recursively, i.e. expressions can contain template strings in turn.
76  //
77  // The code below parses starting from a backtick, up to a closing backtick or
78  // an opening ${. It also maintains a stack of lexing contexts to handle
79  // nested template parts by balancing curly braces.
80  void handleTemplateStrings();
81
82  void tryParsePythonComment();
83
84  bool tryMerge_TMacro();
85
86  bool tryMergeConflictMarkers();
87
88  FormatToken *getStashedToken();
89
90  FormatToken *getNextToken();
91
92  FormatToken *FormatTok;
93  bool IsFirstToken;
94  std::stack<LexerState> StateStack;
95  unsigned Column;
96  unsigned TrailingWhitespace;
97  std::unique_ptr<Lexer> Lex;
98  const SourceManager &SourceMgr;
99  FileID ID;
100  const FormatStyle &Style;
101  IdentifierTable IdentTable;
102  AdditionalKeywords Keywords;
103  encoding::Encoding Encoding;
104  llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
105  // Index (in 'Tokens') of the last token that starts a new line.
106  unsigned FirstInLineIndex;
107  SmallVector<FormatToken *, 16> Tokens;
108
109  llvm::SmallMapVector<IdentifierInfo *, TokenType, 8> Macros;
110
111  bool FormattingDisabled;
112
113  llvm::Regex MacroBlockBeginRegex;
114  llvm::Regex MacroBlockEndRegex;
115
116  void readRawToken(FormatToken &Tok);
117
118  void resetLexer(unsigned Offset);
119};
120
121} // namespace format
122} // namespace clang
123
124#endif
125