forked from extern/nushell
call and response serializers
This commit is contained in:
@ -174,4 +174,12 @@ pub enum ParseError {
|
||||
#[error("Module export not found.")]
|
||||
#[diagnostic(code(nu::parser::export_not_found), url(docsrs))]
|
||||
ExportNotFound(#[label = "could not find imports"] Span),
|
||||
|
||||
#[error("File not found")]
|
||||
#[diagnostic(code(nu::parser::export_not_found), url(docsrs))]
|
||||
FileNotFound(String),
|
||||
|
||||
#[error("Plugin error")]
|
||||
#[diagnostic(code(nu::parser::export_not_found), url(docsrs))]
|
||||
PluginError(String),
|
||||
}
|
||||
|
@ -11,6 +11,6 @@ pub use flatten::{flatten_block, FlatShape};
|
||||
pub use lex::{lex, Token, TokenContents};
|
||||
pub use lite_parse::{lite_parse, LiteBlock};
|
||||
pub use parse_keywords::{
|
||||
parse_alias, parse_def, parse_def_predecl, parse_let, parse_module, parse_use,
|
||||
parse_alias, parse_def, parse_def_predecl, parse_let, parse_module, parse_plugin, parse_use,
|
||||
};
|
||||
pub use parser::{find_captures_in_expr, parse, Import, VarDecl};
|
||||
|
@ -1,3 +1,4 @@
|
||||
use nu_plugin::plugin::get_signature;
|
||||
use nu_protocol::{
|
||||
ast::{Block, Call, Expr, Expression, ImportPatternMember, Pipeline, Statement},
|
||||
engine::StateWorkingSet,
|
||||
@ -854,3 +855,78 @@ pub fn parse_source(
|
||||
)),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn parse_plugin(
|
||||
working_set: &mut StateWorkingSet,
|
||||
spans: &[Span],
|
||||
) -> (Statement, Option<ParseError>) {
|
||||
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]);
|
||||
if let Ok(filename) = String::from_utf8(name_expr.to_vec()) {
|
||||
let source_file = Path::new(&filename);
|
||||
|
||||
// get signature from plugin
|
||||
// create plugin command declaration (need struct impl Command)
|
||||
// store declaration in working set
|
||||
match get_signature(source_file) {
|
||||
Err(err) => Some(ParseError::PluginError(format!("{}", err))),
|
||||
Ok(_signature) => None,
|
||||
}
|
||||
} else {
|
||||
Some(ParseError::NonUtf8(spans[1]))
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
let span = spans[2..].iter().fold(spans[2], |acc, next| Span {
|
||||
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),
|
||||
)),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,8 @@ use nu_protocol::{
|
||||
};
|
||||
|
||||
use crate::parse_keywords::{
|
||||
parse_alias, parse_def, parse_def_predecl, parse_hide, parse_let, parse_module, parse_use,
|
||||
parse_alias, parse_def, parse_def_predecl, parse_hide, parse_let, parse_module, parse_plugin,
|
||||
parse_use,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@ -2987,6 +2988,7 @@ pub fn parse_statement(
|
||||
Some(ParseError::UnexpectedKeyword("export".into(), spans[0])),
|
||||
),
|
||||
b"hide" => parse_hide(working_set, spans),
|
||||
b"register" => parse_plugin(working_set, spans),
|
||||
_ => {
|
||||
let (expr, err) = parse_expression(working_set, spans, true);
|
||||
(Statement::Pipeline(Pipeline::from_vec(vec![expr])), err)
|
||||
|
Reference in New Issue
Block a user