Merge branch 'main' into module-export

This commit is contained in:
Jakub Žádník
2021-10-02 18:53:35 +03:00
committed by GitHub
33 changed files with 1462 additions and 82 deletions

View File

@ -35,4 +35,18 @@ impl Call {
false
}
pub fn get_flag_expr(&self, flag_name: &str) -> Option<Expression> {
for name in &self.named {
if flag_name == name.0 {
return name.1.clone();
}
}
None
}
pub fn nth(&self, pos: usize) -> Option<Expression> {
self.positional.get(pos).cloned()
}
}

View File

@ -1,17 +1,36 @@
use super::Expression;
use crate::Span;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PathMember {
String { val: String, span: Span },
Int { val: usize, span: Span },
}
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CellPath {
pub members: Vec<PathMember>,
}
impl CellPath {
pub fn into_string(&self) -> String {
let mut output = String::new();
for (idx, elem) in self.members.iter().enumerate() {
if idx > 0 {
output.push('.');
}
match elem {
PathMember::Int { val, .. } => output.push_str(&format!("{}", val)),
PathMember::String { val, .. } => output.push_str(val),
}
}
output
}
}
#[derive(Debug, Clone)]
pub struct FullCellPath {
pub head: Expression,

View File

@ -1,4 +1,4 @@
use super::{Call, Expression, FullCellPath, Operator, RangeOperator};
use super::{Call, CellPath, Expression, FullCellPath, Operator, RangeOperator};
use crate::{BlockId, Signature, Span, VarId};
#[derive(Debug, Clone)]
@ -24,6 +24,7 @@ pub enum Expr {
Table(Vec<Expression>, Vec<Vec<Expression>>),
Keyword(Vec<u8>, Span, Box<Expression>),
String(String), // FIXME: improve this in the future?
CellPath(CellPath),
FullCellPath(Box<FullCellPath>),
Signature(Box<Signature>),
Garbage,

View File

@ -1,5 +1,5 @@
use super::Command;
use crate::{ast::Block, BlockId, DeclId, Span, Type, VarId};
use crate::{ast::Block, BlockId, DeclId, Signature, Span, Type, VarId};
use core::panic;
use std::{
collections::{HashMap, HashSet},
@ -178,6 +178,21 @@ impl EngineState {
.expect("internal error: missing declaration")
}
pub fn get_decls(&self) -> Vec<Signature> {
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);
}
}
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, Value, VarId};
use crate::{ShellError, Signature, Value, VarId};
#[derive(Clone)]
pub struct EvaluationContext {
@ -46,6 +46,10 @@ impl EvaluationContext {
pub fn print_stack(&self) {
self.stack.print_stack();
}
pub fn get_commands_info(&self) -> Vec<Signature> {
self.engine_state.borrow().get_decls()
}
}
#[derive(Debug)]

View File

@ -74,4 +74,8 @@ pub enum ShellError {
#[error("Unsupported input")]
#[diagnostic(code(nu::shell::unsupported_input), url(docsrs))]
UnsupportedInput(String, #[label("{0}")] Span),
#[error("Flag not found")]
#[diagnostic(code(nu::shell::flag_not_found), url(docsrs))]
FlagNotFound(String, #[label("{0} not found")] Span),
}

View File

@ -1,6 +1,11 @@
use miette::SourceSpan;
use serde::{Deserialize, Serialize};
pub struct Spanned<T> {
pub item: T,
pub span: Span,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Span {
pub start: usize,

View File

@ -9,7 +9,7 @@ pub use stream::*;
use std::fmt::Debug;
use crate::ast::PathMember;
use crate::ast::{CellPath, PathMember};
use crate::{span, BlockId, Span, Type};
use crate::ShellError;
@ -29,6 +29,10 @@ pub enum Value {
val: u64,
span: Span,
},
Duration {
val: u64,
span: Span,
},
Range {
val: Box<Range>,
span: Span,
@ -68,6 +72,10 @@ pub enum Value {
val: Vec<u8>,
span: Span,
},
CellPath {
val: CellPath,
span: Span,
},
}
impl Value {
@ -86,6 +94,7 @@ impl Value {
Value::Int { span, .. } => *span,
Value::Float { span, .. } => *span,
Value::Filesize { span, .. } => *span,
Value::Duration { span, .. } => *span,
Value::Range { span, .. } => *span,
Value::String { span, .. } => *span,
Value::Record { span, .. } => *span,
@ -94,6 +103,7 @@ impl Value {
Value::Stream { span, .. } => *span,
Value::Nothing { span, .. } => *span,
Value::Binary { span, .. } => *span,
Value::CellPath { span, .. } => *span,
}
}
@ -104,6 +114,7 @@ impl Value {
Value::Int { span, .. } => *span = new_span,
Value::Float { span, .. } => *span = new_span,
Value::Filesize { span, .. } => *span = new_span,
Value::Duration { span, .. } => *span = new_span,
Value::Range { span, .. } => *span = new_span,
Value::String { span, .. } => *span = new_span,
Value::Record { span, .. } => *span = new_span,
@ -113,6 +124,7 @@ impl Value {
Value::Nothing { span, .. } => *span = new_span,
Value::Error { .. } => {}
Value::Binary { span, .. } => *span = new_span,
Value::CellPath { span, .. } => *span = new_span,
}
self
@ -125,6 +137,7 @@ impl Value {
Value::Int { .. } => Type::Int,
Value::Float { .. } => Type::Float,
Value::Filesize { .. } => Type::Filesize,
Value::Duration { .. } => Type::Duration,
Value::Range { .. } => Type::Range,
Value::String { .. } => Type::String,
Value::Record { cols, vals, .. } => {
@ -136,6 +149,7 @@ impl Value {
Value::Stream { .. } => Type::ValueStream,
Value::Error { .. } => Type::Error,
Value::Binary { .. } => Type::Binary,
Value::CellPath { .. } => Type::CellPath,
}
}
@ -146,6 +160,7 @@ impl Value {
Value::Int { val, .. } => val.to_string(),
Value::Float { val, .. } => val.to_string(),
Value::Filesize { val, .. } => format!("{} bytes", val),
Value::Duration { val, .. } => format!("{} ns", val),
Value::Range { val, .. } => {
format!(
"range: [{}]",
@ -176,6 +191,7 @@ impl Value {
Value::Nothing { .. } => String::new(),
Value::Error { error } => format!("{:?}", error),
Value::Binary { val, .. } => format!("{:?}", val),
Value::CellPath { val, .. } => val.into_string(),
}
}
@ -185,6 +201,7 @@ impl Value {
Value::Int { val, .. } => val.to_string(),
Value::Float { val, .. } => val.to_string(),
Value::Filesize { val, .. } => format!("{} bytes", val),
Value::Duration { val, .. } => format!("{} ns", val),
Value::Range { val, .. } => val
.into_iter()
.map(|x| x.into_string())
@ -206,6 +223,7 @@ impl Value {
Value::Nothing { .. } => String::new(),
Value::Error { error } => format!("{:?}", error),
Value::Binary { val, .. } => format!("{:?}", val),
Value::CellPath { .. } => self.into_string(),
}
}