public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Thomas Schwinge <tschwinge@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc/devel/rust/master] dump: Dump macro rules definition
Date: Sat, 22 Oct 2022 10:47:10 +0000 (GMT)	[thread overview]
Message-ID: <20221022104711.A2C363858C53@sourceware.org> (raw)

https://gcc.gnu.org/g:15229ea41faccfd2cbe4309b2afe333b56b090b1

commit 15229ea41faccfd2cbe4309b2afe333b56b090b1
Author: Arthur Cohen <arthur.cohen@embecosm.com>
Date:   Tue Sep 27 16:24:59 2022 +0200

    dump: Dump macro rules definition
    
    Co-authored-by: jdupak <dev@jakubdupak.com>

Diff:
---
 gcc/rust/ast/rust-ast-dump.cc | 126 +++++++++++++++++++++++++++++++++++++++---
 gcc/rust/ast/rust-ast-dump.h  |  16 ++++--
 gcc/rust/ast/rust-macro.h     |   1 +
 3 files changed, 131 insertions(+), 12 deletions(-)

diff --git a/gcc/rust/ast/rust-ast-dump.cc b/gcc/rust/ast/rust-ast-dump.cc
index 7cbdfa21fcb..a37f94beb8a 100644
--- a/gcc/rust/ast/rust-ast-dump.cc
+++ b/gcc/rust/ast/rust-ast-dump.cc
@@ -175,11 +175,27 @@ Dump::format_struct_field (StructField &field)
 
 void
 Dump::visit (Token &tok)
-{}
+{
+  stream << tok.as_string ();
+}
 
 void
 Dump::visit (DelimTokenTree &delim_tok_tree)
-{}
+{
+  auto tokens = delim_tok_tree.to_token_stream ();
+
+  indentation.increment ();
+  stream << '\n' << indentation;
+
+  for (const auto &tok : tokens)
+    {
+      stream << ' ';
+      tok->accept_vis (*this);
+    }
+
+  indentation.decrement ();
+  stream << '\n' << indentation;
+}
 
 void
 Dump::visit (AttrInputMetaItemContainer &input)
@@ -1126,22 +1142,116 @@ Dump::visit (ExternBlock &block)
   stream << "\n" << indentation << "}\n";
 }
 
-// rust-macro.h
+static std::pair<char, char>
+get_delimiters (DelimType delim)
+{
+  auto start_delim = '\0';
+  auto end_delim = '\0';
+
+  switch (delim)
+    {
+    case PARENS:
+      start_delim = '(';
+      end_delim = ')';
+      break;
+    case SQUARE:
+      start_delim = '[';
+      end_delim = ']';
+      break;
+    case CURLY:
+      start_delim = '{';
+      end_delim = '}';
+      break;
+    }
+
+  return {start_delim, end_delim};
+}
+
 void
 Dump::visit (MacroMatchFragment &match)
-{}
+{
+  stream << '$' << match.get_ident () << ':'
+	 << match.get_frag_spec ().as_string ();
+}
 
 void
-Dump::visit (MacroMatchRepetition &match)
-{}
+Dump::visit (MacroMatchRepetition &repetition)
+{
+  stream << "$(";
+
+  for (auto &match : repetition.get_matches ())
+    {
+      match->accept_vis (*this);
+      stream << ' ';
+    }
+
+  auto op_char = '\0';
+  switch (repetition.get_op ())
+    {
+    case MacroMatchRepetition::ANY:
+      op_char = '*';
+      break;
+    case MacroMatchRepetition::ONE_OR_MORE:
+      op_char = '+';
+      break;
+    case MacroMatchRepetition::ZERO_OR_ONE:
+      op_char = '?';
+      break;
+    case MacroMatchRepetition::NONE:
+      break;
+    }
+
+  stream << ')';
+
+  if (repetition.has_sep ())
+    stream << repetition.get_sep ()->as_string ();
+
+  stream << op_char;
+}
 
 void
 Dump::visit (MacroMatcher &matcher)
-{}
+{
+  auto delimiters = get_delimiters (matcher.get_delim_type ());
+
+  stream << delimiters.first;
+
+  for (auto &match : matcher.get_matches ())
+    {
+      match->accept_vis (*this);
+      stream << ' ';
+    }
+
+  stream << delimiters.second;
+}
 
 void
 Dump::visit (MacroRulesDefinition &rules_def)
-{}
+{
+  for (auto &outer_attr : rules_def.get_outer_attrs ())
+    emit_attrib (outer_attr);
+
+  stream << "macro_rules! " << rules_def.get_rule_name () << " {\n";
+
+  indentation.increment ();
+
+  for (auto &rule : rules_def.get_rules ())
+    {
+      stream << indentation;
+
+      rule.get_matcher ().accept_vis (*this);
+
+      stream << " => ";
+
+      rule.get_transcriber ().get_token_tree ().accept_vis (*this);
+
+      stream << ";\n";
+    }
+
+  indentation.decrement ();
+
+  stream << "}\n";
+}
 
 void
 Dump::visit (MacroInvocation &macro_invoc)
diff --git a/gcc/rust/ast/rust-ast-dump.h b/gcc/rust/ast/rust-ast-dump.h
index 2da2736c95b..9fe8ee95493 100644
--- a/gcc/rust/ast/rust-ast-dump.h
+++ b/gcc/rust/ast/rust-ast-dump.h
@@ -72,7 +72,9 @@ private:
   std::ostream &stream;
   Indent indentation;
 
-  // Format together common items of functions: Parameters, return type, block
+  /**
+   * Format together common items of functions: Parameters, return type, block
+   */
   void format_function_common (std::unique_ptr<Type> &return_type,
 			       std::unique_ptr<BlockExpr> &block);
 
@@ -97,13 +99,19 @@ private:
   std::ostream &emit_indented_string (const std::string &value,
 				      const std::string &comment = "");
 
-  // Emit formatted string for generic parameters.
+  /**
+   * Emit formatted string for generic parameters
+   */
   void emit_generic_params (std::vector<std::unique_ptr<GenericParam>> &params);
 
-  // Format a single field of a tuple.
+  /**
+   * Format a single field of a tuple
+   */
   void format_tuple_field (TupleField &field);
 
-  // Format a single field of a struct.
+  /**
+   * Format a single field of a struct
+   */
   void format_struct_field (StructField &field);
 
   // rust-ast.h
diff --git a/gcc/rust/ast/rust-macro.h b/gcc/rust/ast/rust-macro.h
index ce515db0aad..276f441fe0b 100644
--- a/gcc/rust/ast/rust-macro.h
+++ b/gcc/rust/ast/rust-macro.h
@@ -407,6 +407,7 @@ public:
   Location get_locus () const { return locus; }
 
   DelimTokenTree &get_token_tree () { return token_tree; }
+  const DelimTokenTree &get_token_tree () const { return token_tree; }
 };
 
 // A macro rule? Matcher and transcriber pair?

                 reply	other threads:[~2022-10-22 10:47 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221022104711.A2C363858C53@sourceware.org \
    --to=tschwinge@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).