blob: 6e9d872386719096320847226c3b2bf937c113f4 [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"
Daniel Jasperbac016b2012-12-03 18:12:45 +000024#include "clang/Lex/Lexer.h"
25
26namespace clang {
27namespace format {
28
29/// \brief A wrapper around a \c Token storing information about the
30/// whitespace characters preceeding it.
31struct FormatToken {
32 FormatToken() : NewlinesBefore(0), WhiteSpaceLength(0) {
33 }
34
35 /// \brief The \c Token.
36 Token Tok;
37
38 /// \brief The number of newlines immediately before the \c Token.
39 ///
40 /// This can be used to determine what the user wrote in the original code
41 /// and thereby e.g. leave an empty line between two function definitions.
42 unsigned NewlinesBefore;
43
44 /// \brief The location of the start of the whitespace immediately preceeding
45 /// the \c Token.
46 ///
47 /// Used together with \c WhiteSpaceLength to create a \c Replacement.
48 SourceLocation WhiteSpaceStart;
49
50 /// \brief The length in characters of the whitespace immediately preceeding
51 /// the \c Token.
52 unsigned WhiteSpaceLength;
53};
54
55/// \brief An unwrapped line is a sequence of \c Token, that we would like to
56/// put on a single line if there was no column limit.
57///
58/// This is used as a main interface between the \c UnwrappedLineParser and the
59/// \c UnwrappedLineFormatter. The key property is that changing the formatting
60/// within an unwrapped line does not affect any other unwrapped lines.
61struct UnwrappedLine {
62 UnwrappedLine() : Level(0) {
63 }
64
65 /// \brief The \c Token comprising this \c UnwrappedLine.
66 SmallVector<FormatToken, 16> Tokens;
67
68 /// \brief The indent level of the \c UnwrappedLine.
69 unsigned Level;
70};
71
72class UnwrappedLineConsumer {
73public:
Daniel Jasperaccb0b02012-12-04 21:05:31 +000074 virtual ~UnwrappedLineConsumer() {
75 }
Alexander Kornienko720ffb62012-12-05 13:56:52 +000076 virtual void consumeUnwrappedLine(const UnwrappedLine &Line) = 0;
Daniel Jasperbac016b2012-12-03 18:12:45 +000077};
78
79class UnwrappedLineParser {
80public:
81 UnwrappedLineParser(Lexer &Lex, SourceManager &SourceMgr,
82 UnwrappedLineConsumer &Callback);
83
Alexander Kornienkocff563c2012-12-04 17:27:50 +000084 /// Returns true in case of a structural error.
85 bool parse();
Daniel Jasperbac016b2012-12-03 18:12:45 +000086
87private:
Alexander Kornienkocff563c2012-12-04 17:27:50 +000088 bool parseLevel();
89 bool parseBlock();
Daniel Jasperbac016b2012-12-03 18:12:45 +000090 void parsePPDirective();
91 void parseComment();
92 void parseStatement();
93 void parseParens();
94 void parseIfThenElse();
95 void parseDoWhile();
96 void parseLabel();
97 void parseCaseLabel();
98 void parseSwitch();
99 void parseAccessSpecifier();
100 void parseEnum();
101 void addUnwrappedLine();
102 bool eof() const;
103 void nextToken();
104 void parseToken();
105
106 /// Returns the text of \c FormatTok.
107 StringRef tokenText();
108
109 UnwrappedLine Line;
110 FormatToken FormatTok;
111 bool GreaterStashed;
112
113 Lexer &Lex;
114 SourceManager &SourceMgr;
115 IdentifierTable IdentTable;
116 UnwrappedLineConsumer &Callback;
117};
118
119} // end namespace format
120} // end namespace clang
121
122#endif // LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H