From 0108a935ed50f4599a3dc537565d55acdc9a053c Mon Sep 17 00:00:00 2001 From: JT Date: Mon, 27 Sep 2021 13:23:22 +1300 Subject: [PATCH] add import lists --- crates/nu-parser/src/parse_keywords.rs | 18 +++++++++++ crates/nu-parser/src/parser.rs | 34 +++++++++++++++++++- crates/nu-protocol/src/ast/import_pattern.rs | 1 + src/tests.rs | 8 +++++ 4 files changed, 60 insertions(+), 1 deletion(-) diff --git a/crates/nu-parser/src/parse_keywords.rs b/crates/nu-parser/src/parse_keywords.rs index e7bcb38a3b..1908233d19 100644 --- a/crates/nu-parser/src/parse_keywords.rs +++ b/crates/nu-parser/src/parse_keywords.rs @@ -397,6 +397,24 @@ pub fn parse_use( new_exports } + ImportPatternMember::List { names } => { + let mut output = vec![]; + + for (name, span) in names { + let mut new_exports: Vec<(Vec, usize)> = exports + .iter() + .filter_map(|x| if &x.0 == name { Some(x.clone()) } else { None }) + .collect(); + + if new_exports.is_empty() { + error = error.or(Some(ParseError::ExportNotFound(*span))) + } else { + output.append(&mut new_exports) + } + } + + output + } } }; diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index a794b1a292..59508cdfe2 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -1376,7 +1376,7 @@ pub fn parse_type(_working_set: &StateWorkingSet, bytes: &[u8]) -> Type { } pub fn parse_import_pattern( - working_set: &StateWorkingSet, + working_set: &mut StateWorkingSet, span: Span, ) -> (ImportPattern, Option) { let source = working_set.get_span_contents(span); @@ -1409,6 +1409,38 @@ pub fn parse_import_pattern( }, error, ) + } else if tail.starts_with(b"[") { + let (result, err) = parse_list_expression(working_set, tail_span, &SyntaxShape::String); + error = error.or(err); + + let mut output = vec![]; + + match result { + Expression { + expr: Expr::List(list), + .. + } => { + for l in list { + let contents = working_set.get_span_contents(l.span); + output.push((contents.to_vec(), l.span)); + } + + ( + ImportPattern { + head, + members: vec![ImportPatternMember::List { names: output }], + }, + error, + ) + } + _ => ( + ImportPattern { + head, + members: vec![], + }, + Some(ParseError::ExportNotFound(result.span)), + ), + } } else { ( ImportPattern { diff --git a/crates/nu-protocol/src/ast/import_pattern.rs b/crates/nu-protocol/src/ast/import_pattern.rs index c5183d75eb..1b7d6ad0fd 100644 --- a/crates/nu-protocol/src/ast/import_pattern.rs +++ b/crates/nu-protocol/src/ast/import_pattern.rs @@ -4,6 +4,7 @@ use crate::Span; pub enum ImportPatternMember { Glob { span: Span }, Name { name: Vec, span: Span }, + List { names: Vec<(Vec, Span)> }, } #[derive(Debug, Clone)] diff --git a/src/tests.rs b/src/tests.rs index c95d8e6156..22b33e6323 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -374,3 +374,11 @@ fn module_imports_4() -> TestResult { "not find import", ) } + +#[test] +fn module_imports_5() -> TestResult { + run_test( + r#"module foo { def a [] { 1 }; def b [] { 2 }; def c [] { 3 } }; use foo.[a, c]; c"#, + "3", + ) +}