Disallow empty import groups

This commit is contained in:
Greg Shuflin 2021-11-21 02:30:45 -08:00
parent 91985df449
commit 9adae9c262
3 changed files with 9 additions and 11 deletions

View File

@ -197,7 +197,7 @@ fn import(input: Span) -> ParseResult<ImportSpecifier> {
map(
preceded(
tag("::"),
delimited(char('{'), separated_list0(tok(char(',')), identifier), char('}')),
delimited(char('{'), cut(separated_list1(tok(char(',')), identifier)), char('}')),
),
|names| ImportedNames::List(names.into_iter().map(|n| rc_string(n.fragment())).collect()),
),

View File

@ -78,7 +78,13 @@ peg::parser! {
rule import_suffix() -> ImportedNames =
"::*" { ImportedNames::All } /
"::{" __ names:(identifier() ** (_ "," _)) __ "}" { ImportedNames::List(names.into_iter().map(rc_string).collect()) }
"::{" __ names:(identifier() ** (_ "," _)) __ "}" {?
if names.is_empty() {
Err("import groups must have at least one item")
} else {
Ok(ImportedNames::List(names.into_iter().map(rc_string).collect()))
}
}
rule declaration(parser: &mut Parser) -> Declaration =
binding(parser) / type_decl(parser) / annotation(parser) / func(parser) / interface(parser) /

View File

@ -1106,15 +1106,7 @@ fn imports() {
}))]
};
//TODO this shouldn't be legal
assert_ast! {
"import bespouri::{}",
vec![stmt(StatementKind::Import(ImportSpecifier {
id: Default::default(),
path_components: vec![rc("bespouri")],
imported_names: ImportedNames::List(vec![]),
}))]
};
assert_fail!("import bespouri::{}");
assert_ast! {
"import bespouri::*",