blob: 27c11020554b7f221b21ba5dace9505158868b4a [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),
Manuel Klimek95419382013-01-07 07:56:50 +000035 TokenLength(0), 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
Manuel Klimek95419382013-01-07 07:56:50 +000061 /// \brief The length of the non-whitespace parts of the token. This is
62 /// necessary because we need to handle escaped newlines that are stored
63 /// with the token.
64 unsigned TokenLength;
65
Manuel Klimekf6fd00b2013-01-05 22:56:06 +000066 /// \brief Indicates that this is the first token.
67 bool IsFirst;
Daniel Jasperbac016b2012-12-03 18:12:45 +000068};
69
70/// \brief An unwrapped line is a sequence of \c Token, that we would like to
71/// put on a single line if there was no column limit.
72///
73/// This is used as a main interface between the \c UnwrappedLineParser and the
74/// \c UnwrappedLineFormatter. The key property is that changing the formatting
75/// within an unwrapped line does not affect any other unwrapped lines.
76struct UnwrappedLine {
Manuel Klimeka080a182013-01-02 16:30:12 +000077 UnwrappedLine() : Level(0), InPPDirective(false) {
Daniel Jasperbac016b2012-12-03 18:12:45 +000078 }
79
80 /// \brief The \c Token comprising this \c UnwrappedLine.
81 SmallVector<FormatToken, 16> Tokens;
82
83 /// \brief The indent level of the \c UnwrappedLine.
84 unsigned Level;
Manuel Klimeka080a182013-01-02 16:30:12 +000085
86 /// \brief Whether this \c UnwrappedLine is part of a preprocessor directive.
87 bool InPPDirective;
Daniel Jasperbac016b2012-12-03 18:12:45 +000088};
89
90class UnwrappedLineConsumer {
91public:
Daniel Jasperaccb0b02012-12-04 21:05:31 +000092 virtual ~UnwrappedLineConsumer() {
93 }
Alexander Kornienko720ffb62012-12-05 13:56:52 +000094 virtual void consumeUnwrappedLine(const UnwrappedLine &Line) = 0;
Daniel Jasperbac016b2012-12-03 18:12:45 +000095};
96
Alexander Kornienko469a21b2012-12-07 16:15:44 +000097class FormatTokenSource {
98public:
Matt Beaumont-Gay422daa12012-12-07 22:49:27 +000099 virtual ~FormatTokenSource() {
100 }
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000101 virtual FormatToken getNextToken() = 0;
102};
103
Daniel Jasperbac016b2012-12-03 18:12:45 +0000104class UnwrappedLineParser {
105public:
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000106 UnwrappedLineParser(const FormatStyle &Style, FormatTokenSource &Tokens,
Daniel Jasperbac016b2012-12-03 18:12:45 +0000107 UnwrappedLineConsumer &Callback);
108
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000109 /// Returns true in case of a structural error.
110 bool parse();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000111
112private:
Manuel Klimekd4397b92013-01-04 23:34:14 +0000113 bool parseFile();
Manuel Klimeka5342db2013-01-06 20:07:31 +0000114 bool parseLevel(bool HasOpeningBrace);
Alexander Kornienko15757312012-12-06 18:03:27 +0000115 bool parseBlock(unsigned AddLevels = 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000116 void parsePPDirective();
Manuel Klimekd4397b92013-01-04 23:34:14 +0000117 void parsePPDefine();
118 void parsePPUnknown();
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000119 void parseComments();
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000120 void parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000121 void parseParens();
122 void parseIfThenElse();
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000123 void parseForOrWhileLoop();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000124 void parseDoWhile();
125 void parseLabel();
126 void parseCaseLabel();
127 void parseSwitch();
Alexander Kornienko15757312012-12-06 18:03:27 +0000128 void parseNamespace();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000129 void parseAccessSpecifier();
130 void parseEnum();
131 void addUnwrappedLine();
132 bool eof() const;
133 void nextToken();
Manuel Klimekd4397b92013-01-04 23:34:14 +0000134 void readToken();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000135
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000136 // FIXME: We are constantly running into bugs where Line.Level is incorrectly
137 // subtracted from beyond 0. Introduce a method to subtract from Line.Level
138 // and use that everywhere in the Parser.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000139 UnwrappedLine Line;
140 FormatToken FormatTok;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000141
Alexander Kornienko15757312012-12-06 18:03:27 +0000142 const FormatStyle &Style;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000143 FormatTokenSource *Tokens;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000144 UnwrappedLineConsumer &Callback;
145};
146
Daniel Jaspercd162382013-01-07 13:26:07 +0000147} // end namespace format
148} // end namespace clang
Daniel Jasperbac016b2012-12-03 18:12:45 +0000149
Daniel Jaspercd162382013-01-07 13:26:07 +0000150#endif // LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H