aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_mbe/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_mbe/src')
-rw-r--r--crates/ra_mbe/src/lib.rs2
-rw-r--r--crates/ra_mbe/src/mbe_expander/transcriber.rs33
-rw-r--r--crates/ra_mbe/src/syntax_bridge.rs2
-rw-r--r--crates/ra_mbe/src/tests.rs152
4 files changed, 98 insertions, 91 deletions
diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs
index 1a020398e..9c450eaba 100644
--- a/crates/ra_mbe/src/lib.rs
+++ b/crates/ra_mbe/src/lib.rs
@@ -22,7 +22,7 @@ pub enum ParseError {
22 RepetitionEmtpyTokenTree, 22 RepetitionEmtpyTokenTree,
23} 23}
24 24
25#[derive(Debug, PartialEq, Eq)] 25#[derive(Debug, PartialEq, Eq, Clone)]
26pub enum ExpandError { 26pub enum ExpandError {
27 NoMatchingRule, 27 NoMatchingRule,
28 UnexpectedToken, 28 UnexpectedToken,
diff --git a/crates/ra_mbe/src/mbe_expander/transcriber.rs b/crates/ra_mbe/src/mbe_expander/transcriber.rs
index 4b173edd3..7c9bb4d00 100644
--- a/crates/ra_mbe/src/mbe_expander/transcriber.rs
+++ b/crates/ra_mbe/src/mbe_expander/transcriber.rs
@@ -1,4 +1,4 @@
1//! Transcraber takes a template, like `fn $ident() {}`, a set of bindings like 1//! Transcriber takes a template, like `fn $ident() {}`, a set of bindings like
2//! `$ident => foo`, interpolates variables in the template, to get `fn foo() {}` 2//! `$ident => foo`, interpolates variables in the template, to get `fn foo() {}`
3 3
4use ra_syntax::SmolStr; 4use ra_syntax::SmolStr;
@@ -53,7 +53,8 @@ impl Bindings {
53pub(super) fn transcribe(template: &tt::Subtree, bindings: &Bindings) -> ExpandResult<tt::Subtree> { 53pub(super) fn transcribe(template: &tt::Subtree, bindings: &Bindings) -> ExpandResult<tt::Subtree> {
54 assert!(template.delimiter == None); 54 assert!(template.delimiter == None);
55 let mut ctx = ExpandCtx { bindings: &bindings, nesting: Vec::new() }; 55 let mut ctx = ExpandCtx { bindings: &bindings, nesting: Vec::new() };
56 expand_subtree(&mut ctx, template) 56 let mut arena: Vec<tt::TokenTree> = Vec::new();
57 expand_subtree(&mut ctx, template, &mut arena)
57} 58}
58 59
59#[derive(Debug)] 60#[derive(Debug)]
@@ -73,8 +74,13 @@ struct ExpandCtx<'a> {
73 nesting: Vec<NestingState>, 74 nesting: Vec<NestingState>,
74} 75}
75 76
76fn expand_subtree(ctx: &mut ExpandCtx, template: &tt::Subtree) -> ExpandResult<tt::Subtree> { 77fn expand_subtree(
77 let mut buf: Vec<tt::TokenTree> = Vec::new(); 78 ctx: &mut ExpandCtx,
79 template: &tt::Subtree,
80 arena: &mut Vec<tt::TokenTree>,
81) -> ExpandResult<tt::Subtree> {
82 // remember how many elements are in the arena now - when returning, we want to drain exactly how many elements we added. This way, the recursive uses of the arena get their own "view" of the arena, but will reuse the allocation
83 let start_elements = arena.len();
78 let mut err = None; 84 let mut err = None;
79 for op in parse_template(template) { 85 for op in parse_template(template) {
80 let op = match op { 86 let op = match op {
@@ -85,25 +91,27 @@ fn expand_subtree(ctx: &mut ExpandCtx, template: &tt::Subtree) -> ExpandResult<t
85 } 91 }
86 }; 92 };
87 match op { 93 match op {
88 Op::TokenTree(tt @ tt::TokenTree::Leaf(..)) => buf.push(tt.clone()), 94 Op::TokenTree(tt @ tt::TokenTree::Leaf(..)) => arena.push(tt.clone()),
89 Op::TokenTree(tt::TokenTree::Subtree(tt)) => { 95 Op::TokenTree(tt::TokenTree::Subtree(tt)) => {
90 let ExpandResult(tt, e) = expand_subtree(ctx, tt); 96 let ExpandResult(tt, e) = expand_subtree(ctx, tt, arena);
91 err = err.or(e); 97 err = err.or(e);
92 buf.push(tt.into()); 98 arena.push(tt.into());
93 } 99 }
94 Op::Var { name, kind: _ } => { 100 Op::Var { name, kind: _ } => {
95 let ExpandResult(fragment, e) = expand_var(ctx, name); 101 let ExpandResult(fragment, e) = expand_var(ctx, name);
96 err = err.or(e); 102 err = err.or(e);
97 push_fragment(&mut buf, fragment); 103 push_fragment(arena, fragment);
98 } 104 }
99 Op::Repeat { subtree, kind, separator } => { 105 Op::Repeat { subtree, kind, separator } => {
100 let ExpandResult(fragment, e) = expand_repeat(ctx, subtree, kind, separator); 106 let ExpandResult(fragment, e) = expand_repeat(ctx, subtree, kind, separator, arena);
101 err = err.or(e); 107 err = err.or(e);
102 push_fragment(&mut buf, fragment) 108 push_fragment(arena, fragment)
103 } 109 }
104 } 110 }
105 } 111 }
106 ExpandResult(tt::Subtree { delimiter: template.delimiter, token_trees: buf }, err) 112 // drain the elements added in this instance of expand_subtree
113 let tts = arena.drain(start_elements..arena.len()).collect();
114 ExpandResult(tt::Subtree { delimiter: template.delimiter, token_trees: tts }, err)
107} 115}
108 116
109fn expand_var(ctx: &mut ExpandCtx, v: &SmolStr) -> ExpandResult<Fragment> { 117fn expand_var(ctx: &mut ExpandCtx, v: &SmolStr) -> ExpandResult<Fragment> {
@@ -155,6 +163,7 @@ fn expand_repeat(
155 template: &tt::Subtree, 163 template: &tt::Subtree,
156 kind: RepeatKind, 164 kind: RepeatKind,
157 separator: Option<Separator>, 165 separator: Option<Separator>,
166 arena: &mut Vec<tt::TokenTree>,
158) -> ExpandResult<Fragment> { 167) -> ExpandResult<Fragment> {
159 let mut buf: Vec<tt::TokenTree> = Vec::new(); 168 let mut buf: Vec<tt::TokenTree> = Vec::new();
160 ctx.nesting.push(NestingState { idx: 0, at_end: false, hit: false }); 169 ctx.nesting.push(NestingState { idx: 0, at_end: false, hit: false });
@@ -165,7 +174,7 @@ fn expand_repeat(
165 let mut counter = 0; 174 let mut counter = 0;
166 175
167 loop { 176 loop {
168 let ExpandResult(mut t, e) = expand_subtree(ctx, template); 177 let ExpandResult(mut t, e) = expand_subtree(ctx, template, arena);
169 let nesting_state = ctx.nesting.last_mut().unwrap(); 178 let nesting_state = ctx.nesting.last_mut().unwrap();
170 if nesting_state.at_end || !nesting_state.hit { 179 if nesting_state.at_end || !nesting_state.hit {
171 break; 180 break;
diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs
index bb28acfd9..fc4133a67 100644
--- a/crates/ra_mbe/src/syntax_bridge.rs
+++ b/crates/ra_mbe/src/syntax_bridge.rs
@@ -63,7 +63,7 @@ pub fn syntax_node_to_token_tree(node: &SyntaxNode) -> Option<(tt::Subtree, Toke
63// * Items(SmallVec<[P<ast::Item>; 1]>) -> token_tree_to_items 63// * Items(SmallVec<[P<ast::Item>; 1]>) -> token_tree_to_items
64// 64//
65// * TraitItems(SmallVec<[ast::TraitItem; 1]>) 65// * TraitItems(SmallVec<[ast::TraitItem; 1]>)
66// * ImplItems(SmallVec<[ast::ImplItem; 1]>) 66// * AssocItems(SmallVec<[ast::AssocItem; 1]>)
67// * ForeignItems(SmallVec<[ast::ForeignItem; 1]> 67// * ForeignItems(SmallVec<[ast::ForeignItem; 1]>
68 68
69pub fn token_tree_to_syntax_node( 69pub fn token_tree_to_syntax_node(
diff --git a/crates/ra_mbe/src/tests.rs b/crates/ra_mbe/src/tests.rs
index 0d924ce58..c43003fd6 100644
--- a/crates/ra_mbe/src/tests.rs
+++ b/crates/ra_mbe/src/tests.rs
@@ -266,21 +266,20 @@ fn test_expr_order() {
266 [email protected] "(" 266 [email protected] "("
267 [email protected] ")" 267 [email protected] ")"
268 [email protected] 268 [email protected]
269 [email protected] 269 [email protected] "{"
270 [email protected] "{" 270 [email protected]
271 [email protected] 271 [email protected]
272 [email protected] 272 [email protected]
273 [email protected] 273 [email protected]
274 [email protected] 274 [email protected] "1"
275 [email protected] "1" 275 [email protected] "+"
276 [email protected] "+" 276 [email protected]
277 [email protected] 277 [email protected] "1"
278 [email protected] "1" 278 [email protected] "*"
279 [email protected] "*" 279 [email protected]
280 [email protected] 280 [email protected] "2"
281 [email protected] "2" 281 [email protected] ";"
282 [email protected] ";" 282 [email protected] "}""#,
283 [email protected] "}""#,
284 ); 283 );
285} 284}
286 285
@@ -1114,68 +1113,67 @@ fn test_vec() {
1114 assert_eq!( 1113 assert_eq!(
1115 format!("{:#?}", tree).trim(), 1114 format!("{:#?}", tree).trim(),
1116 r#"[email protected] 1115 r#"[email protected]
1117 [email protected] 1116 [email protected] "{"
1118 [email protected] "{" 1117 [email protected]
1119 [email protected] 1118 [email protected] "let"
1120 [email protected] "let" 1119 [email protected]
1121 [email protected] 1120 [email protected] "mut"
1122 [email protected] "mut" 1121 [email protected]
1123 [email protected] 1122 [email protected] "v"
1124 [email protected] "v" 1123 [email protected] "="
1125 [email protected] "=" 1124 [email protected]
1126 [email protected] 1125 [email protected]
1127 [email protected] 1126 [email protected]
1128 [email protected] 1127 [email protected]
1129 [email protected] 1128 [email protected]
1130 [email protected] 1129 [email protected]
1131 [email protected] 1130 [email protected] "Vec"
1132 [email protected] "Vec" 1131 [email protected] "::"
1133 [email protected] "::" 1132 [email protected]
1134 [email protected] 1133 [email protected]
1135 [email protected] 1134 [email protected] "new"
1136 [email protected] "new" 1135 [email protected]
1137 [email protected] 1136 [email protected] "("
1138 [email protected] "(" 1137 [email protected] ")"
1139 [email protected] ")" 1138 [email protected] ";"
1140 [email protected] ";" 1139 [email protected]
1141 [email protected] 1140 [email protected]
1142 [email protected] 1141 [email protected]
1143 [email protected] 1142 [email protected]
1144 [email protected] 1143 [email protected]
1145 [email protected] 1144 [email protected]
1146 [email protected] 1145 [email protected] "v"
1147 [email protected] "v" 1146 [email protected] "."
1148 [email protected] "." 1147 [email protected]
1149 [email protected] 1148 [email protected] "push"
1150 [email protected] "push" 1149 [email protected]
1151 [email protected] 1150 [email protected] "("
1152 [email protected] "(" 1151 [email protected]
1153 [email protected] 1152 [email protected] "1u32"
1154 [email protected] "1u32" 1153 [email protected] ")"
1155 [email protected] ")" 1154 [email protected] ";"
1156 [email protected] ";" 1155 [email protected]
1157 [email protected] 1156 [email protected]
1158 [email protected] 1157 [email protected]
1159 [email protected] 1158 [email protected]
1160 [email protected] 1159 [email protected]
1161 [email protected] 1160 [email protected]
1162 [email protected] 1161 [email protected] "v"
1163 [email protected] "v" 1162 [email protected] "."
1164 [email protected] "." 1163 [email protected]
1165 [email protected] 1164 [email protected] "push"
1166 [email protected] "push" 1165 [email protected]
1167 [email protected] 1166 [email protected] "("
1168 [email protected] "(" 1167 [email protected]
1169 [email protected] 1168 [email protected] "2"
1170 [email protected] "2" 1169 [email protected] ")"
1171 [email protected] ")" 1170 [email protected] ";"
1172 [email protected] ";" 1171 [email protected]
1173 [email protected] 1172 [email protected]
1174 [email protected] 1173 [email protected]
1175 [email protected] 1174 [email protected]
1176 [email protected] 1175 [email protected] "v"
1177 [email protected] "v" 1176 [email protected] "}""#
1178 [email protected] "}""#
1179 ); 1177 );
1180} 1178}
1181 1179