Skip to content

Commit b873dba

Browse files
committed
fix: Update metavariable expression implementation
1 parent 002e611 commit b873dba

File tree

5 files changed

+41
-25
lines changed

5 files changed

+41
-25
lines changed

crates/mbe/src/benchmark.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ fn invocation_fixtures(
199199
});
200200
parent.token_trees.push(subtree.into());
201201
}
202-
Op::Ignore { .. } | Op::Index { .. } | Op::Count { .. } => {}
202+
Op::Ignore { .. } | Op::Index { .. } | Op::Count { .. } | Op::Length { .. } => {}
203203
};
204204

205205
// Simple linear congruential generator for deterministic result

crates/mbe/src/expander/matcher.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,9 @@ fn match_loop_inner<'t, S: Span>(
588588
item.is_error = true;
589589
error_items.push(item);
590590
}
591-
OpDelimited::Op(Op::Ignore { .. } | Op::Index { .. } | Op::Count { .. }) => {
591+
OpDelimited::Op(
592+
Op::Ignore { .. } | Op::Index { .. } | Op::Count { .. } | Op::Length { .. },
593+
) => {
592594
stdx::never!("metavariable expression in lhs found");
593595
}
594596
OpDelimited::Open => {
@@ -851,7 +853,7 @@ fn collect_vars<S: Span>(collector_fun: &mut impl FnMut(SmolStr), pattern: &Meta
851853
Op::Subtree { tokens, .. } => collect_vars(collector_fun, tokens),
852854
Op::Repeat { tokens, .. } => collect_vars(collector_fun, tokens),
853855
Op::Literal(_) | Op::Ident(_) | Op::Punct(_) => {}
854-
Op::Ignore { .. } | Op::Index { .. } | Op::Count { .. } => {
856+
Op::Ignore { .. } | Op::Index { .. } | Op::Count { .. } | Op::Length { .. } => {
855857
stdx::never!("metavariable expression in lhs found");
856858
}
857859
}

crates/mbe/src/expander/transcriber.rs

+23-18
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,21 @@ fn expand_subtree<S: Span>(
232232
.into(),
233233
);
234234
}
235+
Op::Length { depth } => {
236+
let length = ctx.nesting.get(ctx.nesting.len() - 1 - depth).map_or(0, |_nest| {
237+
// FIXME: to be implemented
238+
0
239+
});
240+
arena.push(
241+
tt::Leaf::Literal(tt::Literal {
242+
text: length.to_string().into(),
243+
// FIXME
244+
#[allow(deprecated)]
245+
span: S::DUMMY,
246+
})
247+
.into(),
248+
);
249+
}
235250
Op::Count { name, depth } => {
236251
let mut binding = match ctx.bindings.get(name.as_str()) {
237252
Ok(b) => b,
@@ -518,28 +533,18 @@ fn fix_up_and_push_path_tt<S: Span>(buf: &mut Vec<tt::TokenTree<S>>, subtree: tt
518533
fn count<S>(
519534
ctx: &ExpandCtx<'_, S>,
520535
binding: &Binding<S>,
521-
our_depth: usize,
522-
count_depth: Option<usize>,
536+
depth_curr: usize,
537+
depth_max: usize,
523538
) -> Result<usize, CountError> {
524539
match binding {
525-
Binding::Nested(bs) => match count_depth {
526-
None => bs.iter().map(|b| count(ctx, b, our_depth + 1, None)).sum(),
527-
Some(0) => Ok(bs.len()),
528-
Some(d) => bs.iter().map(|b| count(ctx, b, our_depth + 1, Some(d - 1))).sum(),
529-
},
530-
Binding::Empty => Ok(0),
531-
Binding::Fragment(_) | Binding::Missing(_) => {
532-
if our_depth == 0 {
533-
// `${count(t)}` is placed inside the innermost repetition. This includes cases
534-
// where `t` is not a repeated fragment.
535-
Err(CountError::Misplaced)
536-
} else if count_depth.is_none() {
537-
Ok(1)
540+
Binding::Nested(bs) => {
541+
if depth_curr == depth_max {
542+
Ok(bs.len())
538543
} else {
539-
// We've reached at the innermost repeated fragment, but the user wants us to go
540-
// further!
541-
Err(CountError::OutOfBounds)
544+
bs.iter().map(|b| count(ctx, b, depth_curr + 1, depth_max)).sum()
542545
}
543546
}
547+
Binding::Empty => Ok(0),
548+
Binding::Fragment(_) | Binding::Missing(_) => Ok(1),
544549
}
545550
}

crates/mbe/src/parser.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ pub(crate) enum Op<S> {
5454
Var { name: SmolStr, kind: Option<MetaVarKind>, id: S },
5555
Ignore { name: SmolStr, id: S },
5656
Index { depth: usize },
57-
Count { name: SmolStr, depth: Option<usize> },
57+
Length { depth: usize },
58+
Count { name: SmolStr, depth: usize },
5859
Repeat { tokens: MetaTemplate<S>, kind: RepeatKind, separator: Option<Separator<S>> },
5960
Subtree { tokens: MetaTemplate<S>, delimiter: tt::Delimiter<S> },
6061
Literal(tt::Literal<S>),
@@ -299,15 +300,16 @@ fn parse_metavar_expr<S: Span>(src: &mut TtIter<'_, S>) -> Result<Op<S>, ()> {
299300

300301
let op = match &*func.text {
301302
"ignore" => {
303+
args.expect_dollar()?;
302304
let ident = args.expect_ident()?;
303305
Op::Ignore { name: ident.text.clone(), id: ident.span }
304306
}
305307
"index" => Op::Index { depth: parse_depth(&mut args)? },
308+
"length" => Op::Length { depth: parse_depth(&mut args)? },
306309
"count" => {
310+
args.expect_dollar()?;
307311
let ident = args.expect_ident()?;
308-
// `${count(t)}` and `${count(t,)}` have different meanings. Not sure if this is a bug
309-
// but that's how it's implemented in rustc as of this writing. See rust-lang/rust#111904.
310-
let depth = if try_eat_comma(&mut args) { Some(parse_depth(&mut args)?) } else { None };
312+
let depth = if try_eat_comma(&mut args) { parse_depth(&mut args)? } else { 0 };
311313
Op::Count { name: ident.text.clone(), depth }
312314
}
313315
_ => return Err(()),

crates/mbe/src/tt_iter.rs

+7
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ impl<'a, S: Span> TtIter<'a, S> {
5151
}
5252
}
5353

54+
pub(crate) fn expect_dollar(&mut self) -> Result<(), ()> {
55+
match self.expect_leaf()? {
56+
tt::Leaf::Punct(tt::Punct { char: '$', .. }) => Ok(()),
57+
_ => Err(()),
58+
}
59+
}
60+
5461
pub(crate) fn expect_ident(&mut self) -> Result<&'a tt::Ident<S>, ()> {
5562
match self.expect_leaf()? {
5663
tt::Leaf::Ident(it) if it.text != "_" => Ok(it),

0 commit comments

Comments
 (0)