enforce parsing invariant for patterns

This commit is contained in:
Aleksey Kladov 2022-01-02 17:32:15 +03:00
parent 7989d567e2
commit e78424846e
4 changed files with 42 additions and 2 deletions

View File

@ -141,7 +141,7 @@ macro_rules! m1 { () => (Some(x) left overs) }
macro_rules! m2 { () => ($) }
fn main() {
let Some(x) = ();
let Some(x)left overs = ();
let /* parse error: expected pattern */
$ = ();
}

View File

@ -109,6 +109,19 @@ pub(crate) mod entry {
items::mod_contents(p, false);
m.complete(p, MACRO_ITEMS);
}
pub(crate) fn pattern(p: &mut Parser) {
let m = p.start();
patterns::pattern_single(p);
if p.at(EOF) {
m.abandon(p);
return;
}
while !p.at(EOF) {
p.bump_any();
}
m.complete(p, ERROR);
}
}
}

View File

@ -119,8 +119,8 @@ impl TopEntryPoint {
TopEntryPoint::SourceFile => grammar::entry::top::source_file,
TopEntryPoint::MacroStmts => grammar::entry::top::macro_stmts,
TopEntryPoint::MacroItems => grammar::entry::top::macro_items,
TopEntryPoint::Pattern => grammar::entry::top::pattern,
// FIXME
TopEntryPoint::Pattern => grammar::entry::prefix::pat,
TopEntryPoint::Type => grammar::entry::prefix::ty,
TopEntryPoint::Expr => grammar::entry::prefix::expr,
TopEntryPoint::MetaItem => grammar::entry::prefix::meta_item,

View File

@ -146,6 +146,33 @@ fn macro_pattern() {
R_PAREN ")"
"#]],
);
check(
TopEntryPoint::Pattern,
"None leftover tokens",
expect![[r#"
ERROR
IDENT_PAT
NAME
IDENT "None"
WHITESPACE " "
IDENT "leftover"
WHITESPACE " "
IDENT "tokens"
"#]],
);
check(
TopEntryPoint::Pattern,
"@err",
expect![[r#"
ERROR
ERROR
AT "@"
IDENT "err"
error 0: expected pattern
"#]],
);
}
#[track_caller]