2021-12-03 19:49:11 +00:00
|
|
|
use nu_path::canonicalize;
|
2021-09-26 18:39:19 +00:00
|
|
|
use nu_protocol::{
|
2021-11-15 23:16:06 +00:00
|
|
|
ast::{
|
|
|
|
Block, Call, Expr, Expression, ImportPattern, ImportPatternHead, ImportPatternMember,
|
|
|
|
Pipeline, Statement,
|
|
|
|
},
|
2021-09-26 18:39:19 +00:00
|
|
|
engine::StateWorkingSet,
|
2021-11-15 23:16:06 +00:00
|
|
|
span, Exportable, Overlay, Span, SyntaxShape, Type, CONFIG_VARIABLE_ID,
|
2021-09-26 18:39:19 +00:00
|
|
|
};
|
2021-11-30 06:14:05 +00:00
|
|
|
use std::collections::{HashMap, HashSet};
|
2021-09-26 18:39:19 +00:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
lex, lite_parse,
|
|
|
|
parser::{
|
2021-10-02 02:24:43 +00:00
|
|
|
check_name, garbage, garbage_statement, parse, parse_block_expression,
|
2021-11-14 19:40:26 +00:00
|
|
|
parse_import_pattern, parse_internal_call, parse_signature, parse_string, trim_quotes,
|
2021-09-26 18:39:19 +00:00
|
|
|
},
|
|
|
|
ParseError,
|
|
|
|
};
|
|
|
|
|
2021-10-01 20:16:27 +00:00
|
|
|
pub fn parse_def_predecl(working_set: &mut StateWorkingSet, spans: &[Span]) -> Option<ParseError> {
|
2021-09-26 18:39:19 +00:00
|
|
|
let name = working_set.get_span_contents(spans[0]);
|
|
|
|
|
2021-09-28 17:29:38 +00:00
|
|
|
// handle "export def" same as "def"
|
|
|
|
let (name, spans) = if name == b"export" && spans.len() >= 2 {
|
|
|
|
(working_set.get_span_contents(spans[1]), &spans[1..])
|
|
|
|
} else {
|
|
|
|
(name, spans)
|
|
|
|
};
|
|
|
|
|
2021-09-26 18:39:19 +00:00
|
|
|
if name == b"def" && spans.len() >= 4 {
|
|
|
|
let (name_expr, ..) = parse_string(working_set, spans[1]);
|
|
|
|
let name = name_expr.as_string();
|
|
|
|
|
|
|
|
working_set.enter_scope();
|
|
|
|
// FIXME: because parse_signature will update the scope with the variables it sees
|
|
|
|
// we end up parsing the signature twice per def. The first time is during the predecl
|
|
|
|
// so that we can see the types that are part of the signature, which we need for parsing.
|
|
|
|
// The second time is when we actually parse the body itworking_set.
|
|
|
|
// We can't reuse the first time because the variables that are created during parse_signature
|
|
|
|
// are lost when we exit the scope below.
|
|
|
|
let (sig, ..) = parse_signature(working_set, spans[2]);
|
|
|
|
let signature = sig.as_signature();
|
|
|
|
working_set.exit_scope();
|
|
|
|
|
|
|
|
if let (Some(name), Some(mut signature)) = (name, signature) {
|
|
|
|
signature.name = name;
|
|
|
|
let decl = signature.predeclare();
|
|
|
|
|
2021-10-01 20:16:27 +00:00
|
|
|
if working_set.add_predecl(decl).is_some() {
|
|
|
|
return Some(ParseError::DuplicateCommandDef(spans[1]));
|
|
|
|
}
|
2021-09-26 18:39:19 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-01 20:16:27 +00:00
|
|
|
|
|
|
|
None
|
2021-09-26 18:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse_def(
|
|
|
|
working_set: &mut StateWorkingSet,
|
|
|
|
spans: &[Span],
|
|
|
|
) -> (Statement, Option<ParseError>) {
|
|
|
|
let mut error = None;
|
|
|
|
let name = working_set.get_span_contents(spans[0]);
|
|
|
|
|
2021-09-27 01:03:50 +00:00
|
|
|
if name == b"def" {
|
2021-11-15 23:16:06 +00:00
|
|
|
// TODO: Convert all 'expect("internal error: ...")' to ParseError::InternalError
|
2021-09-27 01:03:50 +00:00
|
|
|
let def_decl_id = working_set
|
|
|
|
.find_decl(b"def")
|
|
|
|
.expect("internal error: missing def command");
|
|
|
|
|
|
|
|
let mut call = Box::new(Call {
|
|
|
|
head: spans[0],
|
|
|
|
decl_id: def_decl_id,
|
|
|
|
positional: vec![],
|
|
|
|
named: vec![],
|
|
|
|
});
|
|
|
|
|
2021-10-23 23:40:27 +00:00
|
|
|
if let Some(name_span) = spans.get(1) {
|
2021-09-27 01:03:50 +00:00
|
|
|
let (name_expr, err) = parse_string(working_set, *name_span);
|
|
|
|
error = error.or(err);
|
|
|
|
|
|
|
|
let name = name_expr.as_string();
|
|
|
|
call.positional.push(name_expr);
|
|
|
|
|
|
|
|
if let Some(sig_span) = spans.get(2) {
|
|
|
|
working_set.enter_scope();
|
|
|
|
let (sig, err) = parse_signature(working_set, *sig_span);
|
|
|
|
error = error.or(err);
|
|
|
|
|
|
|
|
let signature = sig.as_signature();
|
|
|
|
|
|
|
|
call.positional.push(sig);
|
|
|
|
|
|
|
|
if let Some(block_span) = spans.get(3) {
|
|
|
|
let (block, err) = parse_block_expression(
|
|
|
|
working_set,
|
|
|
|
&SyntaxShape::Block(Some(vec![])),
|
|
|
|
*block_span,
|
|
|
|
);
|
|
|
|
error = error.or(err);
|
|
|
|
|
|
|
|
let block_id = block.as_block();
|
|
|
|
|
|
|
|
call.positional.push(block);
|
|
|
|
|
|
|
|
if let (Some(name), Some(mut signature), Some(block_id)) =
|
2021-10-01 19:29:24 +00:00
|
|
|
(&name, signature, block_id)
|
2021-09-27 01:03:50 +00:00
|
|
|
{
|
2021-10-02 00:42:35 +00:00
|
|
|
if let Some(decl_id) = working_set.find_decl(name.as_bytes()) {
|
|
|
|
let declaration = working_set.get_decl_mut(decl_id);
|
2021-09-27 01:03:50 +00:00
|
|
|
|
2021-10-02 00:42:35 +00:00
|
|
|
signature.name = name.clone();
|
2021-09-27 01:03:50 +00:00
|
|
|
|
2021-10-02 00:42:35 +00:00
|
|
|
*declaration = signature.into_block_command(block_id);
|
|
|
|
} else {
|
|
|
|
error = error.or_else(|| {
|
2021-11-15 23:16:06 +00:00
|
|
|
Some(ParseError::InternalError(
|
|
|
|
"Predeclaration failed to add declaration".into(),
|
2021-10-02 00:42:35 +00:00
|
|
|
spans[1],
|
|
|
|
))
|
|
|
|
});
|
|
|
|
};
|
2021-09-27 01:03:50 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let err_span = Span {
|
|
|
|
start: sig_span.end,
|
|
|
|
end: sig_span.end,
|
|
|
|
};
|
|
|
|
|
|
|
|
error = error
|
|
|
|
.or_else(|| Some(ParseError::MissingPositional("block".into(), err_span)));
|
|
|
|
}
|
|
|
|
working_set.exit_scope();
|
|
|
|
|
2021-10-01 19:29:24 +00:00
|
|
|
if let Some(name) = name {
|
|
|
|
// It's OK if it returns None: The decl was already merged in previous parse
|
|
|
|
// pass.
|
|
|
|
working_set.merge_predecl(name.as_bytes());
|
|
|
|
} else {
|
|
|
|
error = error.or_else(|| {
|
|
|
|
Some(ParseError::UnknownState(
|
|
|
|
"Could not get string from string expression".into(),
|
|
|
|
*name_span,
|
|
|
|
))
|
|
|
|
});
|
|
|
|
}
|
2021-09-27 01:03:50 +00:00
|
|
|
} else {
|
|
|
|
let err_span = Span {
|
|
|
|
start: name_span.end,
|
|
|
|
end: name_span.end,
|
|
|
|
};
|
|
|
|
|
|
|
|
error = error
|
|
|
|
.or_else(|| Some(ParseError::MissingPositional("parameters".into(), err_span)));
|
2021-09-26 18:39:19 +00:00
|
|
|
}
|
2021-09-27 01:03:50 +00:00
|
|
|
} else {
|
|
|
|
let err_span = Span {
|
|
|
|
start: spans[0].end,
|
|
|
|
end: spans[0].end,
|
|
|
|
};
|
|
|
|
|
|
|
|
error = error.or_else(|| {
|
|
|
|
Some(ParseError::MissingPositional(
|
|
|
|
"definition name".into(),
|
|
|
|
err_span,
|
|
|
|
))
|
|
|
|
});
|
2021-10-23 23:40:27 +00:00
|
|
|
}
|
2021-09-27 01:03:50 +00:00
|
|
|
|
|
|
|
(
|
|
|
|
Statement::Pipeline(Pipeline::from_vec(vec![Expression {
|
|
|
|
expr: Expr::Call(call),
|
|
|
|
span: span(spans),
|
|
|
|
ty: Type::Unknown,
|
|
|
|
custom_completion: None,
|
|
|
|
}])),
|
|
|
|
error,
|
|
|
|
)
|
2021-09-26 18:39:19 +00:00
|
|
|
} else {
|
|
|
|
(
|
|
|
|
garbage_statement(spans),
|
|
|
|
Some(ParseError::UnknownState(
|
|
|
|
"Expected structure: def <name> [] {}".into(),
|
|
|
|
span(spans),
|
|
|
|
)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse_alias(
|
|
|
|
working_set: &mut StateWorkingSet,
|
|
|
|
spans: &[Span],
|
|
|
|
) -> (Statement, Option<ParseError>) {
|
|
|
|
let name = working_set.get_span_contents(spans[0]);
|
|
|
|
|
|
|
|
if name == b"alias" {
|
|
|
|
if let Some((span, err)) = check_name(working_set, spans) {
|
|
|
|
return (
|
|
|
|
Statement::Pipeline(Pipeline::from_vec(vec![garbage(*span)])),
|
|
|
|
Some(err),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(decl_id) = working_set.find_decl(b"alias") {
|
|
|
|
let (call, call_span, _) =
|
|
|
|
parse_internal_call(working_set, spans[0], &spans[1..], decl_id);
|
|
|
|
|
|
|
|
if spans.len() >= 4 {
|
|
|
|
let alias_name = working_set.get_span_contents(spans[1]);
|
|
|
|
|
|
|
|
let alias_name = if alias_name.starts_with(b"\"")
|
|
|
|
&& alias_name.ends_with(b"\"")
|
|
|
|
&& alias_name.len() > 1
|
|
|
|
{
|
|
|
|
alias_name[1..(alias_name.len() - 1)].to_vec()
|
|
|
|
} else {
|
|
|
|
alias_name.to_vec()
|
|
|
|
};
|
|
|
|
let _equals = working_set.get_span_contents(spans[2]);
|
|
|
|
|
|
|
|
let replacement = spans[3..].to_vec();
|
|
|
|
|
|
|
|
working_set.add_alias(alias_name, replacement);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
Statement::Pipeline(Pipeline::from_vec(vec![Expression {
|
|
|
|
expr: Expr::Call(call),
|
|
|
|
span: call_span,
|
|
|
|
ty: Type::Unknown,
|
|
|
|
custom_completion: None,
|
|
|
|
}])),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(
|
|
|
|
garbage_statement(spans),
|
2021-11-15 23:16:06 +00:00
|
|
|
Some(ParseError::InternalError(
|
|
|
|
"Alias statement unparseable".into(),
|
2021-09-26 18:39:19 +00:00
|
|
|
span(spans),
|
|
|
|
)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-09-28 17:29:38 +00:00
|
|
|
pub fn parse_export(
|
|
|
|
working_set: &mut StateWorkingSet,
|
|
|
|
spans: &[Span],
|
2021-11-15 23:16:06 +00:00
|
|
|
) -> (Statement, Option<Exportable>, Option<ParseError>) {
|
|
|
|
let mut error = None;
|
|
|
|
|
|
|
|
let export_span = if let Some(sp) = spans.get(0) {
|
|
|
|
if working_set.get_span_contents(*sp) != b"export" {
|
|
|
|
return (
|
|
|
|
garbage_statement(spans),
|
|
|
|
None,
|
|
|
|
Some(ParseError::UnknownState(
|
|
|
|
"expected export statement".into(),
|
|
|
|
span(spans),
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
*sp
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
garbage_statement(spans),
|
|
|
|
None,
|
|
|
|
Some(ParseError::UnknownState(
|
|
|
|
"got empty input for parsing export statement".into(),
|
|
|
|
span(spans),
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
let export_decl_id = if let Some(id) = working_set.find_decl(b"export") {
|
|
|
|
id
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
garbage_statement(spans),
|
|
|
|
None,
|
|
|
|
Some(ParseError::InternalError(
|
|
|
|
"missing export command".into(),
|
|
|
|
export_span,
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
};
|
2021-09-28 17:29:38 +00:00
|
|
|
|
2021-11-15 23:16:06 +00:00
|
|
|
let mut call = Box::new(Call {
|
|
|
|
head: spans[0],
|
|
|
|
decl_id: export_decl_id,
|
|
|
|
positional: vec![],
|
|
|
|
named: vec![],
|
|
|
|
});
|
2021-09-28 17:29:38 +00:00
|
|
|
|
2021-11-15 23:16:06 +00:00
|
|
|
let exportable = if let Some(kw_span) = spans.get(1) {
|
|
|
|
let kw_name = working_set.get_span_contents(*kw_span);
|
|
|
|
match kw_name {
|
2021-09-28 18:03:53 +00:00
|
|
|
b"def" => {
|
|
|
|
let (stmt, err) = parse_def(working_set, &spans[1..]);
|
2021-11-15 23:16:06 +00:00
|
|
|
error = error.or(err);
|
2021-09-28 18:03:53 +00:00
|
|
|
|
2021-11-15 23:16:06 +00:00
|
|
|
let export_def_decl_id = if let Some(id) = working_set.find_decl(b"export def") {
|
|
|
|
id
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
garbage_statement(spans),
|
|
|
|
None,
|
|
|
|
Some(ParseError::InternalError(
|
|
|
|
"missing 'export def' command".into(),
|
|
|
|
export_span,
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
};
|
2021-09-28 18:03:53 +00:00
|
|
|
|
|
|
|
// Trying to warp the 'def' call into the 'export def' in a very clumsy way
|
2021-11-15 23:16:06 +00:00
|
|
|
if let Statement::Pipeline(ref pipe) = stmt {
|
|
|
|
if let Some(Expression {
|
|
|
|
expr: Expr::Call(ref def_call),
|
|
|
|
..
|
|
|
|
}) = pipe.expressions.get(0)
|
|
|
|
{
|
|
|
|
call = def_call.clone();
|
|
|
|
|
|
|
|
call.head = span(&spans[0..=1]);
|
|
|
|
call.decl_id = export_def_decl_id;
|
2021-09-28 18:03:53 +00:00
|
|
|
} else {
|
2021-11-15 23:16:06 +00:00
|
|
|
error = error.or_else(|| {
|
|
|
|
Some(ParseError::InternalError(
|
|
|
|
"unexpected output from parsing a definition".into(),
|
|
|
|
span(&spans[1..]),
|
|
|
|
))
|
|
|
|
});
|
2021-09-28 18:03:53 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-11-15 23:16:06 +00:00
|
|
|
error = error.or_else(|| {
|
|
|
|
Some(ParseError::InternalError(
|
|
|
|
"unexpected output from parsing a definition".into(),
|
|
|
|
span(&spans[1..]),
|
|
|
|
))
|
|
|
|
});
|
2021-09-28 18:03:53 +00:00
|
|
|
};
|
|
|
|
|
2021-11-15 23:16:06 +00:00
|
|
|
if error.is_none() {
|
|
|
|
let decl_name = working_set.get_span_contents(spans[2]);
|
|
|
|
let decl_name = trim_quotes(decl_name);
|
|
|
|
if let Some(decl_id) = working_set.find_decl(decl_name) {
|
|
|
|
Some(Exportable::Decl(decl_id))
|
|
|
|
} else {
|
|
|
|
error = error.or_else(|| {
|
|
|
|
Some(ParseError::InternalError(
|
|
|
|
"failed to find added declaration".into(),
|
|
|
|
span(&spans[1..]),
|
|
|
|
))
|
|
|
|
});
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b"env" => {
|
|
|
|
if let Some(id) = working_set.find_decl(b"export env") {
|
|
|
|
call.decl_id = id;
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
garbage_statement(spans),
|
|
|
|
None,
|
|
|
|
Some(ParseError::InternalError(
|
|
|
|
"missing 'export env' command".into(),
|
|
|
|
export_span,
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
call.head = span(&spans[0..=1]);
|
|
|
|
|
|
|
|
if let Some(name_span) = spans.get(2) {
|
|
|
|
let (name_expr, err) = parse_string(working_set, *name_span);
|
|
|
|
error = error.or(err);
|
|
|
|
call.positional.push(name_expr);
|
|
|
|
|
|
|
|
if let Some(block_span) = spans.get(3) {
|
|
|
|
let (block_expr, err) = parse_block_expression(
|
|
|
|
working_set,
|
|
|
|
&SyntaxShape::Block(None),
|
|
|
|
*block_span,
|
|
|
|
);
|
|
|
|
error = error.or(err);
|
|
|
|
|
|
|
|
let exportable = if let Expression {
|
|
|
|
expr: Expr::Block(block_id),
|
|
|
|
..
|
|
|
|
} = block_expr
|
|
|
|
{
|
|
|
|
Some(Exportable::EnvVar(block_id))
|
|
|
|
} else {
|
|
|
|
error = error.or_else(|| {
|
|
|
|
Some(ParseError::InternalError(
|
|
|
|
"block was not parsed as a block".into(),
|
|
|
|
*block_span,
|
|
|
|
))
|
|
|
|
});
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
call.positional.push(block_expr);
|
|
|
|
|
|
|
|
exportable
|
|
|
|
} else {
|
|
|
|
let err_span = Span {
|
|
|
|
start: name_span.end,
|
|
|
|
end: name_span.end,
|
|
|
|
};
|
|
|
|
|
|
|
|
error = error.or_else(|| {
|
|
|
|
Some(ParseError::MissingPositional("block".into(), err_span))
|
|
|
|
});
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let err_span = Span {
|
|
|
|
start: kw_span.end,
|
|
|
|
end: kw_span.end,
|
|
|
|
};
|
|
|
|
|
|
|
|
error = error.or_else(|| {
|
|
|
|
Some(ParseError::MissingPositional(
|
|
|
|
"environment variable name".into(),
|
|
|
|
err_span,
|
|
|
|
))
|
|
|
|
});
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
error = error.or_else(|| {
|
|
|
|
Some(ParseError::Expected(
|
|
|
|
// TODO: Fill in more keywords as they come
|
|
|
|
"def or env keyword".into(),
|
|
|
|
spans[1],
|
|
|
|
))
|
|
|
|
});
|
|
|
|
|
|
|
|
None
|
2021-09-28 18:03:53 +00:00
|
|
|
}
|
2021-09-28 17:29:38 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-11-15 23:16:06 +00:00
|
|
|
error = error.or_else(|| {
|
|
|
|
Some(ParseError::MissingPositional(
|
|
|
|
"def or env keyword".into(), // TODO: keep filling more keywords as they come
|
|
|
|
Span {
|
|
|
|
start: export_span.end,
|
|
|
|
end: export_span.end,
|
|
|
|
},
|
|
|
|
))
|
|
|
|
});
|
|
|
|
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
(
|
|
|
|
Statement::Pipeline(Pipeline::from_vec(vec![Expression {
|
|
|
|
expr: Expr::Call(call),
|
|
|
|
span: span(spans),
|
|
|
|
ty: Type::Unknown,
|
|
|
|
custom_completion: None,
|
|
|
|
}])),
|
|
|
|
exportable,
|
|
|
|
error,
|
|
|
|
)
|
2021-09-28 17:29:38 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:19:25 +00:00
|
|
|
pub fn parse_module_block(
|
|
|
|
working_set: &mut StateWorkingSet,
|
2021-10-31 15:22:10 +00:00
|
|
|
span: Span,
|
2021-11-17 04:23:55 +00:00
|
|
|
) -> (Block, Overlay, Option<ParseError>) {
|
2021-10-18 20:19:25 +00:00
|
|
|
let mut error = None;
|
|
|
|
|
|
|
|
working_set.enter_scope();
|
|
|
|
|
2021-10-31 15:22:10 +00:00
|
|
|
let source = working_set.get_span_contents(span);
|
2021-10-18 20:19:25 +00:00
|
|
|
|
2021-11-21 18:13:09 +00:00
|
|
|
let (output, err) = lex(source, span.start, &[], &[], true);
|
2021-10-18 20:19:25 +00:00
|
|
|
error = error.or(err);
|
|
|
|
|
|
|
|
let (output, err) = lite_parse(&output);
|
|
|
|
error = error.or(err);
|
|
|
|
|
|
|
|
for pipeline in &output.block {
|
2021-11-15 23:16:06 +00:00
|
|
|
// TODO: Should we add export env predecls as well?
|
2021-10-18 20:19:25 +00:00
|
|
|
if pipeline.commands.len() == 1 {
|
|
|
|
parse_def_predecl(working_set, &pipeline.commands[0].parts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-15 23:16:06 +00:00
|
|
|
let mut overlay = Overlay::new();
|
2021-10-18 20:19:25 +00:00
|
|
|
|
|
|
|
let block: Block = output
|
|
|
|
.block
|
|
|
|
.iter()
|
|
|
|
.map(|pipeline| {
|
|
|
|
if pipeline.commands.len() == 1 {
|
|
|
|
let name = working_set.get_span_contents(pipeline.commands[0].parts[0]);
|
|
|
|
|
|
|
|
let (stmt, err) = match name {
|
|
|
|
b"def" => {
|
|
|
|
let (stmt, err) = parse_def(working_set, &pipeline.commands[0].parts);
|
|
|
|
|
|
|
|
(stmt, err)
|
|
|
|
}
|
2021-11-15 23:16:06 +00:00
|
|
|
// TODO: Currently, it is not possible to define a private env var.
|
|
|
|
// TODO: Exported env vars are usable iside the module only if correctly
|
|
|
|
// exported by the user. For example:
|
|
|
|
//
|
|
|
|
// > module foo { export env a { "2" }; export def b [] { $nu.env.a } }
|
|
|
|
//
|
|
|
|
// will work only if you call `use foo *; b` but not with `use foo; foo b`
|
|
|
|
// since in the second case, the name of the env var would be $nu.env."foo a".
|
2021-10-18 20:19:25 +00:00
|
|
|
b"export" => {
|
2021-11-15 23:16:06 +00:00
|
|
|
let (stmt, exportable, err) =
|
|
|
|
parse_export(working_set, &pipeline.commands[0].parts);
|
2021-10-18 20:19:25 +00:00
|
|
|
|
|
|
|
if err.is_none() {
|
2021-11-15 23:16:06 +00:00
|
|
|
let name_span = pipeline.commands[0].parts[2];
|
|
|
|
let name = working_set.get_span_contents(name_span);
|
|
|
|
let name = trim_quotes(name);
|
|
|
|
|
|
|
|
match exportable {
|
|
|
|
Some(Exportable::Decl(decl_id)) => {
|
|
|
|
overlay.add_decl(name, decl_id);
|
|
|
|
}
|
|
|
|
Some(Exportable::EnvVar(block_id)) => {
|
|
|
|
overlay.add_env_var(name, block_id);
|
|
|
|
}
|
|
|
|
None => {} // None should always come with error from parse_export()
|
|
|
|
}
|
2021-10-18 20:19:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
(stmt, err)
|
|
|
|
}
|
|
|
|
_ => (
|
|
|
|
garbage_statement(&pipeline.commands[0].parts),
|
2021-11-15 23:16:06 +00:00
|
|
|
Some(ParseError::UnexpectedKeyword(
|
|
|
|
"expected def or export keyword".into(),
|
2021-10-18 20:19:25 +00:00
|
|
|
pipeline.commands[0].parts[0],
|
|
|
|
)),
|
|
|
|
),
|
|
|
|
};
|
|
|
|
|
|
|
|
if error.is_none() {
|
|
|
|
error = err;
|
|
|
|
}
|
|
|
|
|
|
|
|
stmt
|
|
|
|
} else {
|
2021-10-31 15:22:10 +00:00
|
|
|
error = Some(ParseError::Expected("not a pipeline".into(), span));
|
|
|
|
garbage_statement(&[span])
|
2021-10-18 20:19:25 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.into();
|
|
|
|
|
|
|
|
working_set.exit_scope();
|
|
|
|
|
2021-11-17 04:23:55 +00:00
|
|
|
(block, overlay, error)
|
2021-10-18 20:19:25 +00:00
|
|
|
}
|
|
|
|
|
2021-09-26 18:39:19 +00:00
|
|
|
pub fn parse_module(
|
|
|
|
working_set: &mut StateWorkingSet,
|
|
|
|
spans: &[Span],
|
|
|
|
) -> (Statement, Option<ParseError>) {
|
|
|
|
// TODO: Currently, module is closing over its parent scope (i.e., defs in the parent scope are
|
2021-10-10 11:31:13 +00:00
|
|
|
// visible and usable in this module's scope). We want to disable that for files.
|
2021-09-26 18:39:19 +00:00
|
|
|
|
|
|
|
let mut error = None;
|
|
|
|
let bytes = working_set.get_span_contents(spans[0]);
|
|
|
|
|
|
|
|
if bytes == b"module" && spans.len() >= 3 {
|
|
|
|
let (module_name_expr, err) = parse_string(working_set, spans[1]);
|
|
|
|
error = error.or(err);
|
|
|
|
|
|
|
|
let module_name = module_name_expr
|
|
|
|
.as_string()
|
|
|
|
.expect("internal error: module name is not a string");
|
|
|
|
|
|
|
|
let block_span = spans[2];
|
|
|
|
let block_bytes = working_set.get_span_contents(block_span);
|
|
|
|
let mut start = block_span.start;
|
|
|
|
let mut end = block_span.end;
|
|
|
|
|
|
|
|
if block_bytes.starts_with(b"{") {
|
|
|
|
start += 1;
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
garbage_statement(spans),
|
|
|
|
Some(ParseError::Expected("block".into(), block_span)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if block_bytes.ends_with(b"}") {
|
|
|
|
end -= 1;
|
|
|
|
} else {
|
2021-11-07 23:18:00 +00:00
|
|
|
error =
|
|
|
|
error.or_else(|| Some(ParseError::Unclosed("}".into(), Span { start: end, end })));
|
2021-09-26 18:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let block_span = Span { start, end };
|
|
|
|
|
2021-11-17 04:23:55 +00:00
|
|
|
let (block, overlay, err) = parse_module_block(working_set, block_span);
|
2021-09-26 18:39:19 +00:00
|
|
|
error = error.or(err);
|
|
|
|
|
2021-11-17 04:23:55 +00:00
|
|
|
let block_id = working_set.add_block(block);
|
|
|
|
let _ = working_set.add_overlay(&module_name, overlay);
|
2021-09-26 18:39:19 +00:00
|
|
|
|
|
|
|
let block_expr = Expression {
|
|
|
|
expr: Expr::Block(block_id),
|
|
|
|
span: block_span,
|
|
|
|
ty: Type::Block,
|
|
|
|
custom_completion: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let module_decl_id = working_set
|
|
|
|
.find_decl(b"module")
|
|
|
|
.expect("internal error: missing module command");
|
|
|
|
|
|
|
|
let call = Box::new(Call {
|
|
|
|
head: spans[0],
|
|
|
|
decl_id: module_decl_id,
|
|
|
|
positional: vec![module_name_expr, block_expr],
|
|
|
|
named: vec![],
|
|
|
|
});
|
|
|
|
|
|
|
|
(
|
|
|
|
Statement::Pipeline(Pipeline::from_vec(vec![Expression {
|
|
|
|
expr: Expr::Call(call),
|
|
|
|
span: span(spans),
|
|
|
|
ty: Type::Unknown,
|
|
|
|
custom_completion: None,
|
|
|
|
}])),
|
|
|
|
error,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
(
|
|
|
|
garbage_statement(spans),
|
|
|
|
Some(ParseError::UnknownState(
|
|
|
|
"Expected structure: module <name> {}".into(),
|
|
|
|
span(spans),
|
|
|
|
)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse_use(
|
|
|
|
working_set: &mut StateWorkingSet,
|
|
|
|
spans: &[Span],
|
|
|
|
) -> (Statement, Option<ParseError>) {
|
|
|
|
let mut error = None;
|
|
|
|
let bytes = working_set.get_span_contents(spans[0]);
|
|
|
|
|
|
|
|
if bytes == b"use" && spans.len() >= 2 {
|
2021-10-26 21:30:39 +00:00
|
|
|
for span in spans[1..].iter() {
|
2021-11-15 23:16:06 +00:00
|
|
|
let (_, err) = parse_string(working_set, *span);
|
2021-10-26 21:30:39 +00:00
|
|
|
error = error.or(err);
|
|
|
|
}
|
2021-09-26 18:39:19 +00:00
|
|
|
|
2021-10-26 21:30:39 +00:00
|
|
|
// TODO: Add checking for importing too long import patterns, e.g.:
|
|
|
|
// > use spam foo non existent names here do not throw error
|
2021-10-26 21:06:08 +00:00
|
|
|
let (import_pattern, err) = parse_import_pattern(working_set, &spans[1..]);
|
2021-09-26 18:39:19 +00:00
|
|
|
error = error.or(err);
|
|
|
|
|
2021-11-15 23:16:06 +00:00
|
|
|
let (import_pattern, overlay) =
|
2021-11-17 04:23:55 +00:00
|
|
|
if let Some(overlay_id) = working_set.find_overlay(&import_pattern.head.name) {
|
|
|
|
(import_pattern, working_set.get_overlay(overlay_id).clone())
|
2021-10-19 21:23:59 +00:00
|
|
|
} else {
|
2021-10-31 15:53:53 +00:00
|
|
|
// TODO: Do not close over when loading module from file
|
|
|
|
// It could be a file
|
2021-11-15 23:16:06 +00:00
|
|
|
if let Ok(module_filename) = String::from_utf8(import_pattern.head.name) {
|
2021-12-03 19:49:11 +00:00
|
|
|
if let Ok(module_path) = canonicalize(&module_filename) {
|
|
|
|
let module_name = if let Some(stem) = module_path.file_stem() {
|
|
|
|
stem.to_string_lossy().to_string()
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
garbage_statement(spans),
|
|
|
|
Some(ParseError::ModuleNotFound(spans[1])),
|
|
|
|
);
|
|
|
|
};
|
2021-10-19 21:23:59 +00:00
|
|
|
|
2021-12-03 19:49:11 +00:00
|
|
|
if let Ok(contents) = std::fs::read(module_path) {
|
|
|
|
let span_start = working_set.next_span_start();
|
|
|
|
working_set.add_file(module_filename, &contents);
|
|
|
|
let span_end = working_set.next_span_start();
|
|
|
|
|
|
|
|
let (block, overlay, err) =
|
|
|
|
parse_module_block(working_set, Span::new(span_start, span_end));
|
|
|
|
error = error.or(err);
|
|
|
|
|
|
|
|
let _ = working_set.add_block(block);
|
|
|
|
let _ = working_set.add_overlay(&module_name, overlay.clone());
|
|
|
|
|
|
|
|
(
|
|
|
|
ImportPattern {
|
|
|
|
head: ImportPatternHead {
|
|
|
|
name: module_name.into(),
|
|
|
|
span: spans[1],
|
|
|
|
},
|
|
|
|
members: import_pattern.members,
|
|
|
|
hidden: HashSet::new(),
|
2021-11-15 23:16:06 +00:00
|
|
|
},
|
2021-12-03 19:49:11 +00:00
|
|
|
overlay,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
garbage_statement(spans),
|
|
|
|
Some(ParseError::ModuleNotFound(spans[1])),
|
|
|
|
);
|
|
|
|
}
|
2021-10-31 15:53:53 +00:00
|
|
|
} else {
|
2021-12-03 19:49:11 +00:00
|
|
|
error = error.or(Some(ParseError::FileNotFound(module_filename)));
|
|
|
|
(ImportPattern::new(), Overlay::new())
|
2021-10-31 15:53:53 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
garbage_statement(spans),
|
|
|
|
Some(ParseError::NonUtf8(spans[1])),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
2021-09-26 18:39:19 +00:00
|
|
|
|
2021-11-15 23:16:06 +00:00
|
|
|
let decls_to_use = if import_pattern.members.is_empty() {
|
|
|
|
overlay.decls_with_head(&import_pattern.head.name)
|
2021-09-26 18:39:19 +00:00
|
|
|
} else {
|
|
|
|
match &import_pattern.members[0] {
|
2021-11-15 23:16:06 +00:00
|
|
|
ImportPatternMember::Glob { .. } => overlay.decls(),
|
2021-09-26 18:39:19 +00:00
|
|
|
ImportPatternMember::Name { name, span } => {
|
2021-11-15 23:16:06 +00:00
|
|
|
let mut output = vec![];
|
2021-09-26 18:39:19 +00:00
|
|
|
|
2021-11-15 23:16:06 +00:00
|
|
|
if let Some(id) = overlay.get_decl_id(name) {
|
|
|
|
output.push((name.clone(), id));
|
|
|
|
} else if !overlay.has_env_var(name) {
|
2021-09-26 18:39:19 +00:00
|
|
|
error = error.or(Some(ParseError::ExportNotFound(*span)))
|
|
|
|
}
|
|
|
|
|
2021-11-15 23:16:06 +00:00
|
|
|
output
|
2021-09-26 18:39:19 +00:00
|
|
|
}
|
2021-09-27 00:23:22 +00:00
|
|
|
ImportPatternMember::List { names } => {
|
|
|
|
let mut output = vec![];
|
|
|
|
|
|
|
|
for (name, span) in names {
|
2021-11-15 23:16:06 +00:00
|
|
|
if let Some(id) = overlay.get_decl_id(name) {
|
|
|
|
output.push((name.clone(), id));
|
|
|
|
} else if !overlay.has_env_var(name) {
|
|
|
|
error = error.or(Some(ParseError::ExportNotFound(*span)));
|
|
|
|
break;
|
2021-09-27 00:23:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
output
|
|
|
|
}
|
2021-09-26 18:39:19 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-11-15 23:16:06 +00:00
|
|
|
// Extend the current scope with the module's overlay
|
2021-12-01 22:25:51 +00:00
|
|
|
working_set.use_decls(decls_to_use);
|
2021-09-26 18:39:19 +00:00
|
|
|
|
|
|
|
// Create the Use command call
|
|
|
|
let use_decl_id = working_set
|
|
|
|
.find_decl(b"use")
|
|
|
|
.expect("internal error: missing use command");
|
|
|
|
|
2021-11-15 23:16:06 +00:00
|
|
|
let import_pattern_expr = Expression {
|
|
|
|
expr: Expr::ImportPattern(import_pattern),
|
|
|
|
span: span(&spans[1..]),
|
|
|
|
ty: Type::List(Box::new(Type::String)),
|
|
|
|
custom_completion: None,
|
|
|
|
};
|
|
|
|
|
2021-09-26 18:39:19 +00:00
|
|
|
let call = Box::new(Call {
|
|
|
|
head: spans[0],
|
|
|
|
decl_id: use_decl_id,
|
2021-11-15 23:16:06 +00:00
|
|
|
positional: vec![import_pattern_expr],
|
2021-09-26 18:39:19 +00:00
|
|
|
named: vec![],
|
|
|
|
});
|
|
|
|
|
|
|
|
(
|
|
|
|
Statement::Pipeline(Pipeline::from_vec(vec![Expression {
|
|
|
|
expr: Expr::Call(call),
|
|
|
|
span: span(spans),
|
|
|
|
ty: Type::Unknown,
|
|
|
|
custom_completion: None,
|
|
|
|
}])),
|
|
|
|
error,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
(
|
|
|
|
garbage_statement(spans),
|
|
|
|
Some(ParseError::UnknownState(
|
|
|
|
"Expected structure: use <name>".into(),
|
|
|
|
span(spans),
|
|
|
|
)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-28 20:18:48 +00:00
|
|
|
pub fn parse_hide(
|
|
|
|
working_set: &mut StateWorkingSet,
|
|
|
|
spans: &[Span],
|
|
|
|
) -> (Statement, Option<ParseError>) {
|
|
|
|
let mut error = None;
|
|
|
|
let bytes = working_set.get_span_contents(spans[0]);
|
|
|
|
|
|
|
|
if bytes == b"hide" && spans.len() >= 2 {
|
2021-11-15 23:16:06 +00:00
|
|
|
for span in spans[1..].iter() {
|
|
|
|
let (_, err) = parse_string(working_set, *span);
|
|
|
|
error = error.or(err);
|
|
|
|
}
|
2021-09-28 20:18:48 +00:00
|
|
|
|
2021-10-26 21:06:08 +00:00
|
|
|
let (import_pattern, err) = parse_import_pattern(working_set, &spans[1..]);
|
2021-10-04 17:08:24 +00:00
|
|
|
error = error.or(err);
|
2021-09-28 20:18:48 +00:00
|
|
|
|
2021-11-15 23:16:06 +00:00
|
|
|
let (is_module, overlay) =
|
2021-11-17 04:23:55 +00:00
|
|
|
if let Some(overlay_id) = working_set.find_overlay(&import_pattern.head.name) {
|
|
|
|
(true, working_set.get_overlay(overlay_id).clone())
|
2021-10-04 19:37:43 +00:00
|
|
|
} else if import_pattern.members.is_empty() {
|
|
|
|
// The pattern head can be e.g. a function name, not just a module
|
2021-11-15 23:16:06 +00:00
|
|
|
if let Some(id) = working_set.find_decl(&import_pattern.head.name) {
|
|
|
|
let mut decls = HashMap::new();
|
|
|
|
decls.insert(import_pattern.head.name.clone(), id);
|
|
|
|
|
|
|
|
(
|
|
|
|
false,
|
|
|
|
Overlay {
|
|
|
|
decls,
|
|
|
|
env_vars: HashMap::new(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
} else {
|
2021-11-30 06:14:05 +00:00
|
|
|
// Or it could be an env var
|
|
|
|
(
|
|
|
|
false,
|
|
|
|
Overlay {
|
|
|
|
decls: HashMap::new(),
|
|
|
|
env_vars: HashMap::new(),
|
|
|
|
},
|
|
|
|
)
|
2021-11-15 23:16:06 +00:00
|
|
|
}
|
2021-10-04 17:08:24 +00:00
|
|
|
} else {
|
2021-10-04 19:37:43 +00:00
|
|
|
return (
|
|
|
|
garbage_statement(spans),
|
|
|
|
Some(ParseError::ModuleNotFound(spans[1])),
|
|
|
|
);
|
2021-10-04 17:08:24 +00:00
|
|
|
};
|
2021-09-28 20:18:48 +00:00
|
|
|
|
2021-10-04 17:08:24 +00:00
|
|
|
// This kind of inverts the import pattern matching found in parse_use()
|
2021-11-15 23:16:06 +00:00
|
|
|
let decls_to_hide = if import_pattern.members.is_empty() {
|
2021-10-31 15:38:00 +00:00
|
|
|
if is_module {
|
2021-11-15 23:16:06 +00:00
|
|
|
overlay.decls_with_head(&import_pattern.head.name)
|
2021-10-31 15:38:00 +00:00
|
|
|
} else {
|
2021-11-15 23:16:06 +00:00
|
|
|
overlay.decls()
|
2021-10-31 15:38:00 +00:00
|
|
|
}
|
2021-10-04 17:08:24 +00:00
|
|
|
} else {
|
|
|
|
match &import_pattern.members[0] {
|
2021-11-15 23:16:06 +00:00
|
|
|
ImportPatternMember::Glob { .. } => {
|
|
|
|
overlay.decls_with_head(&import_pattern.head.name)
|
|
|
|
}
|
2021-10-04 17:08:24 +00:00
|
|
|
ImportPatternMember::Name { name, span } => {
|
2021-11-15 23:16:06 +00:00
|
|
|
let mut output = vec![];
|
|
|
|
|
|
|
|
if let Some(item) = overlay.decl_with_head(name, &import_pattern.head.name) {
|
|
|
|
output.push(item);
|
|
|
|
} else if !overlay.has_env_var(name) {
|
|
|
|
error = error.or(Some(ParseError::ExportNotFound(*span)));
|
2021-10-04 17:08:24 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 23:16:06 +00:00
|
|
|
output
|
2021-10-04 17:08:24 +00:00
|
|
|
}
|
|
|
|
ImportPatternMember::List { names } => {
|
|
|
|
let mut output = vec![];
|
2021-09-28 20:18:48 +00:00
|
|
|
|
2021-10-04 17:08:24 +00:00
|
|
|
for (name, span) in names {
|
2021-11-15 23:16:06 +00:00
|
|
|
if let Some(item) = overlay.decl_with_head(name, &import_pattern.head.name)
|
|
|
|
{
|
|
|
|
output.push(item);
|
|
|
|
} else if !overlay.has_env_var(name) {
|
|
|
|
error = error.or(Some(ParseError::ExportNotFound(*span)));
|
|
|
|
break;
|
2021-10-04 17:08:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
output
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-11-15 23:16:06 +00:00
|
|
|
// TODO: `use spam; use spam foo; hide foo` will hide both `foo` and `spam foo` since
|
|
|
|
// they point to the same DeclId. Do we want to keep it that way?
|
|
|
|
working_set.hide_decls(&decls_to_hide);
|
2021-11-30 06:14:05 +00:00
|
|
|
let import_pattern = import_pattern
|
|
|
|
.with_hidden(decls_to_hide.iter().map(|(name, _)| name.clone()).collect());
|
2021-09-28 20:18:48 +00:00
|
|
|
|
|
|
|
// Create the Hide command call
|
|
|
|
let hide_decl_id = working_set
|
|
|
|
.find_decl(b"hide")
|
|
|
|
.expect("internal error: missing hide command");
|
|
|
|
|
2021-11-15 23:16:06 +00:00
|
|
|
let import_pattern_expr = Expression {
|
|
|
|
expr: Expr::ImportPattern(import_pattern),
|
|
|
|
span: span(&spans[1..]),
|
|
|
|
ty: Type::List(Box::new(Type::String)),
|
|
|
|
custom_completion: None,
|
|
|
|
};
|
|
|
|
|
2021-09-28 20:18:48 +00:00
|
|
|
let call = Box::new(Call {
|
|
|
|
head: spans[0],
|
|
|
|
decl_id: hide_decl_id,
|
2021-11-15 23:16:06 +00:00
|
|
|
positional: vec![import_pattern_expr],
|
2021-09-28 20:18:48 +00:00
|
|
|
named: vec![],
|
|
|
|
});
|
|
|
|
|
|
|
|
(
|
|
|
|
Statement::Pipeline(Pipeline::from_vec(vec![Expression {
|
|
|
|
expr: Expr::Call(call),
|
|
|
|
span: span(spans),
|
|
|
|
ty: Type::Unknown,
|
|
|
|
custom_completion: None,
|
|
|
|
}])),
|
|
|
|
error,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
(
|
|
|
|
garbage_statement(spans),
|
|
|
|
Some(ParseError::UnknownState(
|
|
|
|
"Expected structure: hide <name>".into(),
|
|
|
|
span(spans),
|
|
|
|
)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-26 18:39:19 +00:00
|
|
|
pub fn parse_let(
|
|
|
|
working_set: &mut StateWorkingSet,
|
|
|
|
spans: &[Span],
|
|
|
|
) -> (Statement, Option<ParseError>) {
|
|
|
|
let name = working_set.get_span_contents(spans[0]);
|
|
|
|
|
|
|
|
if name == b"let" {
|
|
|
|
if let Some((span, err)) = check_name(working_set, spans) {
|
|
|
|
return (
|
|
|
|
Statement::Pipeline(Pipeline::from_vec(vec![garbage(*span)])),
|
|
|
|
Some(err),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(decl_id) = working_set.find_decl(b"let") {
|
|
|
|
let (call, call_span, err) =
|
|
|
|
parse_internal_call(working_set, spans[0], &spans[1..], decl_id);
|
|
|
|
|
|
|
|
// Update the variable to the known type if we can.
|
|
|
|
if err.is_none() {
|
|
|
|
let var_id = call.positional[0]
|
|
|
|
.as_var()
|
|
|
|
.expect("internal error: expected variable");
|
|
|
|
let rhs_type = call.positional[1].ty.clone();
|
|
|
|
|
2021-11-14 19:25:57 +00:00
|
|
|
if var_id != CONFIG_VARIABLE_ID {
|
|
|
|
working_set.set_variable_type(var_id, rhs_type);
|
|
|
|
}
|
2021-09-26 18:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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: let statement unparseable".into(),
|
|
|
|
span(spans),
|
|
|
|
)),
|
|
|
|
)
|
|
|
|
}
|
2021-10-02 02:24:43 +00:00
|
|
|
|
|
|
|
pub fn parse_source(
|
|
|
|
working_set: &mut StateWorkingSet,
|
|
|
|
spans: &[Span],
|
|
|
|
) -> (Statement, Option<ParseError>) {
|
2021-12-03 19:49:11 +00:00
|
|
|
let mut error = None;
|
2021-10-02 02:24:43 +00:00
|
|
|
let name = working_set.get_span_contents(spans[0]);
|
|
|
|
|
|
|
|
if name == b"source" {
|
|
|
|
if let Some(decl_id) = working_set.find_decl(b"source") {
|
2021-10-06 02:03:18 +00:00
|
|
|
// Is this the right call to be using here?
|
|
|
|
// Some of the others (`parse_let`) use it, some of them (`parse_hide`) don't.
|
2021-10-06 01:59:16 +00:00
|
|
|
let (call, call_span, err) =
|
2021-10-02 02:24:43 +00:00
|
|
|
parse_internal_call(working_set, spans[0], &spans[1..], decl_id);
|
2021-12-03 19:49:11 +00:00
|
|
|
error = error.or(err);
|
2021-10-02 02:24:43 +00:00
|
|
|
|
|
|
|
// 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()) {
|
2021-12-03 19:49:11 +00:00
|
|
|
if let Ok(path) = canonicalize(&filename) {
|
|
|
|
if let Ok(contents) = std::fs::read(&path) {
|
|
|
|
// 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,
|
2021-10-06 01:59:16 +00:00
|
|
|
);
|
|
|
|
|
2021-12-03 19:49:11 +00:00
|
|
|
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],
|
2021-10-06 01:59:16 +00:00
|
|
|
ty: Type::Unknown,
|
|
|
|
custom_completion: None,
|
2021-12-03 19:49:11 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
Statement::Pipeline(Pipeline::from_vec(vec![Expression {
|
|
|
|
expr: Expr::Call(call_with_block),
|
|
|
|
span: call_span,
|
|
|
|
ty: Type::Unknown,
|
|
|
|
custom_completion: None,
|
|
|
|
}])),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
}
|
2021-10-02 02:24:43 +00:00
|
|
|
}
|
2021-12-03 19:49:11 +00:00
|
|
|
} else {
|
|
|
|
error = error.or(Some(ParseError::FileNotFound(filename)));
|
2021-10-02 02:24:43 +00:00
|
|
|
}
|
2021-10-31 15:53:53 +00:00
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
garbage_statement(spans),
|
|
|
|
Some(ParseError::NonUtf8(spans[1])),
|
|
|
|
);
|
2021-10-02 02:24:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
Statement::Pipeline(Pipeline::from_vec(vec![Expression {
|
|
|
|
expr: Expr::Call(call),
|
|
|
|
span: call_span,
|
|
|
|
ty: Type::Unknown,
|
|
|
|
custom_completion: None,
|
|
|
|
}])),
|
2021-12-03 19:49:11 +00:00
|
|
|
error,
|
2021-10-02 02:24:43 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(
|
|
|
|
garbage_statement(spans),
|
|
|
|
Some(ParseError::UnknownState(
|
2021-10-06 02:29:05 +00:00
|
|
|
"internal error: source statement unparseable".into(),
|
2021-10-02 02:24:43 +00:00
|
|
|
span(spans),
|
|
|
|
)),
|
|
|
|
)
|
|
|
|
}
|
2021-10-31 08:17:01 +00:00
|
|
|
|
2021-11-02 20:56:00 +00:00
|
|
|
#[cfg(feature = "plugin")]
|
2021-12-03 14:29:55 +00:00
|
|
|
pub fn parse_register(
|
2021-10-31 08:17:01 +00:00
|
|
|
working_set: &mut StateWorkingSet,
|
|
|
|
spans: &[Span],
|
|
|
|
) -> (Statement, Option<ParseError>) {
|
2021-12-02 05:42:56 +00:00
|
|
|
use std::{path::PathBuf, str::FromStr};
|
|
|
|
|
2021-12-03 14:29:55 +00:00
|
|
|
use nu_plugin::plugin::{get_signature, PluginDeclaration};
|
2021-11-19 02:51:42 +00:00
|
|
|
use nu_protocol::Signature;
|
|
|
|
|
2021-10-31 08:17:01 +00:00
|
|
|
let name = working_set.get_span_contents(spans[0]);
|
|
|
|
|
|
|
|
if name != b"register" {
|
|
|
|
return (
|
|
|
|
garbage_statement(spans),
|
|
|
|
Some(ParseError::UnknownState(
|
|
|
|
"internal error: Wrong call name for parse plugin function".into(),
|
|
|
|
span(spans),
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(decl_id) = working_set.find_decl(b"register") {
|
|
|
|
let (call, call_span, mut err) =
|
|
|
|
parse_internal_call(working_set, spans[0], &spans[1..], decl_id);
|
|
|
|
|
|
|
|
let error = {
|
|
|
|
match spans.len() {
|
|
|
|
1 => Some(ParseError::MissingPositional(
|
|
|
|
"plugin location".into(),
|
|
|
|
spans[0],
|
|
|
|
)),
|
|
|
|
2 => {
|
|
|
|
let name_expr = working_set.get_span_contents(spans[1]);
|
2021-12-02 05:42:56 +00:00
|
|
|
String::from_utf8(name_expr.to_vec())
|
|
|
|
.map_err(|_| ParseError::NonUtf8(spans[1]))
|
|
|
|
.and_then(|name| {
|
|
|
|
canonicalize(&name).map_err(|e| ParseError::FileNotFound(e.to_string()))
|
|
|
|
})
|
|
|
|
.and_then(|path| {
|
|
|
|
if path.exists() & path.is_file() {
|
2021-12-03 14:29:55 +00:00
|
|
|
get_signature(path.as_path())
|
|
|
|
.map_err(|err| {
|
|
|
|
ParseError::LabeledError(
|
|
|
|
"Error getting signatures".into(),
|
|
|
|
err.to_string(),
|
|
|
|
spans[0],
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.map(|signatures| (path, signatures))
|
2021-12-02 05:42:56 +00:00
|
|
|
} else {
|
|
|
|
Err(ParseError::FileNotFound(format!("{:?}", path)))
|
2021-11-01 07:20:33 +00:00
|
|
|
}
|
2021-12-02 05:42:56 +00:00
|
|
|
})
|
2021-12-03 14:29:55 +00:00
|
|
|
.map(|(path, signatures)| {
|
|
|
|
for signature in signatures {
|
|
|
|
// create plugin command declaration (need struct impl Command)
|
|
|
|
// store declaration in working set
|
|
|
|
let plugin_decl = PluginDeclaration::new(path.clone(), signature);
|
|
|
|
|
|
|
|
working_set.add_decl(Box::new(plugin_decl));
|
|
|
|
}
|
|
|
|
|
|
|
|
working_set.mark_plugins_file_dirty();
|
|
|
|
})
|
2021-12-02 05:42:56 +00:00
|
|
|
.err()
|
2021-10-31 08:17:01 +00:00
|
|
|
}
|
2021-11-19 02:51:42 +00:00
|
|
|
3 => {
|
2021-12-02 05:42:56 +00:00
|
|
|
let filename_slice = working_set.get_span_contents(spans[1]);
|
2021-11-19 02:51:42 +00:00
|
|
|
let signature = working_set.get_span_contents(spans[2]);
|
2021-12-02 05:42:56 +00:00
|
|
|
|
|
|
|
String::from_utf8(filename_slice.to_vec())
|
|
|
|
.map_err(|_| ParseError::NonUtf8(spans[1]))
|
|
|
|
.and_then(|name| {
|
|
|
|
PathBuf::from_str(name.as_str()).map_err(|_| {
|
|
|
|
ParseError::InternalError(
|
|
|
|
format!("Unable to create path from string {}", name),
|
|
|
|
spans[0],
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.and_then(|path_inner| {
|
2021-12-03 14:29:55 +00:00
|
|
|
serde_json::from_slice::<Signature>(signature)
|
|
|
|
.map_err(|_| {
|
|
|
|
ParseError::LabeledError(
|
|
|
|
"Signature deserialization error".into(),
|
|
|
|
"unable to deserialize signature".into(),
|
|
|
|
spans[0],
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.map(|signature| (path_inner, signature))
|
2021-12-02 05:42:56 +00:00
|
|
|
})
|
2021-12-03 14:29:55 +00:00
|
|
|
.and_then(|(path, signature)| {
|
2021-12-02 05:42:56 +00:00
|
|
|
if path.exists() & path.is_file() {
|
2021-12-03 14:29:55 +00:00
|
|
|
let plugin_decl = PluginDeclaration::new(path, signature);
|
|
|
|
|
|
|
|
working_set.add_decl(Box::new(plugin_decl));
|
|
|
|
|
|
|
|
working_set.mark_plugins_file_dirty();
|
2021-12-02 05:42:56 +00:00
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(ParseError::FileNotFound(format!("{:?}", path)))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.err()
|
2021-11-19 02:51:42 +00:00
|
|
|
}
|
2021-10-31 08:17:01 +00:00
|
|
|
_ => {
|
2021-11-19 02:51:42 +00:00
|
|
|
let span = spans[3..].iter().fold(spans[3], |acc, next| Span {
|
2021-10-31 08:17:01 +00:00
|
|
|
start: acc.start,
|
|
|
|
end: next.end,
|
|
|
|
});
|
|
|
|
|
|
|
|
Some(ParseError::ExtraPositional(span))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
err = error.or(err);
|
|
|
|
|
|
|
|
(
|
|
|
|
Statement::Pipeline(Pipeline::from_vec(vec![Expression {
|
|
|
|
expr: Expr::Call(call),
|
|
|
|
span: call_span,
|
|
|
|
ty: Type::Unknown,
|
|
|
|
custom_completion: None,
|
|
|
|
}])),
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
(
|
|
|
|
garbage_statement(spans),
|
|
|
|
Some(ParseError::UnknownState(
|
|
|
|
"internal error: Register declaration not found".into(),
|
|
|
|
span(spans),
|
|
|
|
)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|