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 | |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 80 | class FormatTokenSource { |
| 81 | public: |
Matt Beaumont-Gay | 422daa1 | 2012-12-07 22:49:27 +0000 | [diff] [blame] | 82 | virtual ~FormatTokenSource() { |
| 83 | } |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 84 | virtual FormatToken getNextToken() = 0; |
| 85 | }; |
| 86 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 87 | class UnwrappedLineParser { |
| 88 | public: |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 89 | UnwrappedLineParser(const FormatStyle &Style, FormatTokenSource &Tokens, |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 90 | UnwrappedLineConsumer &Callback); |
| 91 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 92 | /// Returns true in case of a structural error. |
| 93 | bool parse(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 94 | |
| 95 | private: |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 96 | bool parseLevel(); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 97 | bool parseBlock(unsigned AddLevels = 1); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 98 | void parsePPDirective(); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 99 | void parseComments(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 100 | void parseStatement(); |
| 101 | void parseParens(); |
| 102 | void parseIfThenElse(); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 103 | void parseForOrWhileLoop(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 104 | void parseDoWhile(); |
| 105 | void parseLabel(); |
| 106 | void parseCaseLabel(); |
| 107 | void parseSwitch(); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 108 | void parseNamespace(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 109 | void parseAccessSpecifier(); |
| 110 | void parseEnum(); |
| 111 | void addUnwrappedLine(); |
| 112 | bool eof() const; |
| 113 | void nextToken(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 114 | |
| 115 | UnwrappedLine Line; |
| 116 | FormatToken FormatTok; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 117 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 118 | const FormatStyle &Style; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 119 | FormatTokenSource &Tokens; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 120 | UnwrappedLineConsumer &Callback; |
| 121 | }; |
| 122 | |
| 123 | } // end namespace format |
| 124 | } // end namespace clang |
| 125 | |
Daniel Jasper | d7610b8 | 2012-12-24 16:51:15 +0000 | [diff] [blame^] | 126 | #endif // LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H |