Martin Probst | c4a0dd4 | 2016-05-20 11:24:24 +0000 | [diff] [blame] | 1 | //===--- FormatTokenLexer.h - Format C++ code ----------------*- C++ ----*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Martin Probst | c4a0dd4 | 2016-05-20 11:24:24 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 10 | /// This file contains FormatTokenLexer, which tokenizes a source file |
Martin Probst | c4a0dd4 | 2016-05-20 11:24:24 +0000 | [diff] [blame] | 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" |
Francois Ferrand | 6f40e21 | 2018-10-02 16:37:51 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/MapVector.h" |
Paul Hoad | 5bcf99b | 2019-03-01 09:09:54 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Regex.h" |
Martin Probst | c4a0dd4 | 2016-05-20 11:24:24 +0000 | [diff] [blame] | 25 | |
Martin Probst | 6181da4 | 2016-08-25 10:13:21 +0000 | [diff] [blame] | 26 | #include <stack> |
| 27 | |
Martin Probst | c4a0dd4 | 2016-05-20 11:24:24 +0000 | [diff] [blame] | 28 | namespace clang { |
| 29 | namespace format { |
| 30 | |
Martin Probst | 6181da4 | 2016-08-25 10:13:21 +0000 | [diff] [blame] | 31 | enum LexerState { |
| 32 | NORMAL, |
| 33 | TEMPLATE_STRING, |
| 34 | TOKEN_STASHED, |
| 35 | }; |
| 36 | |
Martin Probst | c4a0dd4 | 2016-05-20 11:24:24 +0000 | [diff] [blame] | 37 | class FormatTokenLexer { |
| 38 | public: |
Krasimir Georgiev | 9ad83fe | 2017-10-30 14:01:50 +0000 | [diff] [blame] | 39 | FormatTokenLexer(const SourceManager &SourceMgr, FileID ID, unsigned Column, |
Martin Probst | c4a0dd4 | 2016-05-20 11:24:24 +0000 | [diff] [blame] | 40 | const FormatStyle &Style, encoding::Encoding Encoding); |
| 41 | |
| 42 | ArrayRef<FormatToken *> lex(); |
| 43 | |
| 44 | const AdditionalKeywords &getKeywords() { return Keywords; } |
| 45 | |
| 46 | private: |
| 47 | void tryMergePreviousTokens(); |
| 48 | |
| 49 | bool tryMergeLessLess(); |
Alexander Kornienko | d4fa2e6 | 2017-04-11 09:55:00 +0000 | [diff] [blame] | 50 | bool tryMergeNSStringLiteral(); |
Martin Probst | 26a484f4 | 2019-03-19 12:28:41 +0000 | [diff] [blame] | 51 | bool tryMergeJSPrivateIdentifier(); |
Martin Probst | c4a0dd4 | 2016-05-20 11:24:24 +0000 | [diff] [blame] | 52 | |
| 53 | bool tryMergeTokens(ArrayRef<tok::TokenKind> Kinds, TokenType NewType); |
| 54 | |
| 55 | // Returns \c true if \p Tok can only be followed by an operand in JavaScript. |
| 56 | bool precedesOperand(FormatToken *Tok); |
| 57 | |
| 58 | bool canPrecedeRegexLiteral(FormatToken *Prev); |
| 59 | |
| 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(); |
| 65 | |
Martin Probst | 6181da4 | 2016-08-25 10:13:21 +0000 | [diff] [blame] | 66 | // Handles JavaScript template strings. |
| 67 | // |
| 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. |
| 71 | // |
| 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(); |
Martin Probst | c4a0dd4 | 2016-05-20 11:24:24 +0000 | [diff] [blame] | 76 | |
Krasimir Georgiev | 410ed24 | 2017-11-10 12:50:09 +0000 | [diff] [blame] | 77 | void tryParsePythonComment(); |
| 78 | |
Martin Probst | c4a0dd4 | 2016-05-20 11:24:24 +0000 | [diff] [blame] | 79 | bool tryMerge_TMacro(); |
| 80 | |
| 81 | bool tryMergeConflictMarkers(); |
| 82 | |
| 83 | FormatToken *getStashedToken(); |
| 84 | |
| 85 | FormatToken *getNextToken(); |
| 86 | |
| 87 | FormatToken *FormatTok; |
| 88 | bool IsFirstToken; |
Martin Probst | 6181da4 | 2016-08-25 10:13:21 +0000 | [diff] [blame] | 89 | std::stack<LexerState> StateStack; |
Martin Probst | c4a0dd4 | 2016-05-20 11:24:24 +0000 | [diff] [blame] | 90 | unsigned Column; |
| 91 | unsigned TrailingWhitespace; |
| 92 | std::unique_ptr<Lexer> Lex; |
| 93 | const SourceManager &SourceMgr; |
| 94 | FileID ID; |
| 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; |
Francois Ferrand | 6f40e21 | 2018-10-02 16:37:51 +0000 | [diff] [blame] | 103 | |
| 104 | llvm::SmallMapVector<IdentifierInfo *, TokenType, 8> Macros; |
Martin Probst | c4a0dd4 | 2016-05-20 11:24:24 +0000 | [diff] [blame] | 105 | |
| 106 | bool FormattingDisabled; |
| 107 | |
| 108 | llvm::Regex MacroBlockBeginRegex; |
| 109 | llvm::Regex MacroBlockEndRegex; |
| 110 | |
| 111 | void readRawToken(FormatToken &Tok); |
| 112 | |
| 113 | void resetLexer(unsigned Offset); |
| 114 | }; |
| 115 | |
| 116 | } // namespace format |
| 117 | } // namespace clang |
| 118 | |
| 119 | #endif |