blob: 2308c92fa1fe1b80b28aef9d5ba9c90beac2350b [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 {
Manuel Klimeka080a182013-01-02 16:30:12 +000033 FormatToken()
Manuel Klimekf6fd00b2013-01-05 22:56:06 +000034 : NewlinesBefore(0), HasUnescapedNewline(false), WhiteSpaceLength(0),
35 IsFirst(false) {
Daniel Jasperbac016b2012-12-03 18:12:45 +000036 }
37
38 /// \brief The \c Token.
39 Token Tok;
40
41 /// \brief The number of newlines immediately before the \c Token.
42 ///
43 /// This can be used to determine what the user wrote in the original code
44 /// and thereby e.g. leave an empty line between two function definitions.
45 unsigned NewlinesBefore;
46
Manuel Klimeka080a182013-01-02 16:30:12 +000047 /// \brief Whether there is at least one unescaped newline before the \c
48 /// Token.
49 bool HasUnescapedNewline;
50
Daniel Jasperbac016b2012-12-03 18:12:45 +000051 /// \brief The location of the start of the whitespace immediately preceeding
52 /// the \c Token.
53 ///
54 /// Used together with \c WhiteSpaceLength to create a \c Replacement.
55 SourceLocation WhiteSpaceStart;
56
57 /// \brief The length in characters of the whitespace immediately preceeding
58 /// the \c Token.
59 unsigned WhiteSpaceLength;
Manuel Klimekf6fd00b2013-01-05 22:56:06 +000060
61 /// \brief Indicates that this is the first token.
62 bool IsFirst;
Daniel Jasperbac016b2012-12-03 18:12:45 +000063};
64
65/// \brief An unwrapped line is a sequence of \c Token, that we would like to
66/// put on a single line if there was no column limit.
67///
68/// This is used as a main interface between the \c UnwrappedLineParser and the
69/// \c UnwrappedLineFormatter. The key property is that changing the formatting
70/// within an unwrapped line does not affect any other unwrapped lines.
71struct UnwrappedLine {
Manuel Klimeka080a182013-01-02 16:30:12 +000072 UnwrappedLine() : Level(0), InPPDirective(false) {
Daniel Jasperbac016b2012-12-03 18:12:45 +000073 }
74
75 /// \brief The \c Token comprising this \c UnwrappedLine.
76 SmallVector<FormatToken, 16> Tokens;
77
78 /// \brief The indent level of the \c UnwrappedLine.
79 unsigned Level;
Manuel Klimeka080a182013-01-02 16:30:12 +000080
81 /// \brief Whether this \c UnwrappedLine is part of a preprocessor directive.
82 bool InPPDirective;
Daniel Jasperbac016b2012-12-03 18:12:45 +000083};
84
85class UnwrappedLineConsumer {
86public:
Daniel Jasperaccb0b02012-12-04 21:05:31 +000087 virtual ~UnwrappedLineConsumer() {
88 }
Alexander Kornienko720ffb62012-12-05 13:56:52 +000089 virtual void consumeUnwrappedLine(const UnwrappedLine &Line) = 0;
Daniel Jasperbac016b2012-12-03 18:12:45 +000090};
91
Alexander Kornienko469a21b2012-12-07 16:15:44 +000092class FormatTokenSource {
93public:
Matt Beaumont-Gay422daa12012-12-07 22:49:27 +000094 virtual ~FormatTokenSource() {
95 }
Alexander Kornienko469a21b2012-12-07 16:15:44 +000096 virtual FormatToken getNextToken() = 0;
97};
98
Daniel Jasperbac016b2012-12-03 18:12:45 +000099class UnwrappedLineParser {
100public:
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000101 UnwrappedLineParser(const FormatStyle &Style, FormatTokenSource &Tokens,
Daniel Jasperbac016b2012-12-03 18:12:45 +0000102 UnwrappedLineConsumer &Callback);
103
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000104 /// Returns true in case of a structural error.
105 bool parse();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000106
107private:
Manuel Klimekd4397b92013-01-04 23:34:14 +0000108 bool parseFile();
Manuel Klimeka5342db2013-01-06 20:07:31 +0000109 bool parseLevel(bool HasOpeningBrace);
Alexander Kornienko15757312012-12-06 18:03:27 +0000110 bool parseBlock(unsigned AddLevels = 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000111 void parsePPDirective();
Manuel Klimekd4397b92013-01-04 23:34:14 +0000112 void parsePPDefine();
113 void parsePPUnknown();
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000114 void parseComments();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000115 void parseStatement();
116 void parseParens();
117 void parseIfThenElse();
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000118 void parseForOrWhileLoop();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000119 void parseDoWhile();
120 void parseLabel();
121 void parseCaseLabel();
122 void parseSwitch();
Alexander Kornienko15757312012-12-06 18:03:27 +0000123 void parseNamespace();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000124 void parseAccessSpecifier();
125 void parseEnum();
126 void addUnwrappedLine();
127 bool eof() const;
128 void nextToken();
Manuel Klimekd4397b92013-01-04 23:34:14 +0000129 void readToken();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000130
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000131 // FIXME: We are constantly running into bugs where Line.Level is incorrectly
132 // subtracted from beyond 0. Introduce a method to subtract from Line.Level
133 // and use that everywhere in the Parser.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000134 UnwrappedLine Line;
135 FormatToken FormatTok;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000136
Alexander Kornienko15757312012-12-06 18:03:27 +0000137 const FormatStyle &Style;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000138 FormatTokenSource *Tokens;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000139 UnwrappedLineConsumer &Callback;
140};
141
142} // end namespace format
143} // end namespace clang
144
Daniel Jasperd7610b82012-12-24 16:51:15 +0000145#endif // LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H