From a16f6bb27df1a9a9fcb506081bb30ccbf966e5c2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 31 Jan 2019 21:43:54 +0300 Subject: cleanup --- crates/ra_mbe/src/mbe_expander.rs | 34 ++++++++++++++--------------- crates/ra_mbe/src/mbe_parser.rs | 44 ++++++++++++++++++++------------------ crates/ra_mbe/src/syntax_bridge.rs | 4 ++-- 3 files changed, 42 insertions(+), 40 deletions(-) diff --git a/crates/ra_mbe/src/mbe_expander.rs b/crates/ra_mbe/src/mbe_expander.rs index 21c1552ce..19c75404d 100644 --- a/crates/ra_mbe/src/mbe_expander.rs +++ b/crates/ra_mbe/src/mbe_expander.rs @@ -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 { +pub fn exapnd(rules: &crate::MacroRules, input: &tt::Subtree) -> Option { rules.rules.iter().find_map(|it| expand_rule(it, input)) } -fn expand_rule(rule: &mbe::Rule, input: &tt::Subtree) -> Option { +fn expand_rule(rule: &crate::Rule, input: &tt::Subtree) -> Option { 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 { +fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Option { 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 { _ => 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, ) -> Option { @@ -131,13 +131,13 @@ fn expand_subtree( } fn expand_tt( - template: &mbe::TokenTree, + template: &crate::TokenTree, bindings: &Bindings, nesting: &mut Vec, ) -> Option { 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(), diff --git a/crates/ra_mbe/src/mbe_parser.rs b/crates/ra_mbe/src/mbe_parser.rs index a70ed1d52..a76ea84db 100644 --- a/crates/ra_mbe/src/mbe_parser.rs +++ b/crates/ra_mbe/src/mbe_parser.rs @@ -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 { +pub fn parse(tt: &tt::Subtree) -> Option { 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 { +fn parse_rule(p: &mut TtCursor) -> Option { 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 { +fn parse_subtree(tt: &tt::Subtree) -> Option { 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 { +fn parse_var(p: &mut TtCursor) -> Option { 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 { } else { None }; - Some(mbe::Var { text, kind }) + Some(crate::Var { text, kind }) } -fn parse_repeat(p: &mut TtCursor) -> Option { +fn parse_repeat(p: &mut TtCursor) -> Option { 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 { }; 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, diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index aad5f24b7..3223f6ea5 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs @@ -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)}}" ) } -- cgit v1.2.3