aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_mbe
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_mbe')
-rw-r--r--crates/ra_mbe/src/lib.rs28
-rw-r--r--crates/ra_mbe/src/mbe_expander.rs29
-rw-r--r--crates/ra_mbe/src/mbe_parser.rs11
-rw-r--r--crates/ra_mbe/src/syntax_bridge.rs25
4 files changed, 22 insertions, 71 deletions
diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs
index 2c8ad4429..b09837831 100644
--- a/crates/ra_mbe/src/lib.rs
+++ b/crates/ra_mbe/src/lib.rs
@@ -137,18 +137,12 @@ impl_froms!(TokenTree: Leaf, Subtree);
137"#; 137"#;
138 138
139 let source_file = ast::SourceFile::parse(macro_definition); 139 let source_file = ast::SourceFile::parse(macro_definition);
140 let macro_definition = source_file 140 let macro_definition =
141 .syntax() 141 source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
142 .descendants()
143 .find_map(ast::MacroCall::cast)
144 .unwrap();
145 142
146 let source_file = ast::SourceFile::parse(macro_invocation); 143 let source_file = ast::SourceFile::parse(macro_invocation);
147 let macro_invocation = source_file 144 let macro_invocation =
148 .syntax() 145 source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
149 .descendants()
150 .find_map(ast::MacroCall::cast)
151 .unwrap();
152 146
153 let definition_tt = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap(); 147 let definition_tt = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap();
154 let invocation_tt = ast_to_token_tree(macro_invocation.token_tree().unwrap()).unwrap(); 148 let invocation_tt = ast_to_token_tree(macro_invocation.token_tree().unwrap()).unwrap();
@@ -163,11 +157,8 @@ impl_froms!(TokenTree: Leaf, Subtree);
163 157
164 fn create_rules(macro_definition: &str) -> MacroRules { 158 fn create_rules(macro_definition: &str) -> MacroRules {
165 let source_file = ast::SourceFile::parse(macro_definition); 159 let source_file = ast::SourceFile::parse(macro_definition);
166 let macro_definition = source_file 160 let macro_definition =
167 .syntax() 161 source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
168 .descendants()
169 .find_map(ast::MacroCall::cast)
170 .unwrap();
171 162
172 let definition_tt = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap(); 163 let definition_tt = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap();
173 crate::MacroRules::parse(&definition_tt).unwrap() 164 crate::MacroRules::parse(&definition_tt).unwrap()
@@ -175,11 +166,8 @@ impl_froms!(TokenTree: Leaf, Subtree);
175 166
176 fn assert_expansion(rules: &MacroRules, invocation: &str, expansion: &str) { 167 fn assert_expansion(rules: &MacroRules, invocation: &str, expansion: &str) {
177 let source_file = ast::SourceFile::parse(invocation); 168 let source_file = ast::SourceFile::parse(invocation);
178 let macro_invocation = source_file 169 let macro_invocation =
179 .syntax() 170 source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
180 .descendants()
181 .find_map(ast::MacroCall::cast)
182 .unwrap();
183 171
184 let invocation_tt = ast_to_token_tree(macro_invocation.token_tree().unwrap()).unwrap(); 172 let invocation_tt = ast_to_token_tree(macro_invocation.token_tree().unwrap()).unwrap();
185 173
diff --git a/crates/ra_mbe/src/mbe_expander.rs b/crates/ra_mbe/src/mbe_expander.rs
index fb1066eec..31531f4c9 100644
--- a/crates/ra_mbe/src/mbe_expander.rs
+++ b/crates/ra_mbe/src/mbe_expander.rs
@@ -133,11 +133,7 @@ fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Option<Bindings>
133 } 133 }
134 _ => return None, 134 _ => return None,
135 }, 135 },
136 crate::TokenTree::Repeat(crate::Repeat { 136 crate::TokenTree::Repeat(crate::Repeat { subtree, kind: _, separator }) => {
137 subtree,
138 kind: _,
139 separator,
140 }) => {
141 while let Some(nested) = match_lhs(subtree, input) { 137 while let Some(nested) = match_lhs(subtree, input) {
142 res.push_nested(nested)?; 138 res.push_nested(nested)?;
143 if let Some(separator) = *separator { 139 if let Some(separator) = *separator {
@@ -166,10 +162,7 @@ fn expand_subtree(
166 .map(|it| expand_tt(it, bindings, nesting)) 162 .map(|it| expand_tt(it, bindings, nesting))
167 .collect::<Option<Vec<_>>>()?; 163 .collect::<Option<Vec<_>>>()?;
168 164
169 Some(tt::Subtree { 165 Some(tt::Subtree { token_trees, delimiter: template.delimiter })
170 token_trees,
171 delimiter: template.delimiter,
172 })
173} 166}
174 167
175fn expand_tt( 168fn expand_tt(
@@ -188,23 +181,15 @@ fn expand_tt(
188 token_trees.push(t.into()) 181 token_trees.push(t.into())
189 } 182 }
190 nesting.pop().unwrap(); 183 nesting.pop().unwrap();
191 tt::Subtree { 184 tt::Subtree { token_trees, delimiter: tt::Delimiter::None }.into()
192 token_trees,
193 delimiter: tt::Delimiter::None,
194 }
195 .into()
196 } 185 }
197 crate::TokenTree::Leaf(leaf) => match leaf { 186 crate::TokenTree::Leaf(leaf) => match leaf {
198 crate::Leaf::Ident(ident) => tt::Leaf::from(tt::Ident { 187 crate::Leaf::Ident(ident) => {
199 text: ident.text.clone(), 188 tt::Leaf::from(tt::Ident { text: ident.text.clone() }).into()
200 }) 189 }
201 .into(),
202 crate::Leaf::Punct(punct) => tt::Leaf::from(punct.clone()).into(), 190 crate::Leaf::Punct(punct) => tt::Leaf::from(punct.clone()).into(),
203 crate::Leaf::Var(v) => bindings.get(&v.text, nesting)?.clone(), 191 crate::Leaf::Var(v) => bindings.get(&v.text, nesting)?.clone(),
204 crate::Leaf::Literal(l) => tt::Leaf::from(tt::Literal { 192 crate::Leaf::Literal(l) => tt::Leaf::from(tt::Literal { text: l.text.clone() }).into(),
205 text: l.text.clone(),
206 })
207 .into(),
208 }, 193 },
209 }; 194 };
210 Some(res) 195 Some(res)
diff --git a/crates/ra_mbe/src/mbe_parser.rs b/crates/ra_mbe/src/mbe_parser.rs
index abad2e8c8..60e566ed2 100644
--- a/crates/ra_mbe/src/mbe_parser.rs
+++ b/crates/ra_mbe/src/mbe_parser.rs
@@ -52,10 +52,7 @@ fn parse_subtree(tt: &tt::Subtree) -> Option<crate::Subtree> {
52 }; 52 };
53 token_trees.push(child); 53 token_trees.push(child);
54 } 54 }
55 Some(crate::Subtree { 55 Some(crate::Subtree { token_trees, delimiter: tt.delimiter })
56 token_trees,
57 delimiter: tt.delimiter,
58 })
59} 56}
60 57
61fn parse_var(p: &mut TtCursor) -> Option<crate::Var> { 58fn parse_var(p: &mut TtCursor) -> Option<crate::Var> {
@@ -92,9 +89,5 @@ fn parse_repeat(p: &mut TtCursor) -> Option<crate::Repeat> {
92 _ => return None, 89 _ => return None,
93 }; 90 };
94 p.bump(); 91 p.bump();
95 Some(crate::Repeat { 92 Some(crate::Repeat { subtree, kind, separator })
96 subtree,
97 kind,
98 separator,
99 })
100} 93}
diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs
index 2dc04d4e7..9a2eceaba 100644
--- a/crates/ra_mbe/src/syntax_bridge.rs
+++ b/crates/ra_mbe/src/syntax_bridge.rs
@@ -23,23 +23,14 @@ fn convert_tt(tt: &SyntaxNode) -> Option<tt::Subtree> {
23 for char in child.leaf_text().unwrap().chars() { 23 for char in child.leaf_text().unwrap().chars() {
24 if let Some(char) = prev { 24 if let Some(char) = prev {
25 token_trees.push( 25 token_trees.push(
26 tt::Leaf::from(tt::Punct { 26 tt::Leaf::from(tt::Punct { char, spacing: tt::Spacing::Joint }).into(),
27 char,
28 spacing: tt::Spacing::Joint,
29 })
30 .into(),
31 ); 27 );
32 } 28 }
33 prev = Some(char) 29 prev = Some(char)
34 } 30 }
35 if let Some(char) = prev { 31 if let Some(char) = prev {
36 token_trees.push( 32 token_trees
37 tt::Leaf::from(tt::Punct { 33 .push(tt::Leaf::from(tt::Punct { char, spacing: tt::Spacing::Alone }).into());
38 char,
39 spacing: tt::Spacing::Alone,
40 })
41 .into(),
42 );
43 } 34 }
44 } else { 35 } else {
45 let child: tt::TokenTree = if child.kind() == TOKEN_TREE { 36 let child: tt::TokenTree = if child.kind() == TOKEN_TREE {
@@ -48,10 +39,7 @@ fn convert_tt(tt: &SyntaxNode) -> Option<tt::Subtree> {
48 let text = child.leaf_text().unwrap().clone(); 39 let text = child.leaf_text().unwrap().clone();
49 tt::Leaf::from(tt::Ident { text }).into() 40 tt::Leaf::from(tt::Ident { text }).into()
50 } else if child.kind().is_literal() { 41 } else if child.kind().is_literal() {
51 tt::Leaf::from(tt::Literal { 42 tt::Leaf::from(tt::Literal { text: child.leaf_text().unwrap().clone() }).into()
52 text: child.leaf_text().unwrap().clone(),
53 })
54 .into()
55 } else { 43 } else {
56 return None; 44 return None;
57 }; 45 };
@@ -59,9 +47,6 @@ fn convert_tt(tt: &SyntaxNode) -> Option<tt::Subtree> {
59 } 47 }
60 } 48 }
61 49
62 let res = tt::Subtree { 50 let res = tt::Subtree { delimiter, token_trees };
63 delimiter,
64 token_trees,
65 };
66 Some(res) 51 Some(res)
67} 52}