forked from extern/nushell
commit
248decc546
@ -34,7 +34,6 @@ impl Highlighter for NuHighlighter {
|
|||||||
.to_string();
|
.to_string();
|
||||||
output.push((Style::new(), gap));
|
output.push((Style::new(), gap));
|
||||||
}
|
}
|
||||||
|
|
||||||
let next_token = line
|
let next_token = line
|
||||||
[(shape.0.start - global_span_offset)..(shape.0.end - global_span_offset)]
|
[(shape.0.start - global_span_offset)..(shape.0.end - global_span_offset)]
|
||||||
.to_string();
|
.to_string();
|
||||||
|
@ -16,4 +16,4 @@ nu-table = { path = "../nu-table" }
|
|||||||
glob = "0.3.0"
|
glob = "0.3.0"
|
||||||
thiserror = "1.0.29"
|
thiserror = "1.0.29"
|
||||||
sysinfo = "0.20.4"
|
sysinfo = "0.20.4"
|
||||||
chrono = { version="0.4.19", features=["serde"] }
|
chrono = { version="0.4.19", features=["serde"] }
|
||||||
|
@ -7,6 +7,7 @@ mod hide;
|
|||||||
mod if_;
|
mod if_;
|
||||||
mod let_;
|
mod let_;
|
||||||
mod module;
|
mod module;
|
||||||
|
mod source;
|
||||||
mod use_;
|
mod use_;
|
||||||
|
|
||||||
pub use alias::Alias;
|
pub use alias::Alias;
|
||||||
@ -18,4 +19,5 @@ pub use hide::Hide;
|
|||||||
pub use if_::If;
|
pub use if_::If;
|
||||||
pub use let_::Let;
|
pub use let_::Let;
|
||||||
pub use module::Module;
|
pub use module::Module;
|
||||||
|
pub use source::Source;
|
||||||
pub use use_::Use;
|
pub use use_::Use;
|
||||||
|
43
crates/nu-command/src/core_commands/source.rs
Normal file
43
crates/nu-command/src/core_commands/source.rs
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
use nu_engine::{eval_block, CallExt};
|
||||||
|
use nu_protocol::ast::Call;
|
||||||
|
use nu_protocol::engine::{Command, EvaluationContext};
|
||||||
|
use nu_protocol::{ShellError, Signature, SyntaxShape, Value};
|
||||||
|
|
||||||
|
/// Source a file for environment variables.
|
||||||
|
pub struct Source;
|
||||||
|
|
||||||
|
impl Command for Source {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"source"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> Signature {
|
||||||
|
Signature::build("source").required(
|
||||||
|
"filename",
|
||||||
|
SyntaxShape::Filepath,
|
||||||
|
"the filepath to the script file to source",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"Runs a script file in the current context."
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
context: &EvaluationContext,
|
||||||
|
call: &Call,
|
||||||
|
input: Value,
|
||||||
|
) -> Result<Value, ShellError> {
|
||||||
|
// Note: this hidden positional is the block_id that corresponded to the 0th position
|
||||||
|
// it is put here by the parser
|
||||||
|
let block_id: i64 = call.req(context, 1)?;
|
||||||
|
|
||||||
|
let block = context
|
||||||
|
.engine_state
|
||||||
|
.borrow()
|
||||||
|
.get_block(block_id as usize)
|
||||||
|
.clone();
|
||||||
|
eval_block(context, &block, input)
|
||||||
|
}
|
||||||
|
}
|
@ -50,6 +50,8 @@ pub fn create_default_context() -> Rc<RefCell<EngineState>> {
|
|||||||
working_set.add_decl(Box::new(Git));
|
working_set.add_decl(Box::new(Git));
|
||||||
working_set.add_decl(Box::new(GitCheckout));
|
working_set.add_decl(Box::new(GitCheckout));
|
||||||
|
|
||||||
|
working_set.add_decl(Box::new(Source));
|
||||||
|
|
||||||
let sig = Signature::build("exit");
|
let sig = Signature::build("exit");
|
||||||
working_set.add_decl(sig.predeclare());
|
working_set.add_decl(sig.predeclare());
|
||||||
let sig = Signature::build("vars");
|
let sig = Signature::build("vars");
|
||||||
|
@ -3,12 +3,13 @@ use nu_protocol::{
|
|||||||
engine::StateWorkingSet,
|
engine::StateWorkingSet,
|
||||||
span, DeclId, Span, SyntaxShape, Type,
|
span, DeclId, Span, SyntaxShape, Type,
|
||||||
};
|
};
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
lex, lite_parse,
|
lex, lite_parse,
|
||||||
parser::{
|
parser::{
|
||||||
check_name, garbage, garbage_statement, parse_block_expression, parse_import_pattern,
|
check_name, garbage, garbage_statement, parse, parse_block_expression,
|
||||||
parse_internal_call, parse_signature, parse_string,
|
parse_import_pattern, parse_internal_call, parse_signature, parse_string,
|
||||||
},
|
},
|
||||||
ParseError,
|
ParseError,
|
||||||
};
|
};
|
||||||
@ -765,3 +766,95 @@ pub fn parse_let(
|
|||||||
)),
|
)),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn parse_source(
|
||||||
|
working_set: &mut StateWorkingSet,
|
||||||
|
spans: &[Span],
|
||||||
|
) -> (Statement, Option<ParseError>) {
|
||||||
|
let name = working_set.get_span_contents(spans[0]);
|
||||||
|
|
||||||
|
if name == b"source" {
|
||||||
|
if let Some(decl_id) = working_set.find_decl(b"source") {
|
||||||
|
// Is this the right call to be using here?
|
||||||
|
// Some of the others (`parse_let`) use it, some of them (`parse_hide`) don't.
|
||||||
|
let (call, call_span, err) =
|
||||||
|
parse_internal_call(working_set, spans[0], &spans[1..], decl_id);
|
||||||
|
|
||||||
|
// Command and one file name
|
||||||
|
if spans.len() >= 2 {
|
||||||
|
let name_expr = working_set.get_span_contents(spans[1]);
|
||||||
|
if let Ok(filename) = String::from_utf8(name_expr.to_vec()) {
|
||||||
|
let source_file = Path::new(&filename);
|
||||||
|
|
||||||
|
let path = source_file;
|
||||||
|
let contents = std::fs::read(path);
|
||||||
|
|
||||||
|
if let Ok(contents) = contents {
|
||||||
|
// This will load the defs from the file into the
|
||||||
|
// working set, if it was a successful parse.
|
||||||
|
let (block, err) = parse(
|
||||||
|
working_set,
|
||||||
|
path.file_name().and_then(|x| x.to_str()),
|
||||||
|
&contents,
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
|
||||||
|
if err.is_some() {
|
||||||
|
// Unsuccessful parse of file
|
||||||
|
return (
|
||||||
|
Statement::Pipeline(Pipeline::from_vec(vec![Expression {
|
||||||
|
expr: Expr::Call(call),
|
||||||
|
span: span(&spans[1..]),
|
||||||
|
ty: Type::Unknown,
|
||||||
|
custom_completion: None,
|
||||||
|
}])),
|
||||||
|
// Return the file parse error
|
||||||
|
err,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
// Save the block into the working set
|
||||||
|
let block_id = working_set.add_block(block);
|
||||||
|
|
||||||
|
let mut call_with_block = call;
|
||||||
|
|
||||||
|
// Adding this expression to the positional creates a syntax highlighting error
|
||||||
|
// after writing `source example.nu`
|
||||||
|
call_with_block.positional.push(Expression {
|
||||||
|
expr: Expr::Int(block_id as i64),
|
||||||
|
span: spans[1],
|
||||||
|
ty: Type::Unknown,
|
||||||
|
custom_completion: None,
|
||||||
|
});
|
||||||
|
|
||||||
|
return (
|
||||||
|
Statement::Pipeline(Pipeline::from_vec(vec![Expression {
|
||||||
|
expr: Expr::Call(call_with_block),
|
||||||
|
span: call_span,
|
||||||
|
ty: Type::Unknown,
|
||||||
|
custom_completion: None,
|
||||||
|
}])),
|
||||||
|
None,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
Statement::Pipeline(Pipeline::from_vec(vec![Expression {
|
||||||
|
expr: Expr::Call(call),
|
||||||
|
span: call_span,
|
||||||
|
ty: Type::Unknown,
|
||||||
|
custom_completion: None,
|
||||||
|
}])),
|
||||||
|
err,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(
|
||||||
|
garbage_statement(spans),
|
||||||
|
Some(ParseError::UnknownState(
|
||||||
|
"internal error: source statement unparseable".into(),
|
||||||
|
span(spans),
|
||||||
|
)),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
lex, lite_parse,
|
lex, lite_parse,
|
||||||
|
parse_keywords::parse_source,
|
||||||
type_check::{math_result_type, type_compatible},
|
type_check::{math_result_type, type_compatible},
|
||||||
LiteBlock, ParseError, Token, TokenContents,
|
LiteBlock, ParseError, Token, TokenContents,
|
||||||
};
|
};
|
||||||
@ -2870,6 +2871,7 @@ pub fn parse_statement(
|
|||||||
b"alias" => parse_alias(working_set, spans),
|
b"alias" => parse_alias(working_set, spans),
|
||||||
b"module" => parse_module(working_set, spans),
|
b"module" => parse_module(working_set, spans),
|
||||||
b"use" => parse_use(working_set, spans),
|
b"use" => parse_use(working_set, spans),
|
||||||
|
b"source" => parse_source(working_set, spans),
|
||||||
b"export" => (
|
b"export" => (
|
||||||
garbage_statement(spans),
|
garbage_statement(spans),
|
||||||
Some(ParseError::UnexpectedKeyword("export".into(), spans[0])),
|
Some(ParseError::UnexpectedKeyword("export".into(), spans[0])),
|
||||||
|
@ -247,6 +247,7 @@ impl Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Follow a given column path into the value: for example accessing nth elements in a stream or list
|
||||||
pub fn follow_cell_path(self, cell_path: &[PathMember]) -> Result<Value, ShellError> {
|
pub fn follow_cell_path(self, cell_path: &[PathMember]) -> Result<Value, ShellError> {
|
||||||
let mut current = self;
|
let mut current = self;
|
||||||
for member in cell_path {
|
for member in cell_path {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
|
/// A Range is an iterator over integers.
|
||||||
use crate::{
|
use crate::{
|
||||||
ast::{RangeInclusion, RangeOperator},
|
ast::{RangeInclusion, RangeOperator},
|
||||||
*,
|
*,
|
||||||
|
Loading…
Reference in New Issue
Block a user