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 { |
| 33 | FormatToken() : NewlinesBefore(0), WhiteSpaceLength(0) { |
| 34 | } |
| 35 | |
| 36 | /// \brief The \c Token. |
| 37 | Token Tok; |
| 38 | |
| 39 | /// \brief The number of newlines immediately before the \c Token. |
| 40 | /// |
| 41 | /// This can be used to determine what the user wrote in the original code |
| 42 | /// and thereby e.g. leave an empty line between two function definitions. |
| 43 | unsigned NewlinesBefore; |
| 44 | |
| 45 | /// \brief The location of the start of the whitespace immediately preceeding |
| 46 | /// the \c Token. |
| 47 | /// |
| 48 | /// Used together with \c WhiteSpaceLength to create a \c Replacement. |
| 49 | SourceLocation WhiteSpaceStart; |
| 50 | |
| 51 | /// \brief The length in characters of the whitespace immediately preceeding |
| 52 | /// the \c Token. |
| 53 | unsigned WhiteSpaceLength; |
| 54 | }; |
| 55 | |
| 56 | /// \brief An unwrapped line is a sequence of \c Token, that we would like to |
| 57 | /// put on a single line if there was no column limit. |
| 58 | /// |
| 59 | /// This is used as a main interface between the \c UnwrappedLineParser and the |
| 60 | /// \c UnwrappedLineFormatter. The key property is that changing the formatting |
| 61 | /// within an unwrapped line does not affect any other unwrapped lines. |
| 62 | struct UnwrappedLine { |
| 63 | UnwrappedLine() : Level(0) { |
| 64 | } |
| 65 | |
| 66 | /// \brief The \c Token comprising this \c UnwrappedLine. |
| 67 | SmallVector<FormatToken, 16> Tokens; |
| 68 | |
| 69 | /// \brief The indent level of the \c UnwrappedLine. |
| 70 | unsigned Level; |
| 71 | }; |
| 72 | |
| 73 | class UnwrappedLineConsumer { |
| 74 | public: |
Daniel Jasper | accb0b0 | 2012-12-04 21:05:31 +0000 | [diff] [blame] | 75 | virtual ~UnwrappedLineConsumer() { |
| 76 | } |
Alexander Kornienko | 720ffb6 | 2012-12-05 13:56:52 +0000 | [diff] [blame] | 77 | virtual void consumeUnwrappedLine(const UnwrappedLine &Line) = 0; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | class UnwrappedLineParser { |
| 81 | public: |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 82 | UnwrappedLineParser(const FormatStyle &Style, Lexer &Lex, |
| 83 | SourceManager &SourceMgr, |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 84 | UnwrappedLineConsumer &Callback); |
| 85 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 86 | /// Returns true in case of a structural error. |
| 87 | bool parse(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 88 | |
| 89 | private: |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 90 | bool parseLevel(); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 91 | bool parseBlock(unsigned AddLevels = 1); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 92 | void parsePPDirective(); |
| 93 | void parseComment(); |
| 94 | void parseStatement(); |
| 95 | void parseParens(); |
| 96 | void parseIfThenElse(); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 97 | void parseForOrWhileLoop(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 98 | void parseDoWhile(); |
| 99 | void parseLabel(); |
| 100 | void parseCaseLabel(); |
| 101 | void parseSwitch(); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 102 | void parseNamespace(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 103 | void parseAccessSpecifier(); |
| 104 | void parseEnum(); |
| 105 | void addUnwrappedLine(); |
| 106 | bool eof() const; |
| 107 | void nextToken(); |
| 108 | void parseToken(); |
| 109 | |
| 110 | /// Returns the text of \c FormatTok. |
| 111 | StringRef tokenText(); |
| 112 | |
| 113 | UnwrappedLine Line; |
| 114 | FormatToken FormatTok; |
| 115 | bool GreaterStashed; |
| 116 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 117 | const FormatStyle &Style; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 118 | Lexer &Lex; |
| 119 | SourceManager &SourceMgr; |
| 120 | IdentifierTable IdentTable; |
| 121 | UnwrappedLineConsumer &Callback; |
| 122 | }; |
| 123 | |
| 124 | } // end namespace format |
| 125 | } // end namespace clang |
| 126 | |
| 127 | #endif // LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H |