aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust
diff options
context:
space:
mode:
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>2023-06-23 13:59:51 +0200
committerCohenArthur <arthur.cohen@embecosm.com>2023-06-29 12:30:17 +0000
commit18a67ff6bfd0d4e8219e2f5449831ab19b8a8ea7 (patch)
tree9c4a02dff79346c59de85c4ba62da7e62cbc6861 /gcc/rust
parent299edc484ba55772f1d25a8071c79929366665df (diff)
downloadgcc-18a67ff6bfd0d4e8219e2f5449831ab19b8a8ea7.zip
gcc-18a67ff6bfd0d4e8219e2f5449831ab19b8a8ea7.tar.gz
gcc-18a67ff6bfd0d4e8219e2f5449831ab19b8a8ea7.tar.bz2
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>
Diffstat (limited to 'gcc/rust')
-rw-r--r--gcc/rust/ast/rust-ast-collector.cc29
-rw-r--r--gcc/rust/ast/rust-ast-collector.h13
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 6b0ced4..89c693e 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 16206d0..0001c5b 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
*/