Span ID Refactor - Step 1 (#12960)

# Description
First part of SpanID refactoring series. This PR adds a `SpanId` type
and a corresponding `span_id` field to `Expression`. Parser creating
expressions will now add them to an array in `StateWorkingSet`,
generates a corresponding ID and saves the ID to the Expression. The IDs
are not used anywhere yet.

For the rough overall plan, see
https://github.com/nushell/nushell/issues/12963.

# User-Facing Changes
Hopefully none. This is only a refactor of Nushell's internals that
shouldn't have visible side effects.

# Tests + Formatting

# After Submitting
This commit is contained in:
Jakub Žádník 2024-06-05 04:57:14 +03:00 committed by GitHub
parent b10325dff1
commit e4104d0792
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 1029 additions and 1050 deletions

View File

@ -52,18 +52,16 @@ impl Completer for CustomCompletion {
decl_id: self.decl_id, decl_id: self.decl_id,
head: span, head: span,
arguments: vec![ arguments: vec![
Argument::Positional(Expression { Argument::Positional(Expression::new_unknown(
span: Span::unknown(), Expr::String(self.line.clone()),
ty: Type::String, Span::unknown(),
expr: Expr::String(self.line.clone()), Type::String,
custom_completion: None, )),
}), Argument::Positional(Expression::new_unknown(
Argument::Positional(Expression { Expr::Int(line_pos as i64),
span: Span::unknown(), Span::unknown(),
ty: Type::Int, Type::Int,
expr: Expr::Int(line_pos as i64), )),
custom_completion: None,
}),
], ],
parser_info: HashMap::new(), parser_info: HashMap::new(),
}, },

View File

@ -665,12 +665,7 @@ mod test {
#[test] #[test]
fn test_eval_argument() { fn test_eval_argument() {
fn expression(expr: Expr) -> Expression { fn expression(expr: Expr) -> Expression {
Expression { Expression::new_unknown(expr, Span::unknown(), Type::Any)
expr,
span: Span::unknown(),
ty: Type::Any,
custom_completion: None,
}
} }
fn eval(expr: Expr, spread: bool) -> Result<Vec<String>, ShellError> { fn eval(expr: Expr, spread: bool) -> Result<Vec<String>, ShellError> {

View File

@ -2,9 +2,9 @@ use crate::eval_call;
use nu_protocol::{ use nu_protocol::{
ast::{Argument, Call, Expr, Expression, RecordItem}, ast::{Argument, Call, Expr, Expression, RecordItem},
debugger::WithoutDebug, debugger::WithoutDebug,
engine::{Command, EngineState, Stack}, engine::{Command, EngineState, Stack, UNKNOWN_SPAN_ID},
record, Category, Example, IntoPipelineData, PipelineData, Signature, Span, SyntaxShape, Type, record, Category, Example, IntoPipelineData, PipelineData, Signature, Span, SpanId,
Value, SyntaxShape, Type, Value,
}; };
use std::{collections::HashMap, fmt::Write}; use std::{collections::HashMap, fmt::Write};
@ -339,8 +339,9 @@ fn get_ansi_color_for_component_or_default(
if let Some(color) = &engine_state.get_config().color_config.get(theme_component) { if let Some(color) = &engine_state.get_config().color_config.get(theme_component) {
let caller_stack = &mut Stack::new().capture(); let caller_stack = &mut Stack::new().capture();
let span = Span::unknown(); let span = Span::unknown();
let span_id = UNKNOWN_SPAN_ID;
let argument_opt = get_argument_for_color_value(engine_state, color, span); let argument_opt = get_argument_for_color_value(engine_state, color, span, span_id);
// Call ansi command using argument // Call ansi command using argument
if let Some(argument) = argument_opt { if let Some(argument) = argument_opt {
@ -371,6 +372,7 @@ fn get_argument_for_color_value(
engine_state: &EngineState, engine_state: &EngineState,
color: &&Value, color: &&Value,
span: Span, span: Span,
span_id: SpanId,
) -> Option<Argument> { ) -> Option<Argument> {
match color { match color {
Value::Record { val, .. } => { Value::Record { val, .. } => {
@ -378,43 +380,43 @@ fn get_argument_for_color_value(
.iter() .iter()
.map(|(k, v)| { .map(|(k, v)| {
RecordItem::Pair( RecordItem::Pair(
Expression { Expression::new_existing(
expr: Expr::String(k.clone()), Expr::String(k.clone()),
span, span,
ty: Type::String, span_id,
custom_completion: None, Type::String,
}, ),
Expression { Expression::new_existing(
expr: Expr::String( Expr::String(
v.clone().to_expanded_string("", engine_state.get_config()), v.clone().to_expanded_string("", engine_state.get_config()),
), ),
span, span,
ty: Type::String, span_id,
custom_completion: None, Type::String,
}, ),
) )
}) })
.collect(); .collect();
Some(Argument::Positional(Expression { Some(Argument::Positional(Expression::new_existing(
span: Span::unknown(), Expr::Record(record_exp),
ty: Type::Record( Span::unknown(),
UNKNOWN_SPAN_ID,
Type::Record(
[ [
("fg".to_string(), Type::String), ("fg".to_string(), Type::String),
("attr".to_string(), Type::String), ("attr".to_string(), Type::String),
] ]
.into(), .into(),
), ),
expr: Expr::Record(record_exp), )))
custom_completion: None,
}))
} }
Value::String { val, .. } => Some(Argument::Positional(Expression { Value::String { val, .. } => Some(Argument::Positional(Expression::new_existing(
span: Span::unknown(), Expr::String(val.clone()),
ty: Type::String, Span::unknown(),
expr: Expr::String(val.clone()), UNKNOWN_SPAN_ID,
custom_completion: None, Type::String,
})), ))),
_ => None, _ => None,
} }
} }

View File

@ -1,7 +1,7 @@
use nu_engine::command_prelude::*; use nu_engine::command_prelude::*;
use nu_protocol::{ use nu_protocol::{
ast::{Argument, Expr, Expression}, ast::{Argument, Expr, Expression},
engine::CommandType, engine::{CommandType, UNKNOWN_SPAN_ID},
}; };
#[derive(Clone)] #[derive(Clone)]
@ -56,43 +56,49 @@ impl Command for KnownExternal {
}; };
let extern_name: Vec<_> = extern_name.split(' ').collect(); let extern_name: Vec<_> = extern_name.split(' ').collect();
let call_head_id = engine_state
.find_span_id(call.head)
.unwrap_or(UNKNOWN_SPAN_ID);
let arg_extern_name = Expression { let arg_extern_name = Expression::new_existing(
expr: Expr::String(extern_name[0].to_string()), Expr::String(extern_name[0].to_string()),
span: call.head, call.head,
ty: Type::String, call_head_id,
custom_completion: None, Type::String,
}; );
extern_call.add_positional(arg_extern_name); extern_call.add_positional(arg_extern_name);
for subcommand in extern_name.into_iter().skip(1) { for subcommand in extern_name.into_iter().skip(1) {
extern_call.add_positional(Expression { extern_call.add_positional(Expression::new_existing(
expr: Expr::String(subcommand.to_string()), Expr::String(subcommand.to_string()),
span: call.head, call.head,
ty: Type::String, call_head_id,
custom_completion: None, Type::String,
}); ));
} }
for arg in &call.arguments { for arg in &call.arguments {
match arg { match arg {
Argument::Positional(positional) => extern_call.add_positional(positional.clone()), Argument::Positional(positional) => extern_call.add_positional(positional.clone()),
Argument::Named(named) => { Argument::Named(named) => {
let named_span_id = engine_state
.find_span_id(named.0.span)
.unwrap_or(UNKNOWN_SPAN_ID);
if let Some(short) = &named.1 { if let Some(short) = &named.1 {
extern_call.add_positional(Expression { extern_call.add_positional(Expression::new_existing(
expr: Expr::String(format!("-{}", short.item)), Expr::String(format!("-{}", short.item)),
span: named.0.span, named.0.span,
ty: Type::String, named_span_id,
custom_completion: None, Type::String,
}); ));
} else { } else {
extern_call.add_positional(Expression { extern_call.add_positional(Expression::new_existing(
expr: Expr::String(format!("--{}", named.0.item)), Expr::String(format!("--{}", named.0.item)),
span: named.0.span, named.0.span,
ty: Type::String, named_span_id,
custom_completion: None, Type::String,
}); ));
} }
if let Some(arg) = &named.2 { if let Some(arg) = &named.2 {
extern_call.add_positional(arg.clone()); extern_call.add_positional(arg.clone());

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -68,7 +68,7 @@ pub fn type_compatible(lhs: &Type, rhs: &Type) -> bool {
} }
pub fn math_result_type( pub fn math_result_type(
_working_set: &StateWorkingSet, working_set: &mut StateWorkingSet,
lhs: &mut Expression, lhs: &mut Expression,
op: &mut Expression, op: &mut Expression,
rhs: &mut Expression, rhs: &mut Expression,
@ -104,7 +104,7 @@ pub fn math_result_type(
| Type::Filesize, | Type::Filesize,
_, _,
) => { ) => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationRHS( Some(ParseError::UnsupportedOperationRHS(
@ -118,7 +118,7 @@ pub fn math_result_type(
) )
} }
_ => { _ => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationLHS( Some(ParseError::UnsupportedOperationLHS(
@ -130,7 +130,7 @@ pub fn math_result_type(
) )
} }
}, },
Operator::Math(Math::Append) => check_append(lhs, rhs, op), Operator::Math(Math::Append) => check_append(working_set, lhs, rhs, op),
Operator::Math(Math::Minus) => match (&lhs.ty, &rhs.ty) { Operator::Math(Math::Minus) => match (&lhs.ty, &rhs.ty) {
(Type::Int, Type::Int) => (Type::Int, None), (Type::Int, Type::Int) => (Type::Int, None),
(Type::Float, Type::Int) => (Type::Float, None), (Type::Float, Type::Int) => (Type::Float, None),
@ -152,7 +152,7 @@ pub fn math_result_type(
(Type::Any, _) => (Type::Any, None), (Type::Any, _) => (Type::Any, None),
(_, Type::Any) => (Type::Any, None), (_, Type::Any) => (Type::Any, None),
(Type::Int | Type::Float | Type::Date | Type::Duration | Type::Filesize, _) => { (Type::Int | Type::Float | Type::Date | Type::Duration | Type::Filesize, _) => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationRHS( Some(ParseError::UnsupportedOperationRHS(
@ -166,7 +166,7 @@ pub fn math_result_type(
) )
} }
_ => { _ => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationLHS( Some(ParseError::UnsupportedOperationLHS(
@ -209,7 +209,7 @@ pub fn math_result_type(
| (Type::Duration, _) | (Type::Duration, _)
| (Type::Filesize, _) | (Type::Filesize, _)
| (Type::List(_), _) => { | (Type::List(_), _) => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationRHS( Some(ParseError::UnsupportedOperationRHS(
@ -223,7 +223,7 @@ pub fn math_result_type(
) )
} }
_ => { _ => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationLHS( Some(ParseError::UnsupportedOperationLHS(
@ -252,7 +252,7 @@ pub fn math_result_type(
(Type::Any, _) => (Type::Any, None), (Type::Any, _) => (Type::Any, None),
(_, Type::Any) => (Type::Any, None), (_, Type::Any) => (Type::Any, None),
(Type::Int | Type::Float, _) => { (Type::Int | Type::Float, _) => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationRHS( Some(ParseError::UnsupportedOperationRHS(
@ -266,7 +266,7 @@ pub fn math_result_type(
) )
} }
_ => { _ => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationLHS( Some(ParseError::UnsupportedOperationLHS(
@ -302,7 +302,7 @@ pub fn math_result_type(
(Type::Any, _) => (Type::Any, None), (Type::Any, _) => (Type::Any, None),
(_, Type::Any) => (Type::Any, None), (_, Type::Any) => (Type::Any, None),
(Type::Int | Type::Float | Type::Filesize | Type::Duration, _) => { (Type::Int | Type::Float | Type::Filesize | Type::Duration, _) => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationRHS( Some(ParseError::UnsupportedOperationRHS(
@ -316,7 +316,7 @@ pub fn math_result_type(
) )
} }
_ => { _ => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationLHS( Some(ParseError::UnsupportedOperationLHS(
@ -348,7 +348,7 @@ pub fn math_result_type(
(Type::Any, _) => (Type::Any, None), (Type::Any, _) => (Type::Any, None),
(_, Type::Any) => (Type::Any, None), (_, Type::Any) => (Type::Any, None),
(Type::Int | Type::Float | Type::Filesize | Type::Duration, _) => { (Type::Int | Type::Float | Type::Filesize | Type::Duration, _) => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationRHS( Some(ParseError::UnsupportedOperationRHS(
@ -362,7 +362,7 @@ pub fn math_result_type(
) )
} }
_ => { _ => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationLHS( Some(ParseError::UnsupportedOperationLHS(
@ -390,7 +390,7 @@ pub fn math_result_type(
// definitions. As soon as that syntax is added this should be removed // definitions. As soon as that syntax is added this should be removed
(a, b) if a == b => (Type::Bool, None), (a, b) if a == b => (Type::Bool, None),
(Type::Bool, _) => { (Type::Bool, _) => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationRHS( Some(ParseError::UnsupportedOperationRHS(
@ -404,7 +404,7 @@ pub fn math_result_type(
) )
} }
_ => { _ => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationLHS( Some(ParseError::UnsupportedOperationLHS(
@ -441,7 +441,7 @@ pub fn math_result_type(
(Type::Any, _) => (Type::Bool, None), (Type::Any, _) => (Type::Bool, None),
(_, Type::Any) => (Type::Bool, None), (_, Type::Any) => (Type::Bool, None),
(Type::Int | Type::Float | Type::Duration | Type::Filesize, _) => { (Type::Int | Type::Float | Type::Duration | Type::Filesize, _) => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationRHS( Some(ParseError::UnsupportedOperationRHS(
@ -455,7 +455,7 @@ pub fn math_result_type(
) )
} }
_ => { _ => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationLHS( Some(ParseError::UnsupportedOperationLHS(
@ -491,7 +491,7 @@ pub fn math_result_type(
(Type::Any, _) => (Type::Bool, None), (Type::Any, _) => (Type::Bool, None),
(_, Type::Any) => (Type::Bool, None), (_, Type::Any) => (Type::Bool, None),
(Type::Int | Type::Float | Type::Duration | Type::Filesize, _) => { (Type::Int | Type::Float | Type::Duration | Type::Filesize, _) => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationRHS( Some(ParseError::UnsupportedOperationRHS(
@ -505,7 +505,7 @@ pub fn math_result_type(
) )
} }
_ => { _ => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationLHS( Some(ParseError::UnsupportedOperationLHS(
@ -541,7 +541,7 @@ pub fn math_result_type(
(Type::Nothing, _) => (Type::Nothing, None), (Type::Nothing, _) => (Type::Nothing, None),
(_, Type::Nothing) => (Type::Nothing, None), (_, Type::Nothing) => (Type::Nothing, None),
(Type::Int | Type::Float | Type::Duration | Type::Filesize, _) => { (Type::Int | Type::Float | Type::Duration | Type::Filesize, _) => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationRHS( Some(ParseError::UnsupportedOperationRHS(
@ -555,7 +555,7 @@ pub fn math_result_type(
) )
} }
_ => { _ => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationLHS( Some(ParseError::UnsupportedOperationLHS(
@ -591,7 +591,7 @@ pub fn math_result_type(
(Type::Nothing, _) => (Type::Nothing, None), (Type::Nothing, _) => (Type::Nothing, None),
(_, Type::Nothing) => (Type::Nothing, None), (_, Type::Nothing) => (Type::Nothing, None),
(Type::Int | Type::Float | Type::Duration | Type::Filesize, _) => { (Type::Int | Type::Float | Type::Duration | Type::Filesize, _) => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationRHS( Some(ParseError::UnsupportedOperationRHS(
@ -605,7 +605,7 @@ pub fn math_result_type(
) )
} }
_ => { _ => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationLHS( Some(ParseError::UnsupportedOperationLHS(
@ -638,7 +638,7 @@ pub fn math_result_type(
(Type::Custom(a), _) => (Type::Custom(a.clone()), None), (Type::Custom(a), _) => (Type::Custom(a.clone()), None),
(Type::String, _) => { (Type::String, _) => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationRHS( Some(ParseError::UnsupportedOperationRHS(
@ -652,7 +652,7 @@ pub fn math_result_type(
) )
} }
_ => { _ => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationLHS( Some(ParseError::UnsupportedOperationLHS(
@ -673,7 +673,7 @@ pub fn math_result_type(
(Type::Custom(a), _) => (Type::Custom(a.clone()), None), (Type::Custom(a), _) => (Type::Custom(a.clone()), None),
(Type::String, _) => { (Type::String, _) => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationRHS( Some(ParseError::UnsupportedOperationRHS(
@ -687,7 +687,7 @@ pub fn math_result_type(
) )
} }
_ => { _ => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationLHS( Some(ParseError::UnsupportedOperationLHS(
@ -708,7 +708,7 @@ pub fn math_result_type(
(Type::Custom(a), _) => (Type::Custom(a.clone()), None), (Type::Custom(a), _) => (Type::Custom(a.clone()), None),
(Type::String, _) => { (Type::String, _) => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationRHS( Some(ParseError::UnsupportedOperationRHS(
@ -722,7 +722,7 @@ pub fn math_result_type(
) )
} }
_ => { _ => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationLHS( Some(ParseError::UnsupportedOperationLHS(
@ -743,7 +743,7 @@ pub fn math_result_type(
(Type::Custom(a), _) => (Type::Custom(a.clone()), None), (Type::Custom(a), _) => (Type::Custom(a.clone()), None),
(Type::String, _) => { (Type::String, _) => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationRHS( Some(ParseError::UnsupportedOperationRHS(
@ -757,7 +757,7 @@ pub fn math_result_type(
) )
} }
_ => { _ => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationLHS( Some(ParseError::UnsupportedOperationLHS(
@ -781,7 +781,7 @@ pub fn math_result_type(
(Type::Any, _) => (Type::Bool, None), (Type::Any, _) => (Type::Bool, None),
(_, Type::Any) => (Type::Bool, None), (_, Type::Any) => (Type::Bool, None),
(Type::Int | Type::Float | Type::String, _) => { (Type::Int | Type::Float | Type::String, _) => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationRHS( Some(ParseError::UnsupportedOperationRHS(
@ -795,7 +795,7 @@ pub fn math_result_type(
) )
} }
_ => { _ => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationLHS( Some(ParseError::UnsupportedOperationLHS(
@ -819,7 +819,7 @@ pub fn math_result_type(
(Type::Any, _) => (Type::Bool, None), (Type::Any, _) => (Type::Bool, None),
(_, Type::Any) => (Type::Bool, None), (_, Type::Any) => (Type::Bool, None),
(Type::Int | Type::Float | Type::String, _) => { (Type::Int | Type::Float | Type::String, _) => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationRHS( Some(ParseError::UnsupportedOperationRHS(
@ -833,7 +833,7 @@ pub fn math_result_type(
) )
} }
_ => { _ => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationLHS( Some(ParseError::UnsupportedOperationLHS(
@ -855,7 +855,7 @@ pub fn math_result_type(
(Type::Any, _) => (Type::Any, None), (Type::Any, _) => (Type::Any, None),
(_, Type::Any) => (Type::Any, None), (_, Type::Any) => (Type::Any, None),
(Type::Int, _) => { (Type::Int, _) => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationRHS( Some(ParseError::UnsupportedOperationRHS(
@ -869,7 +869,7 @@ pub fn math_result_type(
) )
} }
_ => { _ => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationLHS( Some(ParseError::UnsupportedOperationLHS(
@ -881,7 +881,9 @@ pub fn math_result_type(
) )
} }
}, },
Operator::Assignment(Assignment::AppendAssign) => check_append(lhs, rhs, op), Operator::Assignment(Assignment::AppendAssign) => {
check_append(working_set, lhs, rhs, op)
}
Operator::Assignment(_) => match (&lhs.ty, &rhs.ty) { Operator::Assignment(_) => match (&lhs.ty, &rhs.ty) {
(x, y) if x == y => (Type::Nothing, None), (x, y) if x == y => (Type::Nothing, None),
(Type::Any, _) => (Type::Nothing, None), (Type::Any, _) => (Type::Nothing, None),
@ -894,7 +896,7 @@ pub fn math_result_type(
}, },
}, },
_ => { _ => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
@ -1026,6 +1028,7 @@ pub fn check_block_input_output(working_set: &StateWorkingSet, block: &Block) ->
} }
fn check_append( fn check_append(
working_set: &mut StateWorkingSet,
lhs: &Expression, lhs: &Expression,
rhs: &Expression, rhs: &Expression,
op: &mut Expression, op: &mut Expression,
@ -1050,7 +1053,7 @@ fn check_append(
(Type::Binary, Type::Binary) => (Type::Binary, None), (Type::Binary, Type::Binary) => (Type::Binary, None),
(Type::Any, _) | (_, Type::Any) => (Type::Any, None), (Type::Any, _) | (_, Type::Any) => (Type::Any, None),
(Type::Table(_) | Type::String | Type::Binary, _) => { (Type::Table(_) | Type::String | Type::Binary, _) => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationRHS( Some(ParseError::UnsupportedOperationRHS(
@ -1064,7 +1067,7 @@ fn check_append(
) )
} }
_ => { _ => {
*op = Expression::garbage(op.span); *op = Expression::garbage(working_set, op.span);
( (
Type::Any, Type::Any,
Some(ParseError::UnsupportedOperationLHS( Some(ParseError::UnsupportedOperationLHS(

View File

@ -338,9 +338,13 @@ impl Call {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
use crate::engine::EngineState;
#[test] #[test]
fn argument_span_named() { fn argument_span_named() {
let engine_state = EngineState::new();
let mut working_set = StateWorkingSet::new(&engine_state);
let named = Spanned { let named = Spanned {
item: "named".to_string(), item: "named".to_string(),
span: Span::new(2, 3), span: Span::new(2, 3),
@ -349,7 +353,7 @@ mod test {
item: "short".to_string(), item: "short".to_string(),
span: Span::new(5, 7), span: Span::new(5, 7),
}; };
let expr = Expression::garbage(Span::new(11, 13)); let expr = Expression::garbage(&mut working_set, Span::new(11, 13));
let arg = Argument::Named((named.clone(), None, None)); let arg = Argument::Named((named.clone(), None, None));
@ -370,8 +374,11 @@ mod test {
#[test] #[test]
fn argument_span_positional() { fn argument_span_positional() {
let engine_state = EngineState::new();
let mut working_set = StateWorkingSet::new(&engine_state);
let span = Span::new(2, 3); let span = Span::new(2, 3);
let expr = Expression::garbage(span); let expr = Expression::garbage(&mut working_set, span);
let arg = Argument::Positional(expr); let arg = Argument::Positional(expr);
assert_eq!(span, arg.span()); assert_eq!(span, arg.span());
@ -379,8 +386,11 @@ mod test {
#[test] #[test]
fn argument_span_unknown() { fn argument_span_unknown() {
let engine_state = EngineState::new();
let mut working_set = StateWorkingSet::new(&engine_state);
let span = Span::new(2, 3); let span = Span::new(2, 3);
let expr = Expression::garbage(span); let expr = Expression::garbage(&mut working_set, span);
let arg = Argument::Unknown(expr); let arg = Argument::Unknown(expr);
assert_eq!(span, arg.span()); assert_eq!(span, arg.span());
@ -388,9 +398,12 @@ mod test {
#[test] #[test]
fn call_arguments_span() { fn call_arguments_span() {
let engine_state = EngineState::new();
let mut working_set = StateWorkingSet::new(&engine_state);
let mut call = Call::new(Span::new(0, 1)); let mut call = Call::new(Span::new(0, 1));
call.add_positional(Expression::garbage(Span::new(2, 3))); call.add_positional(Expression::garbage(&mut working_set, Span::new(2, 3)));
call.add_positional(Expression::garbage(Span::new(5, 7))); call.add_positional(Expression::garbage(&mut working_set, Span::new(5, 7)));
assert_eq!(Span::new(2, 7), call.arguments_span()); assert_eq!(Span::new(2, 7), call.arguments_span());
} }

View File

@ -1,7 +1,7 @@
use crate::{ use crate::{
ast::{Argument, Block, Expr, ExternalArgument, ImportPattern, MatchPattern, RecordItem}, ast::{Argument, Block, Expr, ExternalArgument, ImportPattern, MatchPattern, RecordItem},
engine::StateWorkingSet, engine::{EngineState, StateWorkingSet},
BlockId, DeclId, Signature, Span, Type, VarId, IN_VARIABLE_ID, BlockId, DeclId, Signature, Span, SpanId, Type, VarId, IN_VARIABLE_ID,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::sync::Arc; use std::sync::Arc;
@ -10,15 +10,18 @@ use std::sync::Arc;
pub struct Expression { pub struct Expression {
pub expr: Expr, pub expr: Expr,
pub span: Span, pub span: Span,
pub span_id: SpanId,
pub ty: Type, pub ty: Type,
pub custom_completion: Option<DeclId>, pub custom_completion: Option<DeclId>,
} }
impl Expression { impl Expression {
pub fn garbage(span: Span) -> Expression { pub fn garbage(working_set: &mut StateWorkingSet, span: Span) -> Expression {
let span_id = working_set.add_span(span);
Expression { Expression {
expr: Expr::Garbage, expr: Expr::Garbage,
span, span,
span_id,
ty: Type::Any, ty: Type::Any,
custom_completion: None, custom_completion: None,
} }
@ -471,4 +474,49 @@ impl Expression {
Expr::VarDecl(_) => {} Expr::VarDecl(_) => {}
} }
} }
pub fn new(working_set: &mut StateWorkingSet, expr: Expr, span: Span, ty: Type) -> Expression {
let span_id = working_set.add_span(span);
Expression {
expr,
span,
span_id,
ty,
custom_completion: None,
}
}
pub fn new_existing(expr: Expr, span: Span, span_id: SpanId, ty: Type) -> Expression {
Expression {
expr,
span,
span_id,
ty,
custom_completion: None,
}
}
pub fn new_unknown(expr: Expr, span: Span, ty: Type) -> Expression {
Expression {
expr,
span,
span_id: SpanId(0),
ty,
custom_completion: None,
}
}
pub fn with_span_id(self, span_id: SpanId) -> Expression {
Expression {
expr: self.expr,
span: self.span,
span_id,
ty: self.ty,
custom_completion: self.custom_completion,
}
}
pub fn span(&self, engine_state: &EngineState) -> Span {
engine_state.get_span(self.span_id)
}
} }

View File

@ -8,7 +8,7 @@ use crate::{
}, },
eval_const::create_nu_constant, eval_const::create_nu_constant,
BlockId, Category, Config, DeclId, FileId, HistoryConfig, Module, ModuleId, OverlayId, BlockId, Category, Config, DeclId, FileId, HistoryConfig, Module, ModuleId, OverlayId,
ShellError, Signature, Span, Type, Value, VarId, VirtualPathId, ShellError, Signature, Span, SpanId, Type, Value, VarId, VirtualPathId,
}; };
use fancy_regex::Regex; use fancy_regex::Regex;
use lru::LruCache; use lru::LruCache;
@ -81,6 +81,7 @@ pub struct EngineState {
// especially long, so it helps // especially long, so it helps
pub(super) blocks: Arc<Vec<Arc<Block>>>, pub(super) blocks: Arc<Vec<Arc<Block>>>,
pub(super) modules: Arc<Vec<Arc<Module>>>, pub(super) modules: Arc<Vec<Arc<Module>>>,
pub spans: Vec<Span>,
usage: Usage, usage: Usage,
pub scope: ScopeFrame, pub scope: ScopeFrame,
pub ctrlc: Option<Arc<AtomicBool>>, pub ctrlc: Option<Arc<AtomicBool>>,
@ -115,6 +116,9 @@ pub const IN_VARIABLE_ID: usize = 1;
pub const ENV_VARIABLE_ID: usize = 2; pub const ENV_VARIABLE_ID: usize = 2;
// NOTE: If you add more to this list, make sure to update the > checks based on the last in the list // NOTE: If you add more to this list, make sure to update the > checks based on the last in the list
// The first span is unknown span
pub const UNKNOWN_SPAN_ID: SpanId = SpanId(0);
impl EngineState { impl EngineState {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
@ -132,6 +136,7 @@ impl EngineState {
modules: Arc::new(vec![Arc::new(Module::new( modules: Arc::new(vec![Arc::new(Module::new(
DEFAULT_OVERLAY_NAME.as_bytes().to_vec(), DEFAULT_OVERLAY_NAME.as_bytes().to_vec(),
))]), ))]),
spans: vec![Span::unknown()],
usage: Usage::new(), usage: Usage::new(),
// make sure we have some default overlay: // make sure we have some default overlay:
scope: ScopeFrame::with_empty_overlay( scope: ScopeFrame::with_empty_overlay(
@ -184,6 +189,7 @@ impl EngineState {
self.files.extend(delta.files); self.files.extend(delta.files);
self.virtual_paths.extend(delta.virtual_paths); self.virtual_paths.extend(delta.virtual_paths);
self.vars.extend(delta.vars); self.vars.extend(delta.vars);
self.spans.extend(delta.spans);
self.usage.merge_with(delta.usage); self.usage.merge_with(delta.usage);
// Avoid potentially cloning the Arcs if we aren't adding anything // Avoid potentially cloning the Arcs if we aren't adding anything
@ -565,6 +571,9 @@ impl EngineState {
self.modules.len() self.modules.len()
} }
pub fn num_spans(&self) -> usize {
self.spans.len()
}
pub fn print_vars(&self) { pub fn print_vars(&self) {
for var in self.vars.iter().enumerate() { for var in self.vars.iter().enumerate() {
println!("var{}: {:?}", var.0, var.1); println!("var{}: {:?}", var.0, var.1);
@ -1019,6 +1028,25 @@ impl EngineState {
))); )));
} }
} }
/// Add new span and return its ID
pub fn add_span(&mut self, span: Span) -> SpanId {
self.spans.push(span);
SpanId(self.num_spans() - 1)
}
/// Get existing span
pub fn get_span(&self, span_id: SpanId) -> Span {
*self
.spans
.get(span_id.0)
.expect("internal error: missing span")
}
/// Find ID of a span (should be avoided if possible)
pub fn find_span_id(&self, span: Span) -> Option<SpanId> {
self.spans.iter().position(|sp| sp == &span).map(SpanId)
}
} }
impl Default for EngineState { impl Default for EngineState {

View File

@ -4,7 +4,7 @@ use crate::{
usage::Usage, CachedFile, Command, EngineState, OverlayFrame, ScopeFrame, Variable, usage::Usage, CachedFile, Command, EngineState, OverlayFrame, ScopeFrame, Variable,
VirtualPath, VirtualPath,
}, },
Module, Module, Span,
}; };
use std::sync::Arc; use std::sync::Arc;
@ -21,6 +21,7 @@ pub struct StateDelta {
pub(super) decls: Vec<Box<dyn Command>>, // indexed by DeclId pub(super) decls: Vec<Box<dyn Command>>, // indexed by DeclId
pub blocks: Vec<Arc<Block>>, // indexed by BlockId pub blocks: Vec<Arc<Block>>, // indexed by BlockId
pub(super) modules: Vec<Arc<Module>>, // indexed by ModuleId pub(super) modules: Vec<Arc<Module>>, // indexed by ModuleId
pub spans: Vec<Span>, // indexed by SpanId
pub(super) usage: Usage, pub(super) usage: Usage,
pub scope: Vec<ScopeFrame>, pub scope: Vec<ScopeFrame>,
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]
@ -45,6 +46,7 @@ impl StateDelta {
decls: vec![], decls: vec![],
blocks: vec![], blocks: vec![],
modules: vec![], modules: vec![],
spans: vec![],
scope: vec![scope_frame], scope: vec![scope_frame],
usage: Usage::new(), usage: Usage::new(),
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]

View File

@ -5,7 +5,7 @@ use crate::{
StateDelta, Variable, VirtualPath, Visibility, StateDelta, Variable, VirtualPath, Visibility,
}, },
BlockId, Category, Config, DeclId, FileId, Module, ModuleId, ParseError, ParseWarning, Span, BlockId, Category, Config, DeclId, FileId, Module, ModuleId, ParseError, ParseWarning, Span,
Type, Value, VarId, VirtualPathId, SpanId, Type, Value, VarId, VirtualPathId,
}; };
use core::panic; use core::panic;
use std::{ use std::{
@ -1013,6 +1013,25 @@ impl<'a> StateWorkingSet<'a> {
.expect("internal error: missing virtual path") .expect("internal error: missing virtual path")
} }
} }
pub fn add_span(&mut self, span: Span) -> SpanId {
let num_permanent_spans = self.permanent_state.spans.len();
self.delta.spans.push(span);
SpanId(num_permanent_spans + self.delta.spans.len() - 1)
}
pub fn get_span(&self, span_id: SpanId) -> Span {
let num_permanent_spans = self.permanent_state.num_spans();
if span_id.0 < num_permanent_spans {
self.permanent_state.get_span(span_id)
} else {
*self
.delta
.spans
.get(span_id.0 - num_permanent_spans)
.expect("internal error: missing span")
}
}
} }
impl<'a> miette::SourceCode for &StateWorkingSet<'a> { impl<'a> miette::SourceCode for &StateWorkingSet<'a> {

View File

@ -1,3 +1,5 @@
use serde::{Deserialize, Serialize};
pub type VarId = usize; pub type VarId = usize;
pub type DeclId = usize; pub type DeclId = usize;
pub type BlockId = usize; pub type BlockId = usize;
@ -5,3 +7,5 @@ pub type ModuleId = usize;
pub type OverlayId = usize; pub type OverlayId = usize;
pub type FileId = usize; pub type FileId = usize;
pub type VirtualPathId = usize; pub type VirtualPathId = usize;
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct SpanId(pub usize); // more robust ID style used in the new parser

View File

@ -56,12 +56,12 @@ pub fn from_nuon(input: &str, span: Option<Span>) -> Result<Value, ShellError> {
} }
let expr = if block.pipelines.is_empty() { let expr = if block.pipelines.is_empty() {
Expression { Expression::new(
expr: Expr::Nothing, &mut working_set,
span: span.unwrap_or(Span::unknown()), Expr::Nothing,
custom_completion: None, span.unwrap_or(Span::unknown()),
ty: Type::Nothing, Type::Nothing,
} )
} else { } else {
let mut pipeline = Arc::make_mut(&mut block).pipelines.remove(0); let mut pipeline = Arc::make_mut(&mut block).pipelines.remove(0);
@ -81,12 +81,12 @@ pub fn from_nuon(input: &str, span: Option<Span>) -> Result<Value, ShellError> {
} }
if pipeline.elements.is_empty() { if pipeline.elements.is_empty() {
Expression { Expression::new(
expr: Expr::Nothing, &mut working_set,
span: span.unwrap_or(Span::unknown()), Expr::Nothing,
custom_completion: None, span.unwrap_or(Span::unknown()),
ty: Type::Nothing, Type::Nothing,
} )
} else { } else {
pipeline.elements.remove(0).expr pipeline.elements.remove(0).expr
} }