mirror of
https://github.com/nushell/nushell.git
synced 2024-12-23 07:30:13 +01:00
Merge pull request #243 from kubouch/module-files
Loading modules from files
This commit is contained in:
commit
3176f60b5b
@ -15,7 +15,7 @@ impl Command for Use {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn signature(&self) -> nu_protocol::Signature {
|
fn signature(&self) -> nu_protocol::Signature {
|
||||||
Signature::build("use").required("pattern", SyntaxShape::String, "import pattern")
|
Signature::build("use").rest("pattern", SyntaxShape::String, "import pattern parts")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(
|
fn run(
|
||||||
|
@ -93,9 +93,9 @@ pub enum ParseError {
|
|||||||
)]
|
)]
|
||||||
UnknownCommand(#[label = "unknown command"] Span),
|
UnknownCommand(#[label = "unknown command"] Span),
|
||||||
|
|
||||||
#[error("Non-UTF8 code.")]
|
#[error("Non-UTF8 string.")]
|
||||||
#[diagnostic(code(nu::parser::non_utf8), url(docsrs))]
|
#[diagnostic(code(nu::parser::non_utf8), url(docsrs))]
|
||||||
NonUtf8(#[label = "non-UTF8 code"] Span),
|
NonUtf8(#[label = "non-UTF8 string"] Span),
|
||||||
|
|
||||||
#[error("The `{0}` command doesn't have flag `{1}`.")]
|
#[error("The `{0}` command doesn't have flag `{1}`.")]
|
||||||
#[diagnostic(code(nu::parser::unknown_flag), url(docsrs))]
|
#[diagnostic(code(nu::parser::unknown_flag), url(docsrs))]
|
||||||
@ -171,6 +171,10 @@ pub enum ParseError {
|
|||||||
#[diagnostic(code(nu::parser::missing_import_pattern), url(docsrs))]
|
#[diagnostic(code(nu::parser::missing_import_pattern), url(docsrs))]
|
||||||
MissingImportPattern(#[label = "needs an import pattern"] Span),
|
MissingImportPattern(#[label = "needs an import pattern"] Span),
|
||||||
|
|
||||||
|
#[error("Wrong import pattern structure.")]
|
||||||
|
#[diagnostic(code(nu::parser::missing_import_pattern), url(docsrs))]
|
||||||
|
WrongImportPattern(#[label = "invalid import pattern structure"] Span),
|
||||||
|
|
||||||
#[error("Module export not found.")]
|
#[error("Module export not found.")]
|
||||||
#[diagnostic(code(nu::parser::export_not_found), url(docsrs))]
|
#[diagnostic(code(nu::parser::export_not_found), url(docsrs))]
|
||||||
ExportNotFound(#[label = "could not find imports"] Span),
|
ExportNotFound(#[label = "could not find imports"] Span),
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
ast::{Block, Call, Expr, Expression, ImportPatternMember, Pipeline, Statement},
|
ast::{Block, Call, Expr, Expression, ImportPattern, ImportPatternMember, Pipeline, Statement},
|
||||||
engine::StateWorkingSet,
|
engine::StateWorkingSet,
|
||||||
span, DeclId, Span, SyntaxShape, Type,
|
span, DeclId, Span, SyntaxShape, Type,
|
||||||
};
|
};
|
||||||
@ -218,8 +218,6 @@ pub fn parse_alias(
|
|||||||
|
|
||||||
let replacement = spans[3..].to_vec();
|
let replacement = spans[3..].to_vec();
|
||||||
|
|
||||||
//println!("{:?} {:?}", alias_name, replacement);
|
|
||||||
|
|
||||||
working_set.add_alias(alias_name, replacement);
|
working_set.add_alias(alias_name, replacement);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -309,6 +307,88 @@ pub fn parse_export(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn parse_module_block(
|
||||||
|
working_set: &mut StateWorkingSet,
|
||||||
|
span: Span,
|
||||||
|
) -> (Block, Option<ParseError>) {
|
||||||
|
let mut error = None;
|
||||||
|
|
||||||
|
working_set.enter_scope();
|
||||||
|
|
||||||
|
let source = working_set.get_span_contents(span);
|
||||||
|
|
||||||
|
let (output, err) = lex(source, span.start, &[], &[]);
|
||||||
|
error = error.or(err);
|
||||||
|
|
||||||
|
let (output, err) = lite_parse(&output);
|
||||||
|
error = error.or(err);
|
||||||
|
|
||||||
|
for pipeline in &output.block {
|
||||||
|
if pipeline.commands.len() == 1 {
|
||||||
|
parse_def_predecl(working_set, &pipeline.commands[0].parts);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut exports: Vec<(Vec<u8>, DeclId)> = vec![];
|
||||||
|
|
||||||
|
let block: Block = output
|
||||||
|
.block
|
||||||
|
.iter()
|
||||||
|
.map(|pipeline| {
|
||||||
|
if pipeline.commands.len() == 1 {
|
||||||
|
// this one here is doing parse_statement() equivalent
|
||||||
|
// let (stmt, err) = parse_statement(working_set, &pipeline.commands[0].parts);
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
b"export" => {
|
||||||
|
let (stmt, err) = parse_export(working_set, &pipeline.commands[0].parts);
|
||||||
|
|
||||||
|
if err.is_none() {
|
||||||
|
let decl_name =
|
||||||
|
// parts[2] is safe since it's checked in parse_export already
|
||||||
|
working_set.get_span_contents(pipeline.commands[0].parts[2]);
|
||||||
|
|
||||||
|
let decl_id = working_set
|
||||||
|
.find_decl(decl_name)
|
||||||
|
.expect("internal error: failed to find added declaration");
|
||||||
|
|
||||||
|
exports.push((decl_name.into(), decl_id));
|
||||||
|
}
|
||||||
|
|
||||||
|
(stmt, err)
|
||||||
|
}
|
||||||
|
_ => (
|
||||||
|
garbage_statement(&pipeline.commands[0].parts),
|
||||||
|
Some(ParseError::Expected(
|
||||||
|
"def or export keyword".into(),
|
||||||
|
pipeline.commands[0].parts[0],
|
||||||
|
)),
|
||||||
|
),
|
||||||
|
};
|
||||||
|
|
||||||
|
if error.is_none() {
|
||||||
|
error = err;
|
||||||
|
}
|
||||||
|
|
||||||
|
stmt
|
||||||
|
} else {
|
||||||
|
error = Some(ParseError::Expected("not a pipeline".into(), span));
|
||||||
|
garbage_statement(&[span])
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.into();
|
||||||
|
|
||||||
|
working_set.exit_scope();
|
||||||
|
|
||||||
|
(block.with_exports(exports), error)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn parse_module(
|
pub fn parse_module(
|
||||||
working_set: &mut StateWorkingSet,
|
working_set: &mut StateWorkingSet,
|
||||||
spans: &[Span],
|
spans: &[Span],
|
||||||
@ -359,91 +439,9 @@ pub fn parse_module(
|
|||||||
|
|
||||||
let block_span = Span { start, end };
|
let block_span = Span { start, end };
|
||||||
|
|
||||||
let source = working_set.get_span_contents(block_span);
|
let (block, err) = parse_module_block(working_set, block_span);
|
||||||
|
|
||||||
let (output, err) = lex(source, start, &[], &[]);
|
|
||||||
error = error.or(err);
|
error = error.or(err);
|
||||||
|
|
||||||
working_set.enter_scope();
|
|
||||||
|
|
||||||
// Do we need block parameters?
|
|
||||||
|
|
||||||
let (output, err) = lite_parse(&output);
|
|
||||||
error = error.or(err);
|
|
||||||
|
|
||||||
// We probably don't need $it
|
|
||||||
|
|
||||||
// we're doing parse_block() equivalent
|
|
||||||
// let (mut output, err) = parse_block(working_set, &output, false);
|
|
||||||
|
|
||||||
for pipeline in &output.block {
|
|
||||||
if pipeline.commands.len() == 1 {
|
|
||||||
parse_def_predecl(working_set, &pipeline.commands[0].parts);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut exports: Vec<(Vec<u8>, DeclId)> = vec![];
|
|
||||||
|
|
||||||
let block: Block = output
|
|
||||||
.block
|
|
||||||
.iter()
|
|
||||||
.map(|pipeline| {
|
|
||||||
if pipeline.commands.len() == 1 {
|
|
||||||
// this one here is doing parse_statement() equivalent
|
|
||||||
// let (stmt, err) = parse_statement(working_set, &pipeline.commands[0].parts);
|
|
||||||
let name = working_set.get_span_contents(pipeline.commands[0].parts[0]);
|
|
||||||
|
|
||||||
let (stmt, err) = match name {
|
|
||||||
// TODO: Here we can add other stuff that's allowed for modules
|
|
||||||
b"def" => {
|
|
||||||
let (stmt, err) = parse_def(working_set, &pipeline.commands[0].parts);
|
|
||||||
|
|
||||||
(stmt, err)
|
|
||||||
}
|
|
||||||
b"export" => {
|
|
||||||
let (stmt, err) =
|
|
||||||
parse_export(working_set, &pipeline.commands[0].parts);
|
|
||||||
|
|
||||||
if err.is_none() {
|
|
||||||
let decl_name =
|
|
||||||
// parts[2] is safe since it's checked in parse_def already
|
|
||||||
working_set.get_span_contents(pipeline.commands[0].parts[2]);
|
|
||||||
|
|
||||||
let decl_id = working_set
|
|
||||||
.find_decl(decl_name)
|
|
||||||
.expect("internal error: failed to find added declaration");
|
|
||||||
|
|
||||||
exports.push((decl_name.into(), decl_id));
|
|
||||||
}
|
|
||||||
|
|
||||||
(stmt, err)
|
|
||||||
}
|
|
||||||
_ => (
|
|
||||||
garbage_statement(&pipeline.commands[0].parts),
|
|
||||||
Some(ParseError::Expected(
|
|
||||||
// TODO: Fill in more keywords as they come
|
|
||||||
"def or export keyword".into(),
|
|
||||||
pipeline.commands[0].parts[0],
|
|
||||||
)),
|
|
||||||
),
|
|
||||||
};
|
|
||||||
|
|
||||||
if error.is_none() {
|
|
||||||
error = err;
|
|
||||||
}
|
|
||||||
|
|
||||||
stmt
|
|
||||||
} else {
|
|
||||||
error = Some(ParseError::Expected("not a pipeline".into(), block_span));
|
|
||||||
garbage_statement(spans)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.into();
|
|
||||||
|
|
||||||
let block = block.with_exports(exports);
|
|
||||||
|
|
||||||
working_set.exit_scope();
|
|
||||||
|
|
||||||
let block_id = working_set.add_module(&module_name, block);
|
let block_id = working_set.add_module(&module_name, block);
|
||||||
|
|
||||||
let block_expr = Expression {
|
let block_expr = Expression {
|
||||||
@ -492,14 +490,31 @@ pub fn parse_use(
|
|||||||
let bytes = working_set.get_span_contents(spans[0]);
|
let bytes = working_set.get_span_contents(spans[0]);
|
||||||
|
|
||||||
if bytes == b"use" && spans.len() >= 2 {
|
if bytes == b"use" && spans.len() >= 2 {
|
||||||
let (module_name_expr, err) = parse_string(working_set, spans[1]);
|
let mut import_pattern_exprs: Vec<Expression> = vec![];
|
||||||
|
for span in spans[1..].iter() {
|
||||||
|
let (expr, err) = parse_string(working_set, *span);
|
||||||
|
import_pattern_exprs.push(expr);
|
||||||
|
error = error.or(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Add checking for importing too long import patterns, e.g.:
|
||||||
|
// > use spam foo non existent names here do not throw error
|
||||||
|
let (import_pattern, err) = parse_import_pattern(working_set, &spans[1..]);
|
||||||
error = error.or(err);
|
error = error.or(err);
|
||||||
|
|
||||||
let (import_pattern, err) = parse_import_pattern(working_set, spans[1]);
|
let (import_pattern, exports) =
|
||||||
error = error.or(err);
|
if let Some(block_id) = working_set.find_module(&import_pattern.head) {
|
||||||
|
(
|
||||||
let exports = if let Some(block_id) = working_set.find_module(&import_pattern.head) {
|
import_pattern,
|
||||||
working_set.get_block(block_id).exports.clone()
|
working_set.get_block(block_id).exports.clone(),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
// TODO: Do not close over when loading module from file
|
||||||
|
// It could be a file
|
||||||
|
if let Ok(module_filename) = String::from_utf8(import_pattern.head) {
|
||||||
|
let module_path = Path::new(&module_filename);
|
||||||
|
let module_name = if let Some(stem) = module_path.file_stem() {
|
||||||
|
stem.to_string_lossy().to_string()
|
||||||
} else {
|
} else {
|
||||||
return (
|
return (
|
||||||
garbage_statement(spans),
|
garbage_statement(spans),
|
||||||
@ -507,12 +522,44 @@ pub fn parse_use(
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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, err) =
|
||||||
|
parse_module_block(working_set, Span::new(span_start, span_end));
|
||||||
|
error = error.or(err);
|
||||||
|
|
||||||
|
let block_id = working_set.add_module(&module_name, block);
|
||||||
|
|
||||||
|
(
|
||||||
|
ImportPattern {
|
||||||
|
head: module_name.into(),
|
||||||
|
members: import_pattern.members,
|
||||||
|
},
|
||||||
|
working_set.get_block(block_id).exports.clone(),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
return (
|
||||||
|
garbage_statement(spans),
|
||||||
|
Some(ParseError::ModuleNotFound(spans[1])),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return (
|
||||||
|
garbage_statement(spans),
|
||||||
|
Some(ParseError::NonUtf8(spans[1])),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let exports = if import_pattern.members.is_empty() {
|
let exports = if import_pattern.members.is_empty() {
|
||||||
exports
|
exports
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|(name, id)| {
|
.map(|(name, id)| {
|
||||||
let mut new_name = import_pattern.head.to_vec();
|
let mut new_name = import_pattern.head.to_vec();
|
||||||
new_name.push(b'.');
|
new_name.push(b' ');
|
||||||
new_name.extend(&name);
|
new_name.extend(&name);
|
||||||
(new_name, id)
|
(new_name, id)
|
||||||
})
|
})
|
||||||
@ -562,7 +609,7 @@ pub fn parse_use(
|
|||||||
let call = Box::new(Call {
|
let call = Box::new(Call {
|
||||||
head: spans[0],
|
head: spans[0],
|
||||||
decl_id: use_decl_id,
|
decl_id: use_decl_id,
|
||||||
positional: vec![module_name_expr],
|
positional: import_pattern_exprs,
|
||||||
named: vec![],
|
named: vec![],
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -597,20 +644,23 @@ pub fn parse_hide(
|
|||||||
let (name_expr, err) = parse_string(working_set, spans[1]);
|
let (name_expr, err) = parse_string(working_set, spans[1]);
|
||||||
error = error.or(err);
|
error = error.or(err);
|
||||||
|
|
||||||
let (import_pattern, err) = parse_import_pattern(working_set, spans[1]);
|
let (import_pattern, err) = parse_import_pattern(working_set, &spans[1..]);
|
||||||
error = error.or(err);
|
error = error.or(err);
|
||||||
|
|
||||||
let exported_names: Vec<Vec<u8>> =
|
let (is_module, exported_names): (bool, Vec<Vec<u8>>) =
|
||||||
if let Some(block_id) = working_set.find_module(&import_pattern.head) {
|
if let Some(block_id) = working_set.find_module(&import_pattern.head) {
|
||||||
|
(
|
||||||
|
true,
|
||||||
working_set
|
working_set
|
||||||
.get_block(block_id)
|
.get_block(block_id)
|
||||||
.exports
|
.exports
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(name, _)| name.clone())
|
.map(|(name, _)| name.clone())
|
||||||
.collect()
|
.collect(),
|
||||||
|
)
|
||||||
} else if import_pattern.members.is_empty() {
|
} else if import_pattern.members.is_empty() {
|
||||||
// The pattern head can be e.g. a function name, not just a module
|
// The pattern head can be e.g. a function name, not just a module
|
||||||
vec![import_pattern.head.clone()]
|
(false, vec![import_pattern.head.clone()])
|
||||||
} else {
|
} else {
|
||||||
return (
|
return (
|
||||||
garbage_statement(spans),
|
garbage_statement(spans),
|
||||||
@ -620,14 +670,26 @@ pub fn parse_hide(
|
|||||||
|
|
||||||
// This kind of inverts the import pattern matching found in parse_use()
|
// This kind of inverts the import pattern matching found in parse_use()
|
||||||
let names_to_hide = if import_pattern.members.is_empty() {
|
let names_to_hide = if import_pattern.members.is_empty() {
|
||||||
|
if is_module {
|
||||||
exported_names
|
exported_names
|
||||||
|
.into_iter()
|
||||||
|
.map(|name| {
|
||||||
|
let mut new_name = import_pattern.head.to_vec();
|
||||||
|
new_name.push(b' ');
|
||||||
|
new_name.extend(&name);
|
||||||
|
new_name
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
} else {
|
||||||
|
exported_names
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
match &import_pattern.members[0] {
|
match &import_pattern.members[0] {
|
||||||
ImportPatternMember::Glob { .. } => exported_names
|
ImportPatternMember::Glob { .. } => exported_names
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|name| {
|
.map(|name| {
|
||||||
let mut new_name = import_pattern.head.to_vec();
|
let mut new_name = import_pattern.head.to_vec();
|
||||||
new_name.push(b'.');
|
new_name.push(b' ');
|
||||||
new_name.extend(&name);
|
new_name.extend(&name);
|
||||||
new_name
|
new_name
|
||||||
})
|
})
|
||||||
@ -638,7 +700,7 @@ pub fn parse_hide(
|
|||||||
.filter(|n| n == name)
|
.filter(|n| n == name)
|
||||||
.map(|n| {
|
.map(|n| {
|
||||||
let mut new_name = import_pattern.head.to_vec();
|
let mut new_name = import_pattern.head.to_vec();
|
||||||
new_name.push(b'.');
|
new_name.push(b' ');
|
||||||
new_name.extend(&n);
|
new_name.extend(&n);
|
||||||
new_name
|
new_name
|
||||||
})
|
})
|
||||||
@ -659,7 +721,7 @@ pub fn parse_hide(
|
|||||||
.filter_map(|n| if n == name { Some(n.clone()) } else { None })
|
.filter_map(|n| if n == name { Some(n.clone()) } else { None })
|
||||||
.map(|n| {
|
.map(|n| {
|
||||||
let mut new_name = import_pattern.head.to_vec();
|
let mut new_name = import_pattern.head.to_vec();
|
||||||
new_name.push(b'.');
|
new_name.push(b' ');
|
||||||
new_name.extend(n);
|
new_name.extend(n);
|
||||||
new_name
|
new_name
|
||||||
})
|
})
|
||||||
@ -678,6 +740,8 @@ pub fn parse_hide(
|
|||||||
};
|
};
|
||||||
|
|
||||||
for name in names_to_hide {
|
for name in names_to_hide {
|
||||||
|
// 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?
|
||||||
if working_set.hide_decl(&name).is_none() {
|
if working_set.hide_decl(&name).is_none() {
|
||||||
error = error.or_else(|| Some(ParseError::UnknownCommand(spans[1])));
|
error = error.or_else(|| Some(ParseError::UnknownCommand(spans[1])));
|
||||||
}
|
}
|
||||||
@ -833,6 +897,11 @@ pub fn parse_source(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
return (
|
||||||
|
garbage_statement(spans),
|
||||||
|
Some(ParseError::NonUtf8(spans[1])),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return (
|
return (
|
||||||
|
@ -39,6 +39,26 @@ fn is_identifier_byte(b: u8) -> bool {
|
|||||||
b != b'.' && b != b'[' && b != b'(' && b != b'{'
|
b != b'.' && b != b'[' && b != b'(' && b != b'{'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_math_expression_byte(b: u8) -> bool {
|
||||||
|
b == b'0'
|
||||||
|
|| b == b'1'
|
||||||
|
|| b == b'2'
|
||||||
|
|| b == b'3'
|
||||||
|
|| b == b'4'
|
||||||
|
|| b == b'5'
|
||||||
|
|| b == b'6'
|
||||||
|
|| b == b'7'
|
||||||
|
|| b == b'8'
|
||||||
|
|| b == b'9'
|
||||||
|
|| b == b'('
|
||||||
|
|| b == b'{'
|
||||||
|
|| b == b'['
|
||||||
|
|| b == b'$'
|
||||||
|
|| b == b'"'
|
||||||
|
|| b == b'\''
|
||||||
|
|| b == b'-'
|
||||||
|
}
|
||||||
|
|
||||||
fn is_identifier(bytes: &[u8]) -> bool {
|
fn is_identifier(bytes: &[u8]) -> bool {
|
||||||
bytes.iter().all(|x| is_identifier_byte(*x))
|
bytes.iter().all(|x| is_identifier_byte(*x))
|
||||||
}
|
}
|
||||||
@ -595,7 +615,16 @@ pub fn parse_call(
|
|||||||
spans: &[Span],
|
spans: &[Span],
|
||||||
expand_aliases: bool,
|
expand_aliases: bool,
|
||||||
) -> (Expression, Option<ParseError>) {
|
) -> (Expression, Option<ParseError>) {
|
||||||
// assume spans.len() > 0?
|
if spans.is_empty() {
|
||||||
|
return (
|
||||||
|
garbage(Span::unknown()),
|
||||||
|
Some(ParseError::UnknownState(
|
||||||
|
"Encountered command with zero spans".into(),
|
||||||
|
span(spans),
|
||||||
|
)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
let mut pos = 0;
|
let mut pos = 0;
|
||||||
let mut shorthand = vec![];
|
let mut shorthand = vec![];
|
||||||
|
|
||||||
@ -613,19 +642,30 @@ pub fn parse_call(
|
|||||||
|
|
||||||
if pos == spans.len() {
|
if pos == spans.len() {
|
||||||
return (
|
return (
|
||||||
Expression::garbage(span(spans)),
|
garbage(span(spans)),
|
||||||
Some(ParseError::UnknownCommand(spans[0])),
|
Some(ParseError::UnknownCommand(spans[0])),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let name = working_set.get_span_contents(spans[pos]);
|
|
||||||
|
|
||||||
let cmd_start = pos;
|
let cmd_start = pos;
|
||||||
|
let mut name_spans = vec![];
|
||||||
|
|
||||||
|
for word_span in spans[cmd_start..].iter() {
|
||||||
|
// Find the longest group of words that could form a command
|
||||||
|
let bytes = working_set.get_span_contents(*word_span);
|
||||||
|
|
||||||
|
if is_math_expression_byte(bytes[0]) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
name_spans.push(*word_span);
|
||||||
|
|
||||||
|
let name = working_set.get_span_contents(span(&name_spans));
|
||||||
|
|
||||||
if expand_aliases {
|
if expand_aliases {
|
||||||
|
// If the word is an alias, expand it and re-parse the expression
|
||||||
if let Some(expansion) = working_set.find_alias(name) {
|
if let Some(expansion) = working_set.find_alias(name) {
|
||||||
let orig_span = spans[pos];
|
let orig_span = spans[pos];
|
||||||
//let mut spans = spans.to_vec();
|
|
||||||
let mut new_spans: Vec<Span> = vec![];
|
let mut new_spans: Vec<Span> = vec![];
|
||||||
new_spans.extend(&spans[0..pos]);
|
new_spans.extend(&spans[0..pos]);
|
||||||
new_spans.extend(expansion);
|
new_spans.extend(expansion);
|
||||||
@ -658,59 +698,26 @@ pub fn parse_call(
|
|||||||
}
|
}
|
||||||
|
|
||||||
pos += 1;
|
pos += 1;
|
||||||
|
|
||||||
if let Some(mut decl_id) = working_set.find_decl(name) {
|
|
||||||
let mut name = name.to_vec();
|
|
||||||
while pos < spans.len() {
|
|
||||||
// look to see if it's a subcommand
|
|
||||||
let mut new_name = name.to_vec();
|
|
||||||
new_name.push(b' ');
|
|
||||||
new_name.extend(working_set.get_span_contents(spans[pos]));
|
|
||||||
|
|
||||||
if expand_aliases {
|
|
||||||
if let Some(expansion) = working_set.find_alias(&new_name) {
|
|
||||||
let orig_span = span(&spans[cmd_start..pos + 1]);
|
|
||||||
//let mut spans = spans.to_vec();
|
|
||||||
let mut new_spans: Vec<Span> = vec![];
|
|
||||||
new_spans.extend(&spans[0..cmd_start]);
|
|
||||||
new_spans.extend(expansion);
|
|
||||||
if spans.len() > pos {
|
|
||||||
new_spans.extend(&spans[(pos + 1)..]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let (result, err) = parse_expression(working_set, &new_spans, false);
|
let name = working_set.get_span_contents(span(&name_spans));
|
||||||
|
let mut maybe_decl_id = working_set.find_decl(name);
|
||||||
|
|
||||||
let expression = match result {
|
while maybe_decl_id.is_none() {
|
||||||
Expression {
|
// Find the longest command match
|
||||||
expr: Expr::Call(mut call),
|
if name_spans.len() <= 1 {
|
||||||
span,
|
// Keep the first word even if it does not match -- could be external command
|
||||||
ty,
|
|
||||||
custom_completion: None,
|
|
||||||
} => {
|
|
||||||
call.head = orig_span;
|
|
||||||
Expression {
|
|
||||||
expr: Expr::Call(call),
|
|
||||||
span,
|
|
||||||
ty,
|
|
||||||
custom_completion: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
x => x,
|
|
||||||
};
|
|
||||||
|
|
||||||
return (expression, err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(did) = working_set.find_decl(&new_name) {
|
|
||||||
decl_id = did;
|
|
||||||
} else {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
name = new_name;
|
|
||||||
pos += 1;
|
name_spans.pop();
|
||||||
|
pos -= 1;
|
||||||
|
|
||||||
|
let name = working_set.get_span_contents(span(&name_spans));
|
||||||
|
maybe_decl_id = working_set.find_decl(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(decl_id) = maybe_decl_id {
|
||||||
// Before the internal parsing we check if there is no let or alias declarations
|
// Before the internal parsing we check if there is no let or alias declarations
|
||||||
// that are missing their name, e.g.: let = 1 or alias = 2
|
// that are missing their name, e.g.: let = 1 or alias = 2
|
||||||
if spans.len() > 1 {
|
if spans.len() > 1 {
|
||||||
@ -718,7 +725,7 @@ pub fn parse_call(
|
|||||||
|
|
||||||
if test_equal == [b'='] {
|
if test_equal == [b'='] {
|
||||||
return (
|
return (
|
||||||
garbage(Span::new(0, 0)),
|
garbage(Span::unknown()),
|
||||||
Some(ParseError::UnknownState(
|
Some(ParseError::UnknownState(
|
||||||
"Incomplete statement".into(),
|
"Incomplete statement".into(),
|
||||||
span(spans),
|
span(spans),
|
||||||
@ -728,8 +735,12 @@ pub fn parse_call(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// parse internal command
|
// parse internal command
|
||||||
let (call, _, err) =
|
let (call, _, err) = parse_internal_call(
|
||||||
parse_internal_call(working_set, span(&spans[0..pos]), &spans[pos..], decl_id);
|
working_set,
|
||||||
|
span(&spans[cmd_start..pos]),
|
||||||
|
&spans[pos..],
|
||||||
|
decl_id,
|
||||||
|
);
|
||||||
(
|
(
|
||||||
Expression {
|
Expression {
|
||||||
expr: Expr::Call(call),
|
expr: Expr::Call(call),
|
||||||
@ -748,6 +759,8 @@ pub fn parse_call(
|
|||||||
return (range_expr, range_err);
|
return (range_expr, range_err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Otherwise, try external command
|
||||||
parse_external_call(working_set, spans)
|
parse_external_call(working_set, spans)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1703,40 +1716,36 @@ pub fn parse_type(_working_set: &StateWorkingSet, bytes: &[u8]) -> Type {
|
|||||||
|
|
||||||
pub fn parse_import_pattern(
|
pub fn parse_import_pattern(
|
||||||
working_set: &mut StateWorkingSet,
|
working_set: &mut StateWorkingSet,
|
||||||
span: Span,
|
spans: &[Span],
|
||||||
) -> (ImportPattern, Option<ParseError>) {
|
) -> (ImportPattern, Option<ParseError>) {
|
||||||
let source = working_set.get_span_contents(span);
|
|
||||||
let mut error = None;
|
let mut error = None;
|
||||||
|
|
||||||
let (tokens, err) = lex(source, span.start, &[], &[b'.']);
|
let head = if let Some(head_span) = spans.get(0) {
|
||||||
error = error.or(err);
|
working_set.get_span_contents(*head_span).to_vec()
|
||||||
|
} else {
|
||||||
if tokens.is_empty() {
|
|
||||||
return (
|
return (
|
||||||
ImportPattern {
|
ImportPattern {
|
||||||
head: vec![],
|
head: vec![],
|
||||||
members: vec![],
|
members: vec![],
|
||||||
},
|
},
|
||||||
Some(ParseError::MissingImportPattern(span)),
|
Some(ParseError::WrongImportPattern(span(spans))),
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
|
|
||||||
let head = working_set.get_span_contents(tokens[0].span).to_vec();
|
if let Some(tail_span) = spans.get(1) {
|
||||||
|
|
||||||
if let Some(tail) = tokens.get(2) {
|
|
||||||
// FIXME: expand this to handle deeper imports once we support module imports
|
// FIXME: expand this to handle deeper imports once we support module imports
|
||||||
let tail_span = tail.span;
|
let tail = working_set.get_span_contents(*tail_span);
|
||||||
let tail = working_set.get_span_contents(tail.span);
|
|
||||||
if tail == b"*" {
|
if tail == b"*" {
|
||||||
(
|
(
|
||||||
ImportPattern {
|
ImportPattern {
|
||||||
head,
|
head,
|
||||||
members: vec![ImportPatternMember::Glob { span: tail_span }],
|
members: vec![ImportPatternMember::Glob { span: *tail_span }],
|
||||||
},
|
},
|
||||||
error,
|
error,
|
||||||
)
|
)
|
||||||
} else if tail.starts_with(b"[") {
|
} else if tail.starts_with(b"[") {
|
||||||
let (result, err) = parse_list_expression(working_set, tail_span, &SyntaxShape::String);
|
let (result, err) =
|
||||||
|
parse_list_expression(working_set, *tail_span, &SyntaxShape::String);
|
||||||
error = error.or(err);
|
error = error.or(err);
|
||||||
|
|
||||||
let mut output = vec![];
|
let mut output = vec![];
|
||||||
@ -1773,7 +1782,7 @@ pub fn parse_import_pattern(
|
|||||||
head,
|
head,
|
||||||
members: vec![ImportPatternMember::Name {
|
members: vec![ImportPatternMember::Name {
|
||||||
name: tail.to_vec(),
|
name: tail.to_vec(),
|
||||||
span: tail_span,
|
span: *tail_span,
|
||||||
}],
|
}],
|
||||||
},
|
},
|
||||||
error,
|
error,
|
||||||
@ -2945,10 +2954,10 @@ pub fn parse_expression(
|
|||||||
) -> (Expression, Option<ParseError>) {
|
) -> (Expression, Option<ParseError>) {
|
||||||
let bytes = working_set.get_span_contents(spans[0]);
|
let bytes = working_set.get_span_contents(spans[0]);
|
||||||
|
|
||||||
match bytes[0] {
|
if is_math_expression_byte(bytes[0]) {
|
||||||
b'0' | b'1' | b'2' | b'3' | b'4' | b'5' | b'6' | b'7' | b'8' | b'9' | b'(' | b'{'
|
parse_math_expression(working_set, spans, None)
|
||||||
| b'[' | b'$' | b'"' | b'\'' | b'-' => parse_math_expression(working_set, spans, None),
|
} else {
|
||||||
_ => parse_call(working_set, spans, expand_aliases),
|
parse_call(working_set, spans, expand_aliases)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -506,7 +506,7 @@ impl<'a> StateWorkingSet<'a> {
|
|||||||
let permanent_span_start = self.permanent_state.next_span_start();
|
let permanent_span_start = self.permanent_state.next_span_start();
|
||||||
|
|
||||||
if let Some((_, _, last)) = self.delta.file_contents.last() {
|
if let Some((_, _, last)) = self.delta.file_contents.last() {
|
||||||
permanent_span_start + *last
|
*last
|
||||||
} else {
|
} else {
|
||||||
permanent_span_start
|
permanent_span_start
|
||||||
}
|
}
|
||||||
@ -566,7 +566,7 @@ impl<'a> StateWorkingSet<'a> {
|
|||||||
if permanent_end <= span.start {
|
if permanent_end <= span.start {
|
||||||
for (contents, start, finish) in &self.delta.file_contents {
|
for (contents, start, finish) in &self.delta.file_contents {
|
||||||
if (span.start >= *start) && (span.end <= *finish) {
|
if (span.start >= *start) && (span.end <= *finish) {
|
||||||
return &contents[(span.start - permanent_end)..(span.end - permanent_end)];
|
return &contents[(span.start - start)..(span.end - start)];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
44
src/tests.rs
44
src/tests.rs
@ -247,6 +247,14 @@ fn alias_2() -> TestResult {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn alias_2_multi_word() -> TestResult {
|
||||||
|
run_test(
|
||||||
|
r#"def "foo bar" [$x $y] { $x + $y + 10 }; alias f = foo bar 33; f 100"#,
|
||||||
|
"143",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn block_param1() -> TestResult {
|
fn block_param1() -> TestResult {
|
||||||
run_test("[3] | each { $it + 10 } | get 0", "13")
|
run_test("[3] | each { $it + 10 } | get 0", "13")
|
||||||
@ -389,7 +397,7 @@ fn better_block_types() -> TestResult {
|
|||||||
#[test]
|
#[test]
|
||||||
fn module_imports_1() -> TestResult {
|
fn module_imports_1() -> TestResult {
|
||||||
run_test(
|
run_test(
|
||||||
r#"module foo { export def a [] { 1 }; def b [] { 2 } }; use foo; foo.a"#,
|
r#"module foo { export def a [] { 1 }; def b [] { 2 } }; use foo; foo a"#,
|
||||||
"1",
|
"1",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -397,7 +405,7 @@ fn module_imports_1() -> TestResult {
|
|||||||
#[test]
|
#[test]
|
||||||
fn module_imports_2() -> TestResult {
|
fn module_imports_2() -> TestResult {
|
||||||
run_test(
|
run_test(
|
||||||
r#"module foo { export def a [] { 1 }; def b [] { 2 } }; use foo.a; a"#,
|
r#"module foo { export def a [] { 1 }; def b [] { 2 } }; use foo a; a"#,
|
||||||
"1",
|
"1",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -405,7 +413,7 @@ fn module_imports_2() -> TestResult {
|
|||||||
#[test]
|
#[test]
|
||||||
fn module_imports_3() -> TestResult {
|
fn module_imports_3() -> TestResult {
|
||||||
run_test(
|
run_test(
|
||||||
r#"module foo { export def a [] { 1 }; export def b [] { 2 } }; use foo.*; b"#,
|
r#"module foo { export def a [] { 1 }; export def b [] { 2 } }; use foo *; b"#,
|
||||||
"2",
|
"2",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -413,7 +421,7 @@ fn module_imports_3() -> TestResult {
|
|||||||
#[test]
|
#[test]
|
||||||
fn module_imports_4() -> TestResult {
|
fn module_imports_4() -> TestResult {
|
||||||
fail_test(
|
fail_test(
|
||||||
r#"module foo { export def a [] { 1 }; export def b [] { 2 } }; use foo.c"#,
|
r#"module foo { export def a [] { 1 }; export def b [] { 2 } }; use foo c"#,
|
||||||
"not find import",
|
"not find import",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -421,7 +429,7 @@ fn module_imports_4() -> TestResult {
|
|||||||
#[test]
|
#[test]
|
||||||
fn module_imports_5() -> TestResult {
|
fn module_imports_5() -> TestResult {
|
||||||
run_test(
|
run_test(
|
||||||
r#"module foo { export def a [] { 1 }; def b [] { 2 }; export def c [] { 3 } }; use foo.[a, c]; c"#,
|
r#"module foo { export def a [] { 1 }; def b [] { 2 }; export def c [] { 3 } }; use foo [a, c]; c"#,
|
||||||
"3",
|
"3",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -429,7 +437,7 @@ fn module_imports_5() -> TestResult {
|
|||||||
#[test]
|
#[test]
|
||||||
fn module_import_uses_internal_command() -> TestResult {
|
fn module_import_uses_internal_command() -> TestResult {
|
||||||
run_test(
|
run_test(
|
||||||
r#"module foo { def b [] { 2 }; export def a [] { b } }; use foo; foo.a"#,
|
r#"module foo { def b [] { 2 }; export def a [] { b } }; use foo; foo a"#,
|
||||||
"2",
|
"2",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -491,7 +499,7 @@ fn hide_twice_not_allowed() -> TestResult {
|
|||||||
#[test]
|
#[test]
|
||||||
fn hides_import_1() -> TestResult {
|
fn hides_import_1() -> TestResult {
|
||||||
fail_test(
|
fail_test(
|
||||||
r#"module spam { export def foo [] { "foo" } }; use spam; hide spam.foo; foo"#,
|
r#"module spam { export def foo [] { "foo" } }; use spam; hide spam foo; foo"#,
|
||||||
not_found_msg(),
|
not_found_msg(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -499,7 +507,7 @@ fn hides_import_1() -> TestResult {
|
|||||||
#[test]
|
#[test]
|
||||||
fn hides_import_2() -> TestResult {
|
fn hides_import_2() -> TestResult {
|
||||||
fail_test(
|
fail_test(
|
||||||
r#"module spam { export def foo [] { "foo" } }; use spam; hide spam.*; foo"#,
|
r#"module spam { export def foo [] { "foo" } }; use spam; hide spam *; foo"#,
|
||||||
not_found_msg(),
|
not_found_msg(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -507,7 +515,7 @@ fn hides_import_2() -> TestResult {
|
|||||||
#[test]
|
#[test]
|
||||||
fn hides_import_3() -> TestResult {
|
fn hides_import_3() -> TestResult {
|
||||||
fail_test(
|
fail_test(
|
||||||
r#"module spam { export def foo [] { "foo" } }; use spam; hide spam.[foo]; foo"#,
|
r#"module spam { export def foo [] { "foo" } }; use spam; hide spam [foo]; foo"#,
|
||||||
not_found_msg(),
|
not_found_msg(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -515,7 +523,7 @@ fn hides_import_3() -> TestResult {
|
|||||||
#[test]
|
#[test]
|
||||||
fn hides_import_4() -> TestResult {
|
fn hides_import_4() -> TestResult {
|
||||||
fail_test(
|
fail_test(
|
||||||
r#"module spam { export def foo [] { "foo" } }; use spam.foo; hide foo; foo"#,
|
r#"module spam { export def foo [] { "foo" } }; use spam foo; hide foo; foo"#,
|
||||||
not_found_msg(),
|
not_found_msg(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -523,7 +531,15 @@ fn hides_import_4() -> TestResult {
|
|||||||
#[test]
|
#[test]
|
||||||
fn hides_import_5() -> TestResult {
|
fn hides_import_5() -> TestResult {
|
||||||
fail_test(
|
fail_test(
|
||||||
r#"module spam { export def foo [] { "foo" } }; use spam.*; hide foo; foo"#,
|
r#"module spam { export def foo [] { "foo" } }; use spam *; hide foo; foo"#,
|
||||||
|
not_found_msg(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn hides_import_6() -> TestResult {
|
||||||
|
fail_test(
|
||||||
|
r#"module spam { export def foo [] { "foo" } }; use spam; hide spam; foo"#,
|
||||||
not_found_msg(),
|
not_found_msg(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -539,7 +555,7 @@ fn def_twice_should_fail() -> TestResult {
|
|||||||
#[test]
|
#[test]
|
||||||
fn use_import_after_hide() -> TestResult {
|
fn use_import_after_hide() -> TestResult {
|
||||||
run_test(
|
run_test(
|
||||||
r#"module spam { export def foo [] { "foo" } }; use spam.foo; hide foo; use spam.foo; foo"#,
|
r#"module spam { export def foo [] { "foo" } }; use spam foo; hide foo; use spam foo; foo"#,
|
||||||
"foo",
|
"foo",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -547,7 +563,7 @@ fn use_import_after_hide() -> TestResult {
|
|||||||
#[test]
|
#[test]
|
||||||
fn hide_shadowed_decl() -> TestResult {
|
fn hide_shadowed_decl() -> TestResult {
|
||||||
run_test(
|
run_test(
|
||||||
r#"module spam { export def foo [] { "bar" } }; def foo [] { "foo" }; do { use spam.foo; hide foo; foo }"#,
|
r#"module spam { export def foo [] { "bar" } }; def foo [] { "foo" }; do { use spam foo; hide foo; foo }"#,
|
||||||
"foo",
|
"foo",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -555,7 +571,7 @@ fn hide_shadowed_decl() -> TestResult {
|
|||||||
#[test]
|
#[test]
|
||||||
fn hides_all_decls_within_scope() -> TestResult {
|
fn hides_all_decls_within_scope() -> TestResult {
|
||||||
fail_test(
|
fail_test(
|
||||||
r#"module spam { export def foo [] { "bar" } }; def foo [] { "foo" }; use spam.foo; hide foo; foo"#,
|
r#"module spam { export def foo [] { "bar" } }; def foo [] { "foo" }; use spam foo; hide foo; foo"#,
|
||||||
not_found_msg(),
|
not_found_msg(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user