This commit is contained in:
Greg Shuflin 2021-11-12 00:06:42 -08:00
parent e618498881
commit 92c6d7f311
2 changed files with 28 additions and 4 deletions

View File

@ -33,9 +33,33 @@ peg::parser! {
kind:statement_kind() { Statement { id: Default::default(), location: Default::default(), kind } } kind:statement_kind() { Statement { id: Default::default(), location: Default::default(), kind } }
rule statement_kind() -> StatementKind = rule statement_kind() -> StatementKind =
__ import:import() { StatementKind::Import(import) } /
__ decl:declaration() { StatementKind::Declaration(decl) } / __ decl:declaration() { StatementKind::Declaration(decl) } /
__ expr:expression() { StatementKind::Expression(expr) } __ 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<Rc<String>> =
"::"? 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 = rule declaration() -> Declaration =
binding() / type_decl() / annotation() / func() / interface() / implementation() / module() binding() / type_decl() / annotation() / func() / interface() / implementation() / module()

View File

@ -1049,7 +1049,7 @@ fn modules() {
#[test] #[test]
fn imports() { fn imports() {
assert_ast! { assert_ast2! {
"import harbinger::draughts::Norgleheim", "import harbinger::draughts::Norgleheim",
vec![stmt(StatementKind::Import(ImportSpecifier { vec![stmt(StatementKind::Import(ImportSpecifier {
id: ItemId::default(), id: ItemId::default(),
@ -1058,7 +1058,7 @@ fn imports() {
}))] }))]
}; };
assert_ast! { assert_ast2! {
"import harbinger::draughts::{Norgleheim, Xraksenlaigar}", "import harbinger::draughts::{Norgleheim, Xraksenlaigar}",
vec![stmt(StatementKind::Import(ImportSpecifier { vec![stmt(StatementKind::Import(ImportSpecifier {
id: ItemId::default(), id: ItemId::default(),
@ -1068,7 +1068,7 @@ fn imports() {
}))] }))]
}; };
assert_ast! { assert_ast2! {
"import bespouri::{}", "import bespouri::{}",
vec![stmt(StatementKind::Import(ImportSpecifier { vec![stmt(StatementKind::Import(ImportSpecifier {
id: Default::default(), id: Default::default(),
@ -1077,7 +1077,7 @@ fn imports() {
}))] }))]
}; };
assert_ast! { assert_ast2! {
"import bespouri::*", "import bespouri::*",
vec![stmt(StatementKind::Import(ImportSpecifier { vec![stmt(StatementKind::Import(ImportSpecifier {
id: Default::default(), id: Default::default(),