This commit is contained in:
Aleksey Kladov 2019-01-31 21:43:54 +03:00
parent 2d1f0b105d
commit a16f6bb27d
3 changed files with 42 additions and 40 deletions

View file

@ -1,13 +1,13 @@
use rustc_hash::FxHashMap;
use ra_syntax::SmolStr;
use crate::{self as mbe, tt_cursor::TtCursor};
use crate::tt_cursor::TtCursor;
pub fn exapnd(rules: &mbe::MacroRules, input: &tt::Subtree) -> Option<tt::Subtree> {
pub fn exapnd(rules: &crate::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> {
fn expand_rule(rule: &crate::Rule, input: &tt::Subtree) -> Option<tt::Subtree> {
let mut input = TtCursor::new(input);
let bindings = match_lhs(&rule.lhs, &mut input)?;
expand_subtree(&rule.rhs, &bindings, &mut Vec::new())
@ -52,12 +52,12 @@ impl Bindings {
}
}
fn match_lhs(pattern: &mbe::Subtree, input: &mut TtCursor) -> Option<Bindings> {
fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Option<Bindings> {
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 }) => {
crate::TokenTree::Leaf(leaf) => match leaf {
crate::Leaf::Var(crate::Var { text, kind }) => {
let kind = kind.clone()?;
match kind.as_str() {
"ident" => {
@ -70,14 +70,14 @@ fn match_lhs(pattern: &mbe::Subtree, input: &mut TtCursor) -> Option<Bindings> {
_ => return None,
}
}
mbe::Leaf::Punct(punct) => {
crate::Leaf::Punct(punct) => {
if input.eat_punct()? != punct {
return None;
}
}
_ => return None,
},
mbe::TokenTree::Repeat(mbe::Repeat {
crate::TokenTree::Repeat(crate::Repeat {
subtree,
kind: _,
separator,
@ -114,7 +114,7 @@ impl_froms! (Foo: Bar, Baz)
*/
fn expand_subtree(
template: &mbe::Subtree,
template: &crate::Subtree,
bindings: &Bindings,
nesting: &mut Vec<usize>,
) -> Option<tt::Subtree> {
@ -131,13 +131,13 @@ fn expand_subtree(
}
fn expand_tt(
template: &mbe::TokenTree,
template: &crate::TokenTree,
bindings: &Bindings,
nesting: &mut Vec<usize>,
) -> Option<tt::TokenTree> {
let res: tt::TokenTree = match template {
mbe::TokenTree::Subtree(subtree) => expand_subtree(subtree, bindings, nesting)?.into(),
mbe::TokenTree::Repeat(repeat) => {
crate::TokenTree::Subtree(subtree) => expand_subtree(subtree, bindings, nesting)?.into(),
crate::TokenTree::Repeat(repeat) => {
let mut token_trees = Vec::new();
nesting.push(0);
while let Some(t) = expand_subtree(&repeat.subtree, bindings, nesting) {
@ -152,14 +152,14 @@ fn expand_tt(
}
.into()
}
mbe::TokenTree::Leaf(leaf) => match leaf {
mbe::Leaf::Ident(ident) => tt::Leaf::from(tt::Ident {
crate::TokenTree::Leaf(leaf) => match leaf {
crate::Leaf::Ident(ident) => tt::Leaf::from(tt::Ident {
text: ident.text.clone(),
})
.into(),
mbe::Leaf::Punct(punct) => tt::Leaf::from(punct.clone()).into(),
mbe::Leaf::Var(v) => bindings.get(&v.text, nesting)?.clone(),
mbe::Leaf::Literal(l) => tt::Leaf::from(tt::Literal {
crate::Leaf::Punct(punct) => tt::Leaf::from(punct.clone()).into(),
crate::Leaf::Var(v) => bindings.get(&v.text, nesting)?.clone(),
crate::Leaf::Literal(l) => tt::Leaf::from(tt::Literal {
text: l.text.clone(),
})
.into(),

View file

@ -1,58 +1,59 @@
use crate::{self as mbe, tt_cursor::TtCursor};
use crate::tt_cursor::TtCursor;
/// This module parses a raw `tt::TokenStream` into macro-by-example token
/// stream. This is a *mostly* identify function, expect for handling of
/// `$var:tt_kind` and `$(repeat),*` constructs.
pub fn parse(tt: &tt::Subtree) -> Option<mbe::MacroRules> {
pub fn parse(tt: &tt::Subtree) -> Option<crate::MacroRules> {
let mut parser = TtCursor::new(tt);
let mut rules = Vec::new();
while !parser.is_eof() {
rules.push(parse_rule(&mut parser)?)
}
Some(mbe::MacroRules { rules })
Some(crate::MacroRules { rules })
}
fn parse_rule(p: &mut TtCursor) -> Option<mbe::Rule> {
fn parse_rule(p: &mut TtCursor) -> Option<crate::Rule> {
let lhs = parse_subtree(p.eat_subtree()?)?;
p.expect_char('=')?;
p.expect_char('>')?;
let rhs = parse_subtree(p.eat_subtree()?)?;
Some(mbe::Rule { lhs, rhs })
let mut rhs = parse_subtree(p.eat_subtree()?)?;
rhs.delimiter = crate::Delimiter::None;
Some(crate::Rule { lhs, rhs })
}
fn parse_subtree(tt: &tt::Subtree) -> Option<mbe::Subtree> {
fn parse_subtree(tt: &tt::Subtree) -> Option<crate::Subtree> {
let mut token_trees = Vec::new();
let mut p = TtCursor::new(tt);
while let Some(tt) = p.eat() {
let child: mbe::TokenTree = match tt {
let child: crate::TokenTree = match tt {
tt::TokenTree::Leaf(leaf) => match leaf {
tt::Leaf::Punct(tt::Punct { char: '$', .. }) => {
if p.at_ident().is_some() {
mbe::Leaf::from(parse_var(&mut p)?).into()
crate::Leaf::from(parse_var(&mut p)?).into()
} else {
parse_repeat(&mut p)?.into()
}
}
tt::Leaf::Punct(punct) => mbe::Leaf::from(*punct).into(),
tt::Leaf::Punct(punct) => crate::Leaf::from(*punct).into(),
tt::Leaf::Ident(tt::Ident { text }) => {
mbe::Leaf::from(mbe::Ident { text: text.clone() }).into()
crate::Leaf::from(crate::Ident { text: text.clone() }).into()
}
tt::Leaf::Literal(tt::Literal { text }) => {
mbe::Leaf::from(mbe::Literal { text: text.clone() }).into()
crate::Leaf::from(crate::Literal { text: text.clone() }).into()
}
},
tt::TokenTree::Subtree(subtree) => parse_subtree(&subtree)?.into(),
};
token_trees.push(child);
}
Some(mbe::Subtree {
Some(crate::Subtree {
token_trees,
delimiter: tt.delimiter,
})
}
fn parse_var(p: &mut TtCursor) -> Option<mbe::Var> {
fn parse_var(p: &mut TtCursor) -> Option<crate::Var> {
let ident = p.eat_ident().unwrap();
let text = ident.text.clone();
let kind = if p.at_char(':') {
@ -66,12 +67,13 @@ fn parse_var(p: &mut TtCursor) -> Option<mbe::Var> {
} else {
None
};
Some(mbe::Var { text, kind })
Some(crate::Var { text, kind })
}
fn parse_repeat(p: &mut TtCursor) -> Option<mbe::Repeat> {
fn parse_repeat(p: &mut TtCursor) -> Option<crate::Repeat> {
let subtree = p.eat_subtree().unwrap();
let subtree = parse_subtree(subtree)?;
let mut subtree = parse_subtree(subtree)?;
subtree.delimiter = crate::Delimiter::None;
let sep = p.eat_punct()?;
let (separator, rep) = match sep.char {
'*' | '+' | '?' => (None, sep.char),
@ -79,13 +81,13 @@ fn parse_repeat(p: &mut TtCursor) -> Option<mbe::Repeat> {
};
let kind = match rep {
'*' => mbe::RepeatKind::ZeroOrMore,
'+' => mbe::RepeatKind::OneOrMore,
'?' => mbe::RepeatKind::ZeroOrOne,
'*' => crate::RepeatKind::ZeroOrMore,
'+' => crate::RepeatKind::OneOrMore,
'?' => crate::RepeatKind::ZeroOrOne,
_ => return None,
};
p.bump();
Some(mbe::Repeat {
Some(crate::Repeat {
subtree,
kind,
separator,

View file

@ -107,7 +107,7 @@ impl_froms!(TokenTree: Leaf, Subtree);
let expansion = crate::exapnd(&mbe, &invocation_tt).unwrap();
assert_eq!(
expansion.to_string(),
"{(impl From < Leaf > for TokenTree {fn from (it : Leaf) -> TokenTree {TokenTree :: Leaf (it)}}) \
(impl From < Subtree > for TokenTree {fn from (it : Subtree) -> TokenTree {TokenTree :: Subtree (it)}})}"
"impl From < Leaf > for TokenTree {fn from (it : Leaf) -> TokenTree {TokenTree :: Leaf (it)}} \
impl From < Subtree > for TokenTree {fn from (it : Subtree) -> TokenTree {TokenTree :: Subtree (it)}}"
)
}