blob: 1e6899e092fcafe5b97b2f513a7b990c815d1675 [file] [log] [blame]
Daniel Jasperbac016b2012-12-03 18:12:45 +00001//===--- UnwrappedLineParser.cpp - Format C++ code ------------------------===//
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
22#include "clang/Basic/SourceManager.h"
23#include "clang/Basic/IdentifierTable.h"
24#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
82 void parse();
83
84private:
85 void parseLevel();
86 void parseBlock();
87 void parsePPDirective();
88 void parseComment();
89 void parseStatement();
90 void parseParens();
91 void parseIfThenElse();
92 void parseDoWhile();
93 void parseLabel();
94 void parseCaseLabel();
95 void parseSwitch();
96 void parseAccessSpecifier();
97 void parseEnum();
98 void addUnwrappedLine();
99 bool eof() const;
100 void nextToken();
101 void parseToken();
102
103 /// Returns the text of \c FormatTok.
104 StringRef tokenText();
105
106 UnwrappedLine Line;
107 FormatToken FormatTok;
108 bool GreaterStashed;
109
110 Lexer &Lex;
111 SourceManager &SourceMgr;
112 IdentifierTable IdentTable;
113 UnwrappedLineConsumer &Callback;
114};
115
116} // end namespace format
117} // end namespace clang
118
119#endif // LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H