clang  10.0.0svn
FormatTokenLexer.h
Go to the documentation of this file.
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"
22 #include "clang/Format/Format.h"
23 #include "llvm/ADT/MapVector.h"
24 #include "llvm/Support/Regex.h"
25 
26 #include <stack>
27 
28 namespace clang {
29 namespace format {
30 
31 enum LexerState {
35 };
36 
38 public:
39  FormatTokenLexer(const SourceManager &SourceMgr, FileID ID, unsigned Column,
40  const FormatStyle &Style, encoding::Encoding Encoding);
41 
43 
44  const AdditionalKeywords &getKeywords() { return Keywords; }
45 
46 private:
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;
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
Defines the SourceManager interface.
const AdditionalKeywords & getKeywords()
Contains functions for text encoding manipulation.
const FormatToken & Tok
unsigned Offset
Definition: Format.cpp:1809
FormatTokenLexer(const SourceManager &SourceMgr, FileID ID, unsigned Column, const FormatStyle &Style, encoding::Encoding Encoding)
Implements an efficient mapping from strings to IdentifierInfo nodes.
A wrapper around a Token storing information about the whitespace characters preceding it...
Definition: FormatToken.h:129
Various functions to configurably format source code.
Encapsulates keywords that are context sensitive or for languages not properly supported by Clang&#39;s l...
Definition: FormatToken.h:683
The FormatStyle is used to configure the formatting to follow specific guidelines.
Definition: Format.h:49
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Dataflow Directional Tag Classes.
Defines the clang::SourceLocation class and associated facilities.
This file contains the declaration of the FormatToken, a wrapper around Token with additional informa...
This class handles loading and caching of source files into memory.
ArrayRef< FormatToken * > lex()