aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>2023-02-21 10:17:25 +0100
committerPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>2023-03-30 13:52:32 +0200
commit3954270ee7fa782b64bc2f020056b7f6b58195b0 (patch)
tree074dfb3c6c70fe9578d737828166d063ece70a95
parenteeda932733ec3841abdfe185af52b2ecd88d98e4 (diff)
downloadgcc-3954270ee7fa782b64bc2f020056b7f6b58195b0.zip
gcc-3954270ee7fa782b64bc2f020056b7f6b58195b0.tar.gz
gcc-3954270ee7fa782b64bc2f020056b7f6b58195b0.tar.bz2
libproc_macro: Implement FromStr for TokenStream
Implement FromStr for TokenStream rust interface type. ChangeLog: * librust/proc_macro/rust/bridge/token_stream.rs: Add FromStr implementation on internal type. * librust/proc_macro/rust/lib.rs: Implement FromStr. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
-rw-r--r--librust/proc_macro/rust/bridge/token_stream.rs28
-rw-r--r--librust/proc_macro/rust/lib.rs4
2 files changed, 24 insertions, 8 deletions
diff --git a/librust/proc_macro/rust/bridge/token_stream.rs b/librust/proc_macro/rust/bridge/token_stream.rs
index eca0902..c33806f 100644
--- a/librust/proc_macro/rust/bridge/token_stream.rs
+++ b/librust/proc_macro/rust/bridge/token_stream.rs
@@ -1,11 +1,21 @@
use bridge::{group::Group, ident::Ident, literal::Literal, punct::Punct};
use std::convert::TryInto;
+use std::ffi::c_uchar;
use std::fmt;
use std::slice;
+use std::str::FromStr;
+use LexError;
type ExternalTokenTree = crate::TokenTree;
type ExternalTokenStream = crate::TokenStream;
+extern "C" {
+ fn TokenStream__new() -> TokenStream;
+ fn TokenStream__with_capacity(capacity: u64) -> TokenStream;
+ fn TokenStream__push(stream: *mut TokenStream, tree: TokenTree);
+ fn TokenStream__from_string(str: *const c_uchar, ts: *mut TokenStream) -> bool;
+}
+
// TODO: There surely is a better way to achieve this. I don't like this
// "duplication" of the TokenTree enumeration. But I cannot use the public
// one since it's underlying types (public Group, Ident...) are not ffi safe.
@@ -41,12 +51,6 @@ impl From<ExternalTokenTree> for TokenTree {
}
}
-extern "C" {
- fn TokenStream__new() -> TokenStream;
- fn TokenStream__with_capacity(capacity: u64) -> TokenStream;
- fn TokenStream__push(stream: *mut TokenStream, tree: TokenTree);
-}
-
#[repr(C)]
#[derive(Debug, Clone)]
pub struct TokenStream {
@@ -129,3 +133,15 @@ impl fmt::Display for TokenStream {
Ok(())
}
}
+
+impl FromStr for TokenStream {
+ type Err = LexError;
+ fn from_str(string: &str) -> Result<Self, LexError> {
+ let mut ts = TokenStream::new();
+ if unsafe { TokenStream__from_string(string.as_ptr(), &mut ts as *mut TokenStream) } {
+ Err(LexError)
+ } else {
+ Ok(ts)
+ }
+ }
+}
diff --git a/librust/proc_macro/rust/lib.rs b/librust/proc_macro/rust/lib.rs
index 59b4929..a1be0ac 100644
--- a/librust/proc_macro/rust/lib.rs
+++ b/librust/proc_macro/rust/lib.rs
@@ -168,8 +168,8 @@ impl fmt::Debug for TokenStream {
impl FromStr for TokenStream {
type Err = LexError;
- fn from_str(_src: &str) -> Result<Self, LexError> {
- todo!("Implement this function")
+ fn from_str(src: &str) -> Result<Self, LexError> {
+ bridge::token_stream::TokenStream::from_str(src).map(TokenStream)
}
}