Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 1 | //===--- UnwrappedLineParser.h - Format C++ code ----------------*- C++ -*-===// |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
| 11 | /// \brief This file contains the declaration of the UnwrappedLineParser, |
| 12 | /// which turns a stream of tokens into UnwrappedLines. |
| 13 | /// |
| 14 | /// This is EXPERIMENTAL code under heavy development. It is not in a state yet, |
| 15 | /// where it can be used to format real code. |
| 16 | /// |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
| 19 | #ifndef LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H |
| 20 | #define LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H |
| 21 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 22 | #include "clang/Basic/IdentifierTable.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include "clang/Basic/SourceManager.h" |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 24 | #include "clang/Format/Format.h" |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 25 | #include "clang/Lex/Lexer.h" |
| 26 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 27 | #include <vector> |
| 28 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 29 | namespace clang { |
| 30 | namespace format { |
| 31 | |
| 32 | /// \brief A wrapper around a \c Token storing information about the |
| 33 | /// whitespace characters preceeding it. |
| 34 | struct FormatToken { |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 35 | FormatToken() |
Manuel Klimek | f6fd00b | 2013-01-05 22:56:06 +0000 | [diff] [blame] | 36 | : NewlinesBefore(0), HasUnescapedNewline(false), WhiteSpaceLength(0), |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 37 | TokenLength(0), IsFirst(false), MustBreakBefore(false) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | /// \brief The \c Token. |
| 41 | Token Tok; |
| 42 | |
| 43 | /// \brief The number of newlines immediately before the \c Token. |
| 44 | /// |
| 45 | /// This can be used to determine what the user wrote in the original code |
| 46 | /// and thereby e.g. leave an empty line between two function definitions. |
| 47 | unsigned NewlinesBefore; |
| 48 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 49 | /// \brief Whether there is at least one unescaped newline before the \c |
| 50 | /// Token. |
| 51 | bool HasUnescapedNewline; |
| 52 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 53 | /// \brief The location of the start of the whitespace immediately preceeding |
| 54 | /// the \c Token. |
| 55 | /// |
| 56 | /// Used together with \c WhiteSpaceLength to create a \c Replacement. |
| 57 | SourceLocation WhiteSpaceStart; |
| 58 | |
| 59 | /// \brief The length in characters of the whitespace immediately preceeding |
| 60 | /// the \c Token. |
| 61 | unsigned WhiteSpaceLength; |
Manuel Klimek | f6fd00b | 2013-01-05 22:56:06 +0000 | [diff] [blame] | 62 | |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 63 | /// \brief The length of the non-whitespace parts of the token. This is |
| 64 | /// necessary because we need to handle escaped newlines that are stored |
| 65 | /// with the token. |
| 66 | unsigned TokenLength; |
| 67 | |
Manuel Klimek | f6fd00b | 2013-01-05 22:56:06 +0000 | [diff] [blame] | 68 | /// \brief Indicates that this is the first token. |
| 69 | bool IsFirst; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 70 | |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 71 | /// \brief Whether there must be a line break before this token. |
| 72 | /// |
| 73 | /// This happens for example when a preprocessor directive ended directly |
| 74 | /// before the token. |
| 75 | bool MustBreakBefore; |
| 76 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 77 | // FIXME: We currently assume that there is exactly one token in this vector |
| 78 | // except for the very last token that does not have any children. |
| 79 | /// \brief All tokens that logically follow this token. |
| 80 | std::vector<FormatToken> Children; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | /// \brief An unwrapped line is a sequence of \c Token, that we would like to |
| 84 | /// put on a single line if there was no column limit. |
| 85 | /// |
| 86 | /// This is used as a main interface between the \c UnwrappedLineParser and the |
| 87 | /// \c UnwrappedLineFormatter. The key property is that changing the formatting |
| 88 | /// within an unwrapped line does not affect any other unwrapped lines. |
| 89 | struct UnwrappedLine { |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 90 | UnwrappedLine() : Level(0), InPPDirective(false) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /// \brief The \c Token comprising this \c UnwrappedLine. |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 94 | FormatToken RootToken; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 95 | |
| 96 | /// \brief The indent level of the \c UnwrappedLine. |
| 97 | unsigned Level; |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 98 | |
| 99 | /// \brief Whether this \c UnwrappedLine is part of a preprocessor directive. |
| 100 | bool InPPDirective; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | class UnwrappedLineConsumer { |
| 104 | public: |
Daniel Jasper | accb0b0 | 2012-12-04 21:05:31 +0000 | [diff] [blame] | 105 | virtual ~UnwrappedLineConsumer() { |
| 106 | } |
Alexander Kornienko | 720ffb6 | 2012-12-05 13:56:52 +0000 | [diff] [blame] | 107 | virtual void consumeUnwrappedLine(const UnwrappedLine &Line) = 0; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 110 | class FormatTokenSource { |
| 111 | public: |
Matt Beaumont-Gay | 422daa1 | 2012-12-07 22:49:27 +0000 | [diff] [blame] | 112 | virtual ~FormatTokenSource() { |
| 113 | } |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 114 | virtual FormatToken getNextToken() = 0; |
| 115 | }; |
| 116 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 117 | class UnwrappedLineParser { |
| 118 | public: |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 119 | UnwrappedLineParser(const FormatStyle &Style, FormatTokenSource &Tokens, |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 120 | UnwrappedLineConsumer &Callback); |
| 121 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 122 | /// Returns true in case of a structural error. |
| 123 | bool parse(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 124 | |
| 125 | private: |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 126 | bool parseFile(); |
Manuel Klimek | a5342db | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 127 | bool parseLevel(bool HasOpeningBrace); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 128 | bool parseBlock(unsigned AddLevels = 1); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 129 | void parsePPDirective(); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 130 | void parsePPDefine(); |
| 131 | void parsePPUnknown(); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 132 | void parseComments(); |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 133 | void parseStructuralElement(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 134 | void parseParens(); |
| 135 | void parseIfThenElse(); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 136 | void parseForOrWhileLoop(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 137 | void parseDoWhile(); |
| 138 | void parseLabel(); |
| 139 | void parseCaseLabel(); |
| 140 | void parseSwitch(); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 141 | void parseNamespace(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 142 | void parseAccessSpecifier(); |
| 143 | void parseEnum(); |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 144 | void parseStructOrClass(); |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 145 | void parseObjCProtocolList(); |
| 146 | void parseObjCUntilAtEnd(); |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame^] | 147 | void parseObjCInterfaceOrImplementation(); |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 148 | void parseObjCProtocol(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 149 | void addUnwrappedLine(); |
| 150 | bool eof() const; |
| 151 | void nextToken(); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 152 | void readToken(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 153 | |
Manuel Klimek | c37b4d6 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 154 | // FIXME: We are constantly running into bugs where Line.Level is incorrectly |
| 155 | // subtracted from beyond 0. Introduce a method to subtract from Line.Level |
| 156 | // and use that everywhere in the Parser. |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 157 | llvm::OwningPtr<UnwrappedLine> Line; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 158 | bool RootTokenInitialized; |
| 159 | FormatToken *LastInCurrentLine; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 160 | FormatToken FormatTok; |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 161 | bool MustBreakBeforeNextToken; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 162 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 163 | const FormatStyle &Style; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 164 | FormatTokenSource *Tokens; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 165 | UnwrappedLineConsumer &Callback; |
| 166 | }; |
| 167 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 168 | } // end namespace format |
| 169 | } // end namespace clang |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 170 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 171 | #endif // LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H |