From cff2201c30bda7b346e3b47875d95a2cf9cafaa3 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Tue, 2 Feb 2021 04:42:37 +0800 Subject: NFA parser for mbe matcher --- crates/mbe/src/expander.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'crates/mbe/src/expander.rs') diff --git a/crates/mbe/src/expander.rs b/crates/mbe/src/expander.rs index e7e14b3cc..2efff8f52 100644 --- a/crates/mbe/src/expander.rs +++ b/crates/mbe/src/expander.rs @@ -5,7 +5,7 @@ mod matcher; mod transcriber; -use rustc_hash::FxHashMap; +use smallvec::SmallVec; use syntax::SmolStr; use crate::{ExpandError, ExpandResult}; @@ -28,10 +28,10 @@ pub(crate) fn expand_rules( return ExpandResult::ok(value); } } - // Use the rule if we matched more tokens, or had fewer errors + // Use the rule if we matched more tokens, or bound variables count if let Some((prev_match, _)) = &match_ { - if (new_match.unmatched_tts, new_match.err_count) - < (prev_match.unmatched_tts, prev_match.err_count) + if (new_match.unmatched_tts, -(new_match.bound_count as i32)) + < (prev_match.unmatched_tts, -(prev_match.bound_count as i32)) { match_ = Some((new_match, rule)); } @@ -94,19 +94,19 @@ pub(crate) fn expand_rules( /// In other words, `Bindings` is a *multi* mapping from `SmolStr` to /// `tt::TokenTree`, where the index to select a particular `TokenTree` among /// many is not a plain `usize`, but an `&[usize]`. -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq, Eq)] struct Bindings { - inner: FxHashMap, + inner: SmallVec<[(SmolStr, Binding); 4]>, } -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq, Eq)] enum Binding { Fragment(Fragment), Nested(Vec), Empty, } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq, Eq)] enum Fragment { /// token fragments are just copy-pasted into the output Tokens(tt::TokenTree), -- cgit v1.2.3