blob: 69ac7683254ac46189b967eaacd5a1b6060b19d2 [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()
34 : NewlinesBefore(0), HasUnescapedNewline(false), WhiteSpaceLength(0) {
Daniel Jasperbac016b2012-12-03 18:12:45 +000035 }
36
37 /// \brief The \c Token.
38 Token Tok;
39
40 /// \brief The number of newlines immediately before the \c Token.
41 ///
42 /// This can be used to determine what the user wrote in the original code
43 /// and thereby e.g. leave an empty line between two function definitions.
44 unsigned NewlinesBefore;
45
Manuel Klimeka080a182013-01-02 16:30:12 +000046 /// \brief Whether there is at least one unescaped newline before the \c
47 /// Token.
48 bool HasUnescapedNewline;
49
Daniel Jasperbac016b2012-12-03 18:12:45 +000050 /// \brief The location of the start of the whitespace immediately preceeding
51 /// the \c Token.
52 ///
53 /// Used together with \c WhiteSpaceLength to create a \c Replacement.
54 SourceLocation WhiteSpaceStart;
55
56 /// \brief The length in characters of the whitespace immediately preceeding
57 /// the \c Token.
58 unsigned WhiteSpaceLength;
59};
60
61/// \brief An unwrapped line is a sequence of \c Token, that we would like to
62/// put on a single line if there was no column limit.
63///
64/// This is used as a main interface between the \c UnwrappedLineParser and the
65/// \c UnwrappedLineFormatter. The key property is that changing the formatting
66/// within an unwrapped line does not affect any other unwrapped lines.
67struct UnwrappedLine {
Manuel Klimeka080a182013-01-02 16:30:12 +000068 UnwrappedLine() : Level(0), InPPDirective(false) {
Daniel Jasperbac016b2012-12-03 18:12:45 +000069 }
70
71 /// \brief The \c Token comprising this \c UnwrappedLine.
72 SmallVector<FormatToken, 16> Tokens;
73
74 /// \brief The indent level of the \c UnwrappedLine.
75 unsigned Level;
Manuel Klimeka080a182013-01-02 16:30:12 +000076
77 /// \brief Whether this \c UnwrappedLine is part of a preprocessor directive.
78 bool InPPDirective;
Daniel Jasperbac016b2012-12-03 18:12:45 +000079};
80
81class UnwrappedLineConsumer {
82public:
Daniel Jasperaccb0b02012-12-04 21:05:31 +000083 virtual ~UnwrappedLineConsumer() {
84 }
Alexander Kornienko720ffb62012-12-05 13:56:52 +000085 virtual void consumeUnwrappedLine(const UnwrappedLine &Line) = 0;
Daniel Jasperbac016b2012-12-03 18:12:45 +000086};
87
Alexander Kornienko469a21b2012-12-07 16:15:44 +000088class FormatTokenSource {
89public:
Matt Beaumont-Gay422daa12012-12-07 22:49:27 +000090 virtual ~FormatTokenSource() {
91 }
Alexander Kornienko469a21b2012-12-07 16:15:44 +000092 virtual FormatToken getNextToken() = 0;
93};
94
Daniel Jasperbac016b2012-12-03 18:12:45 +000095class UnwrappedLineParser {
96public:
Alexander Kornienko469a21b2012-12-07 16:15:44 +000097 UnwrappedLineParser(const FormatStyle &Style, FormatTokenSource &Tokens,
Daniel Jasperbac016b2012-12-03 18:12:45 +000098 UnwrappedLineConsumer &Callback);
99
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000100 /// Returns true in case of a structural error.
101 bool parse();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000102
103private:
Manuel Klimekd4397b92013-01-04 23:34:14 +0000104 bool parseFile();
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000105 bool parseLevel();
Alexander Kornienko15757312012-12-06 18:03:27 +0000106 bool parseBlock(unsigned AddLevels = 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000107 void parsePPDirective();
Manuel Klimekd4397b92013-01-04 23:34:14 +0000108 void parsePPDefine();
109 void parsePPUnknown();
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000110 void parseComments();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000111 void parseStatement();
112 void parseParens();
113 void parseIfThenElse();
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000114 void parseForOrWhileLoop();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000115 void parseDoWhile();
116 void parseLabel();
117 void parseCaseLabel();
118 void parseSwitch();
Alexander Kornienko15757312012-12-06 18:03:27 +0000119 void parseNamespace();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000120 void parseAccessSpecifier();
121 void parseEnum();
122 void addUnwrappedLine();
123 bool eof() const;
124 void nextToken();
Manuel Klimekd4397b92013-01-04 23:34:14 +0000125 void readToken();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000126
127 UnwrappedLine Line;
128 FormatToken FormatTok;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000129
Alexander Kornienko15757312012-12-06 18:03:27 +0000130 const FormatStyle &Style;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000131 FormatTokenSource *Tokens;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000132 UnwrappedLineConsumer &Callback;
133};
134
135} // end namespace format
136} // end namespace clang
137
Daniel Jasperd7610b82012-12-24 16:51:15 +0000138#endif // LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H