From 266b14d4b5b44d1491e50c7aa2ed4b85020796e1 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Wed, 30 Dec 2020 02:35:21 +0800 Subject: Refactor mbe parsing code --- crates/mbe/src/mbe_expander/transcriber.rs | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) (limited to 'crates/mbe/src/mbe_expander/transcriber.rs') diff --git a/crates/mbe/src/mbe_expander/transcriber.rs b/crates/mbe/src/mbe_expander/transcriber.rs index 57592dc92..720531237 100644 --- a/crates/mbe/src/mbe_expander/transcriber.rs +++ b/crates/mbe/src/mbe_expander/transcriber.rs @@ -6,8 +6,8 @@ use syntax::SmolStr; use super::ExpandResult; use crate::{ mbe_expander::{Binding, Bindings, Fragment}, - parser::{parse_template, Op, RepeatKind, Separator}, - ExpandError, + parser::{Op, RepeatKind, Separator}, + ExpandError, MetaTemplate, }; impl Bindings { @@ -50,7 +50,10 @@ impl Bindings { } } -pub(super) fn transcribe(template: &tt::Subtree, bindings: &Bindings) -> ExpandResult { +pub(super) fn transcribe( + template: &MetaTemplate, + bindings: &Bindings, +) -> ExpandResult { assert!(template.delimiter == None); let mut ctx = ExpandCtx { bindings: &bindings, nesting: Vec::new() }; let mut arena: Vec = Vec::new(); @@ -76,35 +79,35 @@ struct ExpandCtx<'a> { fn expand_subtree( ctx: &mut ExpandCtx, - template: &tt::Subtree, + template: &MetaTemplate, arena: &mut Vec, ) -> ExpandResult { // 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 let start_elements = arena.len(); let mut err = None; - for op in parse_template(template) { + for op in template.iter() { let op = match op { Ok(op) => op, Err(e) => { - err = Some(e); + err = Some(e.clone()); break; } }; match op { - Op::TokenTree(tt @ tt::TokenTree::Leaf(..)) => arena.push(tt.clone()), - Op::TokenTree(tt::TokenTree::Subtree(tt)) => { - let ExpandResult { value: tt, err: e } = expand_subtree(ctx, tt, arena); + Op::Leaf(tt) => arena.push(tt.clone().into()), + Op::Subtree(tt) => { + let ExpandResult { value: tt, err: e } = expand_subtree(ctx, &tt, arena); err = err.or(e); arena.push(tt.into()); } Op::Var { name, .. } => { - let ExpandResult { value: fragment, err: e } = expand_var(ctx, name); + let ExpandResult { value: fragment, err: e } = expand_var(ctx, &name); err = err.or(e); push_fragment(arena, fragment); } Op::Repeat { subtree, kind, separator } => { let ExpandResult { value: fragment, err: e } = - expand_repeat(ctx, subtree, kind, separator, arena); + expand_repeat(ctx, subtree, *kind, separator, arena); err = err.or(e); push_fragment(arena, fragment) } @@ -161,9 +164,9 @@ fn expand_var(ctx: &mut ExpandCtx, v: &SmolStr) -> ExpandResult { fn expand_repeat( ctx: &mut ExpandCtx, - template: &tt::Subtree, + template: &MetaTemplate, kind: RepeatKind, - separator: Option, + separator: &Option, arena: &mut Vec, ) -> ExpandResult { let mut buf: Vec = Vec::new(); -- cgit v1.2.3