public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] ast: Dump: Add Indent class usable with streams
@ 2022-06-08 12:51 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-06-08 12:51 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:c2c26f0499b5faece247c21e3bad7d5d5f5cb31a

commit c2c26f0499b5faece247c21e3bad7d5d5f5cb31a
Author: Arthur Cohen <arthur.cohen@embecosm.com>
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);


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-06-08 12:51 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-08 12:51 [gcc/devel/rust/master] ast: Dump: Add Indent class usable with streams Thomas Schwinge

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).