[gcc r14-7711] gccrs: collector: Implement formatting options

Arthur Cohen cohenarthur@gcc.gnu.org
Tue Jan 16 17:55:52 GMT 2024


https://gcc.gnu.org/g:1d28a62a0d75be55fd08c7cf163825d1b7caf967

commit r14-7711-g1d28a62a0d75be55fd08c7cf163825d1b7caf967
Author: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Date:   Fri Jun 23 13:59:51 2023 +0200

    gccrs: collector: Implement formatting options
    
    The collector did only output the tokens without any formatting.
    
    gcc/rust/ChangeLog:
    
            * ast/rust-ast-collector.cc (TokenCollector::collect): Collect
            CollectItems once done.
            (TokenCollector::newline): Add newline formatting
            implementation.
            (TokenCollector::indentation): Add indentation implementation.
            (TokenCollector::increment_indentation): Add indentation
            increment.
            (TokenCollector::comment): Add new comment formatting option.
            * ast/rust-ast-collector.h: Update prototypes.
    
    Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>

Diff:
---
 gcc/rust/ast/rust-ast-collector.cc | 29 +++++++++++++++++++++++++----
 gcc/rust/ast/rust-ast-collector.h  | 13 +++++++++++++
 2 files changed, 38 insertions(+), 4 deletions(-)

diff --git a/gcc/rust/ast/rust-ast-collector.cc b/gcc/rust/ast/rust-ast-collector.cc
index 6b0ced46664..89c693e0891 100644
--- a/gcc/rust/ast/rust-ast-collector.cc
+++ b/gcc/rust/ast/rust-ast-collector.cc
@@ -34,6 +34,12 @@ TokenCollector::collect_tokens () const
   return result;
 }
 
+std::vector<CollectItem>
+TokenCollector::collect () const
+{
+  return tokens;
+}
+
 void
 TokenCollector::visit (AST::Crate &crate)
 {
@@ -128,19 +134,34 @@ TokenCollector::trailing_comma ()
 
 void
 TokenCollector::newline ()
-{}
+{
+  tokens.push_back ({CollectItem::Kind::Newline});
+}
 
 void
 TokenCollector::indentation ()
-{}
+{
+  tokens.push_back ({indent_level});
+}
 
 void
 TokenCollector::increment_indentation ()
-{}
+{
+  indent_level++;
+}
 
 void
 TokenCollector::decrement_indentation ()
-{}
+{
+  rust_assert (indent_level != 0);
+  indent_level--;
+}
+
+void
+TokenCollector::comment (std::string comment)
+{
+  tokens.push_back ({comment});
+}
 
 void
 TokenCollector::visit (Visitable &v)
diff --git a/gcc/rust/ast/rust-ast-collector.h b/gcc/rust/ast/rust-ast-collector.h
index 16206d09e62..0001c5bbef8 100644
--- a/gcc/rust/ast/rust-ast-collector.h
+++ b/gcc/rust/ast/rust-ast-collector.h
@@ -41,6 +41,7 @@ public:
   CollectItem (TokenPtr token) : token (token), kind (Kind::Token) {}
   CollectItem (std::string comment) : comment (comment), kind (Kind::Comment) {}
   CollectItem (Kind kind) : kind (kind) { rust_assert (kind != Kind::Token); }
+  CollectItem (size_t level) : indent_level (level), kind (Kind::Indentation) {}
 
   Kind get_kind () { return kind; }
 
@@ -56,24 +57,35 @@ public:
     return comment;
   }
 
+  size_t get_indent_level ()
+  {
+    rust_assert (kind == Kind::Indentation);
+    return indent_level;
+  }
+
 private:
   TokenPtr token;
   std::string comment;
+  size_t indent_level;
   Kind kind;
 };
 
 class TokenCollector : public ASTVisitor
 {
 public:
+  TokenCollector () : indent_level (0) {}
+
   bool output_trailing_commas = false;
 
   void visit (AST::Crate &crate);
   void visit (AST::Item &item);
 
   std::vector<TokenPtr> collect_tokens () const;
+  std::vector<CollectItem> collect () const;
 
 private:
   std::vector<CollectItem> tokens;
+  size_t indent_level;
 
   void push (TokenPtr token) { tokens.push_back ({token}); }
 
@@ -117,6 +129,7 @@ private:
   void indentation ();
   void increment_indentation ();
   void decrement_indentation ();
+  void comment (std::string comment);
   /**
    * Visit common items of functions: Parameters, return type, block
    */


More information about the Gcc-cvs mailing list