blob: 63f3659706b620fd6a485c6014ca212e61906204 [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:
74 virtual void formatUnwrappedLine(const UnwrappedLine &Line) = 0;
75};
76
77class UnwrappedLineParser {
78public:
79 UnwrappedLineParser(Lexer &Lex, SourceManager &SourceMgr,
80 UnwrappedLineConsumer &Callback);
81
Alexander Kornienkocff563c2012-12-04 17:27:50 +000082 /// Returns true in case of a structural error.
83 bool parse();
Daniel Jasperbac016b2012-12-03 18:12:45 +000084
85private:
Alexander Kornienkocff563c2012-12-04 17:27:50 +000086 bool parseLevel();
87 bool parseBlock();
Daniel Jasperbac016b2012-12-03 18:12:45 +000088 void parsePPDirective();
89 void parseComment();
90 void parseStatement();
91 void parseParens();
92 void parseIfThenElse();
93 void parseDoWhile();
94 void parseLabel();
95 void parseCaseLabel();
96 void parseSwitch();
97 void parseAccessSpecifier();
98 void parseEnum();
99 void addUnwrappedLine();
100 bool eof() const;
101 void nextToken();
102 void parseToken();
103
104 /// Returns the text of \c FormatTok.
105 StringRef tokenText();
106
107 UnwrappedLine Line;
108 FormatToken FormatTok;
109 bool GreaterStashed;
110
111 Lexer &Lex;
112 SourceManager &SourceMgr;
113 IdentifierTable IdentTable;
114 UnwrappedLineConsumer &Callback;
115};
116
117} // end namespace format
118} // end namespace clang
119
120#endif // LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H