From 462f783faccf28b7a591226e89ed36b7853189f9 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Thu, 1 Aug 2019 13:58:42 +1200 Subject: [PATCH] initial change to Tagged --- src/cli.rs | 7 +- src/commands/autoview.rs | 8 +- src/commands/cd.rs | 4 +- src/commands/classified.rs | 21 +-- src/commands/command.rs | 25 ++- src/commands/config.rs | 8 +- src/commands/date.rs | 43 ++---- src/commands/first.rs | 2 +- src/commands/from_csv.rs | 47 +++--- src/commands/from_ini.rs | 47 +++--- src/commands/from_json.rs | 55 ++++--- src/commands/from_toml.rs | 55 +++---- src/commands/from_xml.rs | 29 ++-- src/commands/from_yaml.rs | 54 ++++--- src/commands/get.rs | 7 +- src/commands/lines.rs | 2 +- src/commands/ls.rs | 5 +- src/commands/open.rs | 23 ++- src/commands/pick.rs | 2 +- src/commands/plugin.rs | 12 +- src/commands/reject.rs | 6 +- src/commands/save.rs | 4 +- src/commands/size.rs | 12 +- src/commands/sort_by.rs | 2 +- src/commands/split_column.rs | 16 +- src/commands/split_row.rs | 7 +- src/commands/to_array.rs | 2 +- src/commands/to_csv.rs | 31 ++-- src/commands/to_json.rs | 2 +- src/commands/to_toml.rs | 4 +- src/commands/to_yaml.rs | 2 +- src/commands/trim.rs | 2 +- src/context.rs | 7 +- src/errors.rs | 53 +++---- src/evaluate/evaluator.rs | 30 ++-- src/format/table.rs | 4 +- src/format/vtable.rs | 2 +- src/lib.rs | 6 +- src/object.rs | 3 +- src/object/base.rs | 87 +++++------ src/object/config.rs | 8 +- src/object/dict.rs | 61 ++++---- src/object/files.rs | 8 +- src/object/into.rs | 11 +- src/object/process.rs | 8 +- src/object/types.rs | 68 ++++---- src/parser.rs | 1 - src/parser/hir.rs | 24 +-- src/parser/hir/baseline_parse.rs | 24 +-- src/parser/hir/baseline_parse_tokens.rs | 65 ++++---- src/parser/hir/binary.rs | 5 +- src/parser/hir/named.rs | 3 +- src/parser/hir/path.rs | 5 +- src/parser/parse.rs | 1 - src/parser/parse/files.rs | 2 +- src/parser/parse/flag.rs | 2 +- src/parser/parse/parser.rs | 33 ++-- src/parser/parse/pipeline.rs | 5 +- src/parser/parse/span.rs | 197 ------------------------ src/parser/parse/token_tree.rs | 42 ++--- src/parser/parse/token_tree_builder.rs | 32 ++-- src/parser/parse/tokens.rs | 5 +- src/parser/parse_command.rs | 26 ++-- src/parser/registry.rs | 104 ++----------- src/plugin.rs | 11 +- src/plugins/add.rs | 13 +- src/plugins/binaryview.rs | 18 +-- src/plugins/edit.rs | 13 +- src/plugins/inc.rs | 36 ++--- src/plugins/skip.rs | 8 +- src/plugins/str.rs | 22 +-- src/plugins/sum.rs | 26 ++-- src/plugins/sys.rs | 59 ++++--- src/plugins/textview.rs | 16 +- src/plugins/tree.rs | 4 +- src/prelude.rs | 5 +- src/shell/helper.rs | 12 +- src/stream.rs | 32 ++-- 78 files changed, 713 insertions(+), 1040 deletions(-) delete mode 100644 src/parser/parse/span.rs diff --git a/src/cli.rs b/src/cli.rs index d6b1602ba7..5d282ddffd 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -12,7 +12,6 @@ crate use crate::errors::ShellError; use crate::evaluate::Scope; use crate::git::current_branch; use crate::object::Value; -use crate::parser::parse::span::Spanned; use crate::parser::registry; use crate::parser::registry::CommandConfig; use crate::parser::{Pipeline, PipelineElement, TokenNode}; @@ -386,7 +385,7 @@ async fn process_line(readline: Result, ctx: &mut Context } (Some(ClassifiedCommand::Sink(left)), None) => { - let input_vec: Vec> = input.objects.into_vec().await; + let input_vec: Vec> = input.objects.into_vec().await; if let Err(err) = left.run(ctx, input_vec) { return LineResult::Error(line.clone(), err); } @@ -516,13 +515,13 @@ fn classify_command( })) } false => { - let arg_list_strings: Vec> = match call.children() { + let arg_list_strings: Vec> = match call.children() { //Some(args) => args.iter().map(|i| i.as_external_arg(source)).collect(), Some(args) => args .iter() .filter_map(|i| match i { TokenNode::Whitespace(_) => None, - other => Some(Spanned::from_item( + other => Some(Tagged::from_item( other.as_external_arg(source), other.span(), )), diff --git a/src/commands/autoview.rs b/src/commands/autoview.rs index 2fb65b1718..0b9c55136c 100644 --- a/src/commands/autoview.rs +++ b/src/commands/autoview.rs @@ -5,7 +5,7 @@ use crate::prelude::*; pub fn autoview(args: SinkCommandArgs) -> Result<(), ShellError> { if args.input.len() > 0 { - if let Spanned { + if let Tagged { item: Value::Binary(_), .. } = args.input[0] @@ -28,7 +28,7 @@ pub fn autoview(args: SinkCommandArgs) -> Result<(), ShellError> { Ok(()) } -fn equal_shapes(input: &Vec>) -> bool { +fn equal_shapes(input: &Vec>) -> bool { let mut items = input.iter(); let item = match items.next() { @@ -47,11 +47,11 @@ fn equal_shapes(input: &Vec>) -> bool { true } -fn is_single_text_value(input: &Vec>) -> bool { +fn is_single_text_value(input: &Vec>) -> bool { if input.len() != 1 { return false; } - if let Spanned { + if let Tagged { item: Value::Primitive(Primitive::String(_)), .. } = input[0] diff --git a/src/commands/cd.rs b/src/commands/cd.rs index b68a0deb70..bd2b5ba913 100644 --- a/src/commands/cd.rs +++ b/src/commands/cd.rs @@ -25,7 +25,7 @@ pub fn cd(args: CommandArgs) -> Result { return Err(ShellError::labeled_error( "Can not change to directory", "directory not found", - v.span.clone(), + v.span().clone(), )); } } @@ -40,7 +40,7 @@ pub fn cd(args: CommandArgs) -> Result { return Err(ShellError::labeled_error( "Can not change to directory", "directory not found", - args.nth(0).unwrap().span.clone(), + args.nth(0).unwrap().span().clone(), )); } else { return Err(ShellError::string("Can not change to directory")); diff --git a/src/commands/classified.rs b/src/commands/classified.rs index da4ce706cc..8d51089335 100644 --- a/src/commands/classified.rs +++ b/src/commands/classified.rs @@ -1,6 +1,6 @@ use crate::commands::command::Sink; use crate::context::SourceMap; -use crate::parser::{registry::Args, Span, Spanned, TokenNode}; +use crate::parser::{registry::Args, TokenNode}; use crate::prelude::*; use bytes::{BufMut, BytesMut}; use futures::stream::StreamExt; @@ -105,11 +105,7 @@ crate struct SinkCommand { } impl SinkCommand { - crate fn run( - self, - context: &mut Context, - input: Vec>, - ) -> Result<(), ShellError> { + crate fn run(self, context: &mut Context, input: Vec>) -> Result<(), ShellError> { context.run_sink(self.command, self.name_span.clone(), self.args, input) } } @@ -173,7 +169,7 @@ crate struct ExternalCommand { crate name: String, #[allow(unused)] crate name_span: Option, - crate args: Vec>, + crate args: Vec>, } crate enum StreamNext { @@ -190,7 +186,7 @@ impl ExternalCommand { stream_next: StreamNext, ) -> Result { let stdin = input.stdin; - let inputs: Vec> = input.objects.into_vec().await; + let inputs: Vec> = input.objects.into_vec().await; let name_span = self.name_span.clone(); trace!(target: "nu::run::external", "-> {}", self.name); @@ -215,7 +211,7 @@ impl ExternalCommand { let mut span = None; for arg in &self.args { if arg.item.contains("$it") { - span = Some(arg.span); + span = Some(arg.span()); } } if let Some(span) = span { @@ -260,7 +256,7 @@ impl ExternalCommand { let mut span = None; for arg in &self.args { if arg.item.contains("$it") { - span = Some(arg.span); + span = Some(arg.span()); } } return Err(ShellError::maybe_labeled_error( @@ -322,10 +318,9 @@ impl ExternalCommand { let stdout = popen.stdout.take().unwrap(); let file = futures::io::AllowStdIo::new(stdout); let stream = Framed::new(file, LinesCodec {}); - let stream = - stream.map(move |line| Value::string(line.unwrap()).spanned(name_span)); + let stream = stream.map(move |line| Value::string(line.unwrap()).tagged(name_span)); Ok(ClassifiedInputStream::from_input_stream( - stream.boxed() as BoxStream<'static, Spanned> + stream.boxed() as BoxStream<'static, Tagged> )) } } diff --git a/src/commands/command.rs b/src/commands/command.rs index 825afa5925..db338eaf32 100644 --- a/src/commands/command.rs +++ b/src/commands/command.rs @@ -2,10 +2,7 @@ use crate::context::SourceMap; use crate::context::SpanSource; use crate::errors::ShellError; use crate::object::Value; -use crate::parser::{ - registry::{self, Args}, - Span, Spanned, -}; +use crate::parser::registry::{self, Args}; use crate::prelude::*; use getset::Getters; use serde::{Deserialize, Serialize}; @@ -29,15 +26,15 @@ pub struct CommandArgs { } impl CommandArgs { - pub fn nth(&self, pos: usize) -> Option<&Spanned> { + pub fn nth(&self, pos: usize) -> Option<&Tagged> { self.call_info.args.nth(pos) } - pub fn positional_iter(&self) -> impl Iterator> { + pub fn positional_iter(&self) -> impl Iterator> { self.call_info.args.positional_iter() } - pub fn expect_nth(&self, pos: usize) -> Result<&Spanned, ShellError> { + pub fn expect_nth(&self, pos: usize) -> Result<&Tagged, ShellError> { self.call_info.args.expect_nth(pos) } @@ -45,7 +42,7 @@ impl CommandArgs { self.call_info.args.len() } - pub fn get(&self, name: &str) -> Option<&Spanned> { + pub fn get(&self, name: &str) -> Option<&Tagged> { self.call_info.args.get(name) } @@ -58,7 +55,7 @@ impl CommandArgs { pub struct SinkCommandArgs { pub ctx: Context, pub call_info: CallInfo, - pub input: Vec>, + pub input: Vec>, } #[derive(Debug, Serialize, Deserialize)] @@ -70,14 +67,14 @@ pub enum CommandAction { #[derive(Debug, Serialize, Deserialize)] pub enum ReturnSuccess { - Value(Spanned), + Value(Tagged), Action(CommandAction), } pub type ReturnValue = Result; -impl From> for ReturnValue { - fn from(input: Spanned) -> ReturnValue { +impl From> for ReturnValue { + fn from(input: Tagged) -> ReturnValue { Ok(ReturnSuccess::Value(input)) } } @@ -87,7 +84,7 @@ impl ReturnSuccess { Ok(ReturnSuccess::Action(CommandAction::ChangePath(path))) } - pub fn value(input: impl Into>) -> ReturnValue { + pub fn value(input: impl Into>) -> ReturnValue { Ok(ReturnSuccess::Value(input.into())) } @@ -96,7 +93,7 @@ impl ReturnSuccess { } pub fn spanned_value(input: Value, span: Span) -> ReturnValue { - Ok(ReturnSuccess::Value(Spanned::from_item(input, span))) + Ok(ReturnSuccess::Value(Tagged::from_item(input, span))) } } diff --git a/src/commands/config.rs b/src/commands/config.rs index 7da3c91c66..210d5e15cb 100644 --- a/src/commands/config.rs +++ b/src/commands/config.rs @@ -62,7 +62,7 @@ pub fn config(args: CommandArgs) -> Result { config::write_config(&result)?; return Ok( - stream![Spanned::from_item(Value::Object(result.into()), v.span())] + stream![Tagged::from_item(Value::Object(result.into()), v.span())] .from_input_stream(), ); } @@ -74,7 +74,7 @@ pub fn config(args: CommandArgs) -> Result { config::write_config(&result)?; return Ok( - stream![Spanned::from_item(Value::Object(result.into()), c.span())].from_input_stream(), + stream![Tagged::from_item(Value::Object(result.into()), c.span())].from_input_stream(), ); } @@ -90,12 +90,12 @@ pub fn config(args: CommandArgs) -> Result { ))); } - let obj = VecDeque::from_iter(vec![Value::Object(result.into()).spanned(v)]); + let obj = VecDeque::from_iter(vec![Value::Object(result.into()).tagged(v)]); return Ok(obj.from_input_stream()); } if args.len() == 0 { - return Ok(vec![Value::Object(result.into()).spanned(args.call_info.name_span)].into()); + return Ok(vec![Value::Object(result.into()).tagged(args.call_info.name_span)].into()); } Err(ShellError::string(format!("Unimplemented"))) diff --git a/src/commands/date.rs b/src/commands/date.rs index 364cdda60c..e50c9e6bdd 100644 --- a/src/commands/date.rs +++ b/src/commands/date.rs @@ -1,6 +1,5 @@ use crate::errors::ShellError; use crate::object::{Dictionary, Value}; -use crate::parser::Spanned; use crate::prelude::*; use chrono::{DateTime, Local, Utc}; @@ -35,7 +34,7 @@ impl Command for Date { } } -pub fn date_to_value(dt: DateTime, span: Span) -> Spanned +pub fn date_to_value(dt: DateTime, span: Span) -> Tagged where T::Offset: Display, { @@ -43,60 +42,36 @@ where indexmap.insert( "year".to_string(), - Spanned { - item: Value::int(dt.year()), - span, - }, + Tagged::from_item(Value::int(dt.year()), span), ); indexmap.insert( "month".to_string(), - Spanned { - item: Value::int(dt.month()), - span, - }, + Tagged::from_item(Value::int(dt.month()), span), ); indexmap.insert( "day".to_string(), - Spanned { - item: Value::int(dt.day()), - span, - }, + Tagged::from_item(Value::int(dt.day()), span), ); indexmap.insert( "hour".to_string(), - Spanned { - item: Value::int(dt.hour()), - span, - }, + Tagged::from_item(Value::int(dt.hour()), span), ); indexmap.insert( "minute".to_string(), - Spanned { - item: Value::int(dt.minute()), - span, - }, + Tagged::from_item(Value::int(dt.minute()), span), ); indexmap.insert( "second".to_string(), - Spanned { - item: Value::int(dt.second()), - span, - }, + Tagged::from_item(Value::int(dt.second()), span), ); let tz = dt.offset(); indexmap.insert( "timezone".to_string(), - Spanned { - item: Value::string(format!("{}", tz)), - span, - }, + Tagged::from_item(Value::string(format!("{}", tz)), span), ); - Spanned { - item: Value::Object(Dictionary::from(indexmap)), - span, - } + Tagged::from_item(Value::Object(Dictionary::from(indexmap)), span) } pub fn date(args: CommandArgs) -> Result { diff --git a/src/commands/first.rs b/src/commands/first.rs index aec66b3a62..e4ee0e520c 100644 --- a/src/commands/first.rs +++ b/src/commands/first.rs @@ -20,7 +20,7 @@ pub fn first(args: CommandArgs) -> Result { return Err(ShellError::labeled_error( "Value is not a number", "expected integer", - args.expect_nth(0)?.span, + args.expect_nth(0)?.span(), )) } }; diff --git a/src/commands/from_csv.rs b/src/commands/from_csv.rs index 22381a35f5..b86ec42619 100644 --- a/src/commands/from_csv.rs +++ b/src/commands/from_csv.rs @@ -1,12 +1,11 @@ -use crate::object::{Primitive, SpannedDictBuilder, Value}; +use crate::object::{Primitive, TaggedDictBuilder, Value}; use crate::prelude::*; use csv::ReaderBuilder; pub fn from_csv_string_to_value( s: String, span: impl Into, -) -> Result, Box> { - +) -> Result, Box> { let mut reader = ReaderBuilder::new() .has_headers(false) .from_reader(s.as_bytes()); @@ -28,25 +27,22 @@ pub fn from_csv_string_to_value( if let Some(row_values) = iter.next() { let row_values = row_values?; - let mut row = SpannedDictBuilder::new(span); + let mut row = TaggedDictBuilder::new(span); for (idx, entry) in row_values.iter().enumerate() { - row.insert_spanned( + row.insert_tagged( fields.get(idx).unwrap(), - Value::Primitive(Primitive::String(String::from(entry))).spanned(span), + Value::Primitive(Primitive::String(String::from(entry))).tagged(span), ); } - rows.push(row.into_spanned_value()); + rows.push(row.into_tagged_value()); } else { break; } } - Ok(Spanned { - item: Value::List(rows), - span, - }) + Ok(Tagged::from_item(Value::List(rows), span)) } pub fn from_csv(args: CommandArgs) -> Result { @@ -55,20 +51,25 @@ pub fn from_csv(args: CommandArgs) -> Result { Ok(out .values - .map(move |a| match a.item { - Value::Primitive(Primitive::String(s)) => match from_csv_string_to_value(s, span) { - Ok(x) => ReturnSuccess::value(x.spanned(a.span)), - Err(_) => Err(ShellError::maybe_labeled_error( - "Could not parse as CSV", - "piped data failed CSV parse", + .map(move |a| { + let value_span = a.span(); + match a.item { + Value::Primitive(Primitive::String(s)) => { + match from_csv_string_to_value(s, value_span) { + Ok(x) => ReturnSuccess::value(x), + Err(_) => Err(ShellError::maybe_labeled_error( + "Could not parse as CSV", + "piped data failed CSV parse", + span, + )), + } + } + _ => Err(ShellError::maybe_labeled_error( + "Expected string values from pipeline", + "expects strings from pipeline", span, )), - }, - _ => Err(ShellError::maybe_labeled_error( - "Expected string values from pipeline", - "expects strings from pipeline", - span, - )), + } }) .to_output_stream()) } diff --git a/src/commands/from_ini.rs b/src/commands/from_ini.rs index 4dfa9640c3..df7f403427 100644 --- a/src/commands/from_ini.rs +++ b/src/commands/from_ini.rs @@ -1,38 +1,38 @@ -use crate::object::{Primitive, SpannedDictBuilder, Value}; +use crate::object::{Primitive, TaggedDictBuilder, Value}; use crate::prelude::*; use std::collections::HashMap; fn convert_ini_second_to_nu_value( v: &HashMap, span: impl Into, -) -> Spanned { - let mut second = SpannedDictBuilder::new(span); +) -> Tagged { + let mut second = TaggedDictBuilder::new(span); for (key, value) in v.into_iter() { second.insert(key.clone(), Primitive::String(value.clone())); } - second.into_spanned_value() + second.into_tagged_value() } fn convert_ini_top_to_nu_value( v: &HashMap>, span: impl Into, -) -> Spanned { +) -> Tagged { let span = span.into(); - let mut top_level = SpannedDictBuilder::new(span); + let mut top_level = TaggedDictBuilder::new(span); for (key, value) in v.iter() { - top_level.insert_spanned(key.clone(), convert_ini_second_to_nu_value(value, span)); + top_level.insert_tagged(key.clone(), convert_ini_second_to_nu_value(value, span)); } - top_level.into_spanned_value() + top_level.into_tagged_value() } pub fn from_ini_string_to_value( s: String, span: impl Into, -) -> Result, Box> { +) -> Result, Box> { let v: HashMap> = serde_ini::from_str(&s)?; Ok(convert_ini_top_to_nu_value(&v, span)) } @@ -42,20 +42,25 @@ pub fn from_ini(args: CommandArgs) -> Result { let span = args.call_info.name_span; Ok(out .values - .map(move |a| match a.item { - Value::Primitive(Primitive::String(s)) => match from_ini_string_to_value(s, span) { - Ok(x) => ReturnSuccess::value(x.spanned(a.span)), - Err(_) => Err(ShellError::maybe_labeled_error( - "Could not parse as INI", - "piped data failed INI parse", + .map(move |a| { + let value_span = a.span(); + match a.item { + Value::Primitive(Primitive::String(s)) => { + match from_ini_string_to_value(s, value_span) { + Ok(x) => ReturnSuccess::value(x), + Err(_) => Err(ShellError::maybe_labeled_error( + "Could not parse as INI", + "piped data failed INI parse", + span, + )), + } + } + _ => Err(ShellError::maybe_labeled_error( + "Expected string values from pipeline", + "expects strings from pipeline", span, )), - }, - _ => Err(ShellError::maybe_labeled_error( - "Expected string values from pipeline", - "expects strings from pipeline", - span, - )), + } }) .to_output_stream()) } diff --git a/src/commands/from_json.rs b/src/commands/from_json.rs index f3968dd120..b01086b1a0 100644 --- a/src/commands/from_json.rs +++ b/src/commands/from_json.rs @@ -1,36 +1,36 @@ use crate::object::base::OF64; -use crate::object::{Primitive, SpannedDictBuilder, Value}; +use crate::object::{Primitive, TaggedDictBuilder, Value}; use crate::prelude::*; -fn convert_json_value_to_nu_value(v: &serde_hjson::Value, span: impl Into) -> Spanned { +fn convert_json_value_to_nu_value(v: &serde_hjson::Value, span: impl Into) -> Tagged { let span = span.into(); match v { serde_hjson::Value::Null => { - Value::Primitive(Primitive::String(String::from(""))).spanned(span) + Value::Primitive(Primitive::String(String::from(""))).tagged(span) } - serde_hjson::Value::Bool(b) => Value::Primitive(Primitive::Boolean(*b)).spanned(span), + serde_hjson::Value::Bool(b) => Value::Primitive(Primitive::Boolean(*b)).tagged(span), serde_hjson::Value::F64(n) => { - Value::Primitive(Primitive::Float(OF64::from(*n))).spanned(span) + Value::Primitive(Primitive::Float(OF64::from(*n))).tagged(span) } - serde_hjson::Value::U64(n) => Value::Primitive(Primitive::Int(*n as i64)).spanned(span), - serde_hjson::Value::I64(n) => Value::Primitive(Primitive::Int(*n as i64)).spanned(span), + serde_hjson::Value::U64(n) => Value::Primitive(Primitive::Int(*n as i64)).tagged(span), + serde_hjson::Value::I64(n) => Value::Primitive(Primitive::Int(*n as i64)).tagged(span), serde_hjson::Value::String(s) => { - Value::Primitive(Primitive::String(String::from(s))).spanned(span) + Value::Primitive(Primitive::String(String::from(s))).tagged(span) } serde_hjson::Value::Array(a) => Value::List( a.iter() .map(|x| convert_json_value_to_nu_value(x, span)) .collect(), ) - .spanned(span), + .tagged(span), serde_hjson::Value::Object(o) => { - let mut collected = SpannedDictBuilder::new(span); + let mut collected = TaggedDictBuilder::new(span); for (k, v) in o.iter() { - collected.insert_spanned(k.clone(), convert_json_value_to_nu_value(v, span)); + collected.insert_tagged(k.clone(), convert_json_value_to_nu_value(v, span)); } - collected.into_spanned_value() + collected.into_tagged_value() } } } @@ -38,7 +38,7 @@ fn convert_json_value_to_nu_value(v: &serde_hjson::Value, span: impl Into) pub fn from_json_string_to_value( s: String, span: impl Into, -) -> serde_hjson::Result> { +) -> serde_hjson::Result> { let v: serde_hjson::Value = serde_hjson::from_str(&s)?; Ok(convert_json_value_to_nu_value(&v, span)) } @@ -48,20 +48,25 @@ pub fn from_json(args: CommandArgs) -> Result { let span = args.call_info.name_span; Ok(out .values - .map(move |a| match a.item { - Value::Primitive(Primitive::String(s)) => match from_json_string_to_value(s, span) { - Ok(x) => ReturnSuccess::value(x.spanned(a.span)), - Err(_) => Err(ShellError::maybe_labeled_error( - "Could not parse as JSON", - "piped data failed JSON parse", + .map(move |a| { + let value_span = a.span(); + match a.item { + Value::Primitive(Primitive::String(s)) => { + match from_json_string_to_value(s, value_span) { + Ok(x) => ReturnSuccess::value(x), + Err(_) => Err(ShellError::maybe_labeled_error( + "Could not parse as JSON", + "piped data failed JSON parse", + span, + )), + } + } + _ => Err(ShellError::maybe_labeled_error( + "Expected string values from pipeline", + "expects strings from pipeline", span, )), - }, - _ => Err(ShellError::maybe_labeled_error( - "Expected string values from pipeline", - "expects strings from pipeline", - span, - )), + } }) .to_output_stream()) } diff --git a/src/commands/from_toml.rs b/src/commands/from_toml.rs index cf650dd99f..dda292ac54 100644 --- a/src/commands/from_toml.rs +++ b/src/commands/from_toml.rs @@ -1,34 +1,32 @@ use crate::object::base::OF64; -use crate::object::{Primitive, SpannedDictBuilder, Value}; +use crate::object::{Primitive, TaggedDictBuilder, Value}; use crate::prelude::*; -fn convert_toml_value_to_nu_value(v: &toml::Value, span: impl Into) -> Spanned { +fn convert_toml_value_to_nu_value(v: &toml::Value, span: impl Into) -> Tagged { let span = span.into(); match v { - toml::Value::Boolean(b) => Value::Primitive(Primitive::Boolean(*b)).spanned(span), - toml::Value::Integer(n) => Value::Primitive(Primitive::Int(*n)).spanned(span), - toml::Value::Float(n) => Value::Primitive(Primitive::Float(OF64::from(*n))).spanned(span), - toml::Value::String(s) => { - Value::Primitive(Primitive::String(String::from(s))).spanned(span) - } + toml::Value::Boolean(b) => Value::Primitive(Primitive::Boolean(*b)).tagged(span), + toml::Value::Integer(n) => Value::Primitive(Primitive::Int(*n)).tagged(span), + toml::Value::Float(n) => Value::Primitive(Primitive::Float(OF64::from(*n))).tagged(span), + toml::Value::String(s) => Value::Primitive(Primitive::String(String::from(s))).tagged(span), toml::Value::Array(a) => Value::List( a.iter() .map(|x| convert_toml_value_to_nu_value(x, span)) .collect(), ) - .spanned(span), + .tagged(span), toml::Value::Datetime(dt) => { - Value::Primitive(Primitive::String(dt.to_string())).spanned(span) + Value::Primitive(Primitive::String(dt.to_string())).tagged(span) } toml::Value::Table(t) => { - let mut collected = SpannedDictBuilder::new(span); + let mut collected = TaggedDictBuilder::new(span); for (k, v) in t.iter() { - collected.insert_spanned(k.clone(), convert_toml_value_to_nu_value(v, span)); + collected.insert_tagged(k.clone(), convert_toml_value_to_nu_value(v, span)); } - collected.into_spanned_value() + collected.into_tagged_value() } } } @@ -36,7 +34,7 @@ fn convert_toml_value_to_nu_value(v: &toml::Value, span: impl Into) -> Spa pub fn from_toml_string_to_value( s: String, span: impl Into, -) -> Result, Box> { +) -> Result, Box> { let v: toml::Value = s.parse::()?; Ok(convert_toml_value_to_nu_value(&v, span)) } @@ -46,20 +44,25 @@ pub fn from_toml(args: CommandArgs) -> Result { let span = args.call_info.name_span; Ok(out .values - .map(move |a| match a.item { - Value::Primitive(Primitive::String(s)) => match from_toml_string_to_value(s, span) { - Ok(x) => ReturnSuccess::value(x.spanned(a.span)), - Err(_) => Err(ShellError::maybe_labeled_error( - "Could not parse as TOML", - "piped data failed TOML parse", + .map(move |a| { + let value_span = a.span(); + match a.item { + Value::Primitive(Primitive::String(s)) => { + match from_toml_string_to_value(s, value_span) { + Ok(x) => ReturnSuccess::value(x), + Err(_) => Err(ShellError::maybe_labeled_error( + "Could not parse as TOML", + "piped data failed TOML parse", + span, + )), + } + } + _ => Err(ShellError::maybe_labeled_error( + "Expected string values from pipeline", + "expects strings from pipeline", span, )), - }, - _ => Err(ShellError::maybe_labeled_error( - "Expected string values from pipeline", - "expects strings from pipeline", - span, - )), + } }) .to_output_stream()) } diff --git a/src/commands/from_xml.rs b/src/commands/from_xml.rs index cc221df33e..082e29c226 100644 --- a/src/commands/from_xml.rs +++ b/src/commands/from_xml.rs @@ -1,10 +1,7 @@ -use crate::object::{Primitive, SpannedDictBuilder, Value}; +use crate::object::{Primitive, TaggedDictBuilder, Value}; use crate::prelude::*; -fn from_node_to_value<'a, 'd>( - n: &roxmltree::Node<'a, 'd>, - span: impl Into, -) -> Spanned { +fn from_node_to_value<'a, 'd>(n: &roxmltree::Node<'a, 'd>, span: impl Into) -> Tagged { let span = span.into(); if n.is_element() { @@ -15,10 +12,10 @@ fn from_node_to_value<'a, 'd>( children_values.push(from_node_to_value(&c, span)); } - let children_values: Vec> = children_values + let children_values: Vec> = children_values .into_iter() .filter(|x| match x { - Spanned { + Tagged { item: Value::Primitive(Primitive::String(f)), .. } => { @@ -32,29 +29,29 @@ fn from_node_to_value<'a, 'd>( }) .collect(); - let mut collected = SpannedDictBuilder::new(span); + let mut collected = TaggedDictBuilder::new(span); collected.insert(name.clone(), Value::List(children_values)); - collected.into_spanned_value() + collected.into_tagged_value() } else if n.is_comment() { - Value::string("").spanned(span) + Value::string("").tagged(span) } else if n.is_pi() { - Value::string("").spanned(span) + Value::string("").tagged(span) } else if n.is_text() { - Value::string(n.text().unwrap()).spanned(span) + Value::string(n.text().unwrap()).tagged(span) } else { - Value::string("").spanned(span) + Value::string("").tagged(span) } } -fn from_document_to_value(d: &roxmltree::Document, span: impl Into) -> Spanned { +fn from_document_to_value(d: &roxmltree::Document, span: impl Into) -> Tagged { from_node_to_value(&d.root_element(), span) } pub fn from_xml_string_to_value( s: String, span: impl Into, -) -> Result, Box> { +) -> Result, Box> { let parsed = roxmltree::Document::parse(&s)?; Ok(from_document_to_value(&parsed, span)) } @@ -66,7 +63,7 @@ pub fn from_xml(args: CommandArgs) -> Result { .values .map(move |a| match a.item { Value::Primitive(Primitive::String(s)) => match from_xml_string_to_value(s, span) { - Ok(x) => ReturnSuccess::value(x.spanned(a.span)), + Ok(x) => ReturnSuccess::value(x), Err(_) => Err(ShellError::maybe_labeled_error( "Could not parse as XML", "piped data failed XML parse", diff --git a/src/commands/from_yaml.rs b/src/commands/from_yaml.rs index 475926d4ab..6323b8d226 100644 --- a/src/commands/from_yaml.rs +++ b/src/commands/from_yaml.rs @@ -1,41 +1,40 @@ use crate::object::base::OF64; -use crate::object::{Primitive, SpannedDictBuilder, Value}; +use crate::object::{Primitive, TaggedDictBuilder, Value}; use crate::prelude::*; -fn convert_yaml_value_to_nu_value(v: &serde_yaml::Value, span: impl Into) -> Spanned { +fn convert_yaml_value_to_nu_value(v: &serde_yaml::Value, span: impl Into) -> Tagged { let span = span.into(); match v { - serde_yaml::Value::Bool(b) => Value::Primitive(Primitive::Boolean(*b)).spanned(span), + serde_yaml::Value::Bool(b) => Value::Primitive(Primitive::Boolean(*b)).tagged(span), serde_yaml::Value::Number(n) if n.is_i64() => { - Value::Primitive(Primitive::Int(n.as_i64().unwrap())).spanned(span) + Value::Primitive(Primitive::Int(n.as_i64().unwrap())).tagged(span) } serde_yaml::Value::Number(n) if n.is_f64() => { - Value::Primitive(Primitive::Float(OF64::from(n.as_f64().unwrap()))).spanned(span) + Value::Primitive(Primitive::Float(OF64::from(n.as_f64().unwrap()))).tagged(span) } - serde_yaml::Value::String(s) => Value::string(s).spanned(span), + serde_yaml::Value::String(s) => Value::string(s).tagged(span), serde_yaml::Value::Sequence(a) => Value::List( a.iter() .map(|x| convert_yaml_value_to_nu_value(x, span)) .collect(), ) - .spanned(span), + .tagged(span), serde_yaml::Value::Mapping(t) => { - let mut collected = SpannedDictBuilder::new(span); + let mut collected = TaggedDictBuilder::new(span); for (k, v) in t.iter() { match k { serde_yaml::Value::String(k) => { - collected - .insert_spanned(k.clone(), convert_yaml_value_to_nu_value(v, span)); + collected.insert_tagged(k.clone(), convert_yaml_value_to_nu_value(v, span)); } _ => unimplemented!("Unknown key type"), } } - collected.into_spanned_value() + collected.into_tagged_value() } - serde_yaml::Value::Null => Value::Primitive(Primitive::Nothing).spanned(span), + serde_yaml::Value::Null => Value::Primitive(Primitive::Nothing).tagged(span), x => unimplemented!("Unsupported yaml case: {:?}", x), } } @@ -43,7 +42,7 @@ fn convert_yaml_value_to_nu_value(v: &serde_yaml::Value, span: impl Into) pub fn from_yaml_string_to_value( s: String, span: impl Into, -) -> serde_yaml::Result> { +) -> serde_yaml::Result> { let v: serde_yaml::Value = serde_yaml::from_str(&s)?; Ok(convert_yaml_value_to_nu_value(&v, span)) } @@ -53,20 +52,25 @@ pub fn from_yaml(args: CommandArgs) -> Result { let span = args.call_info.name_span; Ok(out .values - .map(move |a| match a.item { - Value::Primitive(Primitive::String(s)) => match from_yaml_string_to_value(s, span) { - Ok(x) => ReturnSuccess::value(x.spanned(a.span)), - Err(_) => Err(ShellError::maybe_labeled_error( - "Could not parse as YAML", - "piped data failed YAML parse", + .map(move |a| { + let value_span = a.span(); + match a.item { + Value::Primitive(Primitive::String(s)) => { + match from_yaml_string_to_value(s, value_span) { + Ok(x) => ReturnSuccess::value(x), + Err(_) => Err(ShellError::maybe_labeled_error( + "Could not parse as YAML", + "piped data failed YAML parse", + span, + )), + } + } + _ => Err(ShellError::maybe_labeled_error( + "Expected string values from pipeline", + "expects strings from pipeline", span, )), - }, - _ => Err(ShellError::maybe_labeled_error( - "Expected string values from pipeline", - "expects strings from pipeline", - span, - )), + } }) .to_output_stream()) } diff --git a/src/commands/get.rs b/src/commands/get.rs index 6cdea2e1fb..d5b4454acd 100644 --- a/src/commands/get.rs +++ b/src/commands/get.rs @@ -1,9 +1,8 @@ use crate::errors::ShellError; use crate::object::Value; -use crate::parser::Span; use crate::prelude::*; -fn get_member(path: &str, span: Span, obj: &Spanned) -> Result, ShellError> { +fn get_member(path: &str, span: Span, obj: &Tagged) -> Result, ShellError> { let mut current = obj; for p in path.split(".") { match current.get_data_by_key(p) { @@ -44,7 +43,7 @@ pub fn get(args: CommandArgs) -> Result { let fields: Result, _> = args .positional_iter() - .map(|a| (a.as_string().map(|x| (x, a.span)))) + .map(|a| (a.as_string().map(|x| (x, a.span())))) .collect(); let fields = fields?; @@ -56,7 +55,7 @@ pub fn get(args: CommandArgs) -> Result { let mut result = VecDeque::new(); for field in &fields { match get_member(&field.0, field.1, &item) { - Ok(Spanned { + Ok(Tagged { item: Value::List(l), .. }) => { diff --git a/src/commands/lines.rs b/src/commands/lines.rs index 219f4d39db..05eaf1000f 100644 --- a/src/commands/lines.rs +++ b/src/commands/lines.rs @@ -20,7 +20,7 @@ pub fn lines(args: CommandArgs) -> Result { let mut result = VecDeque::new(); for s in split_result { result.push_back(ReturnSuccess::value( - Value::Primitive(Primitive::String(s.into())).spanned_unknown(), + Value::Primitive(Primitive::String(s.into())).tagged_unknown(), )); } result diff --git a/src/commands/ls.rs b/src/commands/ls.rs index 72ef4883b9..000186ac95 100644 --- a/src/commands/ls.rs +++ b/src/commands/ls.rs @@ -1,6 +1,5 @@ use crate::errors::ShellError; use crate::object::{dir_entry_dict, Primitive, Value}; -use crate::parser::Spanned; use crate::prelude::*; use std::path::{Path, PathBuf}; @@ -9,7 +8,7 @@ pub fn ls(args: CommandArgs) -> Result { let path = env.path.to_path_buf(); let mut full_path = PathBuf::from(path); match &args.nth(0) { - Some(Spanned { + Some(Tagged { item: Value::Primitive(Primitive::String(s)), .. }) => full_path.push(Path::new(&s)), @@ -24,7 +23,7 @@ pub fn ls(args: CommandArgs) -> Result { return Err(ShellError::labeled_error( e.to_string(), e.to_string(), - s.span, + s.span(), )); } else { return Err(ShellError::maybe_labeled_error( diff --git a/src/commands/open.rs b/src/commands/open.rs index a70f01faa6..c454b36fe1 100644 --- a/src/commands/open.rs +++ b/src/commands/open.rs @@ -1,7 +1,6 @@ use crate::context::SpanSource; use crate::errors::ShellError; use crate::object::{Primitive, Switch, Value}; -use crate::parser::parse::span::Span; use crate::prelude::*; use mime::Mime; use std::path::{Path, PathBuf}; @@ -9,7 +8,7 @@ use std::str::FromStr; use uuid::Uuid; command! { - Open as open(args, path: Spanned, --raw: Switch,) { + Open as open(args, path: Tagged, --raw: Switch,) { let span = args.call_info.name_span; let cwd = args @@ -21,9 +20,9 @@ command! { let full_path = PathBuf::from(cwd); - let path_str = path.to_str().ok_or(ShellError::type_error("Path", "invalid path".spanned(path.span)))?; + let path_str = path.to_str().ok_or(ShellError::type_error("Path", "invalid path".tagged(path.span())))?; - let (file_extension, contents, contents_span, span_source) = fetch(&full_path, path_str, path.span)?; + let (file_extension, contents, contents_span, span_source) = fetch(&full_path, path_str, path.span())?; let file_extension = if raw.is_present() { None @@ -48,7 +47,7 @@ command! { )?; match value { - Spanned { item: Value::List(list), .. } => { + Tagged { item: Value::List(list), .. } => { for elem in list { stream.push_back(ReturnSuccess::value(elem)); } @@ -57,7 +56,7 @@ command! { } }, - other => stream.push_back(ReturnSuccess::value(other.spanned(contents_span))), + other => stream.push_back(ReturnSuccess::value(other.tagged(contents_span))), }; stream @@ -206,11 +205,11 @@ pub fn parse_as_value( contents: String, contents_span: Span, name_span: Option, -) -> Result, ShellError> { +) -> Result, ShellError> { match extension { Some(x) if x == "csv" => { crate::commands::from_csv::from_csv_string_to_value(contents, contents_span) - .map(|c| c.spanned(contents_span)) + .map(|c| c.tagged(contents_span)) .map_err(move |_| { ShellError::maybe_labeled_error( "Could not open as CSV", @@ -221,7 +220,7 @@ pub fn parse_as_value( } Some(x) if x == "toml" => { crate::commands::from_toml::from_toml_string_to_value(contents, contents_span) - .map(|c| c.spanned(contents_span)) + .map(|c| c.tagged(contents_span)) .map_err(move |_| { ShellError::maybe_labeled_error( "Could not open as TOML", @@ -232,7 +231,7 @@ pub fn parse_as_value( } Some(x) if x == "json" => { crate::commands::from_json::from_json_string_to_value(contents, contents_span) - .map(|c| c.spanned(contents_span)) + .map(|c| c.tagged(contents_span)) .map_err(move |_| { ShellError::maybe_labeled_error( "Could not open as JSON", @@ -243,7 +242,7 @@ pub fn parse_as_value( } Some(x) if x == "ini" => { crate::commands::from_ini::from_ini_string_to_value(contents, contents_span) - .map(|c| c.spanned(contents_span)) + .map(|c| c.tagged(contents_span)) .map_err(move |_| { ShellError::maybe_labeled_error( "Could not open as INI", @@ -285,6 +284,6 @@ pub fn parse_as_value( }, ) } - _ => Ok(Value::string(contents).spanned(contents_span)), + _ => Ok(Value::string(contents).tagged(contents_span)), } } diff --git a/src/commands/pick.rs b/src/commands/pick.rs index b28660c2a1..2891eda023 100644 --- a/src/commands/pick.rs +++ b/src/commands/pick.rs @@ -17,7 +17,7 @@ pub fn pick(args: CommandArgs) -> Result { let objects = input .values - .map(move |value| select_fields(&value.item, &fields, value.span)); + .map(move |value| select_fields(&value.item, &fields, value.span())); Ok(objects.from_input_stream()) } diff --git a/src/commands/plugin.rs b/src/commands/plugin.rs index 2394a097f4..30633197ec 100644 --- a/src/commands/plugin.rs +++ b/src/commands/plugin.rs @@ -78,11 +78,11 @@ pub fn filter_plugin(path: String, args: CommandArgs) -> Result> = VecDeque::new(); - bos.push_back(Value::Primitive(Primitive::BeginningOfStream).spanned_unknown()); + let mut bos: VecDeque> = VecDeque::new(); + bos.push_back(Value::Primitive(Primitive::BeginningOfStream).tagged_unknown()); - let mut eos: VecDeque> = VecDeque::new(); - eos.push_back(Value::Primitive(Primitive::EndOfStream).spanned_unknown()); + let mut eos: VecDeque> = VecDeque::new(); + eos.push_back(Value::Primitive(Primitive::EndOfStream).tagged_unknown()); let call_info = args.call_info; @@ -90,7 +90,7 @@ pub fn filter_plugin(path: String, args: CommandArgs) -> Result { @@ -136,7 +136,7 @@ pub fn filter_plugin(path: String, args: CommandArgs) -> Result { diff --git a/src/commands/reject.rs b/src/commands/reject.rs index 7752076295..5b669cf579 100644 --- a/src/commands/reject.rs +++ b/src/commands/reject.rs @@ -17,9 +17,9 @@ pub fn reject(args: CommandArgs) -> Result { let fields = fields?; let stream = args.input.values.map(move |item| { - reject_fields(&item, &fields, item.span) - .into_spanned_value() - .spanned(name_span) + reject_fields(&item, &fields, item.span()) + .into_tagged_value() + .tagged(name_span) }); Ok(stream.from_input_stream()) diff --git a/src/commands/save.rs b/src/commands/save.rs index 866f539f08..03216e4a2a 100644 --- a/src/commands/save.rs +++ b/src/commands/save.rs @@ -5,7 +5,7 @@ use crate::commands::to_toml::value_to_toml_value; use crate::commands::to_yaml::value_to_yaml_value; use crate::errors::ShellError; use crate::object::{Primitive, Value}; -use crate::parser::Spanned; +use crate::Tagged; use std::path::{Path, PathBuf}; pub fn save(args: SinkCommandArgs) -> Result<(), ShellError> { @@ -30,7 +30,7 @@ pub fn save(args: SinkCommandArgs) -> Result<(), ShellError> { } let save_raw = match positional.get(1) { - Some(Spanned { + Some(Tagged { item: Value::Primitive(Primitive::String(s)), .. }) if s == "--raw" => true, diff --git a/src/commands/size.rs b/src/commands/size.rs index b255ce4804..40d78581ca 100644 --- a/src/commands/size.rs +++ b/src/commands/size.rs @@ -1,5 +1,5 @@ use crate::errors::ShellError; -use crate::object::{SpannedDictBuilder, Value}; +use crate::object::{TaggedDictBuilder, Value}; use crate::prelude::*; pub fn size(args: CommandArgs) -> Result { @@ -7,17 +7,17 @@ pub fn size(args: CommandArgs) -> Result { Ok(input .values .map(move |v| match v.item { - Value::Primitive(Primitive::String(s)) => ReturnSuccess::value(count(&s, v.span)), + Value::Primitive(Primitive::String(ref s)) => ReturnSuccess::value(count(s, v.span())), _ => Err(ShellError::maybe_labeled_error( "Expected string values from pipeline", "expects strings from pipeline", - Some(v.span), + Some(v.span()), )), }) .to_output_stream()) } -fn count(contents: &str, span: impl Into) -> Spanned { +fn count(contents: &str, span: impl Into) -> Tagged { let mut lines: i64 = 0; let mut words: i64 = 0; let mut chars: i64 = 0; @@ -42,7 +42,7 @@ fn count(contents: &str, span: impl Into) -> Spanned { } } - let mut dict = SpannedDictBuilder::new(span); + let mut dict = TaggedDictBuilder::new(span); //TODO: add back in name when we have it in the span //dict.insert("name", Value::string(name)); dict.insert("lines", Value::int(lines)); @@ -50,5 +50,5 @@ fn count(contents: &str, span: impl Into) -> Spanned { dict.insert("chars", Value::int(chars)); dict.insert("max length", Value::int(bytes)); - dict.into_spanned_value() + dict.into_tagged_value() } diff --git a/src/commands/sort_by.rs b/src/commands/sort_by.rs index e7e69d6da9..2d7306e8a7 100644 --- a/src/commands/sort_by.rs +++ b/src/commands/sort_by.rs @@ -12,7 +12,7 @@ pub fn sort_by(args: CommandArgs) -> Result { fields .iter() .map(|f| item.get_data_by_key(f).map(|i| i.clone())) - .collect::>>>() + .collect::>>>() }); vec.into_iter().collect::>() diff --git a/src/commands/split_column.rs b/src/commands/split_column.rs index 90c12b8f88..e67d078acb 100644 --- a/src/commands/split_column.rs +++ b/src/commands/split_column.rs @@ -1,5 +1,5 @@ use crate::errors::ShellError; -use crate::object::{Primitive, SpannedDictBuilder, Value}; +use crate::object::{Primitive, TaggedDictBuilder, Value}; use crate::prelude::*; use log::trace; @@ -20,7 +20,7 @@ pub fn split_column(args: CommandArgs) -> Result { Ok(input .values .map(move |v| match v.item { - Value::Primitive(Primitive::String(s)) => { + Value::Primitive(Primitive::String(ref s)) => { let splitter = positional[0].as_string().unwrap().replace("\\n", "\n"); trace!("splitting with {:?}", splitter); let split_result: Vec<_> = s.split(&splitter).filter(|s| s.trim() != "").collect(); @@ -34,27 +34,27 @@ pub fn split_column(args: CommandArgs) -> Result { gen_columns.push(format!("Column{}", i + 1)); } - let mut dict = SpannedDictBuilder::new(v.span); + let mut dict = TaggedDictBuilder::new(v.span()); for (&k, v) in split_result.iter().zip(gen_columns.iter()) { dict.insert(v.clone(), Primitive::String(k.into())); } - ReturnSuccess::value(dict.into_spanned_value()) + ReturnSuccess::value(dict.into_tagged_value()) } else if split_result.len() == (positional.len() - 1) { - let mut dict = SpannedDictBuilder::new(v.span); + let mut dict = TaggedDictBuilder::new(v.span()); for (&k, v) in split_result.iter().zip(positional.iter().skip(1)) { dict.insert( v.as_string().unwrap(), Value::Primitive(Primitive::String(k.into())), ); } - ReturnSuccess::value(dict.into_spanned_value()) + ReturnSuccess::value(dict.into_tagged_value()) } else { - let mut dict = SpannedDictBuilder::new(v.span); + let mut dict = TaggedDictBuilder::new(v.span()); for k in positional.iter().skip(1) { dict.insert(k.as_string().unwrap().trim(), Primitive::String("".into())); } - ReturnSuccess::value(dict.into_spanned_value()) + ReturnSuccess::value(dict.into_tagged_value()) } } _ => Err(ShellError::maybe_labeled_error( diff --git a/src/commands/split_row.rs b/src/commands/split_row.rs index fb530ac8a4..1c31f951a5 100644 --- a/src/commands/split_row.rs +++ b/src/commands/split_row.rs @@ -1,11 +1,10 @@ use crate::errors::ShellError; use crate::object::{Primitive, Value}; -use crate::parser::Spanned; use crate::prelude::*; use log::trace; pub fn split_row(args: CommandArgs) -> Result { - let positional: Vec> = args.positional_iter().cloned().collect(); + let positional: Vec> = args.positional_iter().cloned().collect(); let span = args.call_info.name_span; if positional.len() == 0 { @@ -21,7 +20,7 @@ pub fn split_row(args: CommandArgs) -> Result { let stream = input .values .map(move |v| match v.item { - Value::Primitive(Primitive::String(s)) => { + Value::Primitive(Primitive::String(ref s)) => { let splitter = positional[0].as_string().unwrap().replace("\\n", "\n"); trace!("splitting with {:?}", splitter); let split_result: Vec<_> = s.split(&splitter).filter(|s| s.trim() != "").collect(); @@ -31,7 +30,7 @@ pub fn split_row(args: CommandArgs) -> Result { let mut result = VecDeque::new(); for s in split_result { result.push_back(ReturnSuccess::value( - Value::Primitive(Primitive::String(s.into())).spanned(v.span), + Value::Primitive(Primitive::String(s.into())).tagged(v.span()), )); } result diff --git a/src/commands/to_array.rs b/src/commands/to_array.rs index 7cea62dbe8..f6efef0d3d 100644 --- a/src/commands/to_array.rs +++ b/src/commands/to_array.rs @@ -5,7 +5,7 @@ pub fn to_array(args: CommandArgs) -> Result { let out = args.input.values.collect(); Ok(out - .map(|vec: Vec<_>| stream![Value::List(vec).spanned_unknown()]) // TODO: args.input should have a span + .map(|vec: Vec<_>| stream![Value::List(vec).tagged_unknown()]) // TODO: args.input should have a span .flatten_stream() .from_input_stream()) } diff --git a/src/commands/to_csv.rs b/src/commands/to_csv.rs index df1574b955..334696aaed 100644 --- a/src/commands/to_csv.rs +++ b/src/commands/to_csv.rs @@ -9,7 +9,7 @@ pub fn value_to_csv_value(v: &Value) -> Value { Value::Object(o) => Value::Object(o.clone()), Value::List(l) => Value::List(l.clone()), Value::Block(_) => Value::Primitive(Primitive::Nothing), - _ => Value::Primitive(Primitive::Nothing) + _ => Value::Primitive(Primitive::Nothing), } } @@ -25,14 +25,14 @@ pub fn to_string(v: &Value) -> Result> { fields.push_back(k.clone()); values.push_back(to_string(&v)?); } - + wtr.write_record(fields).expect("can not write."); wtr.write_record(values).expect("can not write."); - return Ok(String::from_utf8(wtr.into_inner()?)?) - }, + return Ok(String::from_utf8(wtr.into_inner()?)?); + } Value::Primitive(Primitive::String(s)) => return Ok(s.to_string()), - _ => return Err("Bad input".into()) + _ => return Err("Bad input".into()), } } @@ -41,18 +41,13 @@ pub fn to_csv(args: CommandArgs) -> Result { let name_span = args.call_info.name_span; Ok(out .values - .map( - move |a| match to_string(&value_to_csv_value(&a.item)) { - - Ok(x) => { - ReturnSuccess::value(Value::Primitive(Primitive::String(x)).spanned(name_span)) - } - Err(_) => Err(ShellError::maybe_labeled_error( - "Can not convert to CSV string", - "can not convert piped data to CSV string", - name_span, - )), - }, - ) + .map(move |a| match to_string(&value_to_csv_value(&a.item)) { + Ok(x) => ReturnSuccess::value(Value::Primitive(Primitive::String(x)).tagged(name_span)), + Err(_) => Err(ShellError::maybe_labeled_error( + "Can not convert to CSV string", + "can not convert piped data to CSV string", + name_span, + )), + }) .to_output_stream()) } diff --git a/src/commands/to_json.rs b/src/commands/to_json.rs index 8c7d25339a..eea4936d72 100644 --- a/src/commands/to_json.rs +++ b/src/commands/to_json.rs @@ -49,7 +49,7 @@ pub fn to_json(args: CommandArgs) -> Result { .map( move |a| match serde_json::to_string(&value_to_json_value(&a)) { Ok(x) => { - ReturnSuccess::value(Value::Primitive(Primitive::String(x)).spanned(name_span)) + ReturnSuccess::value(Value::Primitive(Primitive::String(x)).tagged(name_span)) } Err(_) => Err(ShellError::maybe_labeled_error( "Can not convert to JSON string", diff --git a/src/commands/to_toml.rs b/src/commands/to_toml.rs index e641410ec2..e8eb023ca1 100644 --- a/src/commands/to_toml.rs +++ b/src/commands/to_toml.rs @@ -42,13 +42,13 @@ pub fn to_toml(args: CommandArgs) -> Result { .map(move |a| match toml::to_string(&value_to_toml_value(&a)) { Ok(val) => { return ReturnSuccess::value( - Value::Primitive(Primitive::String(val)).spanned(name_span), + Value::Primitive(Primitive::String(val)).tagged(name_span), ) } Err(err) => Err(ShellError::type_error( "Can not convert to a TOML string", - format!("{:?} - {:?}", a.type_name(), err).spanned(name_span), + format!("{:?} - {:?}", a.type_name(), err).tagged(name_span), )), }) .to_output_stream()) diff --git a/src/commands/to_yaml.rs b/src/commands/to_yaml.rs index db529c0351..db025b10ef 100644 --- a/src/commands/to_yaml.rs +++ b/src/commands/to_yaml.rs @@ -47,7 +47,7 @@ pub fn to_yaml(args: CommandArgs) -> Result { .map( move |a| match serde_yaml::to_string(&value_to_yaml_value(&a)) { Ok(x) => { - ReturnSuccess::value(Value::Primitive(Primitive::String(x)).spanned(name_span)) + ReturnSuccess::value(Value::Primitive(Primitive::String(x)).tagged(name_span)) } Err(_) => Err(ShellError::maybe_labeled_error( "Can not convert to YAML string", diff --git a/src/commands/trim.rs b/src/commands/trim.rs index 838875faa0..7e16b0e4b3 100644 --- a/src/commands/trim.rs +++ b/src/commands/trim.rs @@ -9,7 +9,7 @@ pub fn trim(args: CommandArgs) -> Result { .values .map(move |v| { let string = String::extract(&v)?; - ReturnSuccess::value(Value::string(string.trim()).spanned(v.span)) + ReturnSuccess::value(Value::string(string.trim()).tagged(v.span())) }) .to_output_stream()) } diff --git a/src/context.rs b/src/context.rs index 9be7463a0f..5d77ece2b4 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1,8 +1,5 @@ use crate::commands::command::{CallInfo, Sink, SinkCommandArgs}; -use crate::parser::{ - registry::{Args, CommandConfig, CommandRegistry}, - Span, -}; +use crate::parser::registry::{Args, CommandConfig, CommandRegistry}; use crate::prelude::*; use serde::{Deserialize, Serialize}; use uuid::Uuid; @@ -84,7 +81,7 @@ impl Context { command: Arc, name_span: Option, args: Args, - input: Vec>, + input: Vec>, ) -> Result<(), ShellError> { let command_args = SinkCommandArgs { ctx: self.clone(), diff --git a/src/errors.rs b/src/errors.rs index 8a5a1445c5..daaa2c1f83 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,7 +1,6 @@ #[allow(unused)] use crate::prelude::*; -use crate::parser::{Span, Spanned}; use ansi_term::Color; use derive_new::new; use language_reporting::{Diagnostic, Label, Severity}; @@ -9,23 +8,21 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, Eq, PartialEq, Clone, Ord, PartialOrd, Serialize, Deserialize)] pub enum Description { - Source(Spanned), + Source(Tagged), Synthetic(String), } impl Description { - pub fn from(item: Spanned>) -> Description { - match item { - Spanned { - span: - Span { - start: 0, - end: 0, - source: None, - }, - item, - } => Description::Synthetic(item.into()), - Spanned { span, item } => Description::Source(Spanned::from_item(item.into(), span)), + pub fn from(value: Tagged>) -> Description { + let value_span = value.span(); + + match value_span { + Span { + start: 0, + end: 0, + source: None, + } => Description::Synthetic(value.item.into()), + _ => Description::Source(Tagged::from_item(value.item.into(), value_span)), } } } @@ -33,7 +30,7 @@ impl Description { impl Description { fn into_label(self) -> Result, String> { match self { - Description::Source(s) => Ok(Label::new_primary(s.span).with_message(s.item)), + Description::Source(s) => Ok(Label::new_primary(s.span()).with_message(s.item)), Description::Synthetic(s) => Err(s), } } @@ -65,7 +62,7 @@ pub struct ShellError { impl ShellError { crate fn type_error( expected: impl Into, - actual: Spanned>, + actual: Tagged>, ) -> ShellError { ProximateShellError::TypeError { expected: expected.into(), @@ -75,8 +72,8 @@ impl ShellError { } crate fn coerce_error( - left: Spanned>, - right: Spanned>, + left: Tagged>, + right: Tagged>, ) -> ShellError { ProximateShellError::CoerceError { left: left.map(|l| l.into()), @@ -166,9 +163,9 @@ impl ShellError { ProximateShellError::TypeError { expected, actual: - Spanned { + Tagged { item: Some(actual), - span, + tag: Tag { span }, }, } => Diagnostic::new(Severity::Error, "Type Error").with_label( Label::new_primary(span) @@ -177,7 +174,11 @@ impl ShellError { ProximateShellError::TypeError { expected, - actual: Spanned { item: None, span }, + actual: + Tagged { + item: None, + tag: Tag { span }, + }, } => Diagnostic::new(Severity::Error, "Type Error") .with_label(Label::new_primary(span).with_message(expected)), @@ -202,8 +203,8 @@ impl ShellError { ProximateShellError::Diagnostic(diag) => diag.diagnostic, ProximateShellError::CoerceError { left, right } => { Diagnostic::new(Severity::Error, "Coercion error") - .with_label(Label::new_primary(left.span).with_message(left.item)) - .with_label(Label::new_secondary(right.span).with_message(right.item)) + .with_label(Label::new_primary(left.span()).with_message(left.item)) + .with_label(Label::new_secondary(right.span()).with_message(right.item)) } } } @@ -251,7 +252,7 @@ pub enum ProximateShellError { String(StringError), TypeError { expected: String, - actual: Spanned>, + actual: Tagged>, }, MissingProperty { subpath: Description, @@ -264,8 +265,8 @@ pub enum ProximateShellError { }, Diagnostic(ShellDiagnostic), CoerceError { - left: Spanned, - right: Spanned, + left: Tagged, + right: Tagged, }, } impl ProximateShellError { diff --git a/src/evaluate/evaluator.rs b/src/evaluate/evaluator.rs index a8a3915e97..af7ec963ba 100644 --- a/src/evaluate/evaluator.rs +++ b/src/evaluate/evaluator.rs @@ -2,7 +2,7 @@ use crate::errors::Description; use crate::object::base::Block; use crate::parser::{ hir::{self, Expression, RawExpression}, - CommandRegistry, Spanned, Text, + CommandRegistry, Text, }; use crate::prelude::*; use derive_new::new; @@ -10,15 +10,15 @@ use indexmap::IndexMap; #[derive(new)] crate struct Scope { - it: Spanned, + it: Tagged, #[new(default)] - vars: IndexMap>, + vars: IndexMap>, } impl Scope { crate fn empty() -> Scope { Scope { - it: Value::nothing().spanned_unknown(), + it: Value::nothing().tagged_unknown(), vars: IndexMap::new(), } } @@ -29,7 +29,7 @@ crate fn evaluate_baseline_expr( registry: &dyn CommandRegistry, scope: &Scope, source: &Text, -) -> Result, ShellError> { +) -> Result, ShellError> { match &expr.item { RawExpression::Literal(literal) => Ok(evaluate_literal(expr.copy_span(*literal), source)), RawExpression::Variable(var) => evaluate_reference(var, scope, source), @@ -38,15 +38,15 @@ crate fn evaluate_baseline_expr( let right = evaluate_baseline_expr(binary.right(), registry, scope, source)?; match left.compare(binary.op(), &*right) { - Ok(result) => Ok(Spanned::from_item(Value::boolean(result), *expr.span())), + Ok(result) => Ok(Tagged::from_item(Value::boolean(result), expr.span())), Err((left_type, right_type)) => Err(ShellError::coerce_error( binary.left().copy_span(left_type), binary.right().copy_span(right_type), )), } } - RawExpression::Block(block) => Ok(Spanned::from_item( - Value::Block(Block::new(block.clone(), source.clone(), *expr.span())), + RawExpression::Block(block) => Ok(Tagged::from_item( + Value::Block(Block::new(block.clone(), source.clone(), expr.span())), expr.span(), )), RawExpression::Path(path) => { @@ -59,12 +59,12 @@ crate fn evaluate_baseline_expr( match next { None => { return Err(ShellError::missing_property( - Description::from(item.spanned_type_name()), + Description::from(item.tagged_type_name()), Description::from(name.clone()), )) } Some(next) => { - item = Spanned::from_item( + item = Tagged::from_item( next.clone().item, (expr.span().start, name.span().end), ) @@ -72,13 +72,13 @@ crate fn evaluate_baseline_expr( }; } - Ok(Spanned::from_item(item.item().clone(), expr.span())) + Ok(Tagged::from_item(item.item().clone(), expr.span())) } RawExpression::Boolean(_boolean) => unimplemented!(), } } -fn evaluate_literal(literal: Spanned, source: &Text) -> Spanned { +fn evaluate_literal(literal: Tagged, source: &Text) -> Tagged { let result = match literal.item { hir::Literal::Integer(int) => Value::int(int), hir::Literal::Size(int, unit) => unit.compute(int), @@ -93,13 +93,13 @@ fn evaluate_reference( name: &hir::Variable, scope: &Scope, source: &Text, -) -> Result, ShellError> { +) -> Result, ShellError> { match name { - hir::Variable::It(span) => Ok(Spanned::from_item(scope.it.item.clone(), span)), + hir::Variable::It(span) => Ok(Tagged::from_item(scope.it.item.clone(), span)), hir::Variable::Other(span) => Ok(scope .vars .get(span.slice(source)) .map(|v| v.clone()) - .unwrap_or_else(|| Value::nothing().spanned(span))), + .unwrap_or_else(|| Value::nothing().tagged(span))), } } diff --git a/src/format/table.rs b/src/format/table.rs index 4c71879f84..dcc4f5faa7 100644 --- a/src/format/table.rs +++ b/src/format/table.rs @@ -14,7 +14,7 @@ pub struct TableView { } impl TableView { - fn merge_descriptors(values: &[Spanned]) -> Vec { + fn merge_descriptors(values: &[Tagged]) -> Vec { let mut ret = vec![]; for value in values { for desc in value.data_descriptors() { @@ -26,7 +26,7 @@ impl TableView { ret } - pub fn from_list(values: &[Spanned]) -> Option { + pub fn from_list(values: &[Tagged]) -> Option { if values.len() == 0 { return None; } diff --git a/src/format/vtable.rs b/src/format/vtable.rs index ac27f3bc83..30539321a9 100644 --- a/src/format/vtable.rs +++ b/src/format/vtable.rs @@ -12,7 +12,7 @@ pub struct VTableView { } impl VTableView { - pub fn from_list(values: &[Spanned]) -> Option { + pub fn from_list(values: &[Tagged]) -> Option { if values.len() == 0 { return None; } diff --git a/src/lib.rs b/src/lib.rs index e20046165c..ba0db8a6e0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,13 +28,11 @@ pub use crate::commands::command::{CallInfo, ReturnSuccess, ReturnValue}; pub use crate::context::{SourceMap, SpanSource}; pub use crate::env::host::BasicHost; pub use crate::object::base::OF64; -pub use crate::parser::parse::span::Span; -pub use crate::parser::parse::span::SpannedItem; -pub use crate::parser::Spanned; pub use crate::plugin::{serve_plugin, Plugin}; pub use cli::cli; pub use errors::ShellError; pub use object::base::{Primitive, Value}; -pub use object::dict::{Dictionary, SpannedDictBuilder}; +pub use object::dict::{Dictionary, TaggedDictBuilder}; +pub use object::meta::{Span, Tag, Tagged, TaggedItem}; pub use parser::parse::text::Text; pub use parser::registry::{Args, CommandConfig, NamedType, PositionalType}; diff --git a/src/object.rs b/src/object.rs index f094c63a17..d0424f19d8 100644 --- a/src/object.rs +++ b/src/object.rs @@ -3,9 +3,10 @@ crate mod config; crate mod dict; crate mod files; crate mod into; +crate mod meta; crate mod process; crate mod types; crate use base::{Block, Primitive, Switch, Value}; -crate use dict::{Dictionary, SpannedDictBuilder}; +crate use dict::{Dictionary, TaggedDictBuilder}; crate use files::dir_entry_dict; diff --git a/src/object/base.rs b/src/object/base.rs index abe86b2add..f9c5a2236b 100644 --- a/src/object/base.rs +++ b/src/object/base.rs @@ -1,7 +1,7 @@ use crate::errors::ShellError; use crate::evaluate::{evaluate_baseline_expr, Scope}; -use crate::object::SpannedDictBuilder; -use crate::parser::{hir, Operator, Span, Spanned}; +use crate::object::TaggedDictBuilder; +use crate::parser::{hir, Operator}; use crate::prelude::*; use crate::Text; use ansi_term::Color; @@ -164,11 +164,11 @@ impl Deserialize<'de> for Block { } impl Block { - pub fn invoke(&self, value: &Spanned) -> Result, ShellError> { + pub fn invoke(&self, value: &Tagged) -> Result, ShellError> { let scope = Scope::new(value.clone()); if self.expressions.len() == 0 { - return Ok(Spanned::from_item(Value::nothing(), self.span)); + return Ok(Tagged::from_item(Value::nothing(), self.span)); } let mut last = None; @@ -187,17 +187,17 @@ pub enum Value { Object(crate::object::Dictionary), #[serde(with = "serde_bytes")] Binary(Vec), - List(Vec>), + List(Vec>), #[allow(unused)] Block(Block), } -pub fn debug_list(values: &'a Vec>) -> ValuesDebug<'a> { +pub fn debug_list(values: &'a Vec>) -> ValuesDebug<'a> { ValuesDebug { values } } pub struct ValuesDebug<'a> { - values: &'a Vec>, + values: &'a Vec>, } impl fmt::Debug for ValuesDebug<'a> { @@ -209,7 +209,7 @@ impl fmt::Debug for ValuesDebug<'a> { } pub struct ValueDebug<'a> { - value: &'a Spanned, + value: &'a Tagged, } impl fmt::Debug for ValueDebug<'a> { @@ -224,17 +224,17 @@ impl fmt::Debug for ValueDebug<'a> { } } -impl Spanned { - crate fn spanned_type_name(&self) -> Spanned { +impl Tagged { + crate fn tagged_type_name(&self) -> Tagged { let name = self.type_name(); - Spanned::from_item(name, self.span) + Tagged::from_item(name, self.span()) } } -impl std::convert::TryFrom<&'a Spanned> for Block { +impl std::convert::TryFrom<&'a Tagged> for Block { type Error = ShellError; - fn try_from(value: &'a Spanned) -> Result { + fn try_from(value: &'a Tagged) -> Result { match value.item() { Value::Block(block) => Ok(block.clone()), v => Err(ShellError::type_error( @@ -245,10 +245,10 @@ impl std::convert::TryFrom<&'a Spanned> for Block { } } -impl std::convert::TryFrom<&'a Spanned> for i64 { +impl std::convert::TryFrom<&'a Tagged> for i64 { type Error = ShellError; - fn try_from(value: &'a Spanned) -> Result { + fn try_from(value: &'a Tagged) -> Result { match value.item() { Value::Primitive(Primitive::Int(int)) => Ok(*int), v => Err(ShellError::type_error( @@ -273,10 +273,10 @@ impl Switch { } } -impl std::convert::TryFrom>> for Switch { +impl std::convert::TryFrom>> for Switch { type Error = ShellError; - fn try_from(value: Option<&'a Spanned>) -> Result { + fn try_from(value: Option<&'a Tagged>) -> Result { match value { None => Ok(Switch::Absent), Some(value) => match value.item() { @@ -290,7 +290,7 @@ impl std::convert::TryFrom>> for Switch { } } -impl Spanned { +impl Tagged { crate fn debug(&'a self) -> ValueDebug<'a> { ValueDebug { value: self } } @@ -322,13 +322,13 @@ impl Value { } } - crate fn get_data_by_key(&'a self, name: &str) -> Option<&Spanned> { + crate fn get_data_by_key(&'a self, name: &str) -> Option<&Tagged> { match self { Value::Object(o) => o.get_data_by_key(name), Value::List(l) => { for item in l { match item { - Spanned { + Tagged { item: Value::Object(o), .. } => match o.get_data_by_key(name) { @@ -345,14 +345,14 @@ impl Value { } #[allow(unused)] - crate fn get_data_by_index(&'a self, idx: usize) -> Option<&Spanned> { + crate fn get_data_by_index(&'a self, idx: usize) -> Option<&Tagged> { match self { Value::List(l) => l.iter().nth(idx), _ => None, } } - pub fn get_data_by_path(&'a self, span: Span, path: &str) -> Option> { + pub fn get_data_by_path(&'a self, span: Span, path: &str) -> Option> { let mut current = self; for p in path.split(".") { match current.get_data_by_key(p) { @@ -361,10 +361,7 @@ impl Value { } } - Some(Spanned { - item: current, - span, - }) + Some(Tagged::from_item(current, span)) } pub fn insert_data_at_path( @@ -372,7 +369,7 @@ impl Value { span: Span, path: &str, new_value: Value, - ) -> Option> { + ) -> Option> { let mut new_obj = self.clone(); let split_path: Vec<_> = path.split(".").collect(); @@ -387,19 +384,13 @@ impl Value { Value::Object(o) => { o.entries.insert( split_path[idx + 1].to_string(), - Spanned { - item: new_value, - span, - }, + Tagged::from_item(new_value, span), ); } _ => {} } - return Some(Spanned { - item: new_obj, - span, - }); + return Some(Tagged::from_item(new_obj, span)); } else { match next.item { Value::Object(ref mut o) => { @@ -422,7 +413,7 @@ impl Value { span: Span, path: &str, replaced_value: Value, - ) -> Option> { + ) -> Option> { let mut new_obj = self.clone(); let split_path: Vec<_> = path.split(".").collect(); @@ -433,14 +424,8 @@ impl Value { match current.entries.get_mut(split_path[idx]) { Some(next) => { if idx == (split_path.len() - 1) { - *next = Spanned { - item: replaced_value, - span, - }; - return Some(Spanned { - item: new_obj, - span, - }); + *next = Tagged::from_item(replaced_value, span); + return Some(Tagged::from_item(new_obj, span)); } else { match next.item { Value::Object(ref mut o) => { @@ -522,7 +507,7 @@ impl Value { } } - crate fn as_pair(&self) -> Result<(Spanned, Spanned), ShellError> { + crate fn as_pair(&self) -> Result<(Tagged, Tagged), ShellError> { match self { Value::List(list) if list.len() == 2 => Ok((list[0].clone(), list[1].clone())), other => Err(ShellError::string(format!( @@ -616,8 +601,8 @@ impl Value { } } -crate fn select_fields(obj: &Value, fields: &[String], span: impl Into) -> Spanned { - let mut out = SpannedDictBuilder::new(span); +crate fn select_fields(obj: &Value, fields: &[String], span: impl Into) -> Tagged { + let mut out = TaggedDictBuilder::new(span); let descs = obj.data_descriptors(); @@ -628,11 +613,11 @@ crate fn select_fields(obj: &Value, fields: &[String], span: impl Into) -> } } - out.into_spanned_value() + out.into_tagged_value() } -crate fn reject_fields(obj: &Value, fields: &[String], span: impl Into) -> Spanned { - let mut out = SpannedDictBuilder::new(span); +crate fn reject_fields(obj: &Value, fields: &[String], span: impl Into) -> Tagged { + let mut out = TaggedDictBuilder::new(span); let descs = obj.data_descriptors(); @@ -643,7 +628,7 @@ crate fn reject_fields(obj: &Value, fields: &[String], span: impl Into) -> } } - out.into_spanned_value() + out.into_tagged_value() } #[allow(unused)] diff --git a/src/object/config.rs b/src/object/config.rs index 0860feb94b..15046ee313 100644 --- a/src/object/config.rs +++ b/src/object/config.rs @@ -16,10 +16,10 @@ const APP_INFO: AppInfo = AppInfo { #[derive(Deserialize, Serialize)] struct Config { #[serde(flatten)] - extra: IndexMap>, + extra: IndexMap>, } -crate fn write_config(config: &IndexMap>) -> Result<(), ShellError> { +crate fn write_config(config: &IndexMap>) -> Result<(), ShellError> { let location = app_root(AppDataType::UserConfig, &APP_INFO) .map_err(|err| ShellError::string(&format!("Couldn't open config file:\n{}", err)))?; @@ -35,7 +35,7 @@ crate fn write_config(config: &IndexMap>) -> Result<(), S Ok(()) } -crate fn config(span: impl Into) -> Result>, ShellError> { +crate fn config(span: impl Into) -> Result>, ShellError> { let span = span.into(); let location = app_root(AppDataType::UserConfig, &APP_INFO) @@ -47,7 +47,7 @@ crate fn config(span: impl Into) -> Result trace!("config file = {}", filename.display()); let contents = fs::read_to_string(filename) - .map(|v| v.spanned(span)) + .map(|v| v.tagged(span)) .map_err(|err| ShellError::string(&format!("Couldn't read config file:\n{}", err)))?; let parsed: Config = toml::from_str(&contents) diff --git a/src/object/dict.rs b/src/object/dict.rs index 16c7d32818..b7463d2417 100644 --- a/src/object/dict.rs +++ b/src/object/dict.rs @@ -9,7 +9,7 @@ use std::fmt; #[derive(Debug, Default, Eq, PartialEq, Serialize, Deserialize, Clone, new)] pub struct Dictionary { - pub entries: IndexMap>, + pub entries: IndexMap>, } impl PartialOrd for Dictionary { @@ -28,8 +28,8 @@ impl PartialOrd for Dictionary { } } -impl From>> for Dictionary { - fn from(input: IndexMap>) -> Dictionary { +impl From>> for Dictionary { + fn from(input: IndexMap>) -> Dictionary { let mut out = IndexMap::default(); for (key, value) in input { @@ -79,7 +79,7 @@ impl Dictionary { } } - crate fn get_data_by_key(&self, name: &str) -> Option<&Spanned> { + crate fn get_data_by_key(&self, name: &str) -> Option<&Tagged> { match self .entries .iter() @@ -101,72 +101,71 @@ impl Dictionary { } } -pub struct SpannedListBuilder { +pub struct TaggedListBuilder { span: Span, - list: Vec>, + list: Vec>, } -impl SpannedListBuilder { - pub fn new(span: impl Into) -> SpannedListBuilder { - SpannedListBuilder { +impl TaggedListBuilder { + pub fn new(span: impl Into) -> TaggedListBuilder { + TaggedListBuilder { span: span.into(), list: vec![], } } pub fn push(&mut self, value: impl Into) { - self.list.push(value.into().spanned(self.span)); + self.list.push(value.into().tagged(self.span)); } - pub fn insert_spanned(&mut self, value: impl Into>) { + pub fn insert_tagged(&mut self, value: impl Into>) { self.list.push(value.into()); } - pub fn into_spanned_value(self) -> Spanned { - Value::List(self.list).spanned(self.span) + pub fn into_tagged_value(self) -> Tagged { + Value::List(self.list).tagged(self.span) } } -impl From for Spanned { - fn from(input: SpannedListBuilder) -> Spanned { - input.into_spanned_value() +impl From for Tagged { + fn from(input: TaggedListBuilder) -> Tagged { + input.into_tagged_value() } } #[derive(Debug)] -pub struct SpannedDictBuilder { +pub struct TaggedDictBuilder { span: Span, - dict: IndexMap>, + dict: IndexMap>, } -impl SpannedDictBuilder { - pub fn new(span: impl Into) -> SpannedDictBuilder { - SpannedDictBuilder { +impl TaggedDictBuilder { + pub fn new(span: impl Into) -> TaggedDictBuilder { + TaggedDictBuilder { span: span.into(), dict: IndexMap::default(), } } pub fn insert(&mut self, key: impl Into, value: impl Into) { - self.dict - .insert(key.into(), value.into().spanned(self.span)); + self.dict.insert(key.into(), value.into().tagged(self.span)); } - pub fn insert_spanned(&mut self, key: impl Into, value: impl Into>) { + pub fn insert_tagged(&mut self, key: impl Into, value: impl Into>) { self.dict.insert(key.into(), value.into()); } - pub fn into_spanned_value(self) -> Spanned { - self.into_spanned_dict().map(Value::Object) + pub fn into_tagged_value(self) -> Tagged { + self.into_tagged_dict().map(Value::Object) } - pub fn into_spanned_dict(self) -> Spanned { - Dictionary { entries: self.dict }.spanned(self.span) + pub fn into_tagged_dict(self) -> Tagged { + Dictionary { entries: self.dict }.tagged(self.span) } } -impl From for Spanned { - fn from(input: SpannedDictBuilder) -> Spanned { - input.into_spanned_value() +impl From for Tagged { + fn from(input: TaggedDictBuilder) -> Tagged { + input.into_tagged_value() } } diff --git a/src/object/files.rs b/src/object/files.rs index 7cea20a405..94fe1dde3b 100644 --- a/src/object/files.rs +++ b/src/object/files.rs @@ -1,5 +1,5 @@ use crate::errors::ShellError; -use crate::object::{SpannedDictBuilder, Value}; +use crate::object::{TaggedDictBuilder, Value}; use crate::prelude::*; #[derive(Debug)] @@ -12,8 +12,8 @@ pub enum FileType { crate fn dir_entry_dict( entry: &std::fs::DirEntry, span: impl Into, -) -> Result, ShellError> { - let mut dict = SpannedDictBuilder::new(span); +) -> Result, ShellError> { + let mut dict = TaggedDictBuilder::new(span); let filename = entry.file_name(); dict.insert("name", Value::string(filename.to_string_lossy())); @@ -50,5 +50,5 @@ crate fn dir_entry_dict( Err(_) => {} } - Ok(dict.into_spanned_value()) + Ok(dict.into_tagged_value()) } diff --git a/src/object/into.rs b/src/object/into.rs index dceb5192d1..8e996e7551 100644 --- a/src/object/into.rs +++ b/src/object/into.rs @@ -13,11 +13,10 @@ impl From for Value { } } -impl> Spanned { - pub fn into_spanned_value(self) -> Spanned { - let Spanned { item, span } = self; - - let value = item.into(); - value.spanned(span) +impl> Tagged { + pub fn into_tagged_value(self) -> Tagged { + let value_span = self.span(); + let value = self.item.into(); + value.tagged(value_span) } } diff --git a/src/object/process.rs b/src/object/process.rs index 9999724d41..c5529a46e1 100644 --- a/src/object/process.rs +++ b/src/object/process.rs @@ -1,10 +1,10 @@ -use crate::object::{SpannedDictBuilder, Value}; +use crate::object::{TaggedDictBuilder, Value}; use crate::prelude::*; use itertools::join; use sysinfo::ProcessExt; -crate fn process_dict(proc: &sysinfo::Process, span: impl Into) -> Spanned { - let mut dict = SpannedDictBuilder::new(span); +crate fn process_dict(proc: &sysinfo::Process, span: impl Into) -> Tagged { + let mut dict = TaggedDictBuilder::new(span); let cmd = proc.cmd(); @@ -25,5 +25,5 @@ crate fn process_dict(proc: &sysinfo::Process, span: impl Into) -> Spanned _ => dict.insert("name", cmd_value), } - dict.into_spanned_value() + dict.into_tagged_value() } diff --git a/src/object/types.rs b/src/object/types.rs index 0264a33769..9e0cb65740 100644 --- a/src/object/types.rs +++ b/src/object/types.rs @@ -12,19 +12,19 @@ pub trait Type: std::fmt::Debug + Send { } pub trait ExtractType: Sized { - fn extract(value: &Spanned) -> Result; - fn check(value: &'value Spanned) -> Result<&'value Spanned, ShellError>; + fn extract(value: &Tagged) -> Result; + fn check(value: &'value Tagged) -> Result<&'value Tagged, ShellError>; fn syntax_type() -> hir::SyntaxType { hir::SyntaxType::Any } } -impl ExtractType for Spanned { - fn extract(value: &Spanned) -> Result, ShellError> { - Ok(T::extract(value)?.spanned(value.span)) +impl ExtractType for Tagged { + fn extract(value: &Tagged) -> Result, ShellError> { + Ok(T::extract(value)?.tagged(value.span())) } - fn check(value: &'value Spanned) -> Result<&'value Spanned, ShellError> { + fn check(value: &'value Tagged) -> Result<&'value Tagged, ShellError> { T::check(value) } @@ -37,19 +37,19 @@ impl ExtractType for Spanned { pub struct Any; impl Type for Any { - type Extractor = Spanned; + type Extractor = Tagged; fn name(&self) -> &'static str { "Any" } } -impl ExtractType for Spanned { - fn extract(value: &Spanned) -> Result { +impl ExtractType for Tagged { + fn extract(value: &Tagged) -> Result { Ok(value.clone()) } - fn check(value: &'value Spanned) -> Result<&'value Spanned, ShellError> { + fn check(value: &'value Tagged) -> Result<&'value Tagged, ShellError> { Ok(value) } } @@ -59,23 +59,23 @@ impl ExtractType for std::path::PathBuf { hir::SyntaxType::Path } - fn extract(value: &'a Spanned) -> Result { + fn extract(value: &'a Tagged) -> Result { match &value { - Spanned { + Tagged { item: Value::Primitive(Primitive::String(p)), .. } => Ok(PathBuf::from(p)), - other => Err(ShellError::type_error("Path", other.spanned_type_name())), + other => Err(ShellError::type_error("Path", other.tagged_type_name())), } } - fn check(value: &'value Spanned) -> Result<&'value Spanned, ShellError> { + fn check(value: &'value Tagged) -> Result<&'value Tagged, ShellError> { match &value { - v @ Spanned { + v @ Tagged { item: Value::Primitive(Primitive::Path(_)), .. } => Ok(v), - other => Err(ShellError::type_error("Path", other.spanned_type_name())), + other => Err(ShellError::type_error("Path", other.tagged_type_name())), } } } @@ -92,23 +92,23 @@ impl Type for Integer { } impl ExtractType for i64 { - fn extract(value: &Spanned) -> Result { + fn extract(value: &Tagged) -> Result { match value { - &Spanned { + &Tagged { item: Value::Primitive(Primitive::Int(int)), .. } => Ok(int), - other => Err(ShellError::type_error("Integer", other.spanned_type_name())), + other => Err(ShellError::type_error("Integer", other.tagged_type_name())), } } - fn check(value: &'value Spanned) -> Result<&'value Spanned, ShellError> { + fn check(value: &'value Tagged) -> Result<&'value Tagged, ShellError> { match value { - v @ Spanned { + v @ Tagged { item: Value::Primitive(Primitive::Int(_)), .. } => Ok(v), - other => Err(ShellError::type_error("Integer", other.spanned_type_name())), + other => Err(ShellError::type_error("Integer", other.tagged_type_name())), } } } @@ -125,23 +125,23 @@ impl Type for NuString { } impl ExtractType for String { - fn extract(value: &Spanned) -> Result { + fn extract(value: &Tagged) -> Result { match value { - Spanned { + Tagged { item: Value::Primitive(Primitive::String(string)), .. } => Ok(string.clone()), - other => Err(ShellError::type_error("String", other.spanned_type_name())), + other => Err(ShellError::type_error("String", other.tagged_type_name())), } } - fn check(value: &'value Spanned) -> Result<&'value Spanned, ShellError> { + fn check(value: &'value Tagged) -> Result<&'value Tagged, ShellError> { match value { - v @ Spanned { + v @ Tagged { item: Value::Primitive(Primitive::String(_)), .. } => Ok(v), - other => Err(ShellError::type_error("String", other.spanned_type_name())), + other => Err(ShellError::type_error("String", other.tagged_type_name())), } } } @@ -158,23 +158,23 @@ impl Type for Block { } impl ExtractType for value::Block { - fn check(value: &'value Spanned) -> Result<&'value Spanned, ShellError> { + fn check(value: &'value Tagged) -> Result<&'value Tagged, ShellError> { match value { - v @ Spanned { + v @ Tagged { item: Value::Block(_), .. } => Ok(v), - other => Err(ShellError::type_error("Block", other.spanned_type_name())), + other => Err(ShellError::type_error("Block", other.tagged_type_name())), } } - fn extract(value: &Spanned) -> Result { + fn extract(value: &Tagged) -> Result { match value { - Spanned { + Tagged { item: Value::Block(block), .. } => Ok(block.clone()), - other => Err(ShellError::type_error("Block", other.spanned_type_name())), + other => Err(ShellError::type_error("Block", other.tagged_type_name())), } } } diff --git a/src/parser.rs b/src/parser.rs index e9a6ce1ac4..5ef1ef2b37 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -12,7 +12,6 @@ crate use parse::flag::Flag; crate use parse::operator::Operator; crate use parse::parser::{nom_input, pipeline}; crate use parse::pipeline::{Pipeline, PipelineElement}; -pub use parse::span::{Span, Spanned, SpannedItem}; crate use parse::text::Text; crate use parse::token_tree::{DelimitedNode, Delimiter, PathNode, TokenNode}; crate use parse::tokens::{RawToken, Token}; diff --git a/src/parser/hir.rs b/src/parser/hir.rs index 7b0db75084..41b4fbdda2 100644 --- a/src/parser/hir.rs +++ b/src/parser/hir.rs @@ -4,7 +4,9 @@ crate mod binary; crate mod named; crate mod path; -use crate::parser::{Span, Spanned, Unit}; +use crate::parser::Unit; +use crate::Span; +use crate::Tagged; use derive_new::new; use getset::Getters; @@ -14,7 +16,7 @@ crate use binary::Binary; crate use named::NamedArguments; crate use path::Path; -pub fn path(head: impl Into, tail: Vec>>) -> Path { +pub fn path(head: impl Into, tail: Vec>>) -> Path { Path::new( head.into(), tail.into_iter() @@ -58,48 +60,48 @@ impl RawExpression { } } -pub type Expression = Spanned; +pub type Expression = Tagged; impl Expression { fn int(i: impl Into, span: impl Into) -> Expression { - Spanned::from_item(RawExpression::Literal(Literal::Integer(i.into())), span) + Tagged::from_item(RawExpression::Literal(Literal::Integer(i.into())), span) } fn size(i: impl Into, unit: impl Into, span: impl Into) -> Expression { - Spanned::from_item( + Tagged::from_item( RawExpression::Literal(Literal::Size(i.into(), unit.into())), span, ) } fn string(inner: impl Into, outer: impl Into) -> Expression { - Spanned::from_item( + Tagged::from_item( RawExpression::Literal(Literal::String(inner.into())), outer.into(), ) } fn bare(span: impl Into) -> Expression { - Spanned::from_item(RawExpression::Literal(Literal::Bare), span.into()) + Tagged::from_item(RawExpression::Literal(Literal::Bare), span.into()) } fn variable(inner: impl Into, outer: impl Into) -> Expression { - Spanned::from_item( + Tagged::from_item( RawExpression::Variable(Variable::Other(inner.into())), outer.into(), ) } fn it_variable(inner: impl Into, outer: impl Into) -> Expression { - Spanned::from_item( + Tagged::from_item( RawExpression::Variable(Variable::It(inner.into())), outer.into(), ) } } -impl From> for Expression { - fn from(path: Spanned) -> Expression { +impl From> for Expression { + fn from(path: Tagged) -> Expression { path.map(|p| RawExpression::Path(Box::new(p))) } } diff --git a/src/parser/hir/baseline_parse.rs b/src/parser/hir/baseline_parse.rs index 02fa63f54d..8a6f82e656 100644 --- a/src/parser/hir/baseline_parse.rs +++ b/src/parser/hir/baseline_parse.rs @@ -3,26 +3,26 @@ use crate::Text; pub fn baseline_parse_single_token(token: &Token, source: &Text) -> hir::Expression { match *token.item() { - RawToken::Integer(int) => hir::Expression::int(int, token.span), - RawToken::Size(int, unit) => hir::Expression::size(int, unit, token.span), - RawToken::String(span) => hir::Expression::string(span, token.span), + RawToken::Integer(int) => hir::Expression::int(int, token.span()), + RawToken::Size(int, unit) => hir::Expression::size(int, unit, token.span()), + RawToken::String(span) => hir::Expression::string(span, token.span()), RawToken::Variable(span) if span.slice(source) == "it" => { - hir::Expression::it_variable(span, token.span) + hir::Expression::it_variable(span, token.span()) } - RawToken::Variable(span) => hir::Expression::variable(span, token.span), - RawToken::Bare => hir::Expression::bare(token.span), + RawToken::Variable(span) => hir::Expression::variable(span, token.span()), + RawToken::Bare => hir::Expression::bare(token.span()), } } pub fn baseline_parse_token_as_string(token: &Token, source: &Text) -> hir::Expression { match *token.item() { RawToken::Variable(span) if span.slice(source) == "it" => { - hir::Expression::it_variable(span, token.span) + hir::Expression::it_variable(span, token.span()) } - RawToken::Variable(span) => hir::Expression::variable(span, token.span), - RawToken::Integer(_) => hir::Expression::bare(token.span), - RawToken::Size(_, _) => hir::Expression::bare(token.span), - RawToken::Bare => hir::Expression::bare(token.span), - RawToken::String(span) => hir::Expression::string(span, token.span), + RawToken::Variable(span) => hir::Expression::variable(span, token.span()), + RawToken::Integer(_) => hir::Expression::bare(token.span()), + RawToken::Size(_, _) => hir::Expression::bare(token.span()), + RawToken::Bare => hir::Expression::bare(token.span()), + RawToken::String(span) => hir::Expression::string(span, token.span()), } } diff --git a/src/parser/hir/baseline_parse_tokens.rs b/src/parser/hir/baseline_parse_tokens.rs index 8b8dd026d0..0bb658632d 100644 --- a/src/parser/hir/baseline_parse_tokens.rs +++ b/src/parser/hir/baseline_parse_tokens.rs @@ -3,9 +3,9 @@ use crate::parser::registry::CommandRegistry; use crate::parser::{ hir, hir::{baseline_parse_single_token, baseline_parse_token_as_string}, - DelimitedNode, Delimiter, PathNode, RawToken, Span, Spanned, TokenNode, + DelimitedNode, Delimiter, PathNode, RawToken, TokenNode, }; -use crate::{SpannedItem, Text}; +use crate::{Span, Tag, Tagged, TaggedItem, Text}; use derive_new::new; use log::trace; use serde_derive::{Deserialize, Serialize}; @@ -61,7 +61,7 @@ pub fn baseline_parse_next_expr( (SyntaxType::Path, token) => { return Err(ShellError::type_error( "Path", - token.type_name().spanned(token.span()), + token.type_name().tagged(token.span()), )) } @@ -84,7 +84,7 @@ pub fn baseline_parse_next_expr( return Err(ShellError::maybe_labeled_error( "Expected something after an operator", "operator", - Some(op.span), + Some(op.span()), )) } Some(token) => baseline_parse_semantic_token(token, registry, source)?, @@ -94,25 +94,25 @@ pub fn baseline_parse_next_expr( match syntax_type { SyntaxType::Any => { - let span = (first.span.start, second.span.end); + let span = (first.span().start, second.span().end); let binary = hir::Binary::new(first, op, second); let binary = hir::RawExpression::Binary(Box::new(binary)); - let binary = Spanned::from_item(binary, span); + let binary = Tagged::from_item(binary, span); Ok(binary) } SyntaxType::Block => { - let span = (first.span.start, second.span.end); + let span = (first.span().start, second.span().end); - let path: Spanned = match first { - Spanned { + let path: Tagged = match first { + Tagged { item: hir::RawExpression::Literal(hir::Literal::Bare), - span, + tag: Tag { span }, } => { - let string = Spanned::from_item(span.slice(source).to_string(), span); + let string = Tagged::from_item(span.slice(source).to_string(), span); let path = hir::Path::new( - Spanned::from_item( + Tagged::from_item( // TODO: Deal with synthetic nodes that have no representation at all in source hir::RawExpression::Variable(hir::Variable::It(Span::from((0, 0)))), (0, 0), @@ -120,18 +120,15 @@ pub fn baseline_parse_next_expr( vec![string], ); let path = hir::RawExpression::Path(Box::new(path)); - Spanned { - item: path, - span: first.span, - } + Tagged::from_item(path, first.span()) } - Spanned { + Tagged { item: hir::RawExpression::Literal(hir::Literal::String(inner)), - span, + tag: Tag { span }, } => { - let string = Spanned::from_item(inner.slice(source).to_string(), span); + let string = Tagged::from_item(inner.slice(source).to_string(), span); let path = hir::Path::new( - Spanned::from_item( + Tagged::from_item( // TODO: Deal with synthetic nodes that have no representation at all in source hir::RawExpression::Variable(hir::Variable::It(Span::from((0, 0)))), (0, 0), @@ -139,16 +136,16 @@ pub fn baseline_parse_next_expr( vec![string], ); let path = hir::RawExpression::Path(Box::new(path)); - Spanned { - item: path, - span: first.span, - } + Tagged::from_item(path, first.span()) } - Spanned { + Tagged { item: hir::RawExpression::Variable(..), .. } => first, - Spanned { span, item } => { + Tagged { + tag: Tag { span }, + item, + } => { return Err(ShellError::labeled_error( "The first part of an un-braced block must be a column name", item.type_name(), @@ -159,10 +156,10 @@ pub fn baseline_parse_next_expr( let binary = hir::Binary::new(path, op, second); let binary = hir::RawExpression::Binary(Box::new(binary)); - let binary = Spanned::from_item(binary, span); + let binary = Tagged::from_item(binary, span); let block = hir::RawExpression::Block(vec![binary]); - let block = Spanned::from_item(block, span); + let block = Tagged::from_item(block, span); Ok(block) } @@ -196,7 +193,7 @@ pub fn baseline_parse_semantic_token( } pub fn baseline_parse_delimited( - token: &Spanned, + token: &Tagged, registry: &dyn CommandRegistry, source: &Text, ) -> Result { @@ -207,7 +204,7 @@ pub fn baseline_parse_delimited( baseline_parse_tokens(&mut TokensIterator::new(children), registry, source)?; let expr = hir::RawExpression::Block(exprs); - Ok(Spanned::from_item(expr, token.span())) + Ok(Tagged::from_item(expr, token.span())) } Delimiter::Paren => unimplemented!(), Delimiter::Square => unimplemented!(), @@ -215,7 +212,7 @@ pub fn baseline_parse_delimited( } pub fn baseline_parse_path( - token: &Spanned, + token: &Tagged, registry: &dyn CommandRegistry, source: &Text, ) -> Result { @@ -231,7 +228,7 @@ pub fn baseline_parse_path( RawToken::Integer(_) | RawToken::Size(..) | RawToken::Variable(_) => { return Err(ShellError::type_error( "String", - token.type_name().spanned(part), + token.type_name().tagged(part), )) } }, @@ -243,10 +240,10 @@ pub fn baseline_parse_path( } .to_string(); - tail.push(string.spanned(part)); + tail.push(string.tagged(part)); } - Ok(hir::path(head, tail).spanned(token).into()) + Ok(hir::path(head, tail).tagged(token).into()) } #[derive(Debug, new)] diff --git a/src/parser/hir/binary.rs b/src/parser/hir/binary.rs index 315964ed5f..c76d8bb420 100644 --- a/src/parser/hir/binary.rs +++ b/src/parser/hir/binary.rs @@ -1,4 +1,5 @@ -use crate::parser::{hir::Expression, Operator, Spanned}; +use crate::parser::{hir::Expression, Operator}; +use crate::Tagged; use derive_new::new; use getset::Getters; @@ -6,6 +7,6 @@ use getset::Getters; #[get = "crate"] pub struct Binary { left: Expression, - op: Spanned, + op: Tagged, right: Expression, } diff --git a/src/parser/hir/named.rs b/src/parser/hir/named.rs index 07c6db655e..f01a19d21c 100644 --- a/src/parser/hir/named.rs +++ b/src/parser/hir/named.rs @@ -1,5 +1,6 @@ use crate::parser::hir::Expression; -use crate::parser::{Flag, Span}; +use crate::parser::Flag; +use crate::Span; use derive_new::new; use indexmap::IndexMap; use log::trace; diff --git a/src/parser/hir/path.rs b/src/parser/hir/path.rs index 02bf8e5ce3..b22160b652 100644 --- a/src/parser/hir/path.rs +++ b/src/parser/hir/path.rs @@ -1,4 +1,5 @@ -use crate::parser::{hir::Expression, Spanned}; +use crate::parser::hir::Expression; +use crate::Tagged; use derive_new::new; use getset::Getters; @@ -6,5 +7,5 @@ use getset::Getters; #[get = "crate"] pub struct Path { head: Expression, - tail: Vec>, + tail: Vec>, } diff --git a/src/parser/parse.rs b/src/parser/parse.rs index ed2ffd8928..f80a12d35c 100644 --- a/src/parser/parse.rs +++ b/src/parser/parse.rs @@ -4,7 +4,6 @@ crate mod flag; crate mod operator; crate mod parser; crate mod pipeline; -crate mod span; crate mod text; crate mod token_tree; crate mod token_tree_builder; diff --git a/src/parser/parse/files.rs b/src/parser/parse/files.rs index 2743b95975..4b6c6c61b9 100644 --- a/src/parser/parse/files.rs +++ b/src/parser/parse/files.rs @@ -1,4 +1,4 @@ -use crate::parser::parse::span::Span; +use crate::Span; use derive_new::new; use language_reporting::{FileName, Location}; diff --git a/src/parser/parse/flag.rs b/src/parser/parse/flag.rs index 3b587c3354..0d554513a5 100644 --- a/src/parser/parse/flag.rs +++ b/src/parser/parse/flag.rs @@ -1,4 +1,4 @@ -use crate::parser::Span; +use crate::Span; use derive_new::new; use getset::Getters; use serde_derive::{Deserialize, Serialize}; diff --git a/src/parser/parse/parser.rs b/src/parser/parse/parser.rs index 685fa07b40..99ff9f1bac 100644 --- a/src/parser/parse/parser.rs +++ b/src/parser/parse/parser.rs @@ -1,9 +1,10 @@ #![allow(unused)] use crate::parser::parse::{ - call_node::*, flag::*, operator::*, pipeline::*, span::*, token_tree::*, token_tree_builder::*, + call_node::*, flag::*, operator::*, pipeline::*, token_tree::*, token_tree_builder::*, tokens::*, unit::*, }; +use crate::{Span, Tagged}; use nom; use nom::branch::*; use nom::bytes::complete::*; @@ -67,7 +68,7 @@ fn trace_step<'a, T: Debug>( } } -pub fn raw_integer(input: NomSpan) -> IResult> { +pub fn raw_integer(input: NomSpan) -> IResult> { let start = input.offset; trace_step(input, "raw_integer", move |input| { let (input, neg) = opt(tag("-"))(input)?; @@ -76,7 +77,7 @@ pub fn raw_integer(input: NomSpan) -> IResult> { Ok(( input, - Spanned::from_item(int(num.fragment, neg), (start, end)), + Tagged::from_item(int(num.fragment, neg), (start, end)), )) }) } @@ -85,7 +86,7 @@ pub fn integer(input: NomSpan) -> IResult { trace_step(input, "integer", move |input| { let (input, int) = raw_integer(input)?; - Ok((input, TokenTreeBuilder::spanned_int(*int, int.span))) + Ok((input, TokenTreeBuilder::spanned_int(*int, int.span()))) }) } */ @@ -202,7 +203,7 @@ pub fn shorthand(input: NomSpan) -> IResult { }) } -pub fn raw_unit(input: NomSpan) -> IResult> { +pub fn raw_unit(input: NomSpan) -> IResult> { trace_step(input, "raw_unit", move |input| { let start = input.offset; let (input, unit) = alt(( @@ -230,7 +231,7 @@ pub fn raw_unit(input: NomSpan) -> IResult> { Ok(( input, - Spanned::from_item(Unit::from(unit.fragment), (start, end)), + Tagged::from_item(Unit::from(unit.fragment), (start, end)), )) }) } @@ -408,7 +409,7 @@ pub fn delimited_brace(input: NomSpan) -> IResult { }) } -pub fn raw_call(input: NomSpan) -> IResult> { +pub fn raw_call(input: NomSpan) -> IResult> { trace_step(input, "raw_call", move |input| { let left = input.offset; let (input, items) = token_list(input)?; @@ -484,10 +485,10 @@ pub fn pipeline(input: NomSpan) -> IResult { } fn make_call_list( - head: Option<(Spanned, Option, Option)>, + head: Option<(Tagged, Option, Option)>, items: Vec<( Option, - Spanned, + Tagged, Option, Option, )>, @@ -701,12 +702,12 @@ mod tests { fn test_flag() { // assert_leaf! { // parsers [ flag ] - // "--hello" -> 0..7 { Flag(Spanned::from_item(FlagKind::Longhand, span(2, 7))) } + // "--hello" -> 0..7 { Flag(Tagged::from_item(FlagKind::Longhand, span(2, 7))) } // } // assert_leaf! { // parsers [ flag ] - // "--hello-world" -> 0..13 { Flag(Spanned::from_item(FlagKind::Longhand, span(2, 13))) } + // "--hello-world" -> 0..13 { Flag(Tagged::from_item(FlagKind::Longhand, span(2, 13))) } // } } @@ -714,7 +715,7 @@ mod tests { fn test_shorthand() { // assert_leaf! { // parsers [ shorthand ] - // "-alt" -> 0..4 { Flag(Spanned::from_item(FlagKind::Shorthand, span(1, 4))) } + // "-alt" -> 0..4 { Flag(Tagged::from_item(FlagKind::Shorthand, span(1, 4))) } // } } @@ -1024,7 +1025,7 @@ mod tests { right: usize, ) -> TokenNode { let node = DelimitedNode::new(delimiter, children); - let spanned = Spanned::from_item(node, (left, right)); + let spanned = Tagged::from_item(node, (left, right)); TokenNode::Delimited(spanned) } @@ -1033,16 +1034,16 @@ mod tests { Box::new(head), tail.into_iter().map(TokenNode::Token).collect(), ); - let spanned = Spanned::from_item(node, (left, right)); + let spanned = Tagged::from_item(node, (left, right)); TokenNode::Path(spanned) } fn leaf_token(token: RawToken, left: usize, right: usize) -> TokenNode { - TokenNode::Token(Spanned::from_item(token, (left, right))) + TokenNode::Token(Tagged::from_item(token, (left, right))) } fn token(token: RawToken, left: usize, right: usize) -> TokenNode { - TokenNode::Token(Spanned::from_item(token, (left, right))) + TokenNode::Token(Tagged::from_item(token, (left, right))) } fn build(block: CurriedNode) -> T { diff --git a/src/parser/parse/pipeline.rs b/src/parser/parse/pipeline.rs index 8fe8938841..27a13b77f7 100644 --- a/src/parser/parse/pipeline.rs +++ b/src/parser/parse/pipeline.rs @@ -1,4 +1,5 @@ -use crate::parser::{CallNode, Span, Spanned}; +use crate::parser::CallNode; +use crate::{Span, Tagged}; use derive_new::new; use getset::Getters; @@ -12,7 +13,7 @@ pub struct Pipeline { pub struct PipelineElement { pub pre_ws: Option, #[get = "crate"] - call: Spanned, + call: Tagged, pub post_ws: Option, pub post_pipe: Option, } diff --git a/src/parser/parse/span.rs b/src/parser/parse/span.rs deleted file mode 100644 index 7d6aa7334d..0000000000 --- a/src/parser/parse/span.rs +++ /dev/null @@ -1,197 +0,0 @@ -use crate::Text; -use derive_new::new; -use getset::Getters; -use serde::Serialize; -use serde_derive::Deserialize; -use uuid::Uuid; - -#[derive( - new, Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize, Hash, Getters, -)] -#[get = "crate"] -pub struct Spanned { - pub span: Span, - pub item: T, -} - -impl Spanned { - pub fn spanned(self, span: impl Into) -> Spanned { - Spanned::from_item(self.item, span.into()) - } -} - -pub trait SpannedItem: Sized { - fn spanned(self, span: impl Into) -> Spanned { - Spanned::from_item(self, span.into()) - } - - // For now, this is a temporary facility. In many cases, there are other useful spans that we - // could be using, such as the original source spans of JSON or Toml files, but we don't yet - // have the infrastructure to make that work. - fn spanned_unknown(self) -> Spanned { - Spanned::from_item(self, (0, 0)) - } -} - -impl SpannedItem for T {} - -impl std::ops::Deref for Spanned { - type Target = T; - - fn deref(&self) -> &T { - &self.item - } -} - -impl Spanned { - crate fn from_item(item: T, span: impl Into) -> Spanned { - Spanned { - span: span.into(), - item, - } - } - - pub fn map(self, input: impl FnOnce(T) -> U) -> Spanned { - let Spanned { span, item } = self; - - let mapped = input(item); - Spanned { span, item: mapped } - } - - crate fn copy_span(&self, output: U) -> Spanned { - let Spanned { span, .. } = self; - - Spanned { - span: *span, - item: output, - } - } - - pub fn source(&self, source: &Text) -> Text { - Text::from(self.span().slice(source)) - } -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Serialize, Deserialize, Hash)] -pub struct Span { - crate start: usize, - crate end: usize, - pub source: Option, -} - -impl From> for Span { - fn from(input: Option) -> Span { - match input { - None => Span { - start: 0, - end: 0, - source: None, - }, - Some(span) => span, - } - } -} - -impl From<&Spanned> for Span { - fn from(input: &Spanned) -> Span { - input.span - } -} - -impl From<&Span> for Span { - fn from(input: &Span) -> Span { - *input - } -} - -impl From> for Span { - fn from(input: nom5_locate::LocatedSpan<&str>) -> Span { - Span { - start: input.offset, - end: input.offset + input.fragment.len(), - source: None, - } - } -} - -impl From<(nom5_locate::LocatedSpan, nom5_locate::LocatedSpan)> for Span { - fn from(input: (nom5_locate::LocatedSpan, nom5_locate::LocatedSpan)) -> Span { - Span { - start: input.0.offset, - end: input.1.offset, - source: None, - } - } -} - -impl From<(usize, usize)> for Span { - fn from(input: (usize, usize)) -> Span { - Span { - start: input.0, - end: input.1, - source: None, - } - } -} - -impl From<&std::ops::Range> for Span { - fn from(input: &std::ops::Range) -> Span { - Span { - start: input.start, - end: input.end, - source: None, - } - } -} - -impl Span { - pub fn unknown() -> Span { - Span { - start: 0, - end: 0, - source: None, - } - } - - pub fn unknown_with_uuid(uuid: Uuid) -> Span { - Span { - start: 0, - end: 0, - source: Some(uuid), - } - } - - pub fn is_unknown(&self) -> bool { - self.start == 0 && self.end == 0 - } - - pub fn slice(&self, source: &'a str) -> &'a str { - &source[self.start..self.end] - } -} - -impl language_reporting::ReportingSpan for Span { - fn with_start(&self, start: usize) -> Self { - Span { - start, - end: self.end, - source: None, - } - } - - fn with_end(&self, end: usize) -> Self { - Span { - start: self.start, - end, - source: None, - } - } - - fn start(&self) -> usize { - self.start - } - - fn end(&self) -> usize { - self.end - } -} diff --git a/src/parser/parse/token_tree.rs b/src/parser/parse/token_tree.rs index b12bc3e338..6e7af2fa8e 100644 --- a/src/parser/parse/token_tree.rs +++ b/src/parser/parse/token_tree.rs @@ -1,6 +1,6 @@ use crate::errors::ShellError; -use crate::parser::parse::{call_node::*, flag::*, operator::*, pipeline::*, span::*, tokens::*}; -use crate::Text; +use crate::parser::parse::{call_node::*, flag::*, operator::*, pipeline::*, tokens::*}; +use crate::{Span, Tagged, Text}; use derive_new::new; use enum_utils::FromStr; use getset::Getters; @@ -10,16 +10,16 @@ use std::fmt; pub enum TokenNode { Token(Token), #[allow(unused)] - Call(Spanned), - Delimited(Spanned), - Pipeline(Spanned), - Operator(Spanned), - Flag(Spanned), + Call(Tagged), + Delimited(Tagged), + Pipeline(Tagged), + Operator(Tagged), + Flag(Tagged), Member(Span), Whitespace(Span), #[allow(unused)] - Error(Spanned>), - Path(Spanned), + Error(Tagged>), + Path(Tagged), } pub struct DebugTokenNode<'a> { @@ -86,16 +86,16 @@ impl From<&TokenNode> for Span { impl TokenNode { pub fn span(&self) -> Span { match self { - TokenNode::Token(t) => t.span, - TokenNode::Call(s) => s.span, - TokenNode::Delimited(s) => s.span, - TokenNode::Pipeline(s) => s.span, - TokenNode::Operator(s) => s.span, - TokenNode::Flag(s) => s.span, + TokenNode::Token(t) => t.span(), + TokenNode::Call(s) => s.span(), + TokenNode::Delimited(s) => s.span(), + TokenNode::Pipeline(s) => s.span(), + TokenNode::Operator(s) => s.span(), + TokenNode::Flag(s) => s.span(), TokenNode::Member(s) => *s, TokenNode::Whitespace(s) => *s, - TokenNode::Error(s) => s.span, - TokenNode::Path(s) => s.span, + TokenNode::Error(s) => s.span(), + TokenNode::Path(s) => s.span(), } } @@ -129,7 +129,7 @@ impl TokenNode { pub fn is_bare(&self) -> bool { match self { - TokenNode::Token(Spanned { + TokenNode::Token(Tagged { item: RawToken::Bare, .. }) => true, @@ -137,10 +137,10 @@ impl TokenNode { } } - crate fn as_flag(&self, value: &str, source: &Text) -> Option> { + crate fn as_flag(&self, value: &str, source: &Text) -> Option> { match self { TokenNode::Flag( - flag @ Spanned { + flag @ Tagged { item: Flag { .. }, .. }, ) if value == flag.name().slice(source) => Some(*flag), @@ -150,7 +150,7 @@ impl TokenNode { pub fn as_pipeline(&self) -> Result { match self { - TokenNode::Pipeline(Spanned { item, .. }) => Ok(item.clone()), + TokenNode::Pipeline(Tagged { item, .. }) => Ok(item.clone()), _ => Err(ShellError::string("unimplemented")), } } diff --git a/src/parser/parse/token_tree_builder.rs b/src/parser/parse/token_tree_builder.rs index d6a993aa45..1c3d37476c 100644 --- a/src/parser/parse/token_tree_builder.rs +++ b/src/parser/parse/token_tree_builder.rs @@ -4,11 +4,11 @@ use crate::prelude::*; use crate::parser::parse::flag::{Flag, FlagKind}; use crate::parser::parse::operator::Operator; use crate::parser::parse::pipeline::{Pipeline, PipelineElement}; -use crate::parser::parse::span::{Span, Spanned}; use crate::parser::parse::token_tree::{DelimitedNode, Delimiter, PathNode, TokenNode}; use crate::parser::parse::tokens::{RawToken, Token}; use crate::parser::parse::unit::Unit; use crate::parser::CallNode; +use crate::Span; use derive_new::new; #[derive(new)] @@ -20,7 +20,7 @@ pub struct TokenTreeBuilder { #[allow(unused)] pub type CurriedNode = Box T + 'static>; pub type CurriedToken = Box TokenNode + 'static>; -pub type CurriedCall = Box Spanned + 'static>; +pub type CurriedCall = Box Tagged + 'static>; #[allow(unused)] impl TokenTreeBuilder { @@ -92,7 +92,7 @@ impl TokenTreeBuilder { input: (Vec, Option), span: impl Into, ) -> TokenNode { - TokenNode::Pipeline(Spanned::from_item( + TokenNode::Pipeline(Tagged::from_item( Pipeline::new(input.0, input.1.into()), span, )) @@ -111,7 +111,7 @@ impl TokenTreeBuilder { } pub fn spanned_op(input: impl Into, span: impl Into) -> TokenNode { - TokenNode::Operator(Spanned::from_item(input.into(), span.into())) + TokenNode::Operator(Tagged::from_item(input.into(), span.into())) } pub fn string(input: impl Into) -> CurriedToken { @@ -128,7 +128,7 @@ impl TokenTreeBuilder { } pub fn spanned_string(input: impl Into, span: impl Into) -> TokenNode { - TokenNode::Token(Spanned::from_item( + TokenNode::Token(Tagged::from_item( RawToken::String(input.into()), span.into(), )) @@ -146,7 +146,7 @@ impl TokenTreeBuilder { } pub fn spanned_bare(input: impl Into) -> TokenNode { - TokenNode::Token(Spanned::from_item(RawToken::Bare, input.into())) + TokenNode::Token(Tagged::from_item(RawToken::Bare, input.into())) } pub fn int(input: impl Into) -> CurriedToken { @@ -183,7 +183,7 @@ impl TokenTreeBuilder { ) -> TokenNode { let (int, unit) = (input.0.into(), input.1.into()); - TokenNode::Token(Spanned::from_item(RawToken::Size(int, unit), span)) + TokenNode::Token(Tagged::from_item(RawToken::Size(int, unit), span)) } pub fn path(head: CurriedToken, tail: Vec) -> CurriedToken { @@ -206,7 +206,7 @@ impl TokenTreeBuilder { } pub fn spanned_path(input: (TokenNode, Vec), span: impl Into) -> TokenNode { - TokenNode::Path(Spanned::from_item( + TokenNode::Path(Tagged::from_item( PathNode::new(Box::new(input.0), input.1), span, )) @@ -224,7 +224,7 @@ impl TokenTreeBuilder { } pub fn spanned_var(input: impl Into, span: impl Into) -> TokenNode { - TokenNode::Token(Spanned::from_item( + TokenNode::Token(Tagged::from_item( RawToken::Variable(input.into()), span.into(), )) @@ -242,7 +242,7 @@ impl TokenTreeBuilder { } pub fn spanned_flag(input: impl Into, span: impl Into) -> TokenNode { - TokenNode::Flag(Spanned::from_item( + TokenNode::Flag(Tagged::from_item( Flag::new(FlagKind::Longhand, input.into()), span.into(), )) @@ -260,7 +260,7 @@ impl TokenTreeBuilder { } pub fn spanned_shorthand(input: impl Into, span: impl Into) -> TokenNode { - TokenNode::Flag(Spanned::from_item( + TokenNode::Flag(Tagged::from_item( Flag::new(FlagKind::Shorthand, input.into()), span.into(), )) @@ -296,7 +296,7 @@ impl TokenTreeBuilder { }) } - pub fn spanned_call(input: Vec, span: impl Into) -> Spanned { + pub fn spanned_call(input: Vec, span: impl Into) -> Tagged { if input.len() == 0 { panic!("BUG: spanned call (TODO)") } @@ -306,7 +306,7 @@ impl TokenTreeBuilder { let head = input.next().unwrap(); let tail = input.collect(); - Spanned::from_item(CallNode::new(Box::new(head), tail), span) + Tagged::from_item(CallNode::new(Box::new(head), tail), span) } pub fn parens(input: Vec) -> CurriedToken { @@ -324,7 +324,7 @@ impl TokenTreeBuilder { } pub fn spanned_parens(input: impl Into>, span: impl Into) -> TokenNode { - TokenNode::Delimited(Spanned::from_item( + TokenNode::Delimited(Tagged::from_item( DelimitedNode::new(Delimiter::Paren, input.into()), span, )) @@ -345,7 +345,7 @@ impl TokenTreeBuilder { } pub fn spanned_square(input: impl Into>, span: impl Into) -> TokenNode { - TokenNode::Delimited(Spanned::from_item( + TokenNode::Delimited(Tagged::from_item( DelimitedNode::new(Delimiter::Square, input.into()), span, )) @@ -366,7 +366,7 @@ impl TokenTreeBuilder { } pub fn spanned_brace(input: impl Into>, span: impl Into) -> TokenNode { - TokenNode::Delimited(Spanned::from_item( + TokenNode::Delimited(Tagged::from_item( DelimitedNode::new(Delimiter::Brace, input.into()), span, )) diff --git a/src/parser/parse/tokens.rs b/src/parser/parse/tokens.rs index e9528c1506..670edbf832 100644 --- a/src/parser/parse/tokens.rs +++ b/src/parser/parse/tokens.rs @@ -1,6 +1,5 @@ -use crate::parser::parse::span::*; use crate::parser::parse::unit::*; -use crate::Text; +use crate::{Span, Tagged, Text}; use std::fmt; #[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)] @@ -24,7 +23,7 @@ impl RawToken { } } -pub type Token = Spanned; +pub type Token = Tagged; impl Token { pub fn debug(&self, source: &'a Text) -> DebugToken<'a> { diff --git a/src/parser/parse_command.rs b/src/parser/parse_command.rs index 16a9c88169..e04aa7a622 100644 --- a/src/parser/parse_command.rs +++ b/src/parser/parse_command.rs @@ -1,20 +1,20 @@ use crate::errors::{ArgumentError, ShellError}; use crate::parser::registry::{CommandConfig, CommandRegistry, NamedType, PositionalType}; -use crate::parser::{baseline_parse_tokens, CallNode, Span, Spanned}; +use crate::parser::{baseline_parse_tokens, CallNode}; use crate::parser::{ hir::{self, NamedArguments}, Flag, RawToken, TokenNode, }; -use crate::Text; +use crate::{Span, Tag, Tagged, Text}; use log::trace; pub fn parse_command( config: &CommandConfig, registry: &dyn CommandRegistry, - call: &Spanned, + call: &Tagged, source: &Text, ) -> Result { - let Spanned { item: raw_call, .. } = call; + let Tagged { item: raw_call, .. } = call; trace!("Processing {:?}", config); @@ -31,7 +31,7 @@ pub fn parse_command( .collect() }); - match parse_command_tail(&config, registry, children, source, call.span)? { + match parse_command_tail(&config, registry, children, source, call.span())? { None => Ok(hir::Call::new(Box::new(head), None, None)), Some((positional, named)) => Ok(hir::Call::new(Box::new(head), positional, named)), } @@ -40,16 +40,16 @@ pub fn parse_command( fn parse_command_head(head: &TokenNode) -> Result { match head { TokenNode::Token( - spanned @ Spanned { + spanned @ Tagged { item: RawToken::Bare, .. }, ) => Ok(spanned.map(|_| hir::RawExpression::Literal(hir::Literal::Bare))), - TokenNode::Token(Spanned { + TokenNode::Token(Tagged { item: RawToken::String(inner_span), - span, - }) => Ok(Spanned::from_item( + tag: Tag { span }, + }) => Ok(Tagged::from_item( hir::RawExpression::Literal(hir::Literal::String(*inner_span)), *span, )), @@ -96,7 +96,7 @@ fn parse_command_tail( return Err(ShellError::argument_error( config.name.clone(), ArgumentError::MissingValueForName(name.to_string()), - flag.span, + flag.span(), )); } @@ -117,7 +117,7 @@ fn parse_command_tail( return Err(ShellError::argument_error( config.name().clone(), ArgumentError::MissingValueForName(name.to_string()), - flag.span, + flag.span(), )); } @@ -202,7 +202,7 @@ fn extract_mandatory( tokens: &mut hir::TokensIterator<'a>, source: &Text, span: Span, -) -> Result<(usize, Spanned), ShellError> { +) -> Result<(usize, Tagged), ShellError> { let flag = tokens.extract(|t| t.as_flag(name, source)); match flag { @@ -223,7 +223,7 @@ fn extract_optional( name: &str, tokens: &mut hir::TokensIterator<'a>, source: &Text, -) -> Result<(Option<(usize, Spanned)>), ShellError> { +) -> Result<(Option<(usize, Tagged)>), ShellError> { let flag = tokens.extract(|t| t.as_flag(name, source)); match flag { diff --git a/src/parser/registry.rs b/src/parser/registry.rs index e734c6725e..bc26496bef 100644 --- a/src/parser/registry.rs +++ b/src/parser/registry.rs @@ -1,5 +1,5 @@ use crate::evaluate::{evaluate_baseline_expr, Scope}; -use crate::parser::{hir, hir::SyntaxType, parse_command, CallNode, Spanned}; +use crate::parser::{hir, hir::SyntaxType, parse_command, CallNode}; use crate::prelude::*; use derive_new::new; use getset::Getters; @@ -81,13 +81,13 @@ pub struct CommandConfig { #[derive(Debug, Default, new, Serialize, Deserialize, Clone)] pub struct Args { - pub positional: Option>>, - pub named: Option>>, + pub positional: Option>>, + pub named: Option>>, } #[derive(new)] pub struct DebugPositional<'a> { - positional: &'a Option>>, + positional: &'a Option>>, } impl fmt::Debug for DebugPositional<'a> { @@ -104,7 +104,7 @@ impl fmt::Debug for DebugPositional<'a> { #[derive(new)] pub struct DebugNamed<'a> { - named: &'a Option>>, + named: &'a Option>>, } impl fmt::Debug for DebugNamed<'a> { @@ -139,14 +139,14 @@ impl Args { DebugArgs { args: self } } - pub fn nth(&self, pos: usize) -> Option<&Spanned> { + pub fn nth(&self, pos: usize) -> Option<&Tagged> { match &self.positional { None => None, Some(array) => array.iter().nth(pos), } } - pub fn expect_nth(&self, pos: usize) -> Result<&Spanned, ShellError> { + pub fn expect_nth(&self, pos: usize) -> Result<&Tagged, ShellError> { match &self.positional { None => Err(ShellError::unimplemented("Better error: expect_nth")), Some(array) => match array.iter().nth(pos) { @@ -170,7 +170,7 @@ impl Args { } } - pub fn get(&self, name: &str) -> Option<&Spanned> { + pub fn get(&self, name: &str) -> Option<&Tagged> { match &self.named { None => None, Some(named) => named.get(name), @@ -190,11 +190,11 @@ impl Args { pub enum PositionalIter<'a> { Empty, - Array(std::slice::Iter<'a, Spanned>), + Array(std::slice::Iter<'a, Tagged>), } impl Iterator for PositionalIter<'a> { - type Item = &'a Spanned; + type Item = &'a Tagged; fn next(&mut self) -> Option { match self { @@ -207,7 +207,7 @@ impl Iterator for PositionalIter<'a> { impl CommandConfig { crate fn evaluate_args( &self, - call: &Spanned, + call: &Tagged, registry: &dyn CommandRegistry, scope: &Scope, source: &Text, @@ -217,80 +217,6 @@ impl CommandConfig { trace!("parsed args: {:?}", args); evaluate_args(args, registry, scope, source) - - // let mut positional: Vec> = vec![]; - // let mut named: IndexMap = IndexMap::default(); - - // let mut args: Vec = args.cloned().collect(); - - // for (key, ty) in self.named.iter() { - // let index = args.iter().position(|a| a.is_flag(&key, source)); - - // match (index, ty) { - // (Some(i), NamedType::Switch) => { - // args.remove(i); - // named.insert(key.clone(), Value::boolean(true)); - // } - - // (None, NamedType::Switch) => {} - - // (Some(i), NamedType::Optional(v)) => { - // args.remove(i); - // named.insert(key.clone(), extract_named(&mut args, i, v)?); - // } - - // (None, NamedType::Optional(_)) => {} - - // (Some(i), NamedType::Mandatory(v)) => { - // args.remove(i); - // named.insert(key.clone(), extract_named(&mut args, i, v)?); - // } - - // (None, NamedType::Mandatory(_)) => { - // return Err(ShellError::string(&format!( - // "Expected mandatory argument {}, but it was missing", - // key - // ))) - // } - // } - // } - - // let mut args = args.into_iter(); - - // for param in &self.mandatory_positional { - // let arg = args.next(); - - // let value = match arg { - // None => { - // return Err(ShellError::string(format!( - // "expected mandatory positional argument {}", - // param.name() - // ))) - // } - - // Some(arg) => param.evaluate(arg.clone(), scope, source)?, - // }; - - // positional.push(value); - // } - - // if self.rest_positional { - // let rest: Result>, _> = args - // .map(|i| evaluate_baseline_expr(&i, &Scope::empty(), source)) - // .collect(); - // positional.extend(rest?); - // } else { - // let rest: Vec = args.collect(); - - // if rest.len() > 0 { - // return Err(ShellError::string(&format!( - // "Too many arguments, extras: {:?}", - // rest - // ))); - // } - // } - - // Ok(Args { positional, named }) } #[allow(unused)] @@ -317,7 +243,7 @@ fn evaluate_args( let positional = positional?; - let named: Result>>, ShellError> = args + let named: Result>>, ShellError> = args .named() .as_ref() .map(|n| { @@ -326,10 +252,8 @@ fn evaluate_args( for (name, value) in n.named.iter() { match value { hir::named::NamedValue::PresentSwitch(span) => { - results.insert( - name.clone(), - Spanned::from_item(Value::boolean(true), *span), - ); + results + .insert(name.clone(), Tagged::from_item(Value::boolean(true), *span)); } hir::named::NamedValue::Value(expr) => { results.insert( diff --git a/src/plugin.rs b/src/plugin.rs index e96fc988f9..a10343e491 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -1,4 +1,5 @@ -use crate::{CallInfo, CommandConfig, ReturnValue, ShellError, Spanned, Value}; +use crate::Tagged; +use crate::{CallInfo, CommandConfig, ReturnValue, ShellError, Value}; use serde::{Deserialize, Serialize}; use std::io; @@ -10,7 +11,7 @@ pub trait Plugin { Ok(vec![]) } #[allow(unused)] - fn filter(&mut self, input: Spanned) -> Result, ShellError> { + fn filter(&mut self, input: Tagged) -> Result, ShellError> { Ok(vec![]) } #[allow(unused)] @@ -18,7 +19,7 @@ pub trait Plugin { Ok(vec![]) } #[allow(unused)] - fn sink(&mut self, call_info: CallInfo, input: Vec>) {} + fn sink(&mut self, call_info: CallInfo, input: Vec>) {} fn quit(&mut self) {} } @@ -138,11 +139,11 @@ pub enum NuCommand { params: CallInfo, }, filter { - params: Spanned, + params: Tagged, }, end_filter, sink { - params: (CallInfo, Vec>), + params: (CallInfo, Vec>), }, quit, } diff --git a/src/plugins/add.rs b/src/plugins/add.rs index 848bc32d00..845d0ed572 100644 --- a/src/plugins/add.rs +++ b/src/plugins/add.rs @@ -1,7 +1,7 @@ use indexmap::IndexMap; use nu::{ serve_plugin, CallInfo, CommandConfig, Plugin, PositionalType, Primitive, ReturnSuccess, - ReturnValue, ShellError, Spanned, Value, + ReturnValue, ShellError, Tagged, Value, }; struct Add { @@ -16,10 +16,11 @@ impl Add { } } - fn add(&self, value: Spanned) -> Result, ShellError> { + fn add(&self, value: Tagged) -> Result, ShellError> { + let value_span = value.span(); match (value.item, self.value.clone()) { (obj @ Value::Object(_), Some(v)) => match &self.field { - Some(f) => match obj.insert_data_at_path(value.span, &f, v) { + Some(f) => match obj.insert_data_at_path(value_span, &f, v) { Some(v) => return Ok(v), None => { return Err(ShellError::string( @@ -56,7 +57,7 @@ impl Plugin for Add { fn begin_filter(&mut self, call_info: CallInfo) -> Result, ShellError> { if let Some(args) = call_info.args.positional { match &args[0] { - Spanned { + Tagged { item: Value::Primitive(Primitive::String(s)), .. } => { @@ -70,7 +71,7 @@ impl Plugin for Add { } } match &args[1] { - Spanned { item: v, .. } => { + Tagged { item: v, .. } => { self.value = Some(v.clone()); } } @@ -79,7 +80,7 @@ impl Plugin for Add { Ok(vec![]) } - fn filter(&mut self, input: Spanned) -> Result, ShellError> { + fn filter(&mut self, input: Tagged) -> Result, ShellError> { Ok(vec![ReturnSuccess::value(self.add(input)?)]) } } diff --git a/src/plugins/binaryview.rs b/src/plugins/binaryview.rs index 61347bfc28..fce0ed1d82 100644 --- a/src/plugins/binaryview.rs +++ b/src/plugins/binaryview.rs @@ -2,8 +2,7 @@ use crossterm::{cursor, terminal, Attribute, RawScreen}; use indexmap::IndexMap; use nu::{ - serve_plugin, CallInfo, CommandConfig, NamedType, Plugin, ShellError, SpanSource, Spanned, - Value, + serve_plugin, CallInfo, CommandConfig, NamedType, Plugin, ShellError, SpanSource, Tagged, Value, }; use pretty_hex::*; @@ -29,14 +28,15 @@ impl Plugin for BinaryView { }) } - fn sink(&mut self, call_info: CallInfo, input: Vec>) { + fn sink(&mut self, call_info: CallInfo, input: Vec>) { for v in input { - match v { - Spanned { - item: Value::Binary(b), - span, - } => { - let source = span.source.map(|x| call_info.source_map.get(&x)).flatten(); + let value_span = v.span(); + match v.item { + Value::Binary(b) => { + let source = value_span + .source + .map(|x| call_info.source_map.get(&x)) + .flatten(); let _ = view_binary(&b, source, call_info.args.has("lores")); } _ => {} diff --git a/src/plugins/edit.rs b/src/plugins/edit.rs index 01e7344a7f..a9a5da1208 100644 --- a/src/plugins/edit.rs +++ b/src/plugins/edit.rs @@ -1,7 +1,7 @@ use indexmap::IndexMap; use nu::{ serve_plugin, CallInfo, CommandConfig, Plugin, PositionalType, Primitive, ReturnSuccess, - ReturnValue, ShellError, Spanned, Value, + ReturnValue, ShellError, Tagged, Value, }; struct Edit { @@ -16,10 +16,11 @@ impl Edit { } } - fn edit(&self, value: Spanned) -> Result, ShellError> { + fn edit(&self, value: Tagged) -> Result, ShellError> { + let value_span = value.span(); match (value.item, self.value.clone()) { (obj @ Value::Object(_), Some(v)) => match &self.field { - Some(f) => match obj.replace_data_at_path(value.span, &f, v) { + Some(f) => match obj.replace_data_at_path(value_span, &f, v) { Some(v) => return Ok(v), None => { return Err(ShellError::string( @@ -56,7 +57,7 @@ impl Plugin for Edit { fn begin_filter(&mut self, call_info: CallInfo) -> Result, ShellError> { if let Some(args) = call_info.args.positional { match &args[0] { - Spanned { + Tagged { item: Value::Primitive(Primitive::String(s)), .. } => { @@ -70,7 +71,7 @@ impl Plugin for Edit { } } match &args[1] { - Spanned { item: v, .. } => { + Tagged { item: v, .. } => { self.value = Some(v.clone()); } } @@ -79,7 +80,7 @@ impl Plugin for Edit { Ok(vec![]) } - fn filter(&mut self, input: Spanned) -> Result, ShellError> { + fn filter(&mut self, input: Tagged) -> Result, ShellError> { Ok(vec![ReturnSuccess::value(self.edit(input)?)]) } } diff --git a/src/plugins/inc.rs b/src/plugins/inc.rs index ee5b03a1f8..5ed725a875 100644 --- a/src/plugins/inc.rs +++ b/src/plugins/inc.rs @@ -1,7 +1,7 @@ use indexmap::IndexMap; use nu::{ serve_plugin, CallInfo, CommandConfig, NamedType, Plugin, PositionalType, Primitive, - ReturnSuccess, ReturnValue, ShellError, Spanned, SpannedItem, Value, + ReturnSuccess, ReturnValue, ShellError, Tagged, TaggedItem, Value, }; struct Inc { @@ -22,20 +22,20 @@ impl Inc { fn inc( &self, - value: Spanned, + value: Tagged, field: &Option, - ) -> Result, ShellError> { + ) -> Result, ShellError> { match value.item { - Value::Primitive(Primitive::Int(i)) => Ok(Value::int(i + 1).spanned(value.span)), + Value::Primitive(Primitive::Int(i)) => Ok(Value::int(i + 1).tagged(value.span())), Value::Primitive(Primitive::Bytes(b)) => { - Ok(Value::bytes(b + 1 as u64).spanned(value.span)) + Ok(Value::bytes(b + 1 as u64).tagged(value.span())) } - Value::Primitive(Primitive::String(s)) => { + Value::Primitive(Primitive::String(ref s)) => { if let Ok(i) = s.parse::() { - Ok(Spanned { - item: Value::string(format!("{}", i + 1)), - span: value.span, - }) + Ok(Tagged::from_item( + Value::string(format!("{}", i + 1)), + value.span(), + )) } else if let Ok(mut ver) = semver::Version::parse(&s) { if self.major { ver.increment_major(); @@ -45,17 +45,17 @@ impl Inc { self.patch; ver.increment_patch(); } - Ok(Spanned { - item: Value::string(ver.to_string()), - span: value.span, - }) + Ok(Tagged::from_item( + Value::string(ver.to_string()), + value.span(), + )) } else { Err(ShellError::string("string could not be incremented")) } } Value::Object(_) => match field { Some(f) => { - let replacement = match value.item.get_data_by_path(value.span, f) { + let replacement = match value.item.get_data_by_path(value.span(), f) { Some(result) => self.inc(result.map(|x| x.clone()), &None)?, None => { return Err(ShellError::string("inc could not find field to replace")) @@ -63,7 +63,7 @@ impl Inc { }; match value .item - .replace_data_at_path(value.span, f, replacement.item.clone()) + .replace_data_at_path(value.span(), f, replacement.item.clone()) { Some(v) => return Ok(v), None => { @@ -113,7 +113,7 @@ impl Plugin for Inc { if let Some(args) = call_info.args.positional { for arg in args { match arg { - Spanned { + Tagged { item: Value::Primitive(Primitive::String(s)), .. } => { @@ -132,7 +132,7 @@ impl Plugin for Inc { Ok(vec![]) } - fn filter(&mut self, input: Spanned) -> Result, ShellError> { + fn filter(&mut self, input: Tagged) -> Result, ShellError> { Ok(vec![ReturnSuccess::value(self.inc(input, &self.field)?)]) } } diff --git a/src/plugins/skip.rs b/src/plugins/skip.rs index 321f731a35..2a5058a1c4 100644 --- a/src/plugins/skip.rs +++ b/src/plugins/skip.rs @@ -1,7 +1,7 @@ use indexmap::IndexMap; use nu::{ serve_plugin, CallInfo, CommandConfig, Plugin, Primitive, ReturnSuccess, ReturnValue, - ShellError, Spanned, Value, + ShellError, Tagged, Value, }; struct Skip { @@ -28,7 +28,7 @@ impl Plugin for Skip { if let Some(args) = call_info.args.positional { for arg in args { match arg { - Spanned { + Tagged { item: Value::Primitive(Primitive::Int(i)), .. } => { @@ -38,7 +38,7 @@ impl Plugin for Skip { return Err(ShellError::labeled_error( "Unrecognized type in params", "expected an integer", - arg.span, + arg.span(), )) } } @@ -48,7 +48,7 @@ impl Plugin for Skip { Ok(vec![]) } - fn filter(&mut self, input: Spanned) -> Result, ShellError> { + fn filter(&mut self, input: Tagged) -> Result, ShellError> { if self.skip_amount == 0 { Ok(vec![ReturnSuccess::value(input)]) } else { diff --git a/src/plugins/str.rs b/src/plugins/str.rs index 5830d4a008..0bae956182 100644 --- a/src/plugins/str.rs +++ b/src/plugins/str.rs @@ -1,7 +1,7 @@ use indexmap::IndexMap; use nu::{ serve_plugin, CallInfo, CommandConfig, NamedType, Plugin, PositionalType, Primitive, - ReturnSuccess, ReturnValue, ShellError, Spanned, Value, + ReturnSuccess, ReturnValue, ShellError, Tagged, Value, }; struct Str { @@ -69,17 +69,17 @@ impl Str { impl Str { fn strutils( &self, - value: Spanned, + value: Tagged, field: &Option, - ) -> Result, ShellError> { + ) -> Result, ShellError> { match value.item { - Value::Primitive(Primitive::String(s)) => Ok(Spanned { - item: Value::string(self.apply(&s)), - span: value.span, - }), + Value::Primitive(Primitive::String(ref s)) => Ok(Tagged::from_item( + Value::string(self.apply(&s)), + value.span(), + )), Value::Object(_) => match field { Some(f) => { - let replacement = match value.item.get_data_by_path(value.span, f) { + let replacement = match value.item.get_data_by_path(value.span(), f) { Some(result) => self.strutils(result.map(|x| x.clone()), &None)?, None => { return Err(ShellError::string("str could not find field to replace")) @@ -87,7 +87,7 @@ impl Str { }; match value .item - .replace_data_at_path(value.span, f, replacement.item.clone()) + .replace_data_at_path(value.span(), f, replacement.item.clone()) { Some(v) => return Ok(v), None => { @@ -135,7 +135,7 @@ impl Plugin for Str { if let Some(args) = call_info.args.positional { for arg in args { match arg { - Spanned { + Tagged { item: Value::Primitive(Primitive::String(s)), .. } => { @@ -161,7 +161,7 @@ impl Plugin for Str { Ok(vec![]) } - fn filter(&mut self, input: Spanned) -> Result, ShellError> { + fn filter(&mut self, input: Tagged) -> Result, ShellError> { Ok(vec![ReturnSuccess::value( self.strutils(input, &self.field)?, )]) diff --git a/src/plugins/sum.rs b/src/plugins/sum.rs index 83b5e63266..f3f71be86c 100644 --- a/src/plugins/sum.rs +++ b/src/plugins/sum.rs @@ -1,30 +1,27 @@ use indexmap::IndexMap; use nu::{ serve_plugin, CallInfo, CommandConfig, Plugin, Primitive, ReturnSuccess, ReturnValue, - ShellError, Spanned, Value, + ShellError, Tag, Tagged, Value, }; struct Sum { - total: Option>, + total: Option>, } impl Sum { fn new() -> Sum { Sum { total: None } } - fn sum(&mut self, value: Spanned) -> Result<(), ShellError> { + fn sum(&mut self, value: Tagged) -> Result<(), ShellError> { match value.item { Value::Primitive(Primitive::Int(i)) => { match self.total { - Some(Spanned { + Some(Tagged { item: Value::Primitive(Primitive::Int(j)), - span, + tag: Tag { span }, }) => { //TODO: handle overflow - self.total = Some(Spanned { - item: Value::int(i + j), - span, - }); + self.total = Some(Tagged::from_item(Value::int(i + j), span)); Ok(()) } None => { @@ -38,15 +35,12 @@ impl Sum { } Value::Primitive(Primitive::Bytes(b)) => { match self.total { - Some(Spanned { + Some(Tagged { item: Value::Primitive(Primitive::Bytes(j)), - span, + tag: Tag { span }, }) => { //TODO: handle overflow - self.total = Some(Spanned { - item: Value::bytes(b + j), - span, - }); + self.total = Some(Tagged::from_item(Value::bytes(b + j), span)); Ok(()) } None => { @@ -81,7 +75,7 @@ impl Plugin for Sum { Ok(vec![]) } - fn filter(&mut self, input: Spanned) -> Result, ShellError> { + fn filter(&mut self, input: Tagged) -> Result, ShellError> { self.sum(input)?; Ok(vec![]) } diff --git a/src/plugins/sys.rs b/src/plugins/sys.rs index dc1e59f21b..515fe25abb 100644 --- a/src/plugins/sys.rs +++ b/src/plugins/sys.rs @@ -6,7 +6,7 @@ use heim::{disk, memory}; use indexmap::IndexMap; use nu::{ serve_plugin, CallInfo, CommandConfig, Plugin, Primitive, ReturnSuccess, ReturnValue, - ShellError, Span, Spanned, SpannedDictBuilder, Value, OF64, + ShellError, Span, Tagged, TaggedDictBuilder, Value, OF64, }; use std::ffi::OsStr; @@ -19,19 +19,19 @@ impl Sys { //TODO: add more error checking -async fn cpu(span: Span) -> Option> { +async fn cpu(span: Span) -> Option> { if let (Ok(num_cpu), Ok(cpu_speed)) = (sys_info::cpu_num(), sys_info::cpu_speed()) { - let mut cpu_idx = SpannedDictBuilder::new(span); + let mut cpu_idx = TaggedDictBuilder::new(span); cpu_idx.insert("cores", Primitive::Int(num_cpu as i64)); cpu_idx.insert("speed", Primitive::Int(cpu_speed as i64)); - Some(cpu_idx.into_spanned_value()) + Some(cpu_idx.into_tagged_value()) } else { None } } -async fn mem(span: Span) -> Spanned { - let mut dict = SpannedDictBuilder::new(span); +async fn mem(span: Span) -> Tagged { + let mut dict = TaggedDictBuilder::new(span); if let Ok(memory) = memory::memory().await { dict.insert("total", Value::bytes(memory.total().get())); @@ -42,11 +42,11 @@ async fn mem(span: Span) -> Spanned { dict.insert("swap free", Value::bytes(swap.free().get())); } - dict.into_spanned_value() + dict.into_tagged_value() } -async fn host(span: Span) -> Spanned { - let mut dict = SpannedDictBuilder::new(span); +async fn host(span: Span) -> Tagged { + let mut dict = TaggedDictBuilder::new(span); // OS if let Ok(platform) = heim::host::platform().await { @@ -58,7 +58,7 @@ async fn host(span: Span) -> Spanned { // Uptime if let Ok(uptime) = heim::host::uptime().await { - let mut uptime_dict = SpannedDictBuilder::new(span); + let mut uptime_dict = TaggedDictBuilder::new(span); let uptime = uptime.get().round() as i64; let days = uptime / (60 * 60 * 24); @@ -71,7 +71,7 @@ async fn host(span: Span) -> Spanned { uptime_dict.insert("mins", Value::int(minutes)); uptime_dict.insert("secs", Value::int(seconds)); - dict.insert_spanned("uptime", uptime_dict.into_spanned_value()); + dict.insert_tagged("uptime", uptime_dict.into_tagged_value()); } // Users @@ -79,16 +79,13 @@ async fn host(span: Span) -> Spanned { let mut user_vec = vec![]; while let Some(user) = users.next().await { if let Ok(user) = user { - user_vec.push(Spanned { - item: Value::string(user.username()), - span, - }); + user_vec.push(Tagged::from_item(Value::string(user.username()), span)); } } let user_list = Value::List(user_vec); dict.insert("users", user_list); - dict.into_spanned_value() + dict.into_tagged_value() } async fn disks(span: Span) -> Value { @@ -96,7 +93,7 @@ async fn disks(span: Span) -> Value { let mut partitions = disk::partitions_physical(); while let Some(part) = partitions.next().await { if let Ok(part) = part { - let mut dict = SpannedDictBuilder::new(span); + let mut dict = TaggedDictBuilder::new(span); dict.insert( "device", Value::string( @@ -113,7 +110,7 @@ async fn disks(span: Span) -> Value { dict.insert("used", Value::bytes(usage.used().get())); dict.insert("free", Value::bytes(usage.free().get())); } - output.push(dict.into_spanned_value()); + output.push(dict.into_tagged_value()); } } @@ -125,9 +122,9 @@ async fn temp(span: Span) -> Value { let system = sysinfo::System::new_with_specifics(RefreshKind::new().with_system()); let components_list = system.get_components_list(); if components_list.len() > 0 { - let mut v: Vec> = vec![]; + let mut v: Vec> = vec![]; for component in components_list { - let mut component_idx = SpannedDictBuilder::new(span); + let mut component_idx = TaggedDictBuilder::new(span); component_idx.insert("name", Primitive::String(component.get_label().to_string())); component_idx.insert( "temp", @@ -148,7 +145,7 @@ async fn temp(span: Span) -> Value { } } -async fn net(span: Span) -> Spanned { +async fn net(span: Span) -> Tagged { use sysinfo::{NetworkExt, RefreshKind, SystemExt}; let system = sysinfo::System::new_with_specifics(RefreshKind::new().with_network()); @@ -156,25 +153,25 @@ async fn net(span: Span) -> Spanned { let incoming = network.get_income(); let outgoing = network.get_outcome(); - let mut network_idx = SpannedDictBuilder::new(span); + let mut network_idx = TaggedDictBuilder::new(span); network_idx.insert("incoming", Value::bytes(incoming)); network_idx.insert("outgoing", Value::bytes(outgoing)); - network_idx.into_spanned_value() + network_idx.into_tagged_value() } -async fn sysinfo(span: Span) -> Vec> { - let mut sysinfo = SpannedDictBuilder::new(span); +async fn sysinfo(span: Span) -> Vec> { + let mut sysinfo = TaggedDictBuilder::new(span); - sysinfo.insert_spanned("host", host(span).await); + sysinfo.insert_tagged("host", host(span).await); if let Some(cpu) = cpu(span).await { - sysinfo.insert_spanned("cpu", cpu); + sysinfo.insert_tagged("cpu", cpu); } sysinfo.insert("disks", disks(span).await); - sysinfo.insert_spanned("mem", mem(span).await); + sysinfo.insert_tagged("mem", mem(span).await); sysinfo.insert("temp", temp(span).await); - sysinfo.insert_spanned("net", net(span).await); + sysinfo.insert_tagged("net", net(span).await); - vec![sysinfo.into_spanned_value()] + vec![sysinfo.into_tagged_value()] } impl Plugin for Sys { @@ -197,7 +194,7 @@ impl Plugin for Sys { .collect()) } - fn filter(&mut self, _: Spanned) -> Result, ShellError> { + fn filter(&mut self, _: Tagged) -> Result, ShellError> { Ok(vec![]) } } diff --git a/src/plugins/textview.rs b/src/plugins/textview.rs index 86a51d716e..79f524f987 100644 --- a/src/plugins/textview.rs +++ b/src/plugins/textview.rs @@ -4,7 +4,7 @@ use crossterm::{cursor, terminal, RawScreen}; use indexmap::IndexMap; use nu::{ serve_plugin, CallInfo, CommandConfig, Plugin, Primitive, ShellError, SourceMap, SpanSource, - Spanned, Value, + Tagged, Value, }; use rawkey::RawKey; @@ -40,7 +40,7 @@ impl Plugin for TextView { }) } - fn sink(&mut self, call_info: CallInfo, input: Vec>) { + fn sink(&mut self, call_info: CallInfo, input: Vec>) { view_text_value(&input[0], &call_info.source_map); } } @@ -209,13 +209,11 @@ fn scroll_view(s: &str) { scroll_view_lines_if_needed(v, false); } -fn view_text_value(value: &Spanned, source_map: &SourceMap) { - match value { - Spanned { - item: Value::Primitive(Primitive::String(s)), - span, - } => { - let source = span.source.map(|x| source_map.get(&x)).flatten(); +fn view_text_value(value: &Tagged, source_map: &SourceMap) { + let value_span = value.span(); + match value.item { + Value::Primitive(Primitive::String(ref s)) => { + let source = value_span.source.map(|x| source_map.get(&x)).flatten(); if let Some(source) = source { let extension: Option = match source { diff --git a/src/plugins/tree.rs b/src/plugins/tree.rs index ddcb535d60..5ea392257e 100644 --- a/src/plugins/tree.rs +++ b/src/plugins/tree.rs @@ -1,6 +1,6 @@ use derive_new::new; use indexmap::IndexMap; -use nu::{serve_plugin, CallInfo, CommandConfig, Plugin, ShellError, Spanned, Value}; +use nu::{serve_plugin, CallInfo, CommandConfig, Plugin, ShellError, Tagged, Value}; use ptree::item::StringItem; use ptree::output::print_tree_with; use ptree::print_config::PrintConfig; @@ -91,7 +91,7 @@ impl Plugin for TreeViewer { }) } - fn sink(&mut self, _call_info: CallInfo, input: Vec>) { + fn sink(&mut self, _call_info: CallInfo, input: Vec>) { if input.len() > 0 { for i in input.iter() { let view = TreeView::from_value(&i); diff --git a/src/prelude.rs b/src/prelude.rs index 509a0866ac..d1110050db 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -40,10 +40,11 @@ crate use crate::context::Context; crate use crate::env::host::handle_unexpected; crate use crate::env::{Environment, Host}; crate use crate::errors::ShellError; +crate use crate::object::meta::{Tag, Tagged, TaggedItem}; crate use crate::object::types::ExtractType; crate use crate::object::{Primitive, Value}; -crate use crate::parser::{Span, Spanned, SpannedItem}; crate use crate::stream::{InputStream, OutputStream}; +crate use crate::Span; crate use crate::Text; crate use futures::stream::BoxStream; crate use futures::Stream; @@ -58,7 +59,7 @@ pub trait FromInputStream { impl FromInputStream for T where - T: Stream> + Send + 'static, + T: Stream> + Send + 'static, { fn from_input_stream(self) -> OutputStream { OutputStream { diff --git a/src/shell/helper.rs b/src/shell/helper.rs index 809b8c0655..486ee2581b 100644 --- a/src/shell/helper.rs +++ b/src/shell/helper.rs @@ -1,10 +1,10 @@ use crate::parser::nom_input; -use crate::parser::parse::span::Spanned; use crate::parser::parse::token_tree::TokenNode; use crate::parser::parse::tokens::RawToken; use crate::parser::{Pipeline, PipelineElement}; use crate::prelude::*; use crate::shell::completer::NuCompleter; +use crate::Tagged; use ansi_term::Color; use rustyline::completion::{self, Completer, FilenameCompleter}; use rustyline::error::ReadlineError; @@ -107,23 +107,23 @@ fn paint_token_node(token_node: &TokenNode, line: &str) -> String { TokenNode::Delimited(..) => Color::White.paint(token_node.span().slice(line)), TokenNode::Operator(..) => Color::White.normal().paint(token_node.span().slice(line)), TokenNode::Pipeline(..) => Color::Blue.normal().paint(token_node.span().slice(line)), - TokenNode::Token(Spanned { + TokenNode::Token(Tagged { item: RawToken::Integer(..), .. }) => Color::Purple.bold().paint(token_node.span().slice(line)), - TokenNode::Token(Spanned { + TokenNode::Token(Tagged { item: RawToken::Size(..), .. }) => Color::Purple.bold().paint(token_node.span().slice(line)), - TokenNode::Token(Spanned { + TokenNode::Token(Tagged { item: RawToken::String(..), .. }) => Color::Green.normal().paint(token_node.span().slice(line)), - TokenNode::Token(Spanned { + TokenNode::Token(Tagged { item: RawToken::Variable(..), .. }) => Color::Yellow.bold().paint(token_node.span().slice(line)), - TokenNode::Token(Spanned { + TokenNode::Token(Tagged { item: RawToken::Bare, .. }) => Color::Green.normal().paint(token_node.span().slice(line)), diff --git a/src/stream.rs b/src/stream.rs index a4466a6ea2..b4a8730478 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -1,37 +1,37 @@ use crate::prelude::*; pub struct InputStream { - crate values: BoxStream<'static, Spanned>, + crate values: BoxStream<'static, Tagged>, } impl InputStream { - pub fn into_vec(self) -> impl Future>> { + pub fn into_vec(self) -> impl Future>> { self.values.collect() } - pub fn from_stream(input: impl Stream> + Send + 'static) -> InputStream { + pub fn from_stream(input: impl Stream> + Send + 'static) -> InputStream { InputStream { values: input.boxed(), } } } -impl From>> for InputStream { - fn from(input: BoxStream<'static, Spanned>) -> InputStream { +impl From>> for InputStream { + fn from(input: BoxStream<'static, Tagged>) -> InputStream { InputStream { values: input } } } -impl From>> for InputStream { - fn from(input: VecDeque>) -> InputStream { +impl From>> for InputStream { + fn from(input: VecDeque>) -> InputStream { InputStream { values: input.boxed(), } } } -impl From>> for InputStream { - fn from(input: Vec>) -> InputStream { +impl From>> for InputStream { + fn from(input: Vec>) -> InputStream { let mut list = VecDeque::default(); list.extend(input); @@ -52,7 +52,7 @@ impl OutputStream { v.into() } - pub fn from_input(input: impl Stream> + Send + 'static) -> OutputStream { + pub fn from_input(input: impl Stream> + Send + 'static) -> OutputStream { OutputStream { values: input.map(ReturnSuccess::value).boxed(), } @@ -67,8 +67,8 @@ impl From for OutputStream { } } -impl From>> for OutputStream { - fn from(input: BoxStream<'static, Spanned>) -> OutputStream { +impl From>> for OutputStream { + fn from(input: BoxStream<'static, Tagged>) -> OutputStream { OutputStream { values: input.map(ReturnSuccess::value).boxed(), } @@ -89,8 +89,8 @@ impl From> for OutputStream { } } -impl From>> for OutputStream { - fn from(input: VecDeque>) -> OutputStream { +impl From>> for OutputStream { + fn from(input: VecDeque>) -> OutputStream { OutputStream { values: input .into_iter() @@ -112,8 +112,8 @@ impl From> for OutputStream { } } -impl From>> for OutputStream { - fn from(input: Vec>) -> OutputStream { +impl From>> for OutputStream { + fn from(input: Vec>) -> OutputStream { let mut list = VecDeque::default(); list.extend(input.into_iter().map(ReturnSuccess::value));