Fix proc macro TokenStream::from_str token ids

This commit is contained in:
vlad20012 2021-03-01 16:31:03 +03:00
parent cda13d5461
commit a157f19bf5
No known key found for this signature in database
GPG Key ID: 58E62ED31A8B3999

View File

@ -14,7 +14,6 @@
use std::hash::Hash;
use std::iter::FromIterator;
use std::ops::Bound;
use std::str::FromStr;
use std::{ascii, vec::IntoIter};
type Group = tt::Subtree;
@ -278,6 +277,42 @@ fn track_env_var(&mut self, _var: &str, _value: Option<&str>) {
}
}
fn subtree_replace_token_ids_with_unspecified(subtree: tt::Subtree) -> tt::Subtree {
tt::Subtree {
delimiter: subtree.delimiter.map(|d| tt::Delimiter { id: tt::TokenId::unspecified(), ..d }),
token_trees: subtree
.token_trees
.into_iter()
.map(|t| token_tree_replace_token_ids_with_unspecified(t))
.collect(),
}
}
fn token_tree_replace_token_ids_with_unspecified(tt: tt::TokenTree) -> tt::TokenTree {
match tt {
tt::TokenTree::Leaf(leaf) => {
tt::TokenTree::Leaf(leaf_replace_token_ids_with_unspecified(leaf))
}
tt::TokenTree::Subtree(subtree) => {
tt::TokenTree::Subtree(subtree_replace_token_ids_with_unspecified(subtree))
}
}
}
fn leaf_replace_token_ids_with_unspecified(leaf: tt::Leaf) -> tt::Leaf {
match leaf {
tt::Leaf::Literal(lit) => {
tt::Leaf::Literal(tt::Literal { id: tt::TokenId::unspecified(), ..lit })
}
tt::Leaf::Punct(punct) => {
tt::Leaf::Punct(tt::Punct { id: tt::TokenId::unspecified(), ..punct })
}
tt::Leaf::Ident(ident) => {
tt::Leaf::Ident(tt::Ident { id: tt::TokenId::unspecified(), ..ident })
}
}
}
impl server::TokenStream for Rustc {
fn new(&mut self) -> Self::TokenStream {
Self::TokenStream::new()
@ -287,7 +322,8 @@ fn is_empty(&mut self, stream: &Self::TokenStream) -> bool {
stream.is_empty()
}
fn from_str(&mut self, src: &str) -> Self::TokenStream {
Self::TokenStream::from_str(src).expect("cannot parse string")
let (subtree, _) = mbe::parse_to_token_tree(src).expect("cannot parse string");
TokenStream::with_subtree(subtree_replace_token_ids_with_unspecified(subtree))
}
fn to_string(&mut self, stream: &Self::TokenStream) -> String {
stream.to_string()