aboutsummaryrefslogtreecommitdiff
path: root/lib/Format/UnwrappedLineParser.h
diff options
context:
space:
mode:
authorDaniel Jasper <djasper@google.com>2012-12-03 18:12:45 +0000
committerDaniel Jasper <djasper@google.com>2012-12-03 18:12:45 +0000
commitbac016bd3f67ca2f4db1ddc619e611759352b84d (patch)
tree82232dd80409934344e3f0c259da493e5fc35b0e /lib/Format/UnwrappedLineParser.h
parent189f2e421d06526ea8b4a3dcd9f4a072e10a859c (diff)
Initial version of formatting library.
This formatting library will be used by a stand-alone clang-format tool and can also be used when writing other refactorings. Manuel's original design document: https://docs.google.com/a/google.com/document/d/1gpckL2U_6QuU9YW2L1ABsc4Fcogn5UngKk7fE5dDOoA/edit The library can already successfully format itself. Review: http://llvm-reviews.chandlerc.com/D80 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@169137 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Format/UnwrappedLineParser.h')
-rw-r--r--lib/Format/UnwrappedLineParser.h119
1 files changed, 119 insertions, 0 deletions
diff --git a/lib/Format/UnwrappedLineParser.h b/lib/Format/UnwrappedLineParser.h
new file mode 100644
index 0000000000..1e6899e092
--- /dev/null
+++ b/lib/Format/UnwrappedLineParser.h
@@ -0,0 +1,119 @@
+//===--- UnwrappedLineParser.cpp - Format C++ code ------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// \brief This file contains the declaration of the UnwrappedLineParser,
+/// which turns a stream of tokens into UnwrappedLines.
+///
+/// This is EXPERIMENTAL code under heavy development. It is not in a state yet,
+/// where it can be used to format real code.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H
+#define LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H
+
+#include "clang/Basic/SourceManager.h"
+#include "clang/Basic/IdentifierTable.h"
+#include "clang/Lex/Lexer.h"
+
+namespace clang {
+namespace format {
+
+/// \brief A wrapper around a \c Token storing information about the
+/// whitespace characters preceeding it.
+struct FormatToken {
+ FormatToken() : NewlinesBefore(0), WhiteSpaceLength(0) {
+ }
+
+ /// \brief The \c Token.
+ Token Tok;
+
+ /// \brief The number of newlines immediately before the \c Token.
+ ///
+ /// This can be used to determine what the user wrote in the original code
+ /// and thereby e.g. leave an empty line between two function definitions.
+ unsigned NewlinesBefore;
+
+ /// \brief The location of the start of the whitespace immediately preceeding
+ /// the \c Token.
+ ///
+ /// Used together with \c WhiteSpaceLength to create a \c Replacement.
+ SourceLocation WhiteSpaceStart;
+
+ /// \brief The length in characters of the whitespace immediately preceeding
+ /// the \c Token.
+ unsigned WhiteSpaceLength;
+};
+
+/// \brief An unwrapped line is a sequence of \c Token, that we would like to
+/// put on a single line if there was no column limit.
+///
+/// This is used as a main interface between the \c UnwrappedLineParser and the
+/// \c UnwrappedLineFormatter. The key property is that changing the formatting
+/// within an unwrapped line does not affect any other unwrapped lines.
+struct UnwrappedLine {
+ UnwrappedLine() : Level(0) {
+ }
+
+ /// \brief The \c Token comprising this \c UnwrappedLine.
+ SmallVector<FormatToken, 16> Tokens;
+
+ /// \brief The indent level of the \c UnwrappedLine.
+ unsigned Level;
+};
+
+class UnwrappedLineConsumer {
+public:
+ virtual void formatUnwrappedLine(const UnwrappedLine &Line) = 0;
+};
+
+class UnwrappedLineParser {
+public:
+ UnwrappedLineParser(Lexer &Lex, SourceManager &SourceMgr,
+ UnwrappedLineConsumer &Callback);
+
+ void parse();
+
+private:
+ void parseLevel();
+ void parseBlock();
+ void parsePPDirective();
+ void parseComment();
+ void parseStatement();
+ void parseParens();
+ void parseIfThenElse();
+ void parseDoWhile();
+ void parseLabel();
+ void parseCaseLabel();
+ void parseSwitch();
+ void parseAccessSpecifier();
+ void parseEnum();
+ void addUnwrappedLine();
+ bool eof() const;
+ void nextToken();
+ void parseToken();
+
+ /// Returns the text of \c FormatTok.
+ StringRef tokenText();
+
+ UnwrappedLine Line;
+ FormatToken FormatTok;
+ bool GreaterStashed;
+
+ Lexer &Lex;
+ SourceManager &SourceMgr;
+ IdentifierTable IdentTable;
+ UnwrappedLineConsumer &Callback;
+};
+
+} // end namespace format
+} // end namespace clang
+
+#endif // LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H