mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 05:45:12 +00:00
Merge #2205
2205: Implement bulitin line! macro r=matklad a=edwin0cheng This PR implements bulitin macro `line!` and add basic infra-structure for other bulitin macros: 1. Extend `MacroDefId` to support builtin macros 2. Add a `quote!` macro for simple quasi quoting. Note that for support others builtin macros, eager macro expansion have to be supported first, this PR not try to handle it. :) Co-authored-by: Edwin Cheng <edwin0cheng@gmail.com>
This commit is contained in:
commit
ef2a9aedb6
9 changed files with 458 additions and 22 deletions
|
@ -4810,3 +4810,22 @@ fn no_such_field_diagnostics() {
|
||||||
"###
|
"###
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn infer_builtin_macros_line() {
|
||||||
|
assert_snapshot!(
|
||||||
|
infer(r#"
|
||||||
|
#[rustc_builtin_macro]
|
||||||
|
macro_rules! line {() => {}}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let x = line!();
|
||||||
|
}
|
||||||
|
"#),
|
||||||
|
@r###"
|
||||||
|
![0; 1) '6': i32
|
||||||
|
[64; 88) '{ ...!(); }': ()
|
||||||
|
[74; 75) 'x': i32
|
||||||
|
"###
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
//! FIXME: write short doc here
|
//! FIXME: write short doc here
|
||||||
|
|
||||||
use hir_expand::{
|
use hir_expand::{
|
||||||
|
builtin_macro::find_builtin_macro,
|
||||||
name::{self, AsName, Name},
|
name::{self, AsName, Name},
|
||||||
HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroFileKind,
|
HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroDefKind, MacroFileKind,
|
||||||
};
|
};
|
||||||
use ra_cfg::CfgOptions;
|
use ra_cfg::CfgOptions;
|
||||||
use ra_db::{CrateId, FileId};
|
use ra_db::{CrateId, FileId};
|
||||||
|
@ -692,10 +693,30 @@ where
|
||||||
fn collect_macro(&mut self, mac: &raw::MacroData) {
|
fn collect_macro(&mut self, mac: &raw::MacroData) {
|
||||||
let ast_id = AstId::new(self.file_id, mac.ast_id);
|
let ast_id = AstId::new(self.file_id, mac.ast_id);
|
||||||
|
|
||||||
|
// Case 0: builtin macros
|
||||||
|
if mac.builtin {
|
||||||
|
if let Some(name) = &mac.name {
|
||||||
|
let krate = self.def_collector.def_map.krate;
|
||||||
|
if let Some(macro_id) = find_builtin_macro(name, krate, ast_id) {
|
||||||
|
self.def_collector.define_macro(
|
||||||
|
self.module_id,
|
||||||
|
name.clone(),
|
||||||
|
macro_id,
|
||||||
|
mac.export,
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Case 1: macro rules, define a macro in crate-global mutable scope
|
// Case 1: macro rules, define a macro in crate-global mutable scope
|
||||||
if is_macro_rules(&mac.path) {
|
if is_macro_rules(&mac.path) {
|
||||||
if let Some(name) = &mac.name {
|
if let Some(name) = &mac.name {
|
||||||
let macro_id = MacroDefId { ast_id, krate: self.def_collector.def_map.krate };
|
let macro_id = MacroDefId {
|
||||||
|
ast_id,
|
||||||
|
krate: self.def_collector.def_map.krate,
|
||||||
|
kind: MacroDefKind::Declarative,
|
||||||
|
};
|
||||||
self.def_collector.define_macro(self.module_id, name.clone(), macro_id, mac.export);
|
self.def_collector.define_macro(self.module_id, name.clone(), macro_id, mac.export);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -200,6 +200,7 @@ pub(super) struct MacroData {
|
||||||
pub(super) path: Path,
|
pub(super) path: Path,
|
||||||
pub(super) name: Option<Name>,
|
pub(super) name: Option<Name>,
|
||||||
pub(super) export: bool,
|
pub(super) export: bool,
|
||||||
|
pub(super) builtin: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct RawItemsCollector {
|
struct RawItemsCollector {
|
||||||
|
@ -367,7 +368,11 @@ impl RawItemsCollector {
|
||||||
// FIXME: cfg_attr
|
// FIXME: cfg_attr
|
||||||
let export = m.attrs().filter_map(|x| x.simple_name()).any(|name| name == "macro_export");
|
let export = m.attrs().filter_map(|x| x.simple_name()).any(|name| name == "macro_export");
|
||||||
|
|
||||||
let m = self.raw_items.macros.alloc(MacroData { ast_id, path, name, export });
|
// FIXME: cfg_attr
|
||||||
|
let builtin =
|
||||||
|
m.attrs().filter_map(|x| x.simple_name()).any(|name| name == "rustc_builtin_macro");
|
||||||
|
|
||||||
|
let m = self.raw_items.macros.alloc(MacroData { ast_id, path, name, export, builtin });
|
||||||
self.push_item(current_module, attrs, RawItemKind::Macro(m));
|
self.push_item(current_module, attrs, RawItemKind::Macro(m));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
80
crates/ra_hir_expand/src/builtin_macro.rs
Normal file
80
crates/ra_hir_expand/src/builtin_macro.rs
Normal file
|
@ -0,0 +1,80 @@
|
||||||
|
//! Builtin macro
|
||||||
|
use crate::db::AstDatabase;
|
||||||
|
use crate::{
|
||||||
|
ast::{self, AstNode},
|
||||||
|
name, AstId, CrateId, HirFileId, MacroCallId, MacroDefId, MacroDefKind, MacroFileKind,
|
||||||
|
TextUnit,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::quote;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
pub enum BuiltinExpander {
|
||||||
|
Line,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BuiltinExpander {
|
||||||
|
pub fn expand(
|
||||||
|
&self,
|
||||||
|
db: &dyn AstDatabase,
|
||||||
|
id: MacroCallId,
|
||||||
|
tt: &tt::Subtree,
|
||||||
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
||||||
|
match self {
|
||||||
|
BuiltinExpander::Line => line_expand(db, id, tt),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn find_builtin_macro(
|
||||||
|
ident: &name::Name,
|
||||||
|
krate: CrateId,
|
||||||
|
ast_id: AstId<ast::MacroCall>,
|
||||||
|
) -> Option<MacroDefId> {
|
||||||
|
// FIXME: Better registering method
|
||||||
|
if ident == &name::LINE_MACRO {
|
||||||
|
Some(MacroDefId { krate, ast_id, kind: MacroDefKind::BuiltIn(BuiltinExpander::Line) })
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_line_number(db: &dyn AstDatabase, file: HirFileId, pos: TextUnit) -> usize {
|
||||||
|
// FIXME: Use expansion info
|
||||||
|
let file_id = file.original_file(db);
|
||||||
|
let text = db.file_text(file_id);
|
||||||
|
let mut line_num = 1;
|
||||||
|
|
||||||
|
// Count line end
|
||||||
|
for (i, c) in text.chars().enumerate() {
|
||||||
|
if i == pos.to_usize() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if c == '\n' {
|
||||||
|
line_num += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
line_num
|
||||||
|
}
|
||||||
|
|
||||||
|
fn line_expand(
|
||||||
|
db: &dyn AstDatabase,
|
||||||
|
id: MacroCallId,
|
||||||
|
_tt: &tt::Subtree,
|
||||||
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
||||||
|
let loc = db.lookup_intern_macro(id);
|
||||||
|
let macro_call = loc.ast_id.to_node(db);
|
||||||
|
|
||||||
|
let arg = macro_call.token_tree().ok_or_else(|| mbe::ExpandError::UnexpectedToken)?;
|
||||||
|
let arg_start = arg.syntax().text_range().start();
|
||||||
|
|
||||||
|
let file = id.as_file(MacroFileKind::Expr);
|
||||||
|
let line_num = to_line_number(db, file, arg_start);
|
||||||
|
|
||||||
|
let expanded = quote! {
|
||||||
|
#line_num
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(expanded)
|
||||||
|
}
|
|
@ -9,10 +9,37 @@ use ra_prof::profile;
|
||||||
use ra_syntax::{AstNode, Parse, SyntaxNode};
|
use ra_syntax::{AstNode, Parse, SyntaxNode};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ast_id_map::AstIdMap, HirFileId, HirFileIdRepr, MacroCallId, MacroCallLoc, MacroDefId,
|
ast_id_map::AstIdMap, BuiltinExpander, HirFileId, HirFileIdRepr, MacroCallId, MacroCallLoc,
|
||||||
MacroFile, MacroFileKind,
|
MacroDefId, MacroDefKind, MacroFile, MacroFileKind,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||||
|
pub enum TokenExpander {
|
||||||
|
MacroRules(mbe::MacroRules),
|
||||||
|
Builtin(BuiltinExpander),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TokenExpander {
|
||||||
|
pub fn expand(
|
||||||
|
&self,
|
||||||
|
db: &dyn AstDatabase,
|
||||||
|
id: MacroCallId,
|
||||||
|
tt: &tt::Subtree,
|
||||||
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
||||||
|
match self {
|
||||||
|
TokenExpander::MacroRules(it) => it.expand(tt),
|
||||||
|
TokenExpander::Builtin(it) => it.expand(db, id, tt),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn shift(&self) -> u32 {
|
||||||
|
match self {
|
||||||
|
TokenExpander::MacroRules(it) => it.shift(),
|
||||||
|
TokenExpander::Builtin(_) => 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// FIXME: rename to ExpandDatabase
|
// FIXME: rename to ExpandDatabase
|
||||||
#[salsa::query_group(AstDatabaseStorage)]
|
#[salsa::query_group(AstDatabaseStorage)]
|
||||||
pub trait AstDatabase: SourceDatabase {
|
pub trait AstDatabase: SourceDatabase {
|
||||||
|
@ -24,7 +51,7 @@ pub trait AstDatabase: SourceDatabase {
|
||||||
#[salsa::interned]
|
#[salsa::interned]
|
||||||
fn intern_macro(&self, macro_call: MacroCallLoc) -> MacroCallId;
|
fn intern_macro(&self, macro_call: MacroCallLoc) -> MacroCallId;
|
||||||
fn macro_arg(&self, id: MacroCallId) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>>;
|
fn macro_arg(&self, id: MacroCallId) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>>;
|
||||||
fn macro_def(&self, id: MacroDefId) -> Option<Arc<(mbe::MacroRules, mbe::TokenMap)>>;
|
fn macro_def(&self, id: MacroDefId) -> Option<Arc<(TokenExpander, mbe::TokenMap)>>;
|
||||||
fn parse_macro(
|
fn parse_macro(
|
||||||
&self,
|
&self,
|
||||||
macro_file: MacroFile,
|
macro_file: MacroFile,
|
||||||
|
@ -41,7 +68,9 @@ pub(crate) fn ast_id_map(db: &dyn AstDatabase, file_id: HirFileId) -> Arc<AstIdM
|
||||||
pub(crate) fn macro_def(
|
pub(crate) fn macro_def(
|
||||||
db: &dyn AstDatabase,
|
db: &dyn AstDatabase,
|
||||||
id: MacroDefId,
|
id: MacroDefId,
|
||||||
) -> Option<Arc<(mbe::MacroRules, mbe::TokenMap)>> {
|
) -> Option<Arc<(TokenExpander, mbe::TokenMap)>> {
|
||||||
|
match id.kind {
|
||||||
|
MacroDefKind::Declarative => {
|
||||||
let macro_call = id.ast_id.to_node(db);
|
let macro_call = id.ast_id.to_node(db);
|
||||||
let arg = macro_call.token_tree()?;
|
let arg = macro_call.token_tree()?;
|
||||||
let (tt, tmap) = mbe::ast_to_token_tree(&arg).or_else(|| {
|
let (tt, tmap) = mbe::ast_to_token_tree(&arg).or_else(|| {
|
||||||
|
@ -52,7 +81,12 @@ pub(crate) fn macro_def(
|
||||||
log::warn!("fail on macro_def parse: {:#?}", tt);
|
log::warn!("fail on macro_def parse: {:#?}", tt);
|
||||||
None
|
None
|
||||||
})?;
|
})?;
|
||||||
Some(Arc::new((rules, tmap)))
|
Some(Arc::new((TokenExpander::MacroRules(rules), tmap)))
|
||||||
|
}
|
||||||
|
MacroDefKind::BuiltIn(expander) => {
|
||||||
|
Some(Arc::new((TokenExpander::Builtin(expander.clone()), mbe::TokenMap::default())))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn macro_arg(
|
pub(crate) fn macro_arg(
|
||||||
|
@ -74,7 +108,7 @@ pub(crate) fn macro_expand(
|
||||||
let macro_arg = db.macro_arg(id).ok_or("Fail to args in to tt::TokenTree")?;
|
let macro_arg = db.macro_arg(id).ok_or("Fail to args in to tt::TokenTree")?;
|
||||||
|
|
||||||
let macro_rules = db.macro_def(loc.def).ok_or("Fail to find macro definition")?;
|
let macro_rules = db.macro_def(loc.def).ok_or("Fail to find macro definition")?;
|
||||||
let tt = macro_rules.0.expand(¯o_arg.0).map_err(|err| format!("{:?}", err))?;
|
let tt = macro_rules.0.expand(db, id, ¯o_arg.0).map_err(|err| format!("{:?}", err))?;
|
||||||
// Set a hard limit for the expanded tt
|
// Set a hard limit for the expanded tt
|
||||||
let count = tt.count();
|
let count = tt.count();
|
||||||
if count > 65536 {
|
if count > 65536 {
|
||||||
|
|
|
@ -9,7 +9,7 @@ use crate::{
|
||||||
db::AstDatabase,
|
db::AstDatabase,
|
||||||
either::Either,
|
either::Either,
|
||||||
name::{AsName, Name},
|
name::{AsName, Name},
|
||||||
HirFileId, HirFileIdRepr,
|
HirFileId, HirFileIdRepr, MacroDefKind,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -24,7 +24,10 @@ impl Hygiene {
|
||||||
HirFileIdRepr::FileId(_) => None,
|
HirFileIdRepr::FileId(_) => None,
|
||||||
HirFileIdRepr::MacroFile(macro_file) => {
|
HirFileIdRepr::MacroFile(macro_file) => {
|
||||||
let loc = db.lookup_intern_macro(macro_file.macro_call_id);
|
let loc = db.lookup_intern_macro(macro_file.macro_call_id);
|
||||||
Some(loc.def.krate)
|
match loc.def.kind {
|
||||||
|
MacroDefKind::Declarative => Some(loc.def.krate),
|
||||||
|
MacroDefKind::BuiltIn(_) => None,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Hygiene { def_crate }
|
Hygiene { def_crate }
|
||||||
|
|
|
@ -10,6 +10,8 @@ pub mod either;
|
||||||
pub mod name;
|
pub mod name;
|
||||||
pub mod hygiene;
|
pub mod hygiene;
|
||||||
pub mod diagnostics;
|
pub mod diagnostics;
|
||||||
|
pub mod builtin_macro;
|
||||||
|
pub mod quote;
|
||||||
|
|
||||||
use std::hash::{Hash, Hasher};
|
use std::hash::{Hash, Hasher};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
@ -21,6 +23,7 @@ use ra_syntax::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::ast_id_map::FileAstId;
|
use crate::ast_id_map::FileAstId;
|
||||||
|
use crate::builtin_macro::BuiltinExpander;
|
||||||
|
|
||||||
/// Input to the analyzer is a set of files, where each file is identified by
|
/// Input to the analyzer is a set of files, where each file is identified by
|
||||||
/// `FileId` and contains source code. However, another source of source code in
|
/// `FileId` and contains source code. However, another source of source code in
|
||||||
|
@ -122,6 +125,13 @@ impl salsa::InternKey for MacroCallId {
|
||||||
pub struct MacroDefId {
|
pub struct MacroDefId {
|
||||||
pub krate: CrateId,
|
pub krate: CrateId,
|
||||||
pub ast_id: AstId<ast::MacroCall>,
|
pub ast_id: AstId<ast::MacroCall>,
|
||||||
|
pub kind: MacroDefKind,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
pub enum MacroDefKind {
|
||||||
|
Declarative,
|
||||||
|
BuiltIn(BuiltinExpander),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
|
@ -144,7 +154,7 @@ pub struct ExpansionInfo {
|
||||||
pub(crate) def_start: (HirFileId, TextUnit),
|
pub(crate) def_start: (HirFileId, TextUnit),
|
||||||
pub(crate) shift: u32,
|
pub(crate) shift: u32,
|
||||||
|
|
||||||
pub(crate) macro_def: Arc<(mbe::MacroRules, mbe::TokenMap)>,
|
pub(crate) macro_def: Arc<(db::TokenExpander, mbe::TokenMap)>,
|
||||||
pub(crate) macro_arg: Arc<(tt::Subtree, mbe::TokenMap)>,
|
pub(crate) macro_arg: Arc<(tt::Subtree, mbe::TokenMap)>,
|
||||||
pub(crate) exp_map: Arc<mbe::RevTokenMap>,
|
pub(crate) exp_map: Arc<mbe::RevTokenMap>,
|
||||||
}
|
}
|
||||||
|
|
|
@ -140,3 +140,6 @@ pub const RESULT_TYPE: Name = Name::new_inline_ascii(6, b"Result");
|
||||||
pub const OUTPUT_TYPE: Name = Name::new_inline_ascii(6, b"Output");
|
pub const OUTPUT_TYPE: Name = Name::new_inline_ascii(6, b"Output");
|
||||||
pub const TARGET_TYPE: Name = Name::new_inline_ascii(6, b"Target");
|
pub const TARGET_TYPE: Name = Name::new_inline_ascii(6, b"Target");
|
||||||
pub const BOX_TYPE: Name = Name::new_inline_ascii(3, b"Box");
|
pub const BOX_TYPE: Name = Name::new_inline_ascii(3, b"Box");
|
||||||
|
|
||||||
|
// Builtin Macros
|
||||||
|
pub const LINE_MACRO: Name = Name::new_inline_ascii(4, b"line");
|
||||||
|
|
261
crates/ra_hir_expand/src/quote.rs
Normal file
261
crates/ra_hir_expand/src/quote.rs
Normal file
|
@ -0,0 +1,261 @@
|
||||||
|
//! A simplified version of quote-crate like quasi quote macro
|
||||||
|
|
||||||
|
// A helper macro quote macro
|
||||||
|
// FIXME:
|
||||||
|
// 1. Not all puncts are handled
|
||||||
|
// 2. #()* pattern repetition not supported now
|
||||||
|
// * But we can do it manually, see `test_quote_derive_copy_hack`
|
||||||
|
#[doc(hidden)]
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! __quote {
|
||||||
|
() => {
|
||||||
|
Vec::<tt::TokenTree>::new()
|
||||||
|
};
|
||||||
|
|
||||||
|
( @SUBTREE $delim:ident $($tt:tt)* ) => {
|
||||||
|
{
|
||||||
|
let children = $crate::__quote!($($tt)*);
|
||||||
|
let subtree = tt::Subtree {
|
||||||
|
delimiter: tt::Delimiter::$delim,
|
||||||
|
token_trees: $crate::quote::IntoTt::to_tokens(children),
|
||||||
|
};
|
||||||
|
subtree
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
( @PUNCT $first:literal ) => {
|
||||||
|
{
|
||||||
|
vec![
|
||||||
|
tt::Leaf::Punct(tt::Punct {
|
||||||
|
char: $first,
|
||||||
|
spacing: tt::Spacing::Alone,
|
||||||
|
}).into()
|
||||||
|
]
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
( @PUNCT $first:literal, $sec:literal ) => {
|
||||||
|
{
|
||||||
|
vec![
|
||||||
|
tt::Leaf::Punct(tt::Punct {
|
||||||
|
char: $first,
|
||||||
|
spacing: tt::Spacing::Joint,
|
||||||
|
}).into(),
|
||||||
|
tt::Leaf::Punct(tt::Punct {
|
||||||
|
char: $sec,
|
||||||
|
spacing: tt::Spacing::Alone,
|
||||||
|
}).into()
|
||||||
|
]
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// hash variable
|
||||||
|
( # $first:ident $($tail:tt)* ) => {
|
||||||
|
{
|
||||||
|
let token = $crate::quote::ToTokenTree::to_token($first);
|
||||||
|
let mut tokens = vec![token.into()];
|
||||||
|
let mut tail_tokens = $crate::quote::IntoTt::to_tokens($crate::__quote!($($tail)*));
|
||||||
|
tokens.append(&mut tail_tokens);
|
||||||
|
tokens
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Brace
|
||||||
|
( { $($tt:tt)* } ) => { $crate::__quote!(@SUBTREE Brace $($tt)*) };
|
||||||
|
// Bracket
|
||||||
|
( [ $($tt:tt)* ] ) => { $crate::__quote!(@SUBTREE Bracket $($tt)*) };
|
||||||
|
// Parenthesis
|
||||||
|
( ( $($tt:tt)* ) ) => { $crate::__quote!(@SUBTREE Parenthesis $($tt)*) };
|
||||||
|
|
||||||
|
// Literal
|
||||||
|
( $tt:literal ) => { vec![$crate::quote::ToTokenTree::to_token($tt).into()] };
|
||||||
|
// Ident
|
||||||
|
( $tt:ident ) => {
|
||||||
|
vec![ {
|
||||||
|
tt::Leaf::Ident(tt::Ident {
|
||||||
|
text: stringify!($tt).into(),
|
||||||
|
id: tt::TokenId::unspecified(),
|
||||||
|
}).into()
|
||||||
|
}]
|
||||||
|
};
|
||||||
|
|
||||||
|
// Puncts
|
||||||
|
// FIXME: Not all puncts are handled
|
||||||
|
( -> ) => {$crate::__quote!(@PUNCT '-', '>')};
|
||||||
|
( & ) => {$crate::__quote!(@PUNCT '&')};
|
||||||
|
( , ) => {$crate::__quote!(@PUNCT ',')};
|
||||||
|
( : ) => {$crate::__quote!(@PUNCT ':')};
|
||||||
|
( . ) => {$crate::__quote!(@PUNCT '.')};
|
||||||
|
|
||||||
|
( $first:tt $($tail:tt)+ ) => {
|
||||||
|
{
|
||||||
|
let mut tokens = $crate::quote::IntoTt::to_tokens($crate::__quote!($first));
|
||||||
|
let mut tail_tokens = $crate::quote::IntoTt::to_tokens($crate::__quote!($($tail)*));
|
||||||
|
|
||||||
|
tokens.append(&mut tail_tokens);
|
||||||
|
tokens
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// FIXME:
|
||||||
|
/// It probably should implement in proc-macro
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! quote {
|
||||||
|
( $($tt:tt)* ) => {
|
||||||
|
$crate::quote::IntoTt::to_subtree($crate::__quote!($($tt)*))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) trait IntoTt {
|
||||||
|
fn to_subtree(self) -> tt::Subtree;
|
||||||
|
fn to_tokens(self) -> Vec<tt::TokenTree>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IntoTt for Vec<tt::TokenTree> {
|
||||||
|
fn to_subtree(self) -> tt::Subtree {
|
||||||
|
tt::Subtree { delimiter: tt::Delimiter::None, token_trees: self }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_tokens(self) -> Vec<tt::TokenTree> {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IntoTt for tt::Subtree {
|
||||||
|
fn to_subtree(self) -> tt::Subtree {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_tokens(self) -> Vec<tt::TokenTree> {
|
||||||
|
vec![tt::TokenTree::Subtree(self)]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) trait ToTokenTree {
|
||||||
|
fn to_token(self) -> tt::TokenTree;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ToTokenTree for tt::TokenTree {
|
||||||
|
fn to_token(self) -> tt::TokenTree {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ToTokenTree for tt::Subtree {
|
||||||
|
fn to_token(self) -> tt::TokenTree {
|
||||||
|
self.into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! impl_to_to_tokentrees {
|
||||||
|
($($ty:ty => $this:ident $im:block);*) => {
|
||||||
|
$(
|
||||||
|
impl ToTokenTree for $ty {
|
||||||
|
fn to_token($this) -> tt::TokenTree {
|
||||||
|
let leaf: tt::Leaf = $im.into();
|
||||||
|
leaf.into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ToTokenTree for &$ty {
|
||||||
|
fn to_token($this) -> tt::TokenTree {
|
||||||
|
let leaf: tt::Leaf = $im.clone().into();
|
||||||
|
leaf.into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)*
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl_to_to_tokentrees! {
|
||||||
|
u32 => self { tt::Literal{text: self.to_string().into()} };
|
||||||
|
usize => self { tt::Literal{text: self.to_string().into()}};
|
||||||
|
i32 => self { tt::Literal{text: self.to_string().into()}};
|
||||||
|
&str => self { tt::Literal{text: self.to_string().into()}};
|
||||||
|
String => self { tt::Literal{text: self.into()}};
|
||||||
|
tt::Leaf => self { self };
|
||||||
|
tt::Literal => self { self };
|
||||||
|
tt::Ident => self { self };
|
||||||
|
tt::Punct => self { self }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
#[test]
|
||||||
|
fn test_quote_delimiters() {
|
||||||
|
assert_eq!(quote!({}).to_string(), "{}");
|
||||||
|
assert_eq!(quote!(()).to_string(), "()");
|
||||||
|
assert_eq!(quote!([]).to_string(), "[]");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_quote_idents() {
|
||||||
|
assert_eq!(quote!(32).to_string(), "32");
|
||||||
|
assert_eq!(quote!(struct).to_string(), "struct");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_quote_hash_simple_literal() {
|
||||||
|
let a = 20;
|
||||||
|
assert_eq!(quote!(#a).to_string(), "20");
|
||||||
|
let s: String = "hello".into();
|
||||||
|
assert_eq!(quote!(#s).to_string(), "hello");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn mk_ident(name: &str) -> tt::Ident {
|
||||||
|
tt::Ident { text: name.into(), id: tt::TokenId::unspecified() }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_quote_hash_token_tree() {
|
||||||
|
let a = mk_ident("hello");
|
||||||
|
|
||||||
|
let quoted = quote!(#a);
|
||||||
|
assert_eq!(quoted.to_string(), "hello");
|
||||||
|
let t = format!("{:?}", quoted);
|
||||||
|
assert_eq!(t, "Subtree { delimiter: None, token_trees: [Leaf(Ident(Ident { text: \"hello\", id: TokenId(4294967295) }))] }");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_quote_simple_derive_copy() {
|
||||||
|
let name = mk_ident("Foo");
|
||||||
|
|
||||||
|
let quoted = quote! {
|
||||||
|
impl Clone for #name {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
Self {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(quoted.to_string(), "impl Clone for Foo {fn clone (& self) -> Self {Self {}}}");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_quote_derive_copy_hack() {
|
||||||
|
// Assume the given struct is:
|
||||||
|
// struct Foo {
|
||||||
|
// name: String,
|
||||||
|
// id: u32,
|
||||||
|
// }
|
||||||
|
let struct_name = mk_ident("Foo");
|
||||||
|
let fields = [mk_ident("name"), mk_ident("id")];
|
||||||
|
let fields = fields
|
||||||
|
.into_iter()
|
||||||
|
.map(|it| quote!(#it: self.#it.clone(), ).token_trees.clone())
|
||||||
|
.flatten();
|
||||||
|
|
||||||
|
let list = tt::Subtree { delimiter: tt::Delimiter::Brace, token_trees: fields.collect() };
|
||||||
|
|
||||||
|
let quoted = quote! {
|
||||||
|
impl Clone for #struct_name {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
Self #list
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(quoted.to_string(), "impl Clone for Foo {fn clone (& self) -> Self {Self {name : self . name . clone () , id : self . id . clone () ,}}}");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue