blob: 3fbc73fd75b9a04cf4ec586646f7b6f3dc6e64a2 [file] [log] [blame]
Chandler Carruth55fc8732012-12-04 09:13:33 +00001//===--- UnwrappedLineParser.h - Format C++ code ----------------*- C++ -*-===//
Daniel Jasperbac016b2012-12-03 18:12:45 +00002//
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 Jasperbac016b2012-12-03 18:12:45 +000022#include "clang/Basic/IdentifierTable.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000023#include "clang/Basic/SourceManager.h"
Daniel Jasperbac016b2012-12-03 18:12:45 +000024#include "clang/Lex/Lexer.h"
25
26namespace clang {
27namespace format {
28
29/// \brief A wrapper around a \c Token storing information about the
30/// whitespace characters preceeding it.
31struct FormatToken {
32 FormatToken() : NewlinesBefore(0), WhiteSpaceLength(0) {
33 }
34
35 /// \brief The \c Token.
36 Token Tok;
37
38 /// \brief The number of newlines immediately before the \c Token.
39 ///
40 /// This can be used to determine what the user wrote in the original code
41 /// and thereby e.g. leave an empty line between two function definitions.
42 unsigned NewlinesBefore;
43
44 /// \brief The location of the start of the whitespace immediately preceeding
45 /// the \c Token.
46 ///
47 /// Used together with \c WhiteSpaceLength to create a \c Replacement.
48 SourceLocation WhiteSpaceStart;
49
50 /// \brief The length in characters of the whitespace immediately preceeding
51 /// the \c Token.
52 unsigned WhiteSpaceLength;
53};
54
55/// \brief An unwrapped line is a sequence of \c Token, that we would like to
56/// put on a single line if there was no column limit.
57///
58/// This is used as a main interface between the \c UnwrappedLineParser and the
59/// \c UnwrappedLineFormatter. The key property is that changing the formatting
60/// within an unwrapped line does not affect any other unwrapped lines.
61struct UnwrappedLine {
62 UnwrappedLine() : Level(0) {
63 }
64
65 /// \brief The \c Token comprising this \c UnwrappedLine.
66 SmallVector<FormatToken, 16> Tokens;
67
68 /// \brief The indent level of the \c UnwrappedLine.
69 unsigned Level;
70};
71
72class UnwrappedLineConsumer {
73public:
Daniel Jasperaccb0b02012-12-04 21:05:31 +000074 virtual ~UnwrappedLineConsumer() {
75 }
Alexander Kornienko720ffb62012-12-05 13:56:52 +000076 virtual void consumeUnwrappedLine(const UnwrappedLine &Line) = 0;
Daniel Jasperbac016b2012-12-03 18:12:45 +000077};
78
79class UnwrappedLineParser {
80public:
81 UnwrappedLineParser(Lexer &Lex, SourceManager &SourceMgr,
82 UnwrappedLineConsumer &Callback);
83
Alexander Kornienkocff563c2012-12-04 17:27:50 +000084 /// Returns true in case of a structural error.
85 bool parse();
Daniel Jasperbac016b2012-12-03 18:12:45 +000086
87private:
Alexander Kornienkocff563c2012-12-04 17:27:50 +000088 bool parseLevel();
89 bool parseBlock();
Daniel Jasperbac016b2012-12-03 18:12:45 +000090 void parsePPDirective();
91 void parseComment();
92 void parseStatement();
93 void parseParens();
94 void parseIfThenElse();
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +000095 void parseForOrWhileLoop();
Daniel Jasperbac016b2012-12-03 18:12:45 +000096 void parseDoWhile();
97 void parseLabel();
98 void parseCaseLabel();
99 void parseSwitch();
100 void parseAccessSpecifier();
101 void parseEnum();
102 void addUnwrappedLine();
103 bool eof() const;
104 void nextToken();
105 void parseToken();
106
107 /// Returns the text of \c FormatTok.
108 StringRef tokenText();
109
110 UnwrappedLine Line;
111 FormatToken FormatTok;
112 bool GreaterStashed;
113
114 Lexer &Lex;
115 SourceManager &SourceMgr;
116 IdentifierTable IdentTable;
117 UnwrappedLineConsumer &Callback;
118};
119
120} // end namespace format
121} // end namespace clang
122
123#endif // LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H