Skip to content

Commit 30ca5bc

Browse files
committed
NFA parser for mbe matcher
1 parent 557c1e3 commit 30ca5bc

File tree

8 files changed

+545
-151
lines changed

8 files changed

+545
-151
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/mbe/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ syntax = { path = "../syntax", version = "0.0.0" }
1818
parser = { path = "../parser", version = "0.0.0" }
1919
tt = { path = "../tt", version = "0.0.0" }
2020
test_utils = { path = "../test_utils" }
21-
21+
stdx = { path = "../stdx" }

crates/mbe/src/expander.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
mod matcher;
66
mod transcriber;
77

8-
use rustc_hash::FxHashMap;
8+
use smallvec::SmallVec;
99
use syntax::SmolStr;
1010

1111
use crate::{ExpandError, ExpandResult};
@@ -28,10 +28,10 @@ pub(crate) fn expand_rules(
2828
return ExpandResult::ok(value);
2929
}
3030
}
31-
// Use the rule if we matched more tokens, or had fewer errors
31+
// Use the rule if we matched more tokens, or bound variables count
3232
if let Some((prev_match, _)) = &match_ {
33-
if (new_match.unmatched_tts, new_match.err_count)
34-
< (prev_match.unmatched_tts, prev_match.err_count)
33+
if (new_match.unmatched_tts, -(new_match.bound_count as i32))
34+
< (prev_match.unmatched_tts, -(prev_match.bound_count as i32))
3535
{
3636
match_ = Some((new_match, rule));
3737
}
@@ -94,19 +94,19 @@ pub(crate) fn expand_rules(
9494
/// In other words, `Bindings` is a *multi* mapping from `SmolStr` to
9595
/// `tt::TokenTree`, where the index to select a particular `TokenTree` among
9696
/// many is not a plain `usize`, but an `&[usize]`.
97-
#[derive(Debug, Default)]
97+
#[derive(Debug, Default, Clone, PartialEq, Eq)]
9898
struct Bindings {
99-
inner: FxHashMap<SmolStr, Binding>,
99+
inner: SmallVec<[(SmolStr, Binding); 4]>,
100100
}
101101

102-
#[derive(Debug)]
102+
#[derive(Debug, Clone, PartialEq, Eq)]
103103
enum Binding {
104104
Fragment(Fragment),
105105
Nested(Vec<Binding>),
106106
Empty,
107107
}
108108

109-
#[derive(Debug, Clone)]
109+
#[derive(Debug, Clone, PartialEq, Eq)]
110110
enum Fragment {
111111
/// token fragments are just copy-pasted into the output
112112
Tokens(tt::TokenTree),

0 commit comments

Comments
 (0)