rust/crates/ra_macros/src/mbe_expander.rs

67 lines
1.5 KiB
Rust
Raw Normal View History

2019-01-31 04:59:25 -06:00
use rustc_hash::FxHashMap;
use smol_str::SmolStr;
2019-01-31 04:49:57 -06:00
use crate::{mbe, tt};
2019-01-31 04:59:25 -06:00
pub fn exapnd(rules: &mbe::MacroRules, input: &tt::Subtree) -> Option<tt::Subtree> {
rules.rules.iter().find_map(|it| expand_rule(it, input))
}
fn expand_rule(rule: &mbe::Rule, input: &tt::Subtree) -> Option<tt::Subtree> {
2019-01-31 04:59:39 -06:00
let bindings = match_lhs(&rule.lhs, input)?;
2019-01-31 04:59:25 -06:00
expand_rhs(&rule.rhs, &bindings)
}
#[derive(Debug, Default)]
struct Bindings {
2019-01-31 06:22:55 -06:00
inner: FxHashMap<SmolStr, Binding>,
2019-01-31 04:59:25 -06:00
}
2019-01-31 06:22:55 -06:00
#[derive(Debug)]
enum Binding {
Simple(tt::TokenTree),
Nested(Vec<Binding>),
}
/*
macro_rules! impl_froms {
($e:ident: $($v:ident),*) => {
$(
impl From<$v> for $e {
fn from(it: $v) -> $e {
$e::$v(it)
}
}
)*
}
}
impl_froms! (Foo: Bar, Baz)
*/
2019-01-31 04:59:39 -06:00
fn match_lhs(pattern: &mbe::Subtree, input: &tt::Subtree) -> Option<Bindings> {
2019-01-31 06:22:55 -06:00
let mut res = Bindings::default();
for pat in pattern.token_trees.iter() {
match pat {
mbe::TokenTree::Leaf(leaf) => match leaf {
mbe::Leaf::Var(mbe::Var { text, kind }) => {
let kind = kind.clone()?;
match kind.as_str() {
"ident" => (),
_ => return None,
}
}
_ => return None,
},
_ => {}
}
}
Some(res)
2019-01-31 04:59:25 -06:00
}
2019-01-31 04:59:39 -06:00
fn expand_rhs(template: &mbe::Subtree, bindings: &Bindings) -> Option<tt::Subtree> {
2019-01-31 04:59:25 -06:00
None
2019-01-31 04:49:57 -06:00
}