]> gcc.gnu.org Git - gcc.git/commitdiff
gccrs: converter: Add TokenStream conversion function
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Wed, 3 May 2023 08:17:16 +0000 (10:17 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:37:17 +0000 (18:37 +0100)
Add a tokenstream conversion function dispatching to inner elements

gcc/rust/ChangeLog:

* util/rust-token-converter.cc (to_tokenstream): Change function
name from to_tokenstream to convert.
(convert): Likewise.
(from_tokenstream): Add tokenstream handler.
(from_ident): Add empty function.
(from_literal): Likewise.
(from_punct): Likewise.
(from_group): Likewise.
* util/rust-token-converter.h (to_tokenstream): Change function
name from to_tokenstream to convert.
(convert): Likewise.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
gcc/rust/util/rust-token-converter.cc
gcc/rust/util/rust-token-converter.h

index b0d47de512f7b971c967924266e17c71cab15a38..25abf763e241ab5beffff32fdf5f934def7860b0 100644 (file)
@@ -122,7 +122,7 @@ dispatch_integer_literals (ProcMacro::TokenStream &ts, TokenPtr &token)
 }
 
 ProcMacro::TokenStream
-to_tokenstream (std::vector<TokenPtr> tokens)
+convert (std::vector<TokenPtr> tokens)
 {
   std::vector<ProcMacro::TokenStream> trees;
   trees.push_back (ProcMacro::TokenStream::make_tokenstream ());
@@ -297,4 +297,57 @@ to_tokenstream (std::vector<TokenPtr> tokens)
   return trees.back ();
 }
 
+static void
+from_tokenstream (ProcMacro::TokenStream ts, std::vector<TokenPtr> &result);
+
+static void
+from_ident (ProcMacro::Ident ident, std::vector<TokenPtr> &result)
+{}
+
+static void
+from_literal (ProcMacro::Literal literal, std::vector<TokenPtr> &result)
+{}
+
+static void
+from_punct (ProcMacro::Punct punct, std::vector<TokenPtr> &result)
+{}
+
+static void
+from_group (ProcMacro::Group g, std::vector<TokenPtr> &result)
+{}
+
+static void
+from_tokenstream (ProcMacro::TokenStream ts, std::vector<TokenPtr> &result)
+{
+  for (std::uint64_t i = 0; i < ts.size; i++)
+    {
+      ProcMacro::TokenTree &tt = ts.data[i];
+      switch (tt.tag)
+       {
+       case ProcMacro::GROUP:
+         from_group (tt.payload.group, result);
+         break;
+       case ProcMacro::IDENT:
+         from_ident (tt.payload.ident, result);
+         break;
+       case ProcMacro::PUNCT:
+         from_punct (tt.payload.punct, result);
+         break;
+       case ProcMacro::LITERAL:
+         from_literal (tt.payload.literal, result);
+         break;
+       default:
+         gcc_unreachable ();
+       }
+    }
+}
+
+std::vector<TokenPtr>
+convert (ProcMacro::TokenStream ts)
+{
+  std::vector<TokenPtr> result;
+  from_tokenstream (ts, result);
+  return result;
+}
+
 } // namespace Rust
index 5be745cd66c1a438a7568aabbfeef6e5905e873f..ee82d0bc13f3eb83b4eb7e800b54db6805832812 100644 (file)
 
 #include <vector>
 #include "rust-token.h"
-#include "libproc_macro/tokenstream.h"
+#include "libproc_macro/proc_macro.h"
 
 namespace Rust {
 
 ProcMacro::TokenStream
-to_tokenstream (std::vector<TokenPtr> tokens);
+convert (std::vector<TokenPtr> tokens);
+
+std::vector<TokenPtr>
+convert (ProcMacro::TokenStream ts);
 
 } // namespace Rust
 
This page took 0.070622 seconds and 5 git commands to generate.