[committed,59/88] gccrs: expansion: Add `get_token_slice` to `MacroInvocLexer` class

Message ID 20230405140411.3016563-60-arthur.cohen@embecosm.com
State Accepted
Headers
Series [committed,01/88] gccrs: fatal_error_flag: Fix typo in error message |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Arthur Cohen April 5, 2023, 2:03 p.m. UTC
  From: Arthur Cohen <arthur.cohen@embecosm.com>

gcc/rust/ChangeLog:

	* expand/rust-macro-invoc-lexer.cc (MacroInvocLexer::get_token_slice):
	Add API to retrieve token slices when lexing macro expansions.
	* expand/rust-macro-invoc-lexer.h: Declare `get_token_slice`.
---
 gcc/rust/expand/rust-macro-invoc-lexer.cc | 14 ++++++++++++++
 gcc/rust/expand/rust-macro-invoc-lexer.h  |  3 +++
 2 files changed, 17 insertions(+)
  

Patch

diff --git a/gcc/rust/expand/rust-macro-invoc-lexer.cc b/gcc/rust/expand/rust-macro-invoc-lexer.cc
index 8a43d29e0d1..321f0f97a76 100644
--- a/gcc/rust/expand/rust-macro-invoc-lexer.cc
+++ b/gcc/rust/expand/rust-macro-invoc-lexer.cc
@@ -26,4 +26,18 @@  MacroInvocLexer::split_current_token (TokenId new_left __attribute__ ((unused)),
   // FIXME
   gcc_unreachable ();
 }
+
+std::vector<std::unique_ptr<AST::Token>>
+MacroInvocLexer::get_token_slice (size_t start_idx, size_t end_idx) const
+{
+  std::vector<std::unique_ptr<AST::Token>> slice;
+
+  rust_assert (end_idx < token_stream.size ());
+
+  for (size_t i = start_idx; i < end_idx; i++)
+    slice.emplace_back (token_stream[i]->clone_token ());
+
+  return slice;
+}
+
 } // namespace Rust
diff --git a/gcc/rust/expand/rust-macro-invoc-lexer.h b/gcc/rust/expand/rust-macro-invoc-lexer.h
index a0d30164850..0923c18c84d 100644
--- a/gcc/rust/expand/rust-macro-invoc-lexer.h
+++ b/gcc/rust/expand/rust-macro-invoc-lexer.h
@@ -55,6 +55,9 @@  public:
 
   size_t get_offs () const { return offs; }
 
+  std::vector<std::unique_ptr<AST::Token>>
+  get_token_slice (size_t start_idx, size_t end_idx) const;
+
 private:
   size_t offs;
   std::vector<std::unique_ptr<AST::Token>> token_stream;