mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 12:54:58 +00:00
cleanup
This commit is contained in:
parent
2d1f0b105d
commit
a16f6bb27d
3 changed files with 42 additions and 40 deletions
|
@ -1,13 +1,13 @@
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
use ra_syntax::SmolStr;
|
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))
|
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 mut input = TtCursor::new(input);
|
||||||
let bindings = match_lhs(&rule.lhs, &mut input)?;
|
let bindings = match_lhs(&rule.lhs, &mut input)?;
|
||||||
expand_subtree(&rule.rhs, &bindings, &mut Vec::new())
|
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();
|
let mut res = Bindings::default();
|
||||||
for pat in pattern.token_trees.iter() {
|
for pat in pattern.token_trees.iter() {
|
||||||
match pat {
|
match pat {
|
||||||
mbe::TokenTree::Leaf(leaf) => match leaf {
|
crate::TokenTree::Leaf(leaf) => match leaf {
|
||||||
mbe::Leaf::Var(mbe::Var { text, kind }) => {
|
crate::Leaf::Var(crate::Var { text, kind }) => {
|
||||||
let kind = kind.clone()?;
|
let kind = kind.clone()?;
|
||||||
match kind.as_str() {
|
match kind.as_str() {
|
||||||
"ident" => {
|
"ident" => {
|
||||||
|
@ -70,14 +70,14 @@ fn match_lhs(pattern: &mbe::Subtree, input: &mut TtCursor) -> Option<Bindings> {
|
||||||
_ => return None,
|
_ => return None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mbe::Leaf::Punct(punct) => {
|
crate::Leaf::Punct(punct) => {
|
||||||
if input.eat_punct()? != punct {
|
if input.eat_punct()? != punct {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => return None,
|
_ => return None,
|
||||||
},
|
},
|
||||||
mbe::TokenTree::Repeat(mbe::Repeat {
|
crate::TokenTree::Repeat(crate::Repeat {
|
||||||
subtree,
|
subtree,
|
||||||
kind: _,
|
kind: _,
|
||||||
separator,
|
separator,
|
||||||
|
@ -114,7 +114,7 @@ impl_froms! (Foo: Bar, Baz)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
fn expand_subtree(
|
fn expand_subtree(
|
||||||
template: &mbe::Subtree,
|
template: &crate::Subtree,
|
||||||
bindings: &Bindings,
|
bindings: &Bindings,
|
||||||
nesting: &mut Vec<usize>,
|
nesting: &mut Vec<usize>,
|
||||||
) -> Option<tt::Subtree> {
|
) -> Option<tt::Subtree> {
|
||||||
|
@ -131,13 +131,13 @@ fn expand_subtree(
|
||||||
}
|
}
|
||||||
|
|
||||||
fn expand_tt(
|
fn expand_tt(
|
||||||
template: &mbe::TokenTree,
|
template: &crate::TokenTree,
|
||||||
bindings: &Bindings,
|
bindings: &Bindings,
|
||||||
nesting: &mut Vec<usize>,
|
nesting: &mut Vec<usize>,
|
||||||
) -> Option<tt::TokenTree> {
|
) -> Option<tt::TokenTree> {
|
||||||
let res: tt::TokenTree = match template {
|
let res: tt::TokenTree = match template {
|
||||||
mbe::TokenTree::Subtree(subtree) => expand_subtree(subtree, bindings, nesting)?.into(),
|
crate::TokenTree::Subtree(subtree) => expand_subtree(subtree, bindings, nesting)?.into(),
|
||||||
mbe::TokenTree::Repeat(repeat) => {
|
crate::TokenTree::Repeat(repeat) => {
|
||||||
let mut token_trees = Vec::new();
|
let mut token_trees = Vec::new();
|
||||||
nesting.push(0);
|
nesting.push(0);
|
||||||
while let Some(t) = expand_subtree(&repeat.subtree, bindings, nesting) {
|
while let Some(t) = expand_subtree(&repeat.subtree, bindings, nesting) {
|
||||||
|
@ -152,14 +152,14 @@ fn expand_tt(
|
||||||
}
|
}
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
mbe::TokenTree::Leaf(leaf) => match leaf {
|
crate::TokenTree::Leaf(leaf) => match leaf {
|
||||||
mbe::Leaf::Ident(ident) => tt::Leaf::from(tt::Ident {
|
crate::Leaf::Ident(ident) => tt::Leaf::from(tt::Ident {
|
||||||
text: ident.text.clone(),
|
text: ident.text.clone(),
|
||||||
})
|
})
|
||||||
.into(),
|
.into(),
|
||||||
mbe::Leaf::Punct(punct) => tt::Leaf::from(punct.clone()).into(),
|
crate::Leaf::Punct(punct) => tt::Leaf::from(punct.clone()).into(),
|
||||||
mbe::Leaf::Var(v) => bindings.get(&v.text, nesting)?.clone(),
|
crate::Leaf::Var(v) => bindings.get(&v.text, nesting)?.clone(),
|
||||||
mbe::Leaf::Literal(l) => tt::Leaf::from(tt::Literal {
|
crate::Leaf::Literal(l) => tt::Leaf::from(tt::Literal {
|
||||||
text: l.text.clone(),
|
text: l.text.clone(),
|
||||||
})
|
})
|
||||||
.into(),
|
.into(),
|
||||||
|
|
|
@ -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
|
/// This module parses a raw `tt::TokenStream` into macro-by-example token
|
||||||
/// stream. This is a *mostly* identify function, expect for handling of
|
/// stream. This is a *mostly* identify function, expect for handling of
|
||||||
/// `$var:tt_kind` and `$(repeat),*` constructs.
|
/// `$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 parser = TtCursor::new(tt);
|
||||||
let mut rules = Vec::new();
|
let mut rules = Vec::new();
|
||||||
while !parser.is_eof() {
|
while !parser.is_eof() {
|
||||||
rules.push(parse_rule(&mut parser)?)
|
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()?)?;
|
let lhs = parse_subtree(p.eat_subtree()?)?;
|
||||||
p.expect_char('=')?;
|
p.expect_char('=')?;
|
||||||
p.expect_char('>')?;
|
p.expect_char('>')?;
|
||||||
let rhs = parse_subtree(p.eat_subtree()?)?;
|
let mut rhs = parse_subtree(p.eat_subtree()?)?;
|
||||||
Some(mbe::Rule { lhs, rhs })
|
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 token_trees = Vec::new();
|
||||||
let mut p = TtCursor::new(tt);
|
let mut p = TtCursor::new(tt);
|
||||||
while let Some(tt) = p.eat() {
|
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::TokenTree::Leaf(leaf) => match leaf {
|
||||||
tt::Leaf::Punct(tt::Punct { char: '$', .. }) => {
|
tt::Leaf::Punct(tt::Punct { char: '$', .. }) => {
|
||||||
if p.at_ident().is_some() {
|
if p.at_ident().is_some() {
|
||||||
mbe::Leaf::from(parse_var(&mut p)?).into()
|
crate::Leaf::from(parse_var(&mut p)?).into()
|
||||||
} else {
|
} else {
|
||||||
parse_repeat(&mut p)?.into()
|
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 }) => {
|
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 }) => {
|
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(),
|
tt::TokenTree::Subtree(subtree) => parse_subtree(&subtree)?.into(),
|
||||||
};
|
};
|
||||||
token_trees.push(child);
|
token_trees.push(child);
|
||||||
}
|
}
|
||||||
Some(mbe::Subtree {
|
Some(crate::Subtree {
|
||||||
token_trees,
|
token_trees,
|
||||||
delimiter: tt.delimiter,
|
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 ident = p.eat_ident().unwrap();
|
||||||
let text = ident.text.clone();
|
let text = ident.text.clone();
|
||||||
let kind = if p.at_char(':') {
|
let kind = if p.at_char(':') {
|
||||||
|
@ -66,12 +67,13 @@ fn parse_var(p: &mut TtCursor) -> Option<mbe::Var> {
|
||||||
} else {
|
} else {
|
||||||
None
|
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 = 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 sep = p.eat_punct()?;
|
||||||
let (separator, rep) = match sep.char {
|
let (separator, rep) = match sep.char {
|
||||||
'*' | '+' | '?' => (None, sep.char),
|
'*' | '+' | '?' => (None, sep.char),
|
||||||
|
@ -79,13 +81,13 @@ fn parse_repeat(p: &mut TtCursor) -> Option<mbe::Repeat> {
|
||||||
};
|
};
|
||||||
|
|
||||||
let kind = match rep {
|
let kind = match rep {
|
||||||
'*' => mbe::RepeatKind::ZeroOrMore,
|
'*' => crate::RepeatKind::ZeroOrMore,
|
||||||
'+' => mbe::RepeatKind::OneOrMore,
|
'+' => crate::RepeatKind::OneOrMore,
|
||||||
'?' => mbe::RepeatKind::ZeroOrOne,
|
'?' => crate::RepeatKind::ZeroOrOne,
|
||||||
_ => return None,
|
_ => return None,
|
||||||
};
|
};
|
||||||
p.bump();
|
p.bump();
|
||||||
Some(mbe::Repeat {
|
Some(crate::Repeat {
|
||||||
subtree,
|
subtree,
|
||||||
kind,
|
kind,
|
||||||
separator,
|
separator,
|
||||||
|
|
|
@ -107,7 +107,7 @@ impl_froms!(TokenTree: Leaf, Subtree);
|
||||||
let expansion = crate::exapnd(&mbe, &invocation_tt).unwrap();
|
let expansion = crate::exapnd(&mbe, &invocation_tt).unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
expansion.to_string(),
|
expansion.to_string(),
|
||||||
"{(impl From < Leaf > for TokenTree {fn from (it : Leaf) -> TokenTree {TokenTree :: Leaf (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)}})}"
|
impl From < Subtree > for TokenTree {fn from (it : Subtree) -> TokenTree {TokenTree :: Subtree (it)}}"
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue