blob: a9646261461a0ac56a987e9d4675db386b1a201d [file] [log] [blame]
Martin Probstc4a0dd42016-05-20 11:24:24 +00001//===--- FormatTokenLexer.h - Format C++ code ----------------*- C++ ----*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Probstc4a0dd42016-05-20 11:24:24 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000010/// This file contains FormatTokenLexer, which tokenizes a source file
Martin Probstc4a0dd42016-05-20 11:24:24 +000011/// 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 Ferrand6f40e212018-10-02 16:37:51 +000023#include "llvm/ADT/MapVector.h"
Paul Hoad5bcf99b2019-03-01 09:09:54 +000024#include "llvm/Support/Regex.h"
Martin Probstc4a0dd42016-05-20 11:24:24 +000025
Martin Probst6181da42016-08-25 10:13:21 +000026#include <stack>
27
Martin Probstc4a0dd42016-05-20 11:24:24 +000028namespace clang {
29namespace format {
30
Martin Probst6181da42016-08-25 10:13:21 +000031enum LexerState {
32 NORMAL,
33 TEMPLATE_STRING,
34 TOKEN_STASHED,
35};
36
Martin Probstc4a0dd42016-05-20 11:24:24 +000037class FormatTokenLexer {
38public:
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +000039 FormatTokenLexer(const SourceManager &SourceMgr, FileID ID, unsigned Column,
Martin Probstc4a0dd42016-05-20 11:24:24 +000040 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();
Alexander Kornienkod4fa2e62017-04-11 09:55:00 +000050 bool tryMergeNSStringLiteral();
Martin Probst26a484f42019-03-19 12:28:41 +000051 bool tryMergeJSPrivateIdentifier();
Martin Probstc4a0dd42016-05-20 11:24:24 +000052
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 Probst6181da42016-08-25 10:13:21 +000066 // 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 Probstc4a0dd42016-05-20 11:24:24 +000076
Krasimir Georgiev410ed242017-11-10 12:50:09 +000077 void tryParsePythonComment();
78
Martin Probstc4a0dd42016-05-20 11:24:24 +000079 bool tryMerge_TMacro();
80
81 bool tryMergeConflictMarkers();
82
83 FormatToken *getStashedToken();
84
85 FormatToken *getNextToken();
86
87 FormatToken *FormatTok;
88 bool IsFirstToken;
Martin Probst6181da42016-08-25 10:13:21 +000089 std::stack<LexerState> StateStack;
Martin Probstc4a0dd42016-05-20 11:24:24 +000090 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 Ferrand6f40e212018-10-02 16:37:51 +0000103
104 llvm::SmallMapVector<IdentifierInfo *, TokenType, 8> Macros;
Martin Probstc4a0dd42016-05-20 11:24:24 +0000105
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