]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: collector: Implement formatting options
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Fri, 23 Jun 2023 11:59:51 +0000 (13:59 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:46:31 +0000 (18:46 +0100)
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>
gcc/rust/ast/rust-ast-collector.cc
gcc/rust/ast/rust-ast-collector.h

index 6b0ced466644abe8e018ca5ef667496347b033aa..89c693e0891dda540e5f47511362191133445986 100644 (file)
@@ -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)
index 16206d09e627d8f3598a87696f18a30545b70305..0001c5bbef8e8067a07a8cc752e73a8099da1ecc 100644 (file)
@@ -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
    */