From b9bb4692a42c8cd8ccb72c17f03a9a125a70accc Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Mon, 4 Jan 2021 19:32:17 +1300 Subject: [PATCH] Allow source during parsing. Hacky but works (#2855) --- crates/nu-parser/src/parse.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/crates/nu-parser/src/parse.rs b/crates/nu-parser/src/parse.rs index e8b132b108..47273c1d6f 100644 --- a/crates/nu-parser/src/parse.rs +++ b/crates/nu-parser/src/parse.rs @@ -1786,6 +1786,36 @@ fn parse_call( error, ); } + } else if lite_cmd.parts[0].item == "source" { + if lite_cmd.parts.len() != 2 { + return ( + None, + Some(ParseError::argument_error( + lite_cmd.parts[0].clone(), + ArgumentError::MissingMandatoryPositional("a path for sourcing".into()), + )), + ); + } + if lite_cmd.parts[1].item.starts_with('$') { + return ( + None, + Some(ParseError::mismatch( + "a filepath constant", + lite_cmd.parts[1].clone(), + )), + ); + } + if let Ok(contents) = std::fs::read_to_string(&lite_cmd.parts[1].item) { + let _ = parse(&contents, 0, scope); + } else { + return ( + None, + Some(ParseError::mismatch( + "a filepath to a source file", + lite_cmd.parts[1].clone(), + )), + ); + } } else if lite_cmd.parts.len() > 1 { // Check if it's a sub-command if let Some(signature) = scope.get_signature(&format!(