aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-01-31 18:43:54 +0000
committerAleksey Kladov <[email protected]>2019-01-31 20:23:30 +0000
commita16f6bb27df1a9a9fcb506081bb30ccbf966e5c2 (patch)
tree7ec1a33ec57806979951fa5cefc3cc9a05b28987 /crates
parent2d1f0b105d7995088fdf7a90e5e594b5555699c6 (diff)
cleanup
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_mbe/src/mbe_expander.rs34
-rw-r--r--crates/ra_mbe/src/mbe_parser.rs44
-rw-r--r--crates/ra_mbe/src/syntax_bridge.rs4
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 @@
1use rustc_hash::FxHashMap; 1use rustc_hash::FxHashMap;
2use ra_syntax::SmolStr; 2use ra_syntax::SmolStr;
3 3
4use crate::{self as mbe, tt_cursor::TtCursor}; 4use crate::tt_cursor::TtCursor;
5 5
6pub fn exapnd(rules: &mbe::MacroRules, input: &tt::Subtree) -> Option<tt::Subtree> { 6pub fn exapnd(rules: &crate::MacroRules, input: &tt::Subtree) -> Option<tt::Subtree> {
7 rules.rules.iter().find_map(|it| expand_rule(it, input)) 7 rules.rules.iter().find_map(|it| expand_rule(it, input))
8} 8}
9 9
10fn expand_rule(rule: &mbe::Rule, input: &tt::Subtree) -> Option<tt::Subtree> { 10fn expand_rule(rule: &crate::Rule, input: &tt::Subtree) -> Option<tt::Subtree> {
11 let mut input = TtCursor::new(input); 11 let mut input = TtCursor::new(input);
12 let bindings = match_lhs(&rule.lhs, &mut input)?; 12 let bindings = match_lhs(&rule.lhs, &mut input)?;
13 expand_subtree(&rule.rhs, &bindings, &mut Vec::new()) 13 expand_subtree(&rule.rhs, &bindings, &mut Vec::new())
@@ -52,12 +52,12 @@ impl Bindings {
52 } 52 }
53} 53}
54 54
55fn match_lhs(pattern: &mbe::Subtree, input: &mut TtCursor) -> Option<Bindings> { 55fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Option<Bindings> {
56 let mut res = Bindings::default(); 56 let mut res = Bindings::default();
57 for pat in pattern.token_trees.iter() { 57 for pat in pattern.token_trees.iter() {
58 match pat { 58 match pat {
59 mbe::TokenTree::Leaf(leaf) => match leaf { 59 crate::TokenTree::Leaf(leaf) => match leaf {
60 mbe::Leaf::Var(mbe::Var { text, kind }) => { 60 crate::Leaf::Var(crate::Var { text, kind }) => {
61 let kind = kind.clone()?; 61 let kind = kind.clone()?;
62 match kind.as_str() { 62 match kind.as_str() {
63 "ident" => { 63 "ident" => {
@@ -70,14 +70,14 @@ fn match_lhs(pattern: &mbe::Subtree, input: &mut TtCursor) -> Option<Bindings> {
70 _ => return None, 70 _ => return None,
71 } 71 }
72 } 72 }
73 mbe::Leaf::Punct(punct) => { 73 crate::Leaf::Punct(punct) => {
74 if input.eat_punct()? != punct { 74 if input.eat_punct()? != punct {
75 return None; 75 return None;
76 } 76 }
77 } 77 }
78 _ => return None, 78 _ => return None,
79 }, 79 },
80 mbe::TokenTree::Repeat(mbe::Repeat { 80 crate::TokenTree::Repeat(crate::Repeat {
81 subtree, 81 subtree,
82 kind: _, 82 kind: _,
83 separator, 83 separator,
@@ -114,7 +114,7 @@ impl_froms! (Foo: Bar, Baz)
114*/ 114*/
115 115
116fn expand_subtree( 116fn expand_subtree(
117 template: &mbe::Subtree, 117 template: &crate::Subtree,
118 bindings: &Bindings, 118 bindings: &Bindings,
119 nesting: &mut Vec<usize>, 119 nesting: &mut Vec<usize>,
120) -> Option<tt::Subtree> { 120) -> Option<tt::Subtree> {
@@ -131,13 +131,13 @@ fn expand_subtree(
131} 131}
132 132
133fn expand_tt( 133fn expand_tt(
134 template: &mbe::TokenTree, 134 template: &crate::TokenTree,
135 bindings: &Bindings, 135 bindings: &Bindings,
136 nesting: &mut Vec<usize>, 136 nesting: &mut Vec<usize>,
137) -> Option<tt::TokenTree> { 137) -> Option<tt::TokenTree> {
138 let res: tt::TokenTree = match template { 138 let res: tt::TokenTree = match template {
139 mbe::TokenTree::Subtree(subtree) => expand_subtree(subtree, bindings, nesting)?.into(), 139 crate::TokenTree::Subtree(subtree) => expand_subtree(subtree, bindings, nesting)?.into(),
140 mbe::TokenTree::Repeat(repeat) => { 140 crate::TokenTree::Repeat(repeat) => {
141 let mut token_trees = Vec::new(); 141 let mut token_trees = Vec::new();
142 nesting.push(0); 142 nesting.push(0);
143 while let Some(t) = expand_subtree(&repeat.subtree, bindings, nesting) { 143 while let Some(t) = expand_subtree(&repeat.subtree, bindings, nesting) {
@@ -152,14 +152,14 @@ fn expand_tt(
152 } 152 }
153 .into() 153 .into()
154 } 154 }
155 mbe::TokenTree::Leaf(leaf) => match leaf { 155 crate::TokenTree::Leaf(leaf) => match leaf {
156 mbe::Leaf::Ident(ident) => tt::Leaf::from(tt::Ident { 156 crate::Leaf::Ident(ident) => tt::Leaf::from(tt::Ident {
157 text: ident.text.clone(), 157 text: ident.text.clone(),
158 }) 158 })
159 .into(), 159 .into(),
160 mbe::Leaf::Punct(punct) => tt::Leaf::from(punct.clone()).into(), 160 crate::Leaf::Punct(punct) => tt::Leaf::from(punct.clone()).into(),
161 mbe::Leaf::Var(v) => bindings.get(&v.text, nesting)?.clone(), 161 crate::Leaf::Var(v) => bindings.get(&v.text, nesting)?.clone(),
162 mbe::Leaf::Literal(l) => tt::Leaf::from(tt::Literal { 162 crate::Leaf::Literal(l) => tt::Leaf::from(tt::Literal {
163 text: l.text.clone(), 163 text: l.text.clone(),
164 }) 164 })
165 .into(), 165 .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 @@
1use crate::{self as mbe, tt_cursor::TtCursor}; 1use crate::tt_cursor::TtCursor;
2 2
3/// This module parses a raw `tt::TokenStream` into macro-by-example token 3/// This module parses a raw `tt::TokenStream` into macro-by-example token
4/// stream. This is a *mostly* identify function, expect for handling of 4/// stream. This is a *mostly* identify function, expect for handling of
5/// `$var:tt_kind` and `$(repeat),*` constructs. 5/// `$var:tt_kind` and `$(repeat),*` constructs.
6 6
7pub fn parse(tt: &tt::Subtree) -> Option<mbe::MacroRules> { 7pub fn parse(tt: &tt::Subtree) -> Option<crate::MacroRules> {
8 let mut parser = TtCursor::new(tt); 8 let mut parser = TtCursor::new(tt);
9 let mut rules = Vec::new(); 9 let mut rules = Vec::new();
10 while !parser.is_eof() { 10 while !parser.is_eof() {
11 rules.push(parse_rule(&mut parser)?) 11 rules.push(parse_rule(&mut parser)?)
12 } 12 }
13 Some(mbe::MacroRules { rules }) 13 Some(crate::MacroRules { rules })
14} 14}
15 15
16fn parse_rule(p: &mut TtCursor) -> Option<mbe::Rule> { 16fn parse_rule(p: &mut TtCursor) -> Option<crate::Rule> {
17 let lhs = parse_subtree(p.eat_subtree()?)?; 17 let lhs = parse_subtree(p.eat_subtree()?)?;
18 p.expect_char('=')?; 18 p.expect_char('=')?;
19 p.expect_char('>')?; 19 p.expect_char('>')?;
20 let rhs = parse_subtree(p.eat_subtree()?)?; 20 let mut rhs = parse_subtree(p.eat_subtree()?)?;
21 Some(mbe::Rule { lhs, rhs }) 21 rhs.delimiter = crate::Delimiter::None;
22 Some(crate::Rule { lhs, rhs })
22} 23}
23 24
24fn parse_subtree(tt: &tt::Subtree) -> Option<mbe::Subtree> { 25fn parse_subtree(tt: &tt::Subtree) -> Option<crate::Subtree> {
25 let mut token_trees = Vec::new(); 26 let mut token_trees = Vec::new();
26 let mut p = TtCursor::new(tt); 27 let mut p = TtCursor::new(tt);
27 while let Some(tt) = p.eat() { 28 while let Some(tt) = p.eat() {
28 let child: mbe::TokenTree = match tt { 29 let child: crate::TokenTree = match tt {
29 tt::TokenTree::Leaf(leaf) => match leaf { 30 tt::TokenTree::Leaf(leaf) => match leaf {
30 tt::Leaf::Punct(tt::Punct { char: '$', .. }) => { 31 tt::Leaf::Punct(tt::Punct { char: '$', .. }) => {
31 if p.at_ident().is_some() { 32 if p.at_ident().is_some() {
32 mbe::Leaf::from(parse_var(&mut p)?).into() 33 crate::Leaf::from(parse_var(&mut p)?).into()
33 } else { 34 } else {
34 parse_repeat(&mut p)?.into() 35 parse_repeat(&mut p)?.into()
35 } 36 }
36 } 37 }
37 tt::Leaf::Punct(punct) => mbe::Leaf::from(*punct).into(), 38 tt::Leaf::Punct(punct) => crate::Leaf::from(*punct).into(),
38 tt::Leaf::Ident(tt::Ident { text }) => { 39 tt::Leaf::Ident(tt::Ident { text }) => {
39 mbe::Leaf::from(mbe::Ident { text: text.clone() }).into() 40 crate::Leaf::from(crate::Ident { text: text.clone() }).into()
40 } 41 }
41 tt::Leaf::Literal(tt::Literal { text }) => { 42 tt::Leaf::Literal(tt::Literal { text }) => {
42 mbe::Leaf::from(mbe::Literal { text: text.clone() }).into() 43 crate::Leaf::from(crate::Literal { text: text.clone() }).into()
43 } 44 }
44 }, 45 },
45 tt::TokenTree::Subtree(subtree) => parse_subtree(&subtree)?.into(), 46 tt::TokenTree::Subtree(subtree) => parse_subtree(&subtree)?.into(),
46 }; 47 };
47 token_trees.push(child); 48 token_trees.push(child);
48 } 49 }
49 Some(mbe::Subtree { 50 Some(crate::Subtree {
50 token_trees, 51 token_trees,
51 delimiter: tt.delimiter, 52 delimiter: tt.delimiter,
52 }) 53 })
53} 54}
54 55
55fn parse_var(p: &mut TtCursor) -> Option<mbe::Var> { 56fn parse_var(p: &mut TtCursor) -> Option<crate::Var> {
56 let ident = p.eat_ident().unwrap(); 57 let ident = p.eat_ident().unwrap();
57 let text = ident.text.clone(); 58 let text = ident.text.clone();
58 let kind = if p.at_char(':') { 59 let kind = if p.at_char(':') {
@@ -66,12 +67,13 @@ fn parse_var(p: &mut TtCursor) -> Option<mbe::Var> {
66 } else { 67 } else {
67 None 68 None
68 }; 69 };
69 Some(mbe::Var { text, kind }) 70 Some(crate::Var { text, kind })
70} 71}
71 72
72fn parse_repeat(p: &mut TtCursor) -> Option<mbe::Repeat> { 73fn parse_repeat(p: &mut TtCursor) -> Option<crate::Repeat> {
73 let subtree = p.eat_subtree().unwrap(); 74 let subtree = p.eat_subtree().unwrap();
74 let subtree = parse_subtree(subtree)?; 75 let mut subtree = parse_subtree(subtree)?;
76 subtree.delimiter = crate::Delimiter::None;
75 let sep = p.eat_punct()?; 77 let sep = p.eat_punct()?;
76 let (separator, rep) = match sep.char { 78 let (separator, rep) = match sep.char {
77 '*' | '+' | '?' => (None, sep.char), 79 '*' | '+' | '?' => (None, sep.char),
@@ -79,13 +81,13 @@ fn parse_repeat(p: &mut TtCursor) -> Option<mbe::Repeat> {
79 }; 81 };
80 82
81 let kind = match rep { 83 let kind = match rep {
82 '*' => mbe::RepeatKind::ZeroOrMore, 84 '*' => crate::RepeatKind::ZeroOrMore,
83 '+' => mbe::RepeatKind::OneOrMore, 85 '+' => crate::RepeatKind::OneOrMore,
84 '?' => mbe::RepeatKind::ZeroOrOne, 86 '?' => crate::RepeatKind::ZeroOrOne,
85 _ => return None, 87 _ => return None,
86 }; 88 };
87 p.bump(); 89 p.bump();
88 Some(mbe::Repeat { 90 Some(crate::Repeat {
89 subtree, 91 subtree,
90 kind, 92 kind,
91 separator, 93 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);
107 let expansion = crate::exapnd(&mbe, &invocation_tt).unwrap(); 107 let expansion = crate::exapnd(&mbe, &invocation_tt).unwrap();
108 assert_eq!( 108 assert_eq!(
109 expansion.to_string(), 109 expansion.to_string(),
110 "{(impl From < Leaf > for TokenTree {fn from (it : Leaf) -> TokenTree {TokenTree :: Leaf (it)}}) \ 110 "impl From < Leaf > for TokenTree {fn from (it : Leaf) -> TokenTree {TokenTree :: Leaf (it)}} \
111 (impl From < Subtree > for TokenTree {fn from (it : Subtree) -> TokenTree {TokenTree :: Subtree (it)}})}" 111 impl From < Subtree > for TokenTree {fn from (it : Subtree) -> TokenTree {TokenTree :: Subtree (it)}}"
112 ) 112 )
113} 113}