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 | |
| 27 | namespace clang { |
| 28 | namespace format { |
| 29 | |
| 30 | /// \brief A wrapper around a \c Token storing information about the |
| 31 | /// whitespace characters preceeding it. |
| 32 | struct FormatToken { |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 33 | FormatToken() |
Manuel Klimek | f6fd00b | 2013-01-05 22:56:06 +0000 | [diff] [blame] | 34 | : NewlinesBefore(0), HasUnescapedNewline(false), WhiteSpaceLength(0), |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame^] | 35 | TokenLength(0), IsFirst(false) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | /// \brief The \c Token. |
| 39 | Token Tok; |
| 40 | |
| 41 | /// \brief The number of newlines immediately before the \c Token. |
| 42 | /// |
| 43 | /// This can be used to determine what the user wrote in the original code |
| 44 | /// and thereby e.g. leave an empty line between two function definitions. |
| 45 | unsigned NewlinesBefore; |
| 46 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 47 | /// \brief Whether there is at least one unescaped newline before the \c |
| 48 | /// Token. |
| 49 | bool HasUnescapedNewline; |
| 50 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 51 | /// \brief The location of the start of the whitespace immediately preceeding |
| 52 | /// the \c Token. |
| 53 | /// |
| 54 | /// Used together with \c WhiteSpaceLength to create a \c Replacement. |
| 55 | SourceLocation WhiteSpaceStart; |
| 56 | |
| 57 | /// \brief The length in characters of the whitespace immediately preceeding |
| 58 | /// the \c Token. |
| 59 | unsigned WhiteSpaceLength; |
Manuel Klimek | f6fd00b | 2013-01-05 22:56:06 +0000 | [diff] [blame] | 60 | |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame^] | 61 | /// \brief The length of the non-whitespace parts of the token. This is |
| 62 | /// necessary because we need to handle escaped newlines that are stored |
| 63 | /// with the token. |
| 64 | unsigned TokenLength; |
| 65 | |
Manuel Klimek | f6fd00b | 2013-01-05 22:56:06 +0000 | [diff] [blame] | 66 | /// \brief Indicates that this is the first token. |
| 67 | bool IsFirst; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | /// \brief An unwrapped line is a sequence of \c Token, that we would like to |
| 71 | /// put on a single line if there was no column limit. |
| 72 | /// |
| 73 | /// This is used as a main interface between the \c UnwrappedLineParser and the |
| 74 | /// \c UnwrappedLineFormatter. The key property is that changing the formatting |
| 75 | /// within an unwrapped line does not affect any other unwrapped lines. |
| 76 | struct UnwrappedLine { |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 77 | UnwrappedLine() : Level(0), InPPDirective(false) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /// \brief The \c Token comprising this \c UnwrappedLine. |
| 81 | SmallVector<FormatToken, 16> Tokens; |
| 82 | |
| 83 | /// \brief The indent level of the \c UnwrappedLine. |
| 84 | unsigned Level; |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 85 | |
| 86 | /// \brief Whether this \c UnwrappedLine is part of a preprocessor directive. |
| 87 | bool InPPDirective; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | class UnwrappedLineConsumer { |
| 91 | public: |
Daniel Jasper | accb0b0 | 2012-12-04 21:05:31 +0000 | [diff] [blame] | 92 | virtual ~UnwrappedLineConsumer() { |
| 93 | } |
Alexander Kornienko | 720ffb6 | 2012-12-05 13:56:52 +0000 | [diff] [blame] | 94 | virtual void consumeUnwrappedLine(const UnwrappedLine &Line) = 0; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 97 | class FormatTokenSource { |
| 98 | public: |
Matt Beaumont-Gay | 422daa1 | 2012-12-07 22:49:27 +0000 | [diff] [blame] | 99 | virtual ~FormatTokenSource() { |
| 100 | } |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 101 | virtual FormatToken getNextToken() = 0; |
| 102 | }; |
| 103 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 104 | class UnwrappedLineParser { |
| 105 | public: |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 106 | UnwrappedLineParser(const FormatStyle &Style, FormatTokenSource &Tokens, |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 107 | UnwrappedLineConsumer &Callback); |
| 108 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 109 | /// Returns true in case of a structural error. |
| 110 | bool parse(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 111 | |
| 112 | private: |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 113 | bool parseFile(); |
Manuel Klimek | a5342db | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 114 | bool parseLevel(bool HasOpeningBrace); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 115 | bool parseBlock(unsigned AddLevels = 1); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 116 | void parsePPDirective(); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 117 | void parsePPDefine(); |
| 118 | void parsePPUnknown(); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 119 | void parseComments(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 120 | void parseStatement(); |
| 121 | void parseParens(); |
| 122 | void parseIfThenElse(); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 123 | void parseForOrWhileLoop(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 124 | void parseDoWhile(); |
| 125 | void parseLabel(); |
| 126 | void parseCaseLabel(); |
| 127 | void parseSwitch(); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 128 | void parseNamespace(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 129 | void parseAccessSpecifier(); |
| 130 | void parseEnum(); |
| 131 | void addUnwrappedLine(); |
| 132 | bool eof() const; |
| 133 | void nextToken(); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 134 | void readToken(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 135 | |
Manuel Klimek | c37b4d6 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 136 | // FIXME: We are constantly running into bugs where Line.Level is incorrectly |
| 137 | // subtracted from beyond 0. Introduce a method to subtract from Line.Level |
| 138 | // and use that everywhere in the Parser. |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 139 | UnwrappedLine Line; |
| 140 | FormatToken FormatTok; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 141 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 142 | const FormatStyle &Style; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 143 | FormatTokenSource *Tokens; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 144 | UnwrappedLineConsumer &Callback; |
| 145 | }; |
| 146 | |
| 147 | } // end namespace format |
| 148 | } // end namespace clang |
| 149 | |
Daniel Jasper | d7610b8 | 2012-12-24 16:51:15 +0000 | [diff] [blame] | 150 | #endif // LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H |