From 9adae9c262f15fc1ea8b4266ce8bb238a4fc0070 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Sun, 21 Nov 2021 02:30:45 -0800 Subject: [PATCH] Disallow empty import groups --- schala-lang/src/parsing/combinator.rs | 2 +- schala-lang/src/parsing/peg_parser.rs | 8 +++++++- schala-lang/src/parsing/test.rs | 10 +--------- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/schala-lang/src/parsing/combinator.rs b/schala-lang/src/parsing/combinator.rs index bac392d..67f596c 100644 --- a/schala-lang/src/parsing/combinator.rs +++ b/schala-lang/src/parsing/combinator.rs @@ -197,7 +197,7 @@ fn import(input: Span) -> ParseResult { 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()), ), diff --git a/schala-lang/src/parsing/peg_parser.rs b/schala-lang/src/parsing/peg_parser.rs index 3e07b18..a010677 100644 --- a/schala-lang/src/parsing/peg_parser.rs +++ b/schala-lang/src/parsing/peg_parser.rs @@ -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) / diff --git a/schala-lang/src/parsing/test.rs b/schala-lang/src/parsing/test.rs index 6e10f81..751934c 100644 --- a/schala-lang/src/parsing/test.rs +++ b/schala-lang/src/parsing/test.rs @@ -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::*",