2020-05-16 21:56:08 +02:00
//! Transcriber takes a template, like `fn $ident() {}`, a set of bindings like
2019-09-17 02:54:22 +03:00
//! `$ident => foo`, interpolates variables in the template, to get `fn foo() {}`
2020-08-12 18:26:51 +02:00
use syntax ::SmolStr ;
2019-09-17 02:06:14 +03:00
2020-03-13 13:03:31 +01:00
use super ::ExpandResult ;
2019-09-17 02:06:14 +03:00
use crate ::{
mbe_expander ::{ Binding , Bindings , Fragment } ,
2020-12-30 02:35:21 +08:00
parser ::{ Op , RepeatKind , Separator } ,
ExpandError , MetaTemplate ,
2019-09-17 02:06:14 +03:00
} ;
impl Bindings {
2019-09-17 02:54:22 +03:00
fn contains ( & self , name : & str ) -> bool {
2019-09-17 02:06:14 +03:00
self . inner . contains_key ( name )
}
2019-12-28 22:26:24 +01:00
fn get ( & self , name : & str , nesting : & mut [ NestingState ] ) -> Result < & Fragment , ExpandError > {
2019-09-17 02:06:14 +03:00
let mut b = self . inner . get ( name ) . ok_or_else ( | | {
ExpandError ::BindingError ( format! ( " could not find binding ` {} ` " , name ) )
} ) ? ;
2019-12-30 17:07:23 +01:00
for nesting_state in nesting . iter_mut ( ) {
nesting_state . hit = true ;
2019-09-17 02:06:14 +03:00
b = match b {
Binding ::Fragment ( _ ) = > break ,
2019-12-30 17:07:23 +01:00
Binding ::Nested ( bs ) = > bs . get ( nesting_state . idx ) . ok_or_else ( | | {
nesting_state . at_end = true ;
2019-09-17 02:06:14 +03:00
ExpandError ::BindingError ( format! ( " could not find nested binding ` {} ` " , name ) )
} ) ? ,
Binding ::Empty = > {
2019-12-30 17:07:23 +01:00
nesting_state . at_end = true ;
2019-09-17 02:06:14 +03:00
return Err ( ExpandError ::BindingError ( format! (
" could not find empty binding `{}` " ,
name
2019-12-28 22:26:24 +01:00
) ) ) ;
2019-09-17 02:06:14 +03:00
}
} ;
}
match b {
Binding ::Fragment ( it ) = > Ok ( it ) ,
Binding ::Nested ( _ ) = > Err ( ExpandError ::BindingError ( format! (
" expected simple binding, found nested binding `{}` " ,
name
) ) ) ,
Binding ::Empty = > Err ( ExpandError ::BindingError ( format! (
" expected simple binding, found empty binding `{}` " ,
name
) ) ) ,
}
}
}
2020-12-30 02:35:21 +08:00
pub ( super ) fn transcribe (
template : & MetaTemplate ,
bindings : & Bindings ,
) -> ExpandResult < tt ::Subtree > {
2019-12-13 21:53:34 +08:00
assert! ( template . delimiter = = None ) ;
2019-12-28 22:26:24 +01:00
let mut ctx = ExpandCtx { bindings : & bindings , nesting : Vec ::new ( ) } ;
2020-05-16 21:56:08 +02:00
let mut arena : Vec < tt ::TokenTree > = Vec ::new ( ) ;
expand_subtree ( & mut ctx , template , & mut arena )
2019-09-17 02:06:14 +03:00
}
2019-12-28 22:26:24 +01:00
#[ derive(Debug) ]
struct NestingState {
idx : usize ,
2019-12-30 17:07:23 +01:00
/// `hit` is currently necessary to tell `expand_repeat` if it should stop
/// because there is no variable in use by the current repetition
2019-12-28 22:26:24 +01:00
hit : bool ,
2019-12-30 17:07:23 +01:00
/// `at_end` is currently necessary to tell `expand_repeat` if it should stop
/// because there is no more value avaible for the current repetition
2019-12-28 22:26:24 +01:00
at_end : bool ,
}
2019-09-17 02:06:14 +03:00
#[ derive(Debug) ]
struct ExpandCtx < ' a > {
bindings : & ' a Bindings ,
2019-12-28 22:26:24 +01:00
nesting : Vec < NestingState > ,
2019-09-17 02:06:14 +03:00
}
2020-05-16 21:56:08 +02:00
fn expand_subtree (
ctx : & mut ExpandCtx ,
2020-12-30 02:35:21 +08:00
template : & MetaTemplate ,
2020-05-16 21:56:08 +02:00
arena : & mut Vec < tt ::TokenTree > ,
) -> ExpandResult < tt ::Subtree > {
// remember how many elements are in the arena now - when returning, we want to drain exactly how many elements we added. This way, the recursive uses of the arena get their own "view" of the arena, but will reuse the allocation
let start_elements = arena . len ( ) ;
2020-03-13 13:03:31 +01:00
let mut err = None ;
2020-12-30 02:35:21 +08:00
for op in template . iter ( ) {
2020-03-13 13:03:31 +01:00
let op = match op {
Ok ( op ) = > op ,
Err ( e ) = > {
2020-12-30 02:35:21 +08:00
err = Some ( e . clone ( ) ) ;
2020-03-13 13:03:31 +01:00
break ;
}
} ;
match op {
2020-12-30 02:35:21 +08:00
Op ::Leaf ( tt ) = > arena . push ( tt . clone ( ) . into ( ) ) ,
Op ::Subtree ( tt ) = > {
let ExpandResult { value : tt , err : e } = expand_subtree ( ctx , & tt , arena ) ;
2020-03-13 13:03:31 +01:00
err = err . or ( e ) ;
2020-05-16 21:56:08 +02:00
arena . push ( tt . into ( ) ) ;
2019-09-17 02:54:22 +03:00
}
2021-01-04 10:53:31 +08:00
Op ::Var { name , id , .. } = > {
let ExpandResult { value : fragment , err : e } = expand_var ( ctx , & name , * id ) ;
2020-03-13 13:03:31 +01:00
err = err . or ( e ) ;
2020-05-16 21:56:08 +02:00
push_fragment ( arena , fragment ) ;
2019-09-17 02:54:22 +03:00
}
Op ::Repeat { subtree , kind , separator } = > {
2020-11-26 16:04:23 +01:00
let ExpandResult { value : fragment , err : e } =
2020-12-30 02:35:21 +08:00
expand_repeat ( ctx , subtree , * kind , separator , arena ) ;
2020-03-13 13:03:31 +01:00
err = err . or ( e ) ;
2020-05-16 21:56:08 +02:00
push_fragment ( arena , fragment )
2019-09-17 02:54:22 +03:00
}
}
2019-09-17 02:06:14 +03:00
}
2020-05-16 21:56:08 +02:00
// drain the elements added in this instance of expand_subtree
let tts = arena . drain ( start_elements .. arena . len ( ) ) . collect ( ) ;
2020-11-26 16:04:23 +01:00
ExpandResult { value : tt ::Subtree { delimiter : template . delimiter , token_trees : tts } , err }
2019-09-17 02:06:14 +03:00
}
2021-01-04 10:53:31 +08:00
fn expand_var ( ctx : & mut ExpandCtx , v : & SmolStr , id : tt ::TokenId ) -> ExpandResult < Fragment > {
2021-01-08 14:00:16 +08:00
// We already handle $crate case in mbe parser
debug_assert! ( v ! = " crate " ) ;
if ! ctx . bindings . contains ( v ) {
2019-09-17 02:54:22 +03:00
// Note that it is possible to have a `$var` inside a macro which is not bound.
// For example:
// ```
// macro_rules! foo {
// ($a:ident, $b:ident, $c:tt) => {
// macro_rules! bar {
// ($bi:ident) => {
// fn $bi() -> u8 {$c}
// }
// }
// }
// ```
// We just treat it a normal tokens
let tt = tt ::Subtree {
2019-12-13 21:53:34 +08:00
delimiter : None ,
2019-09-17 02:54:22 +03:00
token_trees : vec ! [
2021-01-04 10:53:31 +08:00
tt ::Leaf ::from ( tt ::Punct { char : ' $ ' , spacing : tt ::Spacing ::Alone , id } ) . into ( ) ,
tt ::Leaf ::from ( tt ::Ident { text : v . clone ( ) , id } ) . into ( ) ,
2019-09-17 02:54:22 +03:00
] ,
}
. into ( ) ;
2020-03-16 12:22:10 +01:00
ExpandResult ::ok ( Fragment ::Tokens ( tt ) )
2019-09-17 02:54:22 +03:00
} else {
2020-03-13 13:03:31 +01:00
ctx . bindings . get ( & v , & mut ctx . nesting ) . map_or_else (
2020-11-26 16:04:23 +01:00
| e | ExpandResult { value : Fragment ::Tokens ( tt ::TokenTree ::empty ( ) ) , err : Some ( e ) } ,
2020-03-16 12:22:10 +01:00
| b | ExpandResult ::ok ( b . clone ( ) ) ,
2020-03-13 13:03:31 +01:00
)
}
2019-09-17 02:54:22 +03:00
}
2019-09-17 02:06:14 +03:00
2019-09-17 02:54:22 +03:00
fn expand_repeat (
ctx : & mut ExpandCtx ,
2020-12-30 02:35:21 +08:00
template : & MetaTemplate ,
2019-09-17 02:54:22 +03:00
kind : RepeatKind ,
2020-12-30 02:35:21 +08:00
separator : & Option < Separator > ,
2020-05-16 21:56:08 +02:00
arena : & mut Vec < tt ::TokenTree > ,
2020-03-13 13:03:31 +01:00
) -> ExpandResult < Fragment > {
2019-09-17 02:54:22 +03:00
let mut buf : Vec < tt ::TokenTree > = Vec ::new ( ) ;
2019-12-28 22:26:24 +01:00
ctx . nesting . push ( NestingState { idx : 0 , at_end : false , hit : false } ) ;
2019-09-17 02:54:22 +03:00
// Dirty hack to make macro-expansion terminate.
2020-03-13 13:03:31 +01:00
// This should be replaced by a proper macro-by-example implementation
2019-12-28 22:26:24 +01:00
let limit = 65536 ;
2019-09-17 02:54:22 +03:00
let mut has_seps = 0 ;
let mut counter = 0 ;
2019-12-28 22:26:24 +01:00
loop {
2020-11-26 16:04:23 +01:00
let ExpandResult { value : mut t , err : e } = expand_subtree ( ctx , template , arena ) ;
2019-12-28 22:26:24 +01:00
let nesting_state = ctx . nesting . last_mut ( ) . unwrap ( ) ;
if nesting_state . at_end | | ! nesting_state . hit {
2019-09-17 02:54:22 +03:00
break ;
}
2019-12-28 22:26:24 +01:00
nesting_state . idx + = 1 ;
nesting_state . hit = false ;
2019-09-17 02:54:22 +03:00
counter + = 1 ;
2019-12-28 22:26:24 +01:00
if counter = = limit {
2019-09-17 02:54:22 +03:00
log ::warn! (
" expand_tt excced in repeat pattern exceed limit => {:#?} \n {:#?} " ,
template ,
ctx
) ;
break ;
}
2020-03-13 13:03:31 +01:00
if e . is_some ( ) {
continue ;
}
2019-12-28 22:26:24 +01:00
t . delimiter = None ;
2019-09-17 02:54:22 +03:00
push_subtree ( & mut buf , t ) ;
if let Some ( ref sep ) = separator {
match sep {
Separator ::Ident ( ident ) = > {
has_seps = 1 ;
buf . push ( tt ::Leaf ::from ( ident . clone ( ) ) . into ( ) ) ;
}
Separator ::Literal ( lit ) = > {
has_seps = 1 ;
buf . push ( tt ::Leaf ::from ( lit . clone ( ) ) . into ( ) ) ;
2019-09-17 02:06:14 +03:00
}
2019-09-17 02:54:22 +03:00
Separator ::Puncts ( puncts ) = > {
has_seps = puncts . len ( ) ;
for punct in puncts {
buf . push ( tt ::Leaf ::from ( * punct ) . into ( ) ) ;
}
2019-09-17 02:06:14 +03:00
}
}
2019-09-17 02:54:22 +03:00
}
2019-09-17 02:06:14 +03:00
2019-09-17 02:54:22 +03:00
if RepeatKind ::ZeroOrOne = = kind {
break ;
}
}
2019-09-17 02:06:14 +03:00
2019-09-17 02:54:22 +03:00
ctx . nesting . pop ( ) . unwrap ( ) ;
for _ in 0 .. has_seps {
buf . pop ( ) ;
}
2019-09-17 02:06:14 +03:00
2019-09-17 02:54:22 +03:00
// Check if it is a single token subtree without any delimiter
// e.g {Delimiter:None> ['>'] /Delimiter:None>}
2019-12-13 21:53:34 +08:00
let tt = tt ::Subtree { delimiter : None , token_trees : buf } . into ( ) ;
2020-03-13 13:03:31 +01:00
if RepeatKind ::OneOrMore = = kind & & counter = = 0 {
2020-11-26 16:04:23 +01:00
return ExpandResult {
value : Fragment ::Tokens ( tt ) ,
err : Some ( ExpandError ::UnexpectedToken ) ,
} ;
2020-03-13 13:03:31 +01:00
}
2020-03-16 12:22:10 +01:00
ExpandResult ::ok ( Fragment ::Tokens ( tt ) )
2019-09-17 02:06:14 +03:00
}
fn push_fragment ( buf : & mut Vec < tt ::TokenTree > , fragment : Fragment ) {
match fragment {
Fragment ::Tokens ( tt ::TokenTree ::Subtree ( tt ) ) = > push_subtree ( buf , tt ) ,
Fragment ::Tokens ( tt ) | Fragment ::Ast ( tt ) = > buf . push ( tt ) ,
}
}
fn push_subtree ( buf : & mut Vec < tt ::TokenTree > , tt : tt ::Subtree ) {
match tt . delimiter {
2019-12-13 21:53:34 +08:00
None = > buf . extend ( tt . token_trees ) ,
2019-09-17 02:06:14 +03:00
_ = > buf . push ( tt . into ( ) ) ,
}
}