mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
Rename proc-macro-srv::server to server_impl
This commit is contained in:
parent
23b043a622
commit
0b88dfc8b1
9 changed files with 42 additions and 41 deletions
476
crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs
Normal file
476
crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs
Normal file
|
@ -0,0 +1,476 @@
|
|||
//! proc-macro server backend based on rust-analyzer's internal span representation
|
||||
//! This backend is used solely by rust-analyzer as it ties into rust-analyzer internals.
|
||||
//!
|
||||
//! It is an unfortunate result of how the proc-macro API works that we need to look into the
|
||||
//! concrete representation of the spans, and as such, RustRover cannot make use of this unless they
|
||||
//! change their representation to be compatible with rust-analyzer's.
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
iter,
|
||||
ops::{Bound, Range},
|
||||
};
|
||||
|
||||
use proc_macro::bridge::{self, server};
|
||||
use span::{Span, FIXUP_ERASED_FILE_AST_ID_MARKER};
|
||||
use tt::{TextRange, TextSize};
|
||||
|
||||
use crate::server_impl::{
|
||||
delim_to_external, delim_to_internal, literal_with_stringify_parts,
|
||||
token_stream::TokenStreamBuilder, Symbol, SymbolInternerRef, SYMBOL_INTERNER,
|
||||
};
|
||||
mod tt {
|
||||
pub use tt::*;
|
||||
|
||||
pub type Subtree = ::tt::Subtree<super::Span>;
|
||||
pub type TokenTree = ::tt::TokenTree<super::Span>;
|
||||
pub type Leaf = ::tt::Leaf<super::Span>;
|
||||
pub type Literal = ::tt::Literal<super::Span>;
|
||||
pub type Punct = ::tt::Punct<super::Span>;
|
||||
pub type Ident = ::tt::Ident<super::Span>;
|
||||
}
|
||||
|
||||
type TokenStream = crate::server_impl::TokenStream<Span>;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SourceFile;
|
||||
pub struct FreeFunctions;
|
||||
|
||||
pub struct RaSpanServer {
|
||||
pub(crate) interner: SymbolInternerRef,
|
||||
// FIXME: Report this back to the caller to track as dependencies
|
||||
pub tracked_env_vars: HashMap<Box<str>, Option<Box<str>>>,
|
||||
// FIXME: Report this back to the caller to track as dependencies
|
||||
pub tracked_paths: HashSet<Box<str>>,
|
||||
pub call_site: Span,
|
||||
pub def_site: Span,
|
||||
pub mixed_site: Span,
|
||||
}
|
||||
|
||||
impl server::Types for RaSpanServer {
|
||||
type FreeFunctions = FreeFunctions;
|
||||
type TokenStream = TokenStream;
|
||||
type SourceFile = SourceFile;
|
||||
type Span = Span;
|
||||
type Symbol = Symbol;
|
||||
}
|
||||
|
||||
impl server::FreeFunctions for RaSpanServer {
|
||||
fn injected_env_var(&mut self, _: &str) -> Option<std::string::String> {
|
||||
None
|
||||
}
|
||||
|
||||
fn track_env_var(&mut self, var: &str, value: Option<&str>) {
|
||||
self.tracked_env_vars.insert(var.into(), value.map(Into::into));
|
||||
}
|
||||
fn track_path(&mut self, path: &str) {
|
||||
self.tracked_paths.insert(path.into());
|
||||
}
|
||||
|
||||
fn literal_from_str(
|
||||
&mut self,
|
||||
s: &str,
|
||||
) -> Result<bridge::Literal<Self::Span, Self::Symbol>, ()> {
|
||||
use proc_macro::bridge::LitKind;
|
||||
use rustc_lexer::{LiteralKind, Token, TokenKind};
|
||||
|
||||
let mut tokens = rustc_lexer::tokenize(s);
|
||||
let minus_or_lit = tokens.next().unwrap_or(Token { kind: TokenKind::Eof, len: 0 });
|
||||
|
||||
let lit = if minus_or_lit.kind == TokenKind::Minus {
|
||||
let lit = tokens.next().ok_or(())?;
|
||||
if !matches!(
|
||||
lit.kind,
|
||||
TokenKind::Literal {
|
||||
kind: LiteralKind::Int { .. } | LiteralKind::Float { .. },
|
||||
..
|
||||
}
|
||||
) {
|
||||
return Err(());
|
||||
}
|
||||
lit
|
||||
} else {
|
||||
minus_or_lit
|
||||
};
|
||||
|
||||
if tokens.next().is_some() {
|
||||
return Err(());
|
||||
}
|
||||
|
||||
let TokenKind::Literal { kind, suffix_start } = lit.kind else { return Err(()) };
|
||||
let (kind, start_offset, end_offset) = match kind {
|
||||
LiteralKind::Int { .. } => (LitKind::Integer, 0, 0),
|
||||
LiteralKind::Float { .. } => (LitKind::Float, 0, 0),
|
||||
LiteralKind::Char { terminated } => (LitKind::Char, 1, terminated as usize),
|
||||
LiteralKind::Byte { terminated } => (LitKind::Byte, 2, terminated as usize),
|
||||
LiteralKind::Str { terminated } => (LitKind::Str, 1, terminated as usize),
|
||||
LiteralKind::ByteStr { terminated } => (LitKind::ByteStr, 2, terminated as usize),
|
||||
LiteralKind::CStr { terminated } => (LitKind::CStr, 2, terminated as usize),
|
||||
LiteralKind::RawStr { n_hashes } => (
|
||||
LitKind::StrRaw(n_hashes.unwrap_or_default()),
|
||||
2 + n_hashes.unwrap_or_default() as usize,
|
||||
1 + n_hashes.unwrap_or_default() as usize,
|
||||
),
|
||||
LiteralKind::RawByteStr { n_hashes } => (
|
||||
LitKind::ByteStrRaw(n_hashes.unwrap_or_default()),
|
||||
3 + n_hashes.unwrap_or_default() as usize,
|
||||
1 + n_hashes.unwrap_or_default() as usize,
|
||||
),
|
||||
LiteralKind::RawCStr { n_hashes } => (
|
||||
LitKind::CStrRaw(n_hashes.unwrap_or_default()),
|
||||
3 + n_hashes.unwrap_or_default() as usize,
|
||||
1 + n_hashes.unwrap_or_default() as usize,
|
||||
),
|
||||
};
|
||||
|
||||
let (lit, suffix) = s.split_at(suffix_start as usize);
|
||||
let lit = &lit[start_offset..lit.len() - end_offset];
|
||||
let suffix = match suffix {
|
||||
"" | "_" => None,
|
||||
suffix => Some(Symbol::intern(self.interner, suffix)),
|
||||
};
|
||||
|
||||
Ok(bridge::Literal {
|
||||
kind,
|
||||
symbol: Symbol::intern(self.interner, lit),
|
||||
suffix,
|
||||
span: self.call_site,
|
||||
})
|
||||
}
|
||||
|
||||
fn emit_diagnostic(&mut self, _: bridge::Diagnostic<Self::Span>) {
|
||||
// FIXME handle diagnostic
|
||||
}
|
||||
}
|
||||
|
||||
impl server::TokenStream for RaSpanServer {
|
||||
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, self.call_site).expect("cannot parse string")
|
||||
}
|
||||
fn to_string(&mut self, stream: &Self::TokenStream) -> String {
|
||||
stream.to_string()
|
||||
}
|
||||
fn from_token_tree(
|
||||
&mut self,
|
||||
tree: bridge::TokenTree<Self::TokenStream, Self::Span, Self::Symbol>,
|
||||
) -> Self::TokenStream {
|
||||
match tree {
|
||||
bridge::TokenTree::Group(group) => {
|
||||
let group = tt::Subtree {
|
||||
delimiter: delim_to_internal(group.delimiter, group.span),
|
||||
token_trees: match group.stream {
|
||||
Some(stream) => stream.into_iter().collect(),
|
||||
None => Box::new([]),
|
||||
},
|
||||
};
|
||||
let tree = tt::TokenTree::from(group);
|
||||
Self::TokenStream::from_iter(iter::once(tree))
|
||||
}
|
||||
|
||||
bridge::TokenTree::Ident(ident) => {
|
||||
let text = ident.sym.text(self.interner);
|
||||
let text =
|
||||
if ident.is_raw { ::tt::SmolStr::from_iter(["r#", &text]) } else { text };
|
||||
let ident: tt::Ident = tt::Ident { text, span: ident.span };
|
||||
let leaf = tt::Leaf::from(ident);
|
||||
let tree = tt::TokenTree::from(leaf);
|
||||
Self::TokenStream::from_iter(iter::once(tree))
|
||||
}
|
||||
|
||||
bridge::TokenTree::Literal(literal) => {
|
||||
let text = literal_with_stringify_parts(&literal, self.interner, |parts| {
|
||||
::tt::SmolStr::from_iter(parts.iter().copied())
|
||||
});
|
||||
|
||||
let literal = tt::Literal { text, span: literal.span };
|
||||
let leaf: tt::Leaf = tt::Leaf::from(literal);
|
||||
let tree = tt::TokenTree::from(leaf);
|
||||
Self::TokenStream::from_iter(iter::once(tree))
|
||||
}
|
||||
|
||||
bridge::TokenTree::Punct(p) => {
|
||||
let punct = tt::Punct {
|
||||
char: p.ch as char,
|
||||
spacing: if p.joint { tt::Spacing::Joint } else { tt::Spacing::Alone },
|
||||
span: p.span,
|
||||
};
|
||||
let leaf = tt::Leaf::from(punct);
|
||||
let tree = tt::TokenTree::from(leaf);
|
||||
Self::TokenStream::from_iter(iter::once(tree))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn expand_expr(&mut self, self_: &Self::TokenStream) -> Result<Self::TokenStream, ()> {
|
||||
// FIXME: requires db, more importantly this requires name resolution so we would need to
|
||||
// eagerly expand this proc-macro, but we can't know that this proc-macro is eager until we
|
||||
// expand it ...
|
||||
// This calls for some kind of marker that a proc-macro wants to access this eager API,
|
||||
// otherwise we need to treat every proc-macro eagerly / or not support this.
|
||||
Ok(self_.clone())
|
||||
}
|
||||
|
||||
fn concat_trees(
|
||||
&mut self,
|
||||
base: Option<Self::TokenStream>,
|
||||
trees: Vec<bridge::TokenTree<Self::TokenStream, Self::Span, Self::Symbol>>,
|
||||
) -> Self::TokenStream {
|
||||
let mut builder = TokenStreamBuilder::new();
|
||||
if let Some(base) = base {
|
||||
builder.push(base);
|
||||
}
|
||||
for tree in trees {
|
||||
builder.push(self.from_token_tree(tree));
|
||||
}
|
||||
builder.build()
|
||||
}
|
||||
|
||||
fn concat_streams(
|
||||
&mut self,
|
||||
base: Option<Self::TokenStream>,
|
||||
streams: Vec<Self::TokenStream>,
|
||||
) -> Self::TokenStream {
|
||||
let mut builder = TokenStreamBuilder::new();
|
||||
if let Some(base) = base {
|
||||
builder.push(base);
|
||||
}
|
||||
for stream in streams {
|
||||
builder.push(stream);
|
||||
}
|
||||
builder.build()
|
||||
}
|
||||
|
||||
fn into_trees(
|
||||
&mut self,
|
||||
stream: Self::TokenStream,
|
||||
) -> Vec<bridge::TokenTree<Self::TokenStream, Self::Span, Self::Symbol>> {
|
||||
stream
|
||||
.into_iter()
|
||||
.map(|tree| match tree {
|
||||
tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) => {
|
||||
bridge::TokenTree::Ident(match ident.text.strip_prefix("r#") {
|
||||
Some(text) => bridge::Ident {
|
||||
sym: Symbol::intern(self.interner, text),
|
||||
is_raw: true,
|
||||
span: ident.span,
|
||||
},
|
||||
None => bridge::Ident {
|
||||
sym: Symbol::intern(self.interner, &ident.text),
|
||||
is_raw: false,
|
||||
span: ident.span,
|
||||
},
|
||||
})
|
||||
}
|
||||
tt::TokenTree::Leaf(tt::Leaf::Literal(lit)) => {
|
||||
bridge::TokenTree::Literal(bridge::Literal {
|
||||
span: lit.span,
|
||||
..server::FreeFunctions::literal_from_str(self, &lit.text).unwrap()
|
||||
})
|
||||
}
|
||||
tt::TokenTree::Leaf(tt::Leaf::Punct(punct)) => {
|
||||
bridge::TokenTree::Punct(bridge::Punct {
|
||||
ch: punct.char as u8,
|
||||
joint: punct.spacing == tt::Spacing::Joint,
|
||||
span: punct.span,
|
||||
})
|
||||
}
|
||||
tt::TokenTree::Subtree(subtree) => bridge::TokenTree::Group(bridge::Group {
|
||||
delimiter: delim_to_external(subtree.delimiter),
|
||||
stream: if subtree.token_trees.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(subtree.token_trees.into_vec().into_iter().collect())
|
||||
},
|
||||
span: bridge::DelimSpan::from_single(subtree.delimiter.open),
|
||||
}),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl server::SourceFile for RaSpanServer {
|
||||
fn eq(&mut self, _file1: &Self::SourceFile, _file2: &Self::SourceFile) -> bool {
|
||||
// FIXME
|
||||
true
|
||||
}
|
||||
fn path(&mut self, _file: &Self::SourceFile) -> String {
|
||||
// FIXME
|
||||
String::new()
|
||||
}
|
||||
fn is_real(&mut self, _file: &Self::SourceFile) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
impl server::Span for RaSpanServer {
|
||||
fn debug(&mut self, span: Self::Span) -> String {
|
||||
format!("{:?}", span)
|
||||
}
|
||||
fn source_file(&mut self, _span: Self::Span) -> Self::SourceFile {
|
||||
// FIXME stub, requires db
|
||||
SourceFile {}
|
||||
}
|
||||
fn save_span(&mut self, _span: Self::Span) -> usize {
|
||||
// FIXME, quote is incompatible with third-party tools
|
||||
// This is called by the quote proc-macro which is expanded when the proc-macro is compiled
|
||||
// As such, r-a will never observe this
|
||||
0
|
||||
}
|
||||
fn recover_proc_macro_span(&mut self, _id: usize) -> Self::Span {
|
||||
// FIXME, quote is incompatible with third-party tools
|
||||
// This is called by the expansion of quote!, r-a will observe this, but we don't have
|
||||
// access to the spans that were encoded
|
||||
self.call_site
|
||||
}
|
||||
/// Recent feature, not yet in the proc_macro
|
||||
///
|
||||
/// See PR:
|
||||
/// https://github.com/rust-lang/rust/pull/55780
|
||||
fn source_text(&mut self, _span: Self::Span) -> Option<String> {
|
||||
// FIXME requires db, needs special handling wrt fixup spans
|
||||
None
|
||||
}
|
||||
|
||||
fn parent(&mut self, _span: Self::Span) -> Option<Self::Span> {
|
||||
// FIXME requires db, looks up the parent call site
|
||||
None
|
||||
}
|
||||
fn source(&mut self, span: Self::Span) -> Self::Span {
|
||||
// FIXME requires db, returns the top level call site
|
||||
span
|
||||
}
|
||||
fn byte_range(&mut self, span: Self::Span) -> Range<usize> {
|
||||
// FIXME requires db to resolve the ast id, THIS IS NOT INCREMENTAL
|
||||
Range { start: span.range.start().into(), end: span.range.end().into() }
|
||||
}
|
||||
fn join(&mut self, first: Self::Span, second: Self::Span) -> Option<Self::Span> {
|
||||
// We can't modify the span range for fixup spans, those are meaningful to fixup, so just
|
||||
// prefer the non-fixup span.
|
||||
if first.anchor.ast_id == FIXUP_ERASED_FILE_AST_ID_MARKER {
|
||||
return Some(second);
|
||||
}
|
||||
if second.anchor.ast_id == FIXUP_ERASED_FILE_AST_ID_MARKER {
|
||||
return Some(first);
|
||||
}
|
||||
// FIXME: Once we can talk back to the client, implement a "long join" request for anchors
|
||||
// that differ in [AstId]s as joining those spans requires resolving the AstIds.
|
||||
if first.anchor != second.anchor {
|
||||
return None;
|
||||
}
|
||||
// Differing context, we can't merge these so prefer the one that's root
|
||||
if first.ctx != second.ctx {
|
||||
if first.ctx.is_root() {
|
||||
return Some(second);
|
||||
} else if second.ctx.is_root() {
|
||||
return Some(first);
|
||||
}
|
||||
}
|
||||
Some(Span {
|
||||
range: first.range.cover(second.range),
|
||||
anchor: second.anchor,
|
||||
ctx: second.ctx,
|
||||
})
|
||||
}
|
||||
fn subspan(
|
||||
&mut self,
|
||||
span: Self::Span,
|
||||
start: Bound<usize>,
|
||||
end: Bound<usize>,
|
||||
) -> Option<Self::Span> {
|
||||
// We can't modify the span range for fixup spans, those are meaningful to fixup.
|
||||
if span.anchor.ast_id == FIXUP_ERASED_FILE_AST_ID_MARKER {
|
||||
return Some(span);
|
||||
}
|
||||
let length = span.range.len().into();
|
||||
|
||||
let start: u32 = match start {
|
||||
Bound::Included(lo) => lo,
|
||||
Bound::Excluded(lo) => lo.checked_add(1)?,
|
||||
Bound::Unbounded => 0,
|
||||
}
|
||||
.try_into()
|
||||
.ok()?;
|
||||
|
||||
let end: u32 = match end {
|
||||
Bound::Included(hi) => hi.checked_add(1)?,
|
||||
Bound::Excluded(hi) => hi,
|
||||
Bound::Unbounded => span.range.len().into(),
|
||||
}
|
||||
.try_into()
|
||||
.ok()?;
|
||||
|
||||
// Bounds check the values, preventing addition overflow and OOB spans.
|
||||
let span_start = span.range.start().into();
|
||||
if (u32::MAX - start) < span_start
|
||||
|| (u32::MAX - end) < span_start
|
||||
|| start >= end
|
||||
|| end > length
|
||||
{
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(Span {
|
||||
range: TextRange::new(TextSize::from(start), TextSize::from(end)) + span.range.start(),
|
||||
..span
|
||||
})
|
||||
}
|
||||
|
||||
fn resolved_at(&mut self, span: Self::Span, at: Self::Span) -> Self::Span {
|
||||
Span { ctx: at.ctx, ..span }
|
||||
}
|
||||
|
||||
fn end(&mut self, span: Self::Span) -> Self::Span {
|
||||
// We can't modify the span range for fixup spans, those are meaningful to fixup.
|
||||
if span.anchor.ast_id == FIXUP_ERASED_FILE_AST_ID_MARKER {
|
||||
return span;
|
||||
}
|
||||
Span { range: TextRange::empty(span.range.end()), ..span }
|
||||
}
|
||||
|
||||
fn start(&mut self, span: Self::Span) -> Self::Span {
|
||||
// We can't modify the span range for fixup spans, those are meaningful to fixup.
|
||||
if span.anchor.ast_id == FIXUP_ERASED_FILE_AST_ID_MARKER {
|
||||
return span;
|
||||
}
|
||||
Span { range: TextRange::empty(span.range.start()), ..span }
|
||||
}
|
||||
|
||||
fn line(&mut self, _span: Self::Span) -> usize {
|
||||
// FIXME requires db to resolve line index, THIS IS NOT INCREMENTAL
|
||||
0
|
||||
}
|
||||
|
||||
fn column(&mut self, _span: Self::Span) -> usize {
|
||||
// FIXME requires db to resolve line index, THIS IS NOT INCREMENTAL
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
impl server::Symbol for RaSpanServer {
|
||||
fn normalize_and_validate_ident(&mut self, string: &str) -> Result<Self::Symbol, ()> {
|
||||
// FIXME: nfc-normalize and validate idents
|
||||
Ok(<Self as server::Server>::intern_symbol(string))
|
||||
}
|
||||
}
|
||||
|
||||
impl server::Server for RaSpanServer {
|
||||
fn globals(&mut self) -> bridge::ExpnGlobals<Self::Span> {
|
||||
bridge::ExpnGlobals {
|
||||
def_site: self.def_site,
|
||||
call_site: self.call_site,
|
||||
mixed_site: self.mixed_site,
|
||||
}
|
||||
}
|
||||
|
||||
fn intern_symbol(ident: &str) -> Self::Symbol {
|
||||
// FIXME: should be `self.interner` once the proc-macro api allows it.
|
||||
Symbol::intern(&SYMBOL_INTERNER, &::tt::SmolStr::from(ident))
|
||||
}
|
||||
|
||||
fn with_symbol_string(symbol: &Self::Symbol, f: impl FnOnce(&str)) {
|
||||
// FIXME: should be `self.interner` once the proc-macro api allows it.
|
||||
f(symbol.text(&SYMBOL_INTERNER).as_str())
|
||||
}
|
||||
}
|
48
crates/proc-macro-srv/src/server_impl/symbol.rs
Normal file
48
crates/proc-macro-srv/src/server_impl/symbol.rs
Normal file
|
@ -0,0 +1,48 @@
|
|||
//! Symbol interner for proc-macro-srv
|
||||
|
||||
use std::{cell::RefCell, collections::HashMap, thread::LocalKey};
|
||||
use tt::SmolStr;
|
||||
|
||||
thread_local! {
|
||||
pub(crate) static SYMBOL_INTERNER: RefCell<SymbolInterner> = Default::default();
|
||||
}
|
||||
|
||||
// ID for an interned symbol.
|
||||
#[derive(Hash, Eq, PartialEq, Copy, Clone)]
|
||||
pub struct Symbol(u32);
|
||||
|
||||
pub(crate) type SymbolInternerRef = &'static LocalKey<RefCell<SymbolInterner>>;
|
||||
|
||||
impl Symbol {
|
||||
pub(super) fn intern(interner: SymbolInternerRef, data: &str) -> Symbol {
|
||||
interner.with(|i| i.borrow_mut().intern(data))
|
||||
}
|
||||
|
||||
pub(super) fn text(&self, interner: SymbolInternerRef) -> SmolStr {
|
||||
interner.with(|i| i.borrow().get(self).clone())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub(crate) struct SymbolInterner {
|
||||
idents: HashMap<SmolStr, u32>,
|
||||
ident_data: Vec<SmolStr>,
|
||||
}
|
||||
|
||||
impl SymbolInterner {
|
||||
fn intern(&mut self, data: &str) -> Symbol {
|
||||
if let Some(index) = self.idents.get(data) {
|
||||
return Symbol(*index);
|
||||
}
|
||||
|
||||
let index = self.idents.len() as u32;
|
||||
let data = SmolStr::from(data);
|
||||
self.ident_data.push(data.clone());
|
||||
self.idents.insert(data, index);
|
||||
Symbol(index)
|
||||
}
|
||||
|
||||
fn get(&self, sym: &Symbol) -> &SmolStr {
|
||||
&self.ident_data[sym.0 as usize]
|
||||
}
|
||||
}
|
436
crates/proc-macro-srv/src/server_impl/token_id.rs
Normal file
436
crates/proc-macro-srv/src/server_impl/token_id.rs
Normal file
|
@ -0,0 +1,436 @@
|
|||
//! proc-macro server backend based on [`proc_macro_api::msg::TokenId`] as the backing span.
|
||||
//! This backend is rather inflexible, used by RustRover and older rust-analyzer versions.
|
||||
use std::{
|
||||
iter,
|
||||
ops::{Bound, Range},
|
||||
};
|
||||
|
||||
use proc_macro::bridge::{self, server};
|
||||
|
||||
use crate::server_impl::{
|
||||
delim_to_external, delim_to_internal, literal_with_stringify_parts,
|
||||
token_stream::TokenStreamBuilder, Symbol, SymbolInternerRef, SYMBOL_INTERNER,
|
||||
};
|
||||
mod tt {
|
||||
pub use proc_macro_api::msg::TokenId;
|
||||
|
||||
pub use tt::*;
|
||||
|
||||
pub type Subtree = ::tt::Subtree<TokenId>;
|
||||
pub type TokenTree = ::tt::TokenTree<TokenId>;
|
||||
pub type Leaf = ::tt::Leaf<TokenId>;
|
||||
pub type Literal = ::tt::Literal<TokenId>;
|
||||
pub type Punct = ::tt::Punct<TokenId>;
|
||||
pub type Ident = ::tt::Ident<TokenId>;
|
||||
}
|
||||
type Group = tt::Subtree;
|
||||
type TokenTree = tt::TokenTree;
|
||||
#[allow(unused)]
|
||||
type Punct = tt::Punct;
|
||||
type Spacing = tt::Spacing;
|
||||
#[allow(unused)]
|
||||
type Literal = tt::Literal;
|
||||
type Span = tt::TokenId;
|
||||
type TokenStream = crate::server_impl::TokenStream<Span>;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SourceFile;
|
||||
pub struct FreeFunctions;
|
||||
|
||||
pub struct TokenIdServer {
|
||||
pub(crate) interner: SymbolInternerRef,
|
||||
pub call_site: Span,
|
||||
pub def_site: Span,
|
||||
pub mixed_site: Span,
|
||||
}
|
||||
|
||||
impl server::Types for TokenIdServer {
|
||||
type FreeFunctions = FreeFunctions;
|
||||
type TokenStream = TokenStream;
|
||||
type SourceFile = SourceFile;
|
||||
type Span = Span;
|
||||
type Symbol = Symbol;
|
||||
}
|
||||
|
||||
impl server::FreeFunctions for TokenIdServer {
|
||||
fn injected_env_var(&mut self, _: &str) -> Option<std::string::String> {
|
||||
None
|
||||
}
|
||||
fn track_env_var(&mut self, _var: &str, _value: Option<&str>) {}
|
||||
fn track_path(&mut self, _path: &str) {}
|
||||
fn literal_from_str(
|
||||
&mut self,
|
||||
s: &str,
|
||||
) -> Result<bridge::Literal<Self::Span, Self::Symbol>, ()> {
|
||||
use proc_macro::bridge::LitKind;
|
||||
use rustc_lexer::{LiteralKind, Token, TokenKind};
|
||||
|
||||
let mut tokens = rustc_lexer::tokenize(s);
|
||||
let minus_or_lit = tokens.next().unwrap_or(Token { kind: TokenKind::Eof, len: 0 });
|
||||
|
||||
let lit = if minus_or_lit.kind == TokenKind::Minus {
|
||||
let lit = tokens.next().ok_or(())?;
|
||||
if !matches!(
|
||||
lit.kind,
|
||||
TokenKind::Literal {
|
||||
kind: LiteralKind::Int { .. } | LiteralKind::Float { .. },
|
||||
..
|
||||
}
|
||||
) {
|
||||
return Err(());
|
||||
}
|
||||
lit
|
||||
} else {
|
||||
minus_or_lit
|
||||
};
|
||||
|
||||
if tokens.next().is_some() {
|
||||
return Err(());
|
||||
}
|
||||
|
||||
let TokenKind::Literal { kind, suffix_start } = lit.kind else { return Err(()) };
|
||||
|
||||
let (kind, start_offset, end_offset) = match kind {
|
||||
LiteralKind::Int { .. } => (LitKind::Integer, 0, 0),
|
||||
LiteralKind::Float { .. } => (LitKind::Float, 0, 0),
|
||||
LiteralKind::Char { terminated } => (LitKind::Char, 1, terminated as usize),
|
||||
LiteralKind::Byte { terminated } => (LitKind::Byte, 2, terminated as usize),
|
||||
LiteralKind::Str { terminated } => (LitKind::Str, 1, terminated as usize),
|
||||
LiteralKind::ByteStr { terminated } => (LitKind::ByteStr, 2, terminated as usize),
|
||||
LiteralKind::CStr { terminated } => (LitKind::CStr, 2, terminated as usize),
|
||||
LiteralKind::RawStr { n_hashes } => (
|
||||
LitKind::StrRaw(n_hashes.unwrap_or_default()),
|
||||
2 + n_hashes.unwrap_or_default() as usize,
|
||||
1 + n_hashes.unwrap_or_default() as usize,
|
||||
),
|
||||
LiteralKind::RawByteStr { n_hashes } => (
|
||||
LitKind::ByteStrRaw(n_hashes.unwrap_or_default()),
|
||||
3 + n_hashes.unwrap_or_default() as usize,
|
||||
1 + n_hashes.unwrap_or_default() as usize,
|
||||
),
|
||||
LiteralKind::RawCStr { n_hashes } => (
|
||||
LitKind::CStrRaw(n_hashes.unwrap_or_default()),
|
||||
3 + n_hashes.unwrap_or_default() as usize,
|
||||
1 + n_hashes.unwrap_or_default() as usize,
|
||||
),
|
||||
};
|
||||
|
||||
let (lit, suffix) = s.split_at(suffix_start as usize);
|
||||
let lit = &lit[start_offset..lit.len() - end_offset];
|
||||
let suffix = match suffix {
|
||||
"" | "_" => None,
|
||||
suffix => Some(Symbol::intern(self.interner, suffix)),
|
||||
};
|
||||
|
||||
Ok(bridge::Literal {
|
||||
kind,
|
||||
symbol: Symbol::intern(self.interner, lit),
|
||||
suffix,
|
||||
span: self.call_site,
|
||||
})
|
||||
}
|
||||
|
||||
fn emit_diagnostic(&mut self, _: bridge::Diagnostic<Self::Span>) {}
|
||||
}
|
||||
|
||||
impl server::TokenStream for TokenIdServer {
|
||||
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, self.call_site).expect("cannot parse string")
|
||||
}
|
||||
fn to_string(&mut self, stream: &Self::TokenStream) -> String {
|
||||
stream.to_string()
|
||||
}
|
||||
fn from_token_tree(
|
||||
&mut self,
|
||||
tree: bridge::TokenTree<Self::TokenStream, Self::Span, Self::Symbol>,
|
||||
) -> Self::TokenStream {
|
||||
match tree {
|
||||
bridge::TokenTree::Group(group) => {
|
||||
let group = Group {
|
||||
delimiter: delim_to_internal(group.delimiter, group.span),
|
||||
token_trees: match group.stream {
|
||||
Some(stream) => stream.into_iter().collect(),
|
||||
None => Box::new([]),
|
||||
},
|
||||
};
|
||||
let tree = TokenTree::from(group);
|
||||
Self::TokenStream::from_iter(iter::once(tree))
|
||||
}
|
||||
|
||||
bridge::TokenTree::Ident(ident) => {
|
||||
let text = ident.sym.text(self.interner);
|
||||
let text =
|
||||
if ident.is_raw { ::tt::SmolStr::from_iter(["r#", &text]) } else { text };
|
||||
let ident: tt::Ident = tt::Ident { text, span: ident.span };
|
||||
let leaf = tt::Leaf::from(ident);
|
||||
let tree = TokenTree::from(leaf);
|
||||
Self::TokenStream::from_iter(iter::once(tree))
|
||||
}
|
||||
|
||||
bridge::TokenTree::Literal(literal) => {
|
||||
let text = literal_with_stringify_parts(&literal, self.interner, |parts| {
|
||||
::tt::SmolStr::from_iter(parts.iter().copied())
|
||||
});
|
||||
|
||||
let literal = tt::Literal { text, span: literal.span };
|
||||
|
||||
let leaf = tt::Leaf::from(literal);
|
||||
let tree = TokenTree::from(leaf);
|
||||
Self::TokenStream::from_iter(iter::once(tree))
|
||||
}
|
||||
|
||||
bridge::TokenTree::Punct(p) => {
|
||||
let punct = tt::Punct {
|
||||
char: p.ch as char,
|
||||
spacing: if p.joint { Spacing::Joint } else { Spacing::Alone },
|
||||
span: p.span,
|
||||
};
|
||||
let leaf = tt::Leaf::from(punct);
|
||||
let tree = TokenTree::from(leaf);
|
||||
Self::TokenStream::from_iter(iter::once(tree))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn expand_expr(&mut self, self_: &Self::TokenStream) -> Result<Self::TokenStream, ()> {
|
||||
Ok(self_.clone())
|
||||
}
|
||||
|
||||
fn concat_trees(
|
||||
&mut self,
|
||||
base: Option<Self::TokenStream>,
|
||||
trees: Vec<bridge::TokenTree<Self::TokenStream, Self::Span, Self::Symbol>>,
|
||||
) -> Self::TokenStream {
|
||||
let mut builder = TokenStreamBuilder::new();
|
||||
if let Some(base) = base {
|
||||
builder.push(base);
|
||||
}
|
||||
for tree in trees {
|
||||
builder.push(self.from_token_tree(tree));
|
||||
}
|
||||
builder.build()
|
||||
}
|
||||
|
||||
fn concat_streams(
|
||||
&mut self,
|
||||
base: Option<Self::TokenStream>,
|
||||
streams: Vec<Self::TokenStream>,
|
||||
) -> Self::TokenStream {
|
||||
let mut builder = TokenStreamBuilder::new();
|
||||
if let Some(base) = base {
|
||||
builder.push(base);
|
||||
}
|
||||
for stream in streams {
|
||||
builder.push(stream);
|
||||
}
|
||||
builder.build()
|
||||
}
|
||||
|
||||
fn into_trees(
|
||||
&mut self,
|
||||
stream: Self::TokenStream,
|
||||
) -> Vec<bridge::TokenTree<Self::TokenStream, Self::Span, Self::Symbol>> {
|
||||
stream
|
||||
.into_iter()
|
||||
.map(|tree| match tree {
|
||||
tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) => {
|
||||
bridge::TokenTree::Ident(bridge::Ident {
|
||||
sym: Symbol::intern(self.interner, ident.text.trim_start_matches("r#")),
|
||||
is_raw: ident.text.starts_with("r#"),
|
||||
span: ident.span,
|
||||
})
|
||||
}
|
||||
tt::TokenTree::Leaf(tt::Leaf::Literal(lit)) => {
|
||||
bridge::TokenTree::Literal(bridge::Literal {
|
||||
span: lit.span,
|
||||
..server::FreeFunctions::literal_from_str(self, &lit.text)
|
||||
.unwrap_or_else(|_| panic!("`{}`", lit.text))
|
||||
})
|
||||
}
|
||||
tt::TokenTree::Leaf(tt::Leaf::Punct(punct)) => {
|
||||
bridge::TokenTree::Punct(bridge::Punct {
|
||||
ch: punct.char as u8,
|
||||
joint: punct.spacing == Spacing::Joint,
|
||||
span: punct.span,
|
||||
})
|
||||
}
|
||||
tt::TokenTree::Subtree(subtree) => bridge::TokenTree::Group(bridge::Group {
|
||||
delimiter: delim_to_external(subtree.delimiter),
|
||||
stream: if subtree.token_trees.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(TokenStream { token_trees: subtree.token_trees.into_vec() })
|
||||
},
|
||||
span: bridge::DelimSpan::from_single(subtree.delimiter.open),
|
||||
}),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl server::SourceFile for TokenIdServer {
|
||||
fn eq(&mut self, _file1: &Self::SourceFile, _file2: &Self::SourceFile) -> bool {
|
||||
true
|
||||
}
|
||||
fn path(&mut self, _file: &Self::SourceFile) -> String {
|
||||
String::new()
|
||||
}
|
||||
fn is_real(&mut self, _file: &Self::SourceFile) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
impl server::Span for TokenIdServer {
|
||||
fn debug(&mut self, span: Self::Span) -> String {
|
||||
format!("{:?}", span.0)
|
||||
}
|
||||
fn source_file(&mut self, _span: Self::Span) -> Self::SourceFile {
|
||||
SourceFile {}
|
||||
}
|
||||
fn save_span(&mut self, _span: Self::Span) -> usize {
|
||||
0
|
||||
}
|
||||
fn recover_proc_macro_span(&mut self, _id: usize) -> Self::Span {
|
||||
self.call_site
|
||||
}
|
||||
/// Recent feature, not yet in the proc_macro
|
||||
///
|
||||
/// See PR:
|
||||
/// https://github.com/rust-lang/rust/pull/55780
|
||||
fn source_text(&mut self, _span: Self::Span) -> Option<String> {
|
||||
None
|
||||
}
|
||||
|
||||
fn parent(&mut self, _span: Self::Span) -> Option<Self::Span> {
|
||||
None
|
||||
}
|
||||
fn source(&mut self, span: Self::Span) -> Self::Span {
|
||||
span
|
||||
}
|
||||
fn byte_range(&mut self, _span: Self::Span) -> Range<usize> {
|
||||
Range { start: 0, end: 0 }
|
||||
}
|
||||
fn join(&mut self, first: Self::Span, _second: Self::Span) -> Option<Self::Span> {
|
||||
// Just return the first span again, because some macros will unwrap the result.
|
||||
Some(first)
|
||||
}
|
||||
fn subspan(
|
||||
&mut self,
|
||||
span: Self::Span,
|
||||
_start: Bound<usize>,
|
||||
_end: Bound<usize>,
|
||||
) -> Option<Self::Span> {
|
||||
// Just return the span again, because some macros will unwrap the result.
|
||||
Some(span)
|
||||
}
|
||||
fn resolved_at(&mut self, _span: Self::Span, _at: Self::Span) -> Self::Span {
|
||||
self.call_site
|
||||
}
|
||||
|
||||
fn end(&mut self, _self_: Self::Span) -> Self::Span {
|
||||
self.call_site
|
||||
}
|
||||
|
||||
fn start(&mut self, _self_: Self::Span) -> Self::Span {
|
||||
self.call_site
|
||||
}
|
||||
|
||||
fn line(&mut self, _span: Self::Span) -> usize {
|
||||
0
|
||||
}
|
||||
|
||||
fn column(&mut self, _span: Self::Span) -> usize {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
impl server::Symbol for TokenIdServer {
|
||||
fn normalize_and_validate_ident(&mut self, string: &str) -> Result<Self::Symbol, ()> {
|
||||
// FIXME: nfc-normalize and validate idents
|
||||
Ok(<Self as server::Server>::intern_symbol(string))
|
||||
}
|
||||
}
|
||||
|
||||
impl server::Server for TokenIdServer {
|
||||
fn globals(&mut self) -> bridge::ExpnGlobals<Self::Span> {
|
||||
bridge::ExpnGlobals {
|
||||
def_site: self.def_site,
|
||||
call_site: self.call_site,
|
||||
mixed_site: self.mixed_site,
|
||||
}
|
||||
}
|
||||
|
||||
fn intern_symbol(ident: &str) -> Self::Symbol {
|
||||
Symbol::intern(&SYMBOL_INTERNER, &::tt::SmolStr::from(ident))
|
||||
}
|
||||
|
||||
fn with_symbol_string(symbol: &Self::Symbol, f: impl FnOnce(&str)) {
|
||||
f(symbol.text(&SYMBOL_INTERNER).as_str())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_ra_server_to_string() {
|
||||
let s = TokenStream {
|
||||
token_trees: vec![
|
||||
tt::TokenTree::Leaf(tt::Leaf::Ident(tt::Ident {
|
||||
text: "struct".into(),
|
||||
span: tt::TokenId(0),
|
||||
})),
|
||||
tt::TokenTree::Leaf(tt::Leaf::Ident(tt::Ident {
|
||||
text: "T".into(),
|
||||
span: tt::TokenId(0),
|
||||
})),
|
||||
tt::TokenTree::Subtree(tt::Subtree {
|
||||
delimiter: tt::Delimiter {
|
||||
open: tt::TokenId(0),
|
||||
close: tt::TokenId(0),
|
||||
kind: tt::DelimiterKind::Brace,
|
||||
},
|
||||
token_trees: Box::new([]),
|
||||
}),
|
||||
],
|
||||
};
|
||||
|
||||
assert_eq!(s.to_string(), "struct T {}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ra_server_from_str() {
|
||||
let subtree_paren_a = tt::TokenTree::Subtree(tt::Subtree {
|
||||
delimiter: tt::Delimiter {
|
||||
open: tt::TokenId(0),
|
||||
close: tt::TokenId(0),
|
||||
kind: tt::DelimiterKind::Parenthesis,
|
||||
},
|
||||
token_trees: Box::new([tt::TokenTree::Leaf(tt::Leaf::Ident(tt::Ident {
|
||||
text: "a".into(),
|
||||
span: tt::TokenId(0),
|
||||
}))]),
|
||||
});
|
||||
|
||||
let t1 = TokenStream::from_str("(a)", tt::TokenId(0)).unwrap();
|
||||
assert_eq!(t1.token_trees.len(), 1);
|
||||
assert_eq!(t1.token_trees[0], subtree_paren_a);
|
||||
|
||||
let t2 = TokenStream::from_str("(a);", tt::TokenId(0)).unwrap();
|
||||
assert_eq!(t2.token_trees.len(), 2);
|
||||
assert_eq!(t2.token_trees[0], subtree_paren_a);
|
||||
|
||||
let underscore = TokenStream::from_str("_", tt::TokenId(0)).unwrap();
|
||||
assert_eq!(
|
||||
underscore.token_trees[0],
|
||||
tt::TokenTree::Leaf(tt::Leaf::Ident(tt::Ident {
|
||||
text: "_".into(),
|
||||
span: tt::TokenId(0),
|
||||
}))
|
||||
);
|
||||
}
|
||||
}
|
155
crates/proc-macro-srv/src/server_impl/token_stream.rs
Normal file
155
crates/proc-macro-srv/src/server_impl/token_stream.rs
Normal file
|
@ -0,0 +1,155 @@
|
|||
//! TokenStream implementation used by sysroot ABI
|
||||
|
||||
use tt::TokenTree;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct TokenStream<S> {
|
||||
pub(super) token_trees: Vec<TokenTree<S>>,
|
||||
}
|
||||
|
||||
impl<S> Default for TokenStream<S> {
|
||||
fn default() -> Self {
|
||||
Self { token_trees: vec![] }
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> TokenStream<S> {
|
||||
pub(crate) fn new() -> Self {
|
||||
TokenStream::default()
|
||||
}
|
||||
|
||||
pub(crate) fn with_subtree(subtree: tt::Subtree<S>) -> Self {
|
||||
if subtree.delimiter.kind != tt::DelimiterKind::Invisible {
|
||||
TokenStream { token_trees: vec![TokenTree::Subtree(subtree)] }
|
||||
} else {
|
||||
TokenStream { token_trees: subtree.token_trees.into_vec() }
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn into_subtree(self, call_site: S) -> tt::Subtree<S>
|
||||
where
|
||||
S: Copy,
|
||||
{
|
||||
tt::Subtree {
|
||||
delimiter: tt::Delimiter {
|
||||
open: call_site,
|
||||
close: call_site,
|
||||
kind: tt::DelimiterKind::Invisible,
|
||||
},
|
||||
token_trees: self.token_trees.into_boxed_slice(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn is_empty(&self) -> bool {
|
||||
self.token_trees.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a token stream containing a single token tree.
|
||||
impl<S> From<TokenTree<S>> for TokenStream<S> {
|
||||
fn from(tree: TokenTree<S>) -> TokenStream<S> {
|
||||
TokenStream { token_trees: vec![tree] }
|
||||
}
|
||||
}
|
||||
|
||||
/// Collects a number of token trees into a single stream.
|
||||
impl<S> FromIterator<TokenTree<S>> for TokenStream<S> {
|
||||
fn from_iter<I: IntoIterator<Item = TokenTree<S>>>(trees: I) -> Self {
|
||||
trees.into_iter().map(TokenStream::from).collect()
|
||||
}
|
||||
}
|
||||
|
||||
/// A "flattening" operation on token streams, collects token trees
|
||||
/// from multiple token streams into a single stream.
|
||||
impl<S> FromIterator<TokenStream<S>> for TokenStream<S> {
|
||||
fn from_iter<I: IntoIterator<Item = TokenStream<S>>>(streams: I) -> Self {
|
||||
let mut builder = TokenStreamBuilder::new();
|
||||
streams.into_iter().for_each(|stream| builder.push(stream));
|
||||
builder.build()
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Extend<TokenTree<S>> for TokenStream<S> {
|
||||
fn extend<I: IntoIterator<Item = TokenTree<S>>>(&mut self, trees: I) {
|
||||
self.extend(trees.into_iter().map(TokenStream::from));
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Extend<TokenStream<S>> for TokenStream<S> {
|
||||
fn extend<I: IntoIterator<Item = TokenStream<S>>>(&mut self, streams: I) {
|
||||
for item in streams {
|
||||
for tkn in item {
|
||||
match tkn {
|
||||
tt::TokenTree::Subtree(subtree)
|
||||
if subtree.delimiter.kind == tt::DelimiterKind::Invisible =>
|
||||
{
|
||||
self.token_trees.extend(subtree.token_trees.into_vec().into_iter());
|
||||
}
|
||||
_ => {
|
||||
self.token_trees.push(tkn);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) struct TokenStreamBuilder<S> {
|
||||
acc: TokenStream<S>,
|
||||
}
|
||||
|
||||
/// pub(super)lic implementation details for the `TokenStream` type, such as iterators.
|
||||
pub(super) mod token_stream {
|
||||
|
||||
use core::fmt;
|
||||
|
||||
use super::{TokenStream, TokenTree};
|
||||
|
||||
/// An iterator over `TokenStream`'s `TokenTree`s.
|
||||
/// The iteration is "shallow", e.g., the iterator doesn't recurse into delimited groups,
|
||||
/// and returns whole groups as token trees.
|
||||
impl<S> IntoIterator for TokenStream<S> {
|
||||
type Item = TokenTree<S>;
|
||||
type IntoIter = std::vec::IntoIter<TokenTree<S>>;
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
self.token_trees.into_iter()
|
||||
}
|
||||
}
|
||||
|
||||
/// Attempts to break the string into tokens and parse those tokens into a token stream.
|
||||
/// May fail for a number of reasons, for example, if the string contains unbalanced delimiters
|
||||
/// or characters not existing in the language.
|
||||
/// All tokens in the parsed stream get `Span::call_site()` spans.
|
||||
///
|
||||
/// NOTE: some errors may cause panics instead of returning `LexError`. We reserve the right to
|
||||
/// change these errors into `LexError`s later.
|
||||
impl<S: Copy + fmt::Debug> TokenStream<S> {
|
||||
pub(crate) fn from_str(src: &str, call_site: S) -> Result<TokenStream<S>, String> {
|
||||
let subtree =
|
||||
mbe::parse_to_token_tree_static_span(call_site, src).ok_or("lexing error")?;
|
||||
|
||||
Ok(TokenStream::with_subtree(subtree))
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> ToString for TokenStream<S> {
|
||||
fn to_string(&self) -> String {
|
||||
::tt::pretty(&self.token_trees)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> TokenStreamBuilder<S> {
|
||||
pub(super) fn new() -> TokenStreamBuilder<S> {
|
||||
TokenStreamBuilder { acc: TokenStream::new() }
|
||||
}
|
||||
|
||||
pub(super) fn push(&mut self, stream: TokenStream<S>) {
|
||||
self.acc.extend(stream.into_iter())
|
||||
}
|
||||
|
||||
pub(super) fn build(self) -> TokenStream<S> {
|
||||
self.acc
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue