mirror of
https://gitee.com/openharmony/third_party_rust_quote
synced 2024-11-23 23:29:43 +00:00
Reduce surface area of Tokens
This commit is contained in:
parent
1454aee700
commit
0625b2fe07
@ -2,8 +2,7 @@ use super::ToTokens;
|
||||
use std::fmt::{self, Debug, Display};
|
||||
|
||||
use proc_macro;
|
||||
use proc_macro2::{Span, Term, TokenNode, TokenStream, TokenTree};
|
||||
use proc_macro2::Delimiter;
|
||||
use proc_macro2::{TokenStream, TokenTree};
|
||||
|
||||
/// Tokens produced by a `quote!(...)` invocation.
|
||||
#[derive(Clone)]
|
||||
@ -27,39 +26,6 @@ impl Tokens {
|
||||
self.tts.push(token.into());
|
||||
}
|
||||
|
||||
/// Add `tokens` into `self`.
|
||||
pub fn append_tokens<T: ToTokens>(&mut self, tokens: T) {
|
||||
tokens.to_tokens(self)
|
||||
}
|
||||
|
||||
/// Add the symbol specified to this list of tokens.
|
||||
pub fn append_sym(&mut self, sym: &str, span: Span) {
|
||||
self.append(TokenTree {
|
||||
span: span,
|
||||
kind: TokenNode::Term(Term::intern(sym)),
|
||||
});
|
||||
}
|
||||
|
||||
pub fn append_delimited<F, R>(&mut self, delim: &str, span: Span, f: F) -> R
|
||||
where
|
||||
F: FnOnce(&mut Tokens) -> R,
|
||||
{
|
||||
let delim = match delim {
|
||||
"(" => Delimiter::Parenthesis,
|
||||
"[" => Delimiter::Bracket,
|
||||
"{" => Delimiter::Brace,
|
||||
" " => Delimiter::None,
|
||||
_ => panic!("unknown delimiter: {}", delim),
|
||||
};
|
||||
let mut child = Tokens::new();
|
||||
let ret = f(&mut child);
|
||||
self.append(TokenTree {
|
||||
span: span,
|
||||
kind: TokenNode::Group(delim, child.into()),
|
||||
});
|
||||
ret
|
||||
}
|
||||
|
||||
/// For use by `ToTokens` implementations.
|
||||
///
|
||||
/// ```
|
||||
|
@ -40,16 +40,6 @@ fn test_quote_impl() {
|
||||
assert_eq!(expected, tokens.to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_append_tokens() {
|
||||
let mut tokens = quote!(let x =);
|
||||
tokens.append_tokens(quote!("Hello World!";));
|
||||
|
||||
let expected = "let x = \"Hello World!\" ;";
|
||||
|
||||
assert_eq!(expected, tokens.to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_substitution() {
|
||||
let x = X;
|
||||
|
Loading…
Reference in New Issue
Block a user