aboutsummaryrefslogtreecommitdiff
path: root/crates/mbe/src/mbe_expander
diff options
context:
space:
mode:
authorJonas Schievink <[email protected]>2020-11-26 15:04:23 +0000
committerJonas Schievink <[email protected]>2020-11-26 15:04:23 +0000
commitc784c8ec766edf4c7ab558f71d3f87e6c7c870b9 (patch)
tree58b0150a27a6b35000d745807461100786952e93 /crates/mbe/src/mbe_expander
parent0bcd8147ec08db2a94dd2a3068a527ba6c47a4cd (diff)
Use named fields in `ExpandResult`
Diffstat (limited to 'crates/mbe/src/mbe_expander')
-rw-r--r--crates/mbe/src/mbe_expander/matcher.rs9
-rw-r--r--crates/mbe/src/mbe_expander/transcriber.rs18
2 files changed, 16 insertions, 11 deletions
diff --git a/crates/mbe/src/mbe_expander/matcher.rs b/crates/mbe/src/mbe_expander/matcher.rs
index 39a8eefbd..3f8445897 100644
--- a/crates/mbe/src/mbe_expander/matcher.rs
+++ b/crates/mbe/src/mbe_expander/matcher.rs
@@ -158,7 +158,8 @@ fn match_subtree(
158 continue; 158 continue;
159 } 159 }
160 }; 160 };
161 let ExpandResult(matched, match_err) = match_meta_var(kind.as_str(), src); 161 let ExpandResult { value: matched, err: match_err } =
162 match_meta_var(kind.as_str(), src);
162 match matched { 163 match matched {
163 Some(fragment) => { 164 Some(fragment) => {
164 res.bindings.inner.insert(name.clone(), Binding::Fragment(fragment)); 165 res.bindings.inner.insert(name.clone(), Binding::Fragment(fragment));
@@ -342,17 +343,17 @@ impl<'a> TtIter<'a> {
342 token_trees: res.into_iter().cloned().collect(), 343 token_trees: res.into_iter().cloned().collect(),
343 })), 344 })),
344 }; 345 };
345 ExpandResult(res, err) 346 ExpandResult { value: res, err }
346 } 347 }
347 348
348 pub(crate) fn eat_vis(&mut self) -> Option<tt::TokenTree> { 349 pub(crate) fn eat_vis(&mut self) -> Option<tt::TokenTree> {
349 let mut fork = self.clone(); 350 let mut fork = self.clone();
350 match fork.expect_fragment(Visibility) { 351 match fork.expect_fragment(Visibility) {
351 ExpandResult(tt, None) => { 352 ExpandResult { value: tt, err: None } => {
352 *self = fork; 353 *self = fork;
353 tt 354 tt
354 } 355 }
355 ExpandResult(_, Some(_)) => None, 356 ExpandResult { value: _, err: Some(_) } => None,
356 } 357 }
357 } 358 }
358} 359}
diff --git a/crates/mbe/src/mbe_expander/transcriber.rs b/crates/mbe/src/mbe_expander/transcriber.rs
index c9525c5bf..616119ba9 100644
--- a/crates/mbe/src/mbe_expander/transcriber.rs
+++ b/crates/mbe/src/mbe_expander/transcriber.rs
@@ -93,17 +93,18 @@ fn expand_subtree(
93 match op { 93 match op {
94 Op::TokenTree(tt @ tt::TokenTree::Leaf(..)) => arena.push(tt.clone()), 94 Op::TokenTree(tt @ tt::TokenTree::Leaf(..)) => arena.push(tt.clone()),
95 Op::TokenTree(tt::TokenTree::Subtree(tt)) => { 95 Op::TokenTree(tt::TokenTree::Subtree(tt)) => {
96 let ExpandResult(tt, e) = expand_subtree(ctx, tt, arena); 96 let ExpandResult { value: tt, err: e } = expand_subtree(ctx, tt, arena);
97 err = err.or(e); 97 err = err.or(e);
98 arena.push(tt.into()); 98 arena.push(tt.into());
99 } 99 }
100 Op::Var { name, kind: _ } => { 100 Op::Var { name, kind: _ } => {
101 let ExpandResult(fragment, e) = expand_var(ctx, name); 101 let ExpandResult { value: fragment, err: e } = expand_var(ctx, name);
102 err = err.or(e); 102 err = err.or(e);
103 push_fragment(arena, fragment); 103 push_fragment(arena, fragment);
104 } 104 }
105 Op::Repeat { subtree, kind, separator } => { 105 Op::Repeat { subtree, kind, separator } => {
106 let ExpandResult(fragment, e) = expand_repeat(ctx, subtree, kind, separator, arena); 106 let ExpandResult { value: fragment, err: e } =
107 expand_repeat(ctx, subtree, kind, separator, arena);
107 err = err.or(e); 108 err = err.or(e);
108 push_fragment(arena, fragment) 109 push_fragment(arena, fragment)
109 } 110 }
@@ -111,7 +112,7 @@ fn expand_subtree(
111 } 112 }
112 // drain the elements added in this instance of expand_subtree 113 // drain the elements added in this instance of expand_subtree
113 let tts = arena.drain(start_elements..arena.len()).collect(); 114 let tts = arena.drain(start_elements..arena.len()).collect();
114 ExpandResult(tt::Subtree { delimiter: template.delimiter, token_trees: tts }, err) 115 ExpandResult { value: tt::Subtree { delimiter: template.delimiter, token_trees: tts }, err }
115} 116}
116 117
117fn expand_var(ctx: &mut ExpandCtx, v: &SmolStr) -> ExpandResult<Fragment> { 118fn expand_var(ctx: &mut ExpandCtx, v: &SmolStr) -> ExpandResult<Fragment> {
@@ -152,7 +153,7 @@ fn expand_var(ctx: &mut ExpandCtx, v: &SmolStr) -> ExpandResult<Fragment> {
152 ExpandResult::ok(Fragment::Tokens(tt)) 153 ExpandResult::ok(Fragment::Tokens(tt))
153 } else { 154 } else {
154 ctx.bindings.get(&v, &mut ctx.nesting).map_or_else( 155 ctx.bindings.get(&v, &mut ctx.nesting).map_or_else(
155 |e| ExpandResult(Fragment::Tokens(tt::TokenTree::empty()), Some(e)), 156 |e| ExpandResult { value: Fragment::Tokens(tt::TokenTree::empty()), err: Some(e) },
156 |b| ExpandResult::ok(b.clone()), 157 |b| ExpandResult::ok(b.clone()),
157 ) 158 )
158 } 159 }
@@ -174,7 +175,7 @@ fn expand_repeat(
174 let mut counter = 0; 175 let mut counter = 0;
175 176
176 loop { 177 loop {
177 let ExpandResult(mut t, e) = expand_subtree(ctx, template, arena); 178 let ExpandResult { value: mut t, err: e } = expand_subtree(ctx, template, arena);
178 let nesting_state = ctx.nesting.last_mut().unwrap(); 179 let nesting_state = ctx.nesting.last_mut().unwrap();
179 if nesting_state.at_end || !nesting_state.hit { 180 if nesting_state.at_end || !nesting_state.hit {
180 break; 181 break;
@@ -234,7 +235,10 @@ fn expand_repeat(
234 let tt = tt::Subtree { delimiter: None, token_trees: buf }.into(); 235 let tt = tt::Subtree { delimiter: None, token_trees: buf }.into();
235 236
236 if RepeatKind::OneOrMore == kind && counter == 0 { 237 if RepeatKind::OneOrMore == kind && counter == 0 {
237 return ExpandResult(Fragment::Tokens(tt), Some(ExpandError::UnexpectedToken)); 238 return ExpandResult {
239 value: Fragment::Tokens(tt),
240 err: Some(ExpandError::UnexpectedToken),
241 };
238 } 242 }
239 ExpandResult::ok(Fragment::Tokens(tt)) 243 ExpandResult::ok(Fragment::Tokens(tt))
240} 244}