From: Pierre-Emmanuel Patry Date: Fri, 23 Jun 2023 11:59:51 +0000 (+0200) Subject: gccrs: collector: Implement formatting options X-Git-Tag: basepoints/gcc-15~2428 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1d28a62a0d75be55fd08c7cf163825d1b7caf967;p=thirdparty%2Fgcc.git 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 --- diff --git a/gcc/rust/ast/rust-ast-collector.cc b/gcc/rust/ast/rust-ast-collector.cc index 6b0ced466644..89c693e0891d 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 +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 16206d09e627..0001c5bbef8e 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 collect_tokens () const; + std::vector collect () const; private: std::vector 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 */