diff options
author | Arthur Cohen <arthur.cohen@embecosm.com> | 2022-05-30 10:38:31 +0200 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2022-05-30 10:38:31 +0200 |
commit | c2c26f0499b5faece247c21e3bad7d5d5f5cb31a (patch) | |
tree | df5b4e8d387a0f78b6e58c6b25f3b9213af7d087 /gcc | |
parent | 79b14bdf165d904ed83101a81f810e3161e957b6 (diff) | |
download | gcc-c2c26f0499b5faece247c21e3bad7d5d5f5cb31a.zip gcc-c2c26f0499b5faece247c21e3bad7d5d5f5cb31a.tar.gz gcc-c2c26f0499b5faece247c21e3bad7d5d5f5cb31a.tar.bz2 |
ast: Dump: Add Indent class usable with streams
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/ast/rust-ast-dump.cc | 33 | ||||
-rw-r--r-- | gcc/rust/ast/rust-ast-dump.h | 27 |
2 files changed, 60 insertions, 0 deletions
diff --git a/gcc/rust/ast/rust-ast-dump.cc b/gcc/rust/ast/rust-ast-dump.cc index 2d4cb70..99b6e86 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 166b511..99568c6 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); |