From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id 5F3F7388B5AB; Wed, 8 Jun 2022 12:51:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5F3F7388B5AB Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] ast: Dump: Add Indent class usable with streams X-Act-Checkin: gcc X-Git-Author: Arthur Cohen X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: 79b14bdf165d904ed83101a81f810e3161e957b6 X-Git-Newrev: c2c26f0499b5faece247c21e3bad7d5d5f5cb31a Message-Id: <20220608125105.5F3F7388B5AB@sourceware.org> Date: Wed, 8 Jun 2022 12:51:05 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Jun 2022 12:51:05 -0000 https://gcc.gnu.org/g:c2c26f0499b5faece247c21e3bad7d5d5f5cb31a commit c2c26f0499b5faece247c21e3bad7d5d5f5cb31a Author: Arthur Cohen Date: Mon May 30 10:38:31 2022 +0200 ast: Dump: Add Indent class usable with streams Diff: --- gcc/rust/ast/rust-ast-dump.cc | 33 +++++++++++++++++++++++++++++++++ gcc/rust/ast/rust-ast-dump.h | 27 +++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/gcc/rust/ast/rust-ast-dump.cc b/gcc/rust/ast/rust-ast-dump.cc index 2d4cb704590..99b6e86523c 100644 --- a/gcc/rust/ast/rust-ast-dump.cc +++ b/gcc/rust/ast/rust-ast-dump.cc @@ -21,6 +21,39 @@ namespace Rust { namespace AST { +Indent::Indent () : tabs (0) {} + +std::ofstream & +Indent::operator<< (std::ofstream &stream) +{ + for (size_t i = 0; i < tabs; i++) + stream << '\t'; + + return stream; +} + +void +Indent::increment () +{ + tabs++; +} + +void +Indent::decrement () +{ + rust_assert (tabs != 0); + tabs--; +} + +Dump::Dump (std::ofstream &stream) : stream (stream), indentation (Indent ()) {} + +void +Dump::go (AST::Crate &crate) +{ + for (auto &item : crate.items) + item->accept_vis (*this); +} + void Dump::visit (Token &tok) {} diff --git a/gcc/rust/ast/rust-ast-dump.h b/gcc/rust/ast/rust-ast-dump.h index 166b5114e72..99568c65e36 100644 --- a/gcc/rust/ast/rust-ast-dump.h +++ b/gcc/rust/ast/rust-ast-dump.h @@ -25,8 +25,35 @@ namespace Rust { namespace AST { +// TODO: We might want to reuse this class somewhere else +class Indent +{ +public: + Indent (); + + std::ofstream &operator<< (std::ofstream &stream); + + void increment (); + void decrement (); + +private: + size_t tabs; +}; + class Dump : public ASTVisitor { +public: + Dump (std::ofstream &stream); + + /** + * Run the visitor on an entire crate and its items + */ + void go (AST::Crate &crate); + +private: + std::ofstream &stream; + Indent indentation; + // rust-ast.h void visit (Token &tok); void visit (DelimTokenTree &delim_tok_tree);