Merge branch 'main' of https://github.com/nushell/engine-q into unit-test

This commit is contained in:
Fernando Herrera
2021-10-09 14:17:07 +01:00
38 changed files with 2354 additions and 96 deletions

View File

@ -14,7 +14,7 @@ pub enum Expr {
),
Var(VarId),
Call(Box<Call>),
ExternalCall(Span, Vec<Span>),
ExternalCall(String, Span, Vec<Expression>),
Operator(Operator),
RowCondition(VarId, Box<Expression>),
BinaryOp(Box<Expression>, Box<Expression>, Box<Expression>), //lhs, op, rhs

View File

@ -1,5 +1,5 @@
use super::Command;
use crate::{ast::Block, BlockId, DeclId, Signature, Span, Type, VarId};
use crate::{ast::Block, BlockId, DeclId, Example, Signature, Span, Type, VarId};
use core::panic;
use std::{
collections::{HashMap, HashSet},
@ -178,7 +178,7 @@ impl EngineState {
.expect("internal error: missing declaration")
}
pub fn get_decls(&self) -> Vec<Signature> {
pub fn get_signatures(&self) -> Vec<Signature> {
let mut output = vec![];
for decl in self.decls.iter() {
if decl.get_block_id().is_none() {
@ -193,6 +193,21 @@ impl EngineState {
output
}
pub fn get_signatures_with_examples(&self) -> Vec<(Signature, Vec<Example>)> {
let mut output = vec![];
for decl in self.decls.iter() {
if decl.get_block_id().is_none() {
let mut signature = (*decl).signature();
signature.usage = decl.usage().to_string();
signature.extra_usage = decl.extra_usage().to_string();
output.push((signature, decl.examples()));
}
}
output
}
pub fn get_block(&self, block_id: BlockId) -> &Block {
self.blocks
.get(block_id)

View File

@ -1,7 +1,7 @@
use super::EngineState;
use std::{cell::RefCell, collections::HashMap, rc::Rc};
use crate::{ShellError, Signature, Value, VarId};
use crate::{Example, ShellError, Signature, Value, VarId};
#[derive(Clone)]
pub struct EvaluationContext {
@ -47,8 +47,12 @@ impl EvaluationContext {
self.stack.print_stack();
}
pub fn get_commands_info(&self) -> Vec<Signature> {
self.engine_state.borrow().get_decls()
pub fn get_signatures(&self) -> Vec<Signature> {
self.engine_state.borrow().get_signatures()
}
pub fn get_signatures_with_examples(&self) -> Vec<(Signature, Vec<Example>)> {
self.engine_state.borrow().get_signatures_with_examples()
}
}

View File

@ -19,6 +19,16 @@ pub enum ShellError {
rhs_span: Span,
},
#[error("Pipeline mismatch.")]
#[diagnostic(code(nu::shell::pipeline_mismatch), url(docsrs))]
PipelineMismatch {
expected: Type,
#[label("expected: {expected}")]
expected_span: Span,
#[label("value originates from here")]
origin: Span,
},
#[error("Unsupported operator: {0}.")]
#[diagnostic(code(nu::shell::unsupported_operator), url(docsrs))]
UnsupportedOperator(Operator, #[label = "unsupported operator"] Span),
@ -27,6 +37,10 @@ pub enum ShellError {
#[diagnostic(code(nu::shell::unknown_operator), url(docsrs))]
UnknownOperator(String, #[label = "unsupported operator"] Span),
#[error("Missing parameter: {0}.")]
#[diagnostic(code(nu::shell::missing_parameter), url(docsrs))]
MissingParameter(String, #[label = "missing parameter: {0}"] Span),
#[error("External commands not yet supported")]
#[diagnostic(code(nu::shell::external_commands), url(docsrs))]
ExternalNotSupported(#[label = "external not supported"] Span),
@ -75,6 +89,10 @@ pub enum ShellError {
#[diagnostic(code(nu::shell::unsupported_input), url(docsrs))]
UnsupportedInput(String, #[label("{0}")] Span),
#[error("Command not found")]
#[diagnostic(code(nu::shell::command_not_found), url(docsrs))]
CommandNotFound(#[label("command not found")] Span),
#[error("Flag not found")]
#[diagnostic(code(nu::shell::flag_not_found), url(docsrs))]
FlagNotFound(String, #[label("{0} not found")] Span),
@ -109,6 +127,10 @@ pub enum ShellError {
#[error("Move not possible")]
#[diagnostic(code(nu::shell::move_not_possible_single), url(docsrs))]
MoveNotPossibleSingle(String, #[label("{0}")] Span),
#[error("Create not possible")]
#[diagnostic(code(nu::shell::create_not_possible), url(docsrs))]
CreateNotPossible(String, #[label("{0}")] Span),
}
impl From<std::io::Error> for ShellError {

View File

@ -364,6 +364,73 @@ impl Value {
_ => vec![],
}
}
pub fn map<F>(self, span: Span, mut f: F) -> Value
where
Self: Sized,
F: FnMut(Self) -> Value + 'static,
{
match self {
Value::List { vals, .. } => Value::List {
vals: vals.into_iter().map(f).collect(),
span,
},
Value::Stream { stream, .. } => Value::Stream {
stream: stream.map(f).into_value_stream(),
span,
},
v => {
if v.as_string().is_ok() {
Value::List {
vals: vec![f(v)],
span,
}
} else {
Value::Error {
error: ShellError::PipelineMismatch {
expected: Type::String,
expected_span: span,
origin: v.span(),
},
}
}
}
}
}
pub fn flat_map<U, F>(self, span: Span, mut f: F) -> Value
where
Self: Sized,
U: IntoIterator<Item = Value>,
F: FnMut(Self) -> U + 'static,
{
match self {
Value::List { vals, .. } => Value::List {
vals: vals.into_iter().map(f).flatten().collect(),
span,
},
Value::Stream { stream, .. } => Value::Stream {
stream: stream.map(f).flatten().into_value_stream(),
span,
},
v => {
if v.as_string().is_ok() {
Value::List {
vals: f(v).into_iter().collect(),
span,
}
} else {
Value::Error {
error: ShellError::PipelineMismatch {
expected: Type::String,
expected_span: span,
origin: v.span(),
},
}
}
}
}
}
}
impl PartialEq for Value {