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/matcher.rs | 32 +++++++++++++++--------------- crates/mbe/src/mbe_expander/transcriber.rs | 29 +++++++++++++++------------ 2 files changed, 32 insertions(+), 29 deletions(-) (limited to 'crates/mbe/src/mbe_expander') diff --git a/crates/mbe/src/mbe_expander/matcher.rs b/crates/mbe/src/mbe_expander/matcher.rs index 44722c0f1..ab5f87c48 100644 --- a/crates/mbe/src/mbe_expander/matcher.rs +++ b/crates/mbe/src/mbe_expander/matcher.rs @@ -2,10 +2,10 @@ use crate::{ mbe_expander::{Binding, Bindings, Fragment}, - parser::{parse_pattern, Op, RepeatKind, Separator}, + parser::{Op, RepeatKind, Separator}, subtree_source::SubtreeTokenSource, tt_iter::TtIter, - ExpandError, + ExpandError, MetaTemplate, }; use super::ExpandResult; @@ -83,7 +83,7 @@ impl Match { // sense to try using it. Matching errors are added to the `Match`. It might // make sense to make pattern parsing a separate step? -pub(super) fn match_(pattern: &tt::Subtree, src: &tt::Subtree) -> Result { +pub(super) fn match_(pattern: &MetaTemplate, src: &tt::Subtree) -> Result { assert!(pattern.delimiter == None); let mut res = Match::default(); @@ -101,12 +101,12 @@ pub(super) fn match_(pattern: &tt::Subtree, src: &tt::Subtree) -> Result Result<(), ExpandError> { - for op in parse_pattern(pattern) { - match op? { - Op::TokenTree(tt::TokenTree::Leaf(lhs)) => { + for op in pattern.iter() { + match op.as_ref().map_err(|err| err.clone())? { + Op::Leaf(lhs) => { let rhs = match src.expect_leaf() { Ok(l) => l, Err(()) => { @@ -132,7 +132,7 @@ fn match_subtree( } } } - Op::TokenTree(tt::TokenTree::Subtree(lhs)) => { + Op::Subtree(lhs) => { let rhs = match src.expect_subtree() { Ok(s) => s, Err(()) => { @@ -172,7 +172,7 @@ fn match_subtree( } } Op::Repeat { subtree, kind, separator } => { - match_repeat(res, subtree, kind, separator, src)?; + match_repeat(res, subtree, *kind, separator, src)?; } } } @@ -372,9 +372,9 @@ impl<'a> TtIter<'a> { pub(super) fn match_repeat( res: &mut Match, - pattern: &tt::Subtree, + pattern: &MetaTemplate, kind: RepeatKind, - separator: Option, + separator: &Option, src: &mut TtIter, ) -> Result<(), ExpandError> { // Dirty hack to make macro-expansion terminate. @@ -489,12 +489,12 @@ fn match_meta_var(kind: &str, input: &mut TtIter) -> ExpandResult, pattern: &tt::Subtree) -> Result<(), ExpandError> { - for op in parse_pattern(pattern) { - match op? { +fn collect_vars(buf: &mut Vec, pattern: &MetaTemplate) -> Result<(), ExpandError> { + for op in pattern.iter() { + match op.as_ref().map_err(|e| e.clone())? { Op::Var { name, .. } => buf.push(name.clone()), - Op::TokenTree(tt::TokenTree::Leaf(_)) => (), - Op::TokenTree(tt::TokenTree::Subtree(subtree)) => collect_vars(buf, subtree)?, + Op::Leaf(_) => (), + Op::Subtree(subtree) => collect_vars(buf, subtree)?, Op::Repeat { subtree, .. } => collect_vars(buf, subtree)?, } } 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