Skip to content

Commit bb8110c

Browse files
committed
Update the macro parser to allow at most once repetitions for ? Kleene
1 parent 760879b commit bb8110c

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

src/libsyntax/ext/tt/macro_parser.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,8 @@ fn inner_parse_loop(
486486
match item.top_elts.get_tt(idx) {
487487
// Need to descend into a sequence
488488
TokenTree::Sequence(sp, seq) => {
489-
if seq.op == quoted::KleeneOp::ZeroOrMore {
490-
// Examine the case where there are 0 matches of this sequence
489+
// Examine the case where there are 0 matches of this sequence
490+
if seq.op == quoted::KleeneOp::ZeroOrMore || seq.op == quoted::KleeneOp::ZeroOrOne {
491491
let mut new_item = item.clone();
492492
new_item.match_cur += seq.num_captures;
493493
new_item.idx += 1;
@@ -497,20 +497,26 @@ fn inner_parse_loop(
497497
cur_items.push(new_item);
498498
}
499499

500-
// Examine the case where there is at least one match of this sequence
501-
let matches = create_matches(item.matches.len());
502-
cur_items.push(Box::new(MatcherPos {
503-
stack: vec![],
504-
sep: seq.separator.clone(),
505-
idx: 0,
506-
matches,
507-
match_lo: item.match_cur,
508-
match_cur: item.match_cur,
509-
match_hi: item.match_cur + seq.num_captures,
510-
up: Some(item),
511-
sp_lo: sp.lo(),
512-
top_elts: Tt(TokenTree::Sequence(sp, seq)),
513-
}));
500+
// For ZeroOrMore and OneOrMore, we want to examine the case were there is at
501+
// least one match. For ZeroOrOne, we only want the case where there is exactly
502+
// one match.
503+
if (seq.op == quoted::KleeneOp::ZeroOrOne && seq.num_captures == 1) ||
504+
seq.op != quoted::KleeneOp::ZeroOrOne {
505+
506+
let matches = create_matches(item.matches.len());
507+
cur_items.push(Box::new(MatcherPos {
508+
stack: vec![],
509+
sep: seq.separator.clone(),
510+
idx: 0,
511+
matches,
512+
match_lo: item.match_cur,
513+
match_cur: item.match_cur,
514+
match_hi: item.match_cur + seq.num_captures,
515+
up: Some(item),
516+
sp_lo: sp.lo(),
517+
top_elts: Tt(TokenTree::Sequence(sp, seq)),
518+
}));
519+
}
514520
}
515521

516522
// We need to match a metavar (but the identifier is invalid)... this is an error

0 commit comments

Comments
 (0)