blob: 03dda9957da12f44e24ab4a605c8b21d12b4fe61 [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"
Alexander Kornienko15757312012-12-06 18:03:27 +000024#include "clang/Format/Format.h"
Daniel Jasperbac016b2012-12-03 18:12:45 +000025#include "clang/Lex/Lexer.h"
26
27namespace clang {
28namespace format {
29
30/// \brief A wrapper around a \c Token storing information about the
31/// whitespace characters preceeding it.
32struct 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.
62struct 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
73class UnwrappedLineConsumer {
74public:
Daniel Jasperaccb0b02012-12-04 21:05:31 +000075 virtual ~UnwrappedLineConsumer() {
76 }
Alexander Kornienko720ffb62012-12-05 13:56:52 +000077 virtual void consumeUnwrappedLine(const UnwrappedLine &Line) = 0;
Daniel Jasperbac016b2012-12-03 18:12:45 +000078};
79
80class UnwrappedLineParser {
81public:
Alexander Kornienko15757312012-12-06 18:03:27 +000082 UnwrappedLineParser(const FormatStyle &Style, Lexer &Lex,
83 SourceManager &SourceMgr,
Daniel Jasperbac016b2012-12-03 18:12:45 +000084 UnwrappedLineConsumer &Callback);
85
Alexander Kornienkocff563c2012-12-04 17:27:50 +000086 /// Returns true in case of a structural error.
87 bool parse();
Daniel Jasperbac016b2012-12-03 18:12:45 +000088
89private:
Alexander Kornienkocff563c2012-12-04 17:27:50 +000090 bool parseLevel();
Alexander Kornienko15757312012-12-06 18:03:27 +000091 bool parseBlock(unsigned AddLevels = 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +000092 void parsePPDirective();
93 void parseComment();
94 void parseStatement();
95 void parseParens();
96 void parseIfThenElse();
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +000097 void parseForOrWhileLoop();
Daniel Jasperbac016b2012-12-03 18:12:45 +000098 void parseDoWhile();
99 void parseLabel();
100 void parseCaseLabel();
101 void parseSwitch();
Alexander Kornienko15757312012-12-06 18:03:27 +0000102 void parseNamespace();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000103 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 Kornienko15757312012-12-06 18:03:27 +0000117 const FormatStyle &Style;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000118 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