1
Fork 0

Update the macro parser to allow at most once repetitions for ? Kleene

This commit is contained in:
Mark Mansi 2018-01-18 19:30:15 -06:00
parent 760879bc88
commit bb8110c1fc

View file

@ -486,8 +486,8 @@ fn inner_parse_loop(
match item.top_elts.get_tt(idx) { match item.top_elts.get_tt(idx) {
// Need to descend into a sequence // Need to descend into a sequence
TokenTree::Sequence(sp, seq) => { TokenTree::Sequence(sp, seq) => {
if seq.op == quoted::KleeneOp::ZeroOrMore {
// Examine the case where there are 0 matches of this sequence // Examine the case where there are 0 matches of this sequence
if seq.op == quoted::KleeneOp::ZeroOrMore || seq.op == quoted::KleeneOp::ZeroOrOne {
let mut new_item = item.clone(); let mut new_item = item.clone();
new_item.match_cur += seq.num_captures; new_item.match_cur += seq.num_captures;
new_item.idx += 1; new_item.idx += 1;
@ -497,7 +497,12 @@ fn inner_parse_loop(
cur_items.push(new_item); cur_items.push(new_item);
} }
// Examine the case where there is at least one match of this sequence // For ZeroOrMore and OneOrMore, we want to examine the case were there is at
// least one match. For ZeroOrOne, we only want the case where there is exactly
// one match.
if (seq.op == quoted::KleeneOp::ZeroOrOne && seq.num_captures == 1) ||
seq.op != quoted::KleeneOp::ZeroOrOne {
let matches = create_matches(item.matches.len()); let matches = create_matches(item.matches.len());
cur_items.push(Box::new(MatcherPos { cur_items.push(Box::new(MatcherPos {
stack: vec![], stack: vec![],
@ -512,6 +517,7 @@ fn inner_parse_loop(
top_elts: Tt(TokenTree::Sequence(sp, seq)), top_elts: Tt(TokenTree::Sequence(sp, seq)),
})); }));
} }
}
// We need to match a metavar (but the identifier is invalid)... this is an error // We need to match a metavar (but the identifier is invalid)... this is an error
TokenTree::MetaVarDecl(span, _, id) if id.name == keywords::Invalid.name() => { TokenTree::MetaVarDecl(span, _, id) if id.name == keywords::Invalid.name() => {