blob: 92a51abf10ee7472f10282ed573e80b0ecb5d110 [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
Alexander Kornienko469a21b2012-12-07 16:15:44 +000080class FormatTokenSource {
81public:
82 virtual FormatToken getNextToken() = 0;
83};
84
Daniel Jasperbac016b2012-12-03 18:12:45 +000085class UnwrappedLineParser {
86public:
Alexander Kornienko469a21b2012-12-07 16:15:44 +000087 UnwrappedLineParser(const FormatStyle &Style, FormatTokenSource &Tokens,
Daniel Jasperbac016b2012-12-03 18:12:45 +000088 UnwrappedLineConsumer &Callback);
89
Alexander Kornienkocff563c2012-12-04 17:27:50 +000090 /// Returns true in case of a structural error.
91 bool parse();
Daniel Jasperbac016b2012-12-03 18:12:45 +000092
93private:
Alexander Kornienkocff563c2012-12-04 17:27:50 +000094 bool parseLevel();
Alexander Kornienko15757312012-12-06 18:03:27 +000095 bool parseBlock(unsigned AddLevels = 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +000096 void parsePPDirective();
97 void parseComment();
98 void parseStatement();
99 void parseParens();
100 void parseIfThenElse();
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000101 void parseForOrWhileLoop();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000102 void parseDoWhile();
103 void parseLabel();
104 void parseCaseLabel();
105 void parseSwitch();
Alexander Kornienko15757312012-12-06 18:03:27 +0000106 void parseNamespace();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000107 void parseAccessSpecifier();
108 void parseEnum();
109 void addUnwrappedLine();
110 bool eof() const;
111 void nextToken();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000112
113 UnwrappedLine Line;
114 FormatToken FormatTok;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000115
Alexander Kornienko15757312012-12-06 18:03:27 +0000116 const FormatStyle &Style;
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000117 FormatTokenSource &Tokens;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000118 UnwrappedLineConsumer &Callback;
119};
120
121} // end namespace format
122} // end namespace clang
123
124#endif // LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H