From 92c6d7f31193f547798729f33426846052515062 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Fri, 12 Nov 2021 00:06:42 -0800 Subject: [PATCH] imports --- schala-lang/language/src/parsing/new.rs | 24 ++++++++++++++++++++++++ schala-lang/language/src/parsing/test.rs | 8 ++++---- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/schala-lang/language/src/parsing/new.rs b/schala-lang/language/src/parsing/new.rs index 9cd32cf..5be93ae 100644 --- a/schala-lang/language/src/parsing/new.rs +++ b/schala-lang/language/src/parsing/new.rs @@ -33,9 +33,33 @@ peg::parser! { kind:statement_kind() { Statement { id: Default::default(), location: Default::default(), kind } } rule statement_kind() -> StatementKind = + __ import:import() { StatementKind::Import(import) } / __ decl:declaration() { StatementKind::Declaration(decl) } / __ expr:expression() { StatementKind::Expression(expr) } + rule import() -> ImportSpecifier = + "import" _ path_components:path_components() suffix:import_suffix()? { + ImportSpecifier { + id: Default::default(), + path_components, + imported_names: suffix.unwrap_or_else(|| ImportedNames::LastOfPath) + } + } + + rule path_components() -> Vec> = + "::"? name:identifier() rest:path_component()* { + let mut items = vec![rc_string(name)]; + items.extend(rest.into_iter().map(|n| rc_string(n))); + items + } + + rule path_component() -> &'input str = "::" ident:identifier() { ident } + + rule import_suffix() -> ImportedNames = + "::*" { ImportedNames::All } / + "::{" __ names:(identifier() ** (_ "," _)) __ "}" { ImportedNames::List(names.into_iter().map(rc_string).collect()) } + + rule declaration() -> Declaration = binding() / type_decl() / annotation() / func() / interface() / implementation() / module() diff --git a/schala-lang/language/src/parsing/test.rs b/schala-lang/language/src/parsing/test.rs index b25e511..1d0b95f 100644 --- a/schala-lang/language/src/parsing/test.rs +++ b/schala-lang/language/src/parsing/test.rs @@ -1049,7 +1049,7 @@ fn modules() { #[test] fn imports() { - assert_ast! { + assert_ast2! { "import harbinger::draughts::Norgleheim", vec![stmt(StatementKind::Import(ImportSpecifier { id: ItemId::default(), @@ -1058,7 +1058,7 @@ fn imports() { }))] }; - assert_ast! { + assert_ast2! { "import harbinger::draughts::{Norgleheim, Xraksenlaigar}", vec![stmt(StatementKind::Import(ImportSpecifier { id: ItemId::default(), @@ -1068,7 +1068,7 @@ fn imports() { }))] }; - assert_ast! { + assert_ast2! { "import bespouri::{}", vec![stmt(StatementKind::Import(ImportSpecifier { id: Default::default(), @@ -1077,7 +1077,7 @@ fn imports() { }))] }; - assert_ast! { + assert_ast2! { "import bespouri::*", vec![stmt(StatementKind::Import(ImportSpecifier { id: Default::default(),