blob: 010bad8eceaebd5c904822029f629bf2dc3299de [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
Daniel Jasper26f7e782013-01-08 14:56:18 +000027#include <vector>
28
Daniel Jasperbac016b2012-12-03 18:12:45 +000029namespace clang {
30namespace format {
31
32/// \brief A wrapper around a \c Token storing information about the
33/// whitespace characters preceeding it.
34struct FormatToken {
Manuel Klimeka080a182013-01-02 16:30:12 +000035 FormatToken()
Manuel Klimekf6fd00b2013-01-05 22:56:06 +000036 : NewlinesBefore(0), HasUnescapedNewline(false), WhiteSpaceLength(0),
Manuel Klimek95419382013-01-07 07:56:50 +000037 TokenLength(0), IsFirst(false) {
Daniel Jasperbac016b2012-12-03 18:12:45 +000038 }
39
40 /// \brief The \c Token.
41 Token Tok;
42
43 /// \brief The number of newlines immediately before the \c Token.
44 ///
45 /// This can be used to determine what the user wrote in the original code
46 /// and thereby e.g. leave an empty line between two function definitions.
47 unsigned NewlinesBefore;
48
Manuel Klimeka080a182013-01-02 16:30:12 +000049 /// \brief Whether there is at least one unescaped newline before the \c
50 /// Token.
51 bool HasUnescapedNewline;
52
Daniel Jasperbac016b2012-12-03 18:12:45 +000053 /// \brief The location of the start of the whitespace immediately preceeding
54 /// the \c Token.
55 ///
56 /// Used together with \c WhiteSpaceLength to create a \c Replacement.
57 SourceLocation WhiteSpaceStart;
58
59 /// \brief The length in characters of the whitespace immediately preceeding
60 /// the \c Token.
61 unsigned WhiteSpaceLength;
Manuel Klimekf6fd00b2013-01-05 22:56:06 +000062
Manuel Klimek95419382013-01-07 07:56:50 +000063 /// \brief The length of the non-whitespace parts of the token. This is
64 /// necessary because we need to handle escaped newlines that are stored
65 /// with the token.
66 unsigned TokenLength;
67
Manuel Klimekf6fd00b2013-01-05 22:56:06 +000068 /// \brief Indicates that this is the first token.
69 bool IsFirst;
Daniel Jasper26f7e782013-01-08 14:56:18 +000070
71 // FIXME: We currently assume that there is exactly one token in this vector
72 // except for the very last token that does not have any children.
73 /// \brief All tokens that logically follow this token.
74 std::vector<FormatToken> Children;
Daniel Jasperbac016b2012-12-03 18:12:45 +000075};
76
77/// \brief An unwrapped line is a sequence of \c Token, that we would like to
78/// put on a single line if there was no column limit.
79///
80/// This is used as a main interface between the \c UnwrappedLineParser and the
81/// \c UnwrappedLineFormatter. The key property is that changing the formatting
82/// within an unwrapped line does not affect any other unwrapped lines.
83struct UnwrappedLine {
Manuel Klimeka080a182013-01-02 16:30:12 +000084 UnwrappedLine() : Level(0), InPPDirective(false) {
Daniel Jasperbac016b2012-12-03 18:12:45 +000085 }
86
87 /// \brief The \c Token comprising this \c UnwrappedLine.
Daniel Jasper26f7e782013-01-08 14:56:18 +000088 FormatToken RootToken;
Daniel Jasperbac016b2012-12-03 18:12:45 +000089
90 /// \brief The indent level of the \c UnwrappedLine.
91 unsigned Level;
Manuel Klimeka080a182013-01-02 16:30:12 +000092
93 /// \brief Whether this \c UnwrappedLine is part of a preprocessor directive.
94 bool InPPDirective;
Daniel Jasperbac016b2012-12-03 18:12:45 +000095};
96
97class UnwrappedLineConsumer {
98public:
Daniel Jasperaccb0b02012-12-04 21:05:31 +000099 virtual ~UnwrappedLineConsumer() {
100 }
Alexander Kornienko720ffb62012-12-05 13:56:52 +0000101 virtual void consumeUnwrappedLine(const UnwrappedLine &Line) = 0;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000102};
103
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000104class FormatTokenSource {
105public:
Matt Beaumont-Gay422daa12012-12-07 22:49:27 +0000106 virtual ~FormatTokenSource() {
107 }
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000108 virtual FormatToken getNextToken() = 0;
109};
110
Daniel Jasperbac016b2012-12-03 18:12:45 +0000111class UnwrappedLineParser {
112public:
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000113 UnwrappedLineParser(const FormatStyle &Style, FormatTokenSource &Tokens,
Daniel Jasperbac016b2012-12-03 18:12:45 +0000114 UnwrappedLineConsumer &Callback);
115
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000116 /// Returns true in case of a structural error.
117 bool parse();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000118
119private:
Manuel Klimekd4397b92013-01-04 23:34:14 +0000120 bool parseFile();
Manuel Klimeka5342db2013-01-06 20:07:31 +0000121 bool parseLevel(bool HasOpeningBrace);
Alexander Kornienko15757312012-12-06 18:03:27 +0000122 bool parseBlock(unsigned AddLevels = 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000123 void parsePPDirective();
Manuel Klimekd4397b92013-01-04 23:34:14 +0000124 void parsePPDefine();
125 void parsePPUnknown();
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000126 void parseComments();
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000127 void parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000128 void parseParens();
129 void parseIfThenElse();
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000130 void parseForOrWhileLoop();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000131 void parseDoWhile();
132 void parseLabel();
133 void parseCaseLabel();
134 void parseSwitch();
Alexander Kornienko15757312012-12-06 18:03:27 +0000135 void parseNamespace();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000136 void parseAccessSpecifier();
137 void parseEnum();
Manuel Klimekde768542013-01-07 18:10:23 +0000138 void parseStructOrClass();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000139 void addUnwrappedLine();
140 bool eof() const;
141 void nextToken();
Manuel Klimekd4397b92013-01-04 23:34:14 +0000142 void readToken();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000143
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000144 // FIXME: We are constantly running into bugs where Line.Level is incorrectly
145 // subtracted from beyond 0. Introduce a method to subtract from Line.Level
146 // and use that everywhere in the Parser.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000147 UnwrappedLine Line;
Daniel Jasper26f7e782013-01-08 14:56:18 +0000148 bool RootTokenInitialized;
149 FormatToken *LastInCurrentLine;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000150 FormatToken FormatTok;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000151
Alexander Kornienko15757312012-12-06 18:03:27 +0000152 const FormatStyle &Style;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000153 FormatTokenSource *Tokens;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000154 UnwrappedLineConsumer &Callback;
155};
156
Daniel Jaspercd162382013-01-07 13:26:07 +0000157} // end namespace format
158} // end namespace clang
Daniel Jasperbac016b2012-12-03 18:12:45 +0000159
Daniel Jaspercd162382013-01-07 13:26:07 +0000160#endif // LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H