initial change to Tagged<Value>

This commit is contained in:
Jonathan Turner 2019-08-01 13:58:42 +12:00
parent c61a1108ff
commit 462f783fac
78 changed files with 713 additions and 1040 deletions

View File

@ -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<String, ReadlineError>, ctx: &mut Context
}
(Some(ClassifiedCommand::Sink(left)), None) => {
let input_vec: Vec<Spanned<Value>> = input.objects.into_vec().await;
let input_vec: Vec<Tagged<Value>> = 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<Spanned<String>> = match call.children() {
let arg_list_strings: Vec<Tagged<String>> = 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(),
)),

View File

@ -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<Spanned<Value>>) -> bool {
fn equal_shapes(input: &Vec<Tagged<Value>>) -> bool {
let mut items = input.iter();
let item = match items.next() {
@ -47,11 +47,11 @@ fn equal_shapes(input: &Vec<Spanned<Value>>) -> bool {
true
}
fn is_single_text_value(input: &Vec<Spanned<Value>>) -> bool {
fn is_single_text_value(input: &Vec<Tagged<Value>>) -> bool {
if input.len() != 1 {
return false;
}
if let Spanned {
if let Tagged {
item: Value::Primitive(Primitive::String(_)),
..
} = input[0]

View File

@ -25,7 +25,7 @@ pub fn cd(args: CommandArgs) -> Result<OutputStream, ShellError> {
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<OutputStream, ShellError> {
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"));

View File

@ -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<Spanned<Value>>,
) -> Result<(), ShellError> {
crate fn run(self, context: &mut Context, input: Vec<Tagged<Value>>) -> 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<Span>,
crate args: Vec<Spanned<String>>,
crate args: Vec<Tagged<String>>,
}
crate enum StreamNext {
@ -190,7 +186,7 @@ impl ExternalCommand {
stream_next: StreamNext,
) -> Result<ClassifiedInputStream, ShellError> {
let stdin = input.stdin;
let inputs: Vec<Spanned<Value>> = input.objects.into_vec().await;
let inputs: Vec<Tagged<Value>> = 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<Value>>
stream.boxed() as BoxStream<'static, Tagged<Value>>
))
}
}

View File

@ -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<Value>> {
pub fn nth(&self, pos: usize) -> Option<&Tagged<Value>> {
self.call_info.args.nth(pos)
}
pub fn positional_iter(&self) -> impl Iterator<Item = &Spanned<Value>> {
pub fn positional_iter(&self) -> impl Iterator<Item = &Tagged<Value>> {
self.call_info.args.positional_iter()
}
pub fn expect_nth(&self, pos: usize) -> Result<&Spanned<Value>, ShellError> {
pub fn expect_nth(&self, pos: usize) -> Result<&Tagged<Value>, 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<Value>> {
pub fn get(&self, name: &str) -> Option<&Tagged<Value>> {
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<Spanned<Value>>,
pub input: Vec<Tagged<Value>>,
}
#[derive(Debug, Serialize, Deserialize)]
@ -70,14 +67,14 @@ pub enum CommandAction {
#[derive(Debug, Serialize, Deserialize)]
pub enum ReturnSuccess {
Value(Spanned<Value>),
Value(Tagged<Value>),
Action(CommandAction),
}
pub type ReturnValue = Result<ReturnSuccess, ShellError>;
impl From<Spanned<Value>> for ReturnValue {
fn from(input: Spanned<Value>) -> ReturnValue {
impl From<Tagged<Value>> for ReturnValue {
fn from(input: Tagged<Value>) -> ReturnValue {
Ok(ReturnSuccess::Value(input))
}
}
@ -87,7 +84,7 @@ impl ReturnSuccess {
Ok(ReturnSuccess::Action(CommandAction::ChangePath(path)))
}
pub fn value(input: impl Into<Spanned<Value>>) -> ReturnValue {
pub fn value(input: impl Into<Tagged<Value>>) -> 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)))
}
}

View File

@ -62,7 +62,7 @@ pub fn config(args: CommandArgs) -> Result<OutputStream, ShellError> {
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<OutputStream, ShellError> {
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<OutputStream, ShellError> {
)));
}
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")))

View File

@ -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<T: TimeZone>(dt: DateTime<T>, span: Span) -> Spanned<Value>
pub fn date_to_value<T: TimeZone>(dt: DateTime<T>, span: Span) -> Tagged<Value>
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<OutputStream, ShellError> {

View File

@ -20,7 +20,7 @@ pub fn first(args: CommandArgs) -> Result<OutputStream, ShellError> {
return Err(ShellError::labeled_error(
"Value is not a number",
"expected integer",
args.expect_nth(0)?.span,
args.expect_nth(0)?.span(),
))
}
};

View File

@ -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<Span>,
) -> Result<Spanned<Value>, Box<dyn std::error::Error>> {
) -> Result<Tagged<Value>, Box<dyn std::error::Error>> {
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<OutputStream, ShellError> {
@ -55,20 +51,25 @@ pub fn from_csv(args: CommandArgs) -> Result<OutputStream, ShellError> {
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())
}

View File

@ -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<String, String>,
span: impl Into<Span>,
) -> Spanned<Value> {
let mut second = SpannedDictBuilder::new(span);
) -> Tagged<Value> {
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<String, HashMap<String, String>>,
span: impl Into<Span>,
) -> Spanned<Value> {
) -> Tagged<Value> {
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<Span>,
) -> Result<Spanned<Value>, Box<dyn std::error::Error>> {
) -> Result<Tagged<Value>, Box<dyn std::error::Error>> {
let v: HashMap<String, HashMap<String, String>> = 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<OutputStream, ShellError> {
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())
}

View File

@ -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<Span>) -> Spanned<Value> {
fn convert_json_value_to_nu_value(v: &serde_hjson::Value, span: impl Into<Span>) -> Tagged<Value> {
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<Span>)
pub fn from_json_string_to_value(
s: String,
span: impl Into<Span>,
) -> serde_hjson::Result<Spanned<Value>> {
) -> serde_hjson::Result<Tagged<Value>> {
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<OutputStream, ShellError> {
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())
}

View File

@ -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<Span>) -> Spanned<Value> {
fn convert_toml_value_to_nu_value(v: &toml::Value, span: impl Into<Span>) -> Tagged<Value> {
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<Span>) -> Spa
pub fn from_toml_string_to_value(
s: String,
span: impl Into<Span>,
) -> Result<Spanned<Value>, Box<dyn std::error::Error>> {
) -> Result<Tagged<Value>, Box<dyn std::error::Error>> {
let v: toml::Value = s.parse::<toml::Value>()?;
Ok(convert_toml_value_to_nu_value(&v, span))
}
@ -46,20 +44,25 @@ pub fn from_toml(args: CommandArgs) -> Result<OutputStream, ShellError> {
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())
}

View File

@ -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<Span>,
) -> Spanned<Value> {
fn from_node_to_value<'a, 'd>(n: &roxmltree::Node<'a, 'd>, span: impl Into<Span>) -> Tagged<Value> {
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<Spanned<Value>> = children_values
let children_values: Vec<Tagged<Value>> = 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("<comment>").spanned(span)
Value::string("<comment>").tagged(span)
} else if n.is_pi() {
Value::string("<processing_instruction>").spanned(span)
Value::string("<processing_instruction>").tagged(span)
} else if n.is_text() {
Value::string(n.text().unwrap()).spanned(span)
Value::string(n.text().unwrap()).tagged(span)
} else {
Value::string("<unknown>").spanned(span)
Value::string("<unknown>").tagged(span)
}
}
fn from_document_to_value(d: &roxmltree::Document, span: impl Into<Span>) -> Spanned<Value> {
fn from_document_to_value(d: &roxmltree::Document, span: impl Into<Span>) -> Tagged<Value> {
from_node_to_value(&d.root_element(), span)
}
pub fn from_xml_string_to_value(
s: String,
span: impl Into<Span>,
) -> Result<Spanned<Value>, Box<dyn std::error::Error>> {
) -> Result<Tagged<Value>, Box<dyn std::error::Error>> {
let parsed = roxmltree::Document::parse(&s)?;
Ok(from_document_to_value(&parsed, span))
}
@ -66,7 +63,7 @@ pub fn from_xml(args: CommandArgs) -> Result<OutputStream, ShellError> {
.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",

View File

@ -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<Span>) -> Spanned<Value> {
fn convert_yaml_value_to_nu_value(v: &serde_yaml::Value, span: impl Into<Span>) -> Tagged<Value> {
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<Span>)
pub fn from_yaml_string_to_value(
s: String,
span: impl Into<Span>,
) -> serde_yaml::Result<Spanned<Value>> {
) -> serde_yaml::Result<Tagged<Value>> {
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<OutputStream, ShellError> {
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())
}

View File

@ -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<Value>) -> Result<Spanned<Value>, ShellError> {
fn get_member(path: &str, span: Span, obj: &Tagged<Value>) -> Result<Tagged<Value>, 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<OutputStream, ShellError> {
let fields: Result<Vec<(String, Span)>, _> = 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<OutputStream, ShellError> {
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),
..
}) => {

View File

@ -20,7 +20,7 @@ pub fn lines(args: CommandArgs) -> Result<OutputStream, ShellError> {
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

View File

@ -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<OutputStream, ShellError> {
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<OutputStream, ShellError> {
return Err(ShellError::labeled_error(
e.to_string(),
e.to_string(),
s.span,
s.span(),
));
} else {
return Err(ShellError::maybe_labeled_error(

View File

@ -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<PathBuf>, --raw: Switch,) {
Open as open(args, path: Tagged<PathBuf>, --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<Span>,
) -> Result<Spanned<Value>, ShellError> {
) -> Result<Tagged<Value>, 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)),
}
}

View File

@ -17,7 +17,7 @@ pub fn pick(args: CommandArgs) -> Result<OutputStream, ShellError> {
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())
}

View File

@ -78,11 +78,11 @@ pub fn filter_plugin(path: String, args: CommandArgs) -> Result<OutputStream, Sh
.spawn()
.expect("Failed to spawn child process");
let mut bos: VecDeque<Spanned<Value>> = VecDeque::new();
bos.push_back(Value::Primitive(Primitive::BeginningOfStream).spanned_unknown());
let mut bos: VecDeque<Tagged<Value>> = VecDeque::new();
bos.push_back(Value::Primitive(Primitive::BeginningOfStream).tagged_unknown());
let mut eos: VecDeque<Spanned<Value>> = VecDeque::new();
eos.push_back(Value::Primitive(Primitive::EndOfStream).spanned_unknown());
let mut eos: VecDeque<Tagged<Value>> = 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<OutputStream, Sh
.chain(args.input.values)
.chain(eos)
.map(move |v| match v {
Spanned {
Tagged {
item: Value::Primitive(Primitive::BeginningOfStream),
..
} => {
@ -136,7 +136,7 @@ pub fn filter_plugin(path: String, args: CommandArgs) -> Result<OutputStream, Sh
}
}
}
Spanned {
Tagged {
item: Value::Primitive(Primitive::EndOfStream),
..
} => {

View File

@ -17,9 +17,9 @@ pub fn reject(args: CommandArgs) -> Result<OutputStream, ShellError> {
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())

View File

@ -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,

View File

@ -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<OutputStream, ShellError> {
@ -7,17 +7,17 @@ pub fn size(args: CommandArgs) -> Result<OutputStream, ShellError> {
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<Span>) -> Spanned<Value> {
fn count(contents: &str, span: impl Into<Span>) -> Tagged<Value> {
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<Span>) -> Spanned<Value> {
}
}
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<Span>) -> Spanned<Value> {
dict.insert("chars", Value::int(chars));
dict.insert("max length", Value::int(bytes));
dict.into_spanned_value()
dict.into_tagged_value()
}

View File

@ -12,7 +12,7 @@ pub fn sort_by(args: CommandArgs) -> Result<OutputStream, ShellError> {
fields
.iter()
.map(|f| item.get_data_by_key(f).map(|i| i.clone()))
.collect::<Vec<Option<Spanned<Value>>>>()
.collect::<Vec<Option<Tagged<Value>>>>()
});
vec.into_iter().collect::<VecDeque<_>>()

View File

@ -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<OutputStream, ShellError> {
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<OutputStream, ShellError> {
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(

View File

@ -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<OutputStream, ShellError> {
let positional: Vec<Spanned<Value>> = args.positional_iter().cloned().collect();
let positional: Vec<Tagged<Value>> = 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<OutputStream, ShellError> {
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<OutputStream, ShellError> {
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

View File

@ -5,7 +5,7 @@ pub fn to_array(args: CommandArgs) -> Result<OutputStream, ShellError> {
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())
}

View File

@ -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<String, Box<dyn std::error::Error>> {
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<OutputStream, ShellError> {
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())
}

View File

@ -49,7 +49,7 @@ pub fn to_json(args: CommandArgs) -> Result<OutputStream, ShellError> {
.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",

View File

@ -42,13 +42,13 @@ pub fn to_toml(args: CommandArgs) -> Result<OutputStream, ShellError> {
.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())

View File

@ -47,7 +47,7 @@ pub fn to_yaml(args: CommandArgs) -> Result<OutputStream, ShellError> {
.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",

View File

@ -9,7 +9,7 @@ pub fn trim(args: CommandArgs) -> Result<OutputStream, ShellError> {
.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())
}

View File

@ -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<dyn Sink>,
name_span: Option<Span>,
args: Args,
input: Vec<Spanned<Value>>,
input: Vec<Tagged<Value>>,
) -> Result<(), ShellError> {
let command_args = SinkCommandArgs {
ctx: self.clone(),

View File

@ -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<String>),
Source(Tagged<String>),
Synthetic(String),
}
impl Description {
pub fn from(item: Spanned<impl Into<String>>) -> 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<impl Into<String>>) -> 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<Label<Span>, 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<String>,
actual: Spanned<impl Into<String>>,
actual: Tagged<impl Into<String>>,
) -> ShellError {
ProximateShellError::TypeError {
expected: expected.into(),
@ -75,8 +72,8 @@ impl ShellError {
}
crate fn coerce_error(
left: Spanned<impl Into<String>>,
right: Spanned<impl Into<String>>,
left: Tagged<impl Into<String>>,
right: Tagged<impl Into<String>>,
) -> 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<Option<String>>,
actual: Tagged<Option<String>>,
},
MissingProperty {
subpath: Description,
@ -264,8 +265,8 @@ pub enum ProximateShellError {
},
Diagnostic(ShellDiagnostic),
CoerceError {
left: Spanned<String>,
right: Spanned<String>,
left: Tagged<String>,
right: Tagged<String>,
},
}
impl ProximateShellError {

View File

@ -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<Value>,
it: Tagged<Value>,
#[new(default)]
vars: IndexMap<String, Spanned<Value>>,
vars: IndexMap<String, Tagged<Value>>,
}
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<Spanned<Value>, ShellError> {
) -> Result<Tagged<Value>, 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<hir::Literal>, source: &Text) -> Spanned<Value> {
fn evaluate_literal(literal: Tagged<hir::Literal>, source: &Text) -> Tagged<Value> {
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<Spanned<Value>, ShellError> {
) -> Result<Tagged<Value>, 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))),
}
}

View File

@ -14,7 +14,7 @@ pub struct TableView {
}
impl TableView {
fn merge_descriptors(values: &[Spanned<Value>]) -> Vec<String> {
fn merge_descriptors(values: &[Tagged<Value>]) -> Vec<String> {
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<Value>]) -> Option<TableView> {
pub fn from_list(values: &[Tagged<Value>]) -> Option<TableView> {
if values.len() == 0 {
return None;
}

View File

@ -12,7 +12,7 @@ pub struct VTableView {
}
impl VTableView {
pub fn from_list(values: &[Spanned<Value>]) -> Option<VTableView> {
pub fn from_list(values: &[Tagged<Value>]) -> Option<VTableView> {
if values.len() == 0 {
return None;
}

View File

@ -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};

View File

@ -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;

View File

@ -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<Value>) -> Result<Spanned<Value>, ShellError> {
pub fn invoke(&self, value: &Tagged<Value>) -> Result<Tagged<Value>, 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<u8>),
List(Vec<Spanned<Value>>),
List(Vec<Tagged<Value>>),
#[allow(unused)]
Block(Block),
}
pub fn debug_list(values: &'a Vec<Spanned<Value>>) -> ValuesDebug<'a> {
pub fn debug_list(values: &'a Vec<Tagged<Value>>) -> ValuesDebug<'a> {
ValuesDebug { values }
}
pub struct ValuesDebug<'a> {
values: &'a Vec<Spanned<Value>>,
values: &'a Vec<Tagged<Value>>,
}
impl fmt::Debug for ValuesDebug<'a> {
@ -209,7 +209,7 @@ impl fmt::Debug for ValuesDebug<'a> {
}
pub struct ValueDebug<'a> {
value: &'a Spanned<Value>,
value: &'a Tagged<Value>,
}
impl fmt::Debug for ValueDebug<'a> {
@ -224,17 +224,17 @@ impl fmt::Debug for ValueDebug<'a> {
}
}
impl Spanned<Value> {
crate fn spanned_type_name(&self) -> Spanned<String> {
impl Tagged<Value> {
crate fn tagged_type_name(&self) -> Tagged<String> {
let name = self.type_name();
Spanned::from_item(name, self.span)
Tagged::from_item(name, self.span())
}
}
impl std::convert::TryFrom<&'a Spanned<Value>> for Block {
impl std::convert::TryFrom<&'a Tagged<Value>> for Block {
type Error = ShellError;
fn try_from(value: &'a Spanned<Value>) -> Result<Block, ShellError> {
fn try_from(value: &'a Tagged<Value>) -> Result<Block, ShellError> {
match value.item() {
Value::Block(block) => Ok(block.clone()),
v => Err(ShellError::type_error(
@ -245,10 +245,10 @@ impl std::convert::TryFrom<&'a Spanned<Value>> for Block {
}
}
impl std::convert::TryFrom<&'a Spanned<Value>> for i64 {
impl std::convert::TryFrom<&'a Tagged<Value>> for i64 {
type Error = ShellError;
fn try_from(value: &'a Spanned<Value>) -> Result<i64, ShellError> {
fn try_from(value: &'a Tagged<Value>) -> Result<i64, ShellError> {
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<Option<&'a Spanned<Value>>> for Switch {
impl std::convert::TryFrom<Option<&'a Tagged<Value>>> for Switch {
type Error = ShellError;
fn try_from(value: Option<&'a Spanned<Value>>) -> Result<Switch, ShellError> {
fn try_from(value: Option<&'a Tagged<Value>>) -> Result<Switch, ShellError> {
match value {
None => Ok(Switch::Absent),
Some(value) => match value.item() {
@ -290,7 +290,7 @@ impl std::convert::TryFrom<Option<&'a Spanned<Value>>> for Switch {
}
}
impl Spanned<Value> {
impl Tagged<Value> {
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<Value>> {
crate fn get_data_by_key(&'a self, name: &str) -> Option<&Tagged<Value>> {
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<Value>> {
crate fn get_data_by_index(&'a self, idx: usize) -> Option<&Tagged<Value>> {
match self {
Value::List(l) => l.iter().nth(idx),
_ => None,
}
}
pub fn get_data_by_path(&'a self, span: Span, path: &str) -> Option<Spanned<&Value>> {
pub fn get_data_by_path(&'a self, span: Span, path: &str) -> Option<Tagged<&Value>> {
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<Spanned<Value>> {
) -> Option<Tagged<Value>> {
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<Spanned<Value>> {
) -> Option<Tagged<Value>> {
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<Value>, Spanned<Value>), ShellError> {
crate fn as_pair(&self) -> Result<(Tagged<Value>, Tagged<Value>), 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<Span>) -> Spanned<Value> {
let mut out = SpannedDictBuilder::new(span);
crate fn select_fields(obj: &Value, fields: &[String], span: impl Into<Span>) -> Tagged<Value> {
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<Span>) ->
}
}
out.into_spanned_value()
out.into_tagged_value()
}
crate fn reject_fields(obj: &Value, fields: &[String], span: impl Into<Span>) -> Spanned<Value> {
let mut out = SpannedDictBuilder::new(span);
crate fn reject_fields(obj: &Value, fields: &[String], span: impl Into<Span>) -> Tagged<Value> {
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<Span>) ->
}
}
out.into_spanned_value()
out.into_tagged_value()
}
#[allow(unused)]

View File

@ -16,10 +16,10 @@ const APP_INFO: AppInfo = AppInfo {
#[derive(Deserialize, Serialize)]
struct Config {
#[serde(flatten)]
extra: IndexMap<String, Spanned<Value>>,
extra: IndexMap<String, Tagged<Value>>,
}
crate fn write_config(config: &IndexMap<String, Spanned<Value>>) -> Result<(), ShellError> {
crate fn write_config(config: &IndexMap<String, Tagged<Value>>) -> 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<String, Spanned<Value>>) -> Result<(), S
Ok(())
}
crate fn config(span: impl Into<Span>) -> Result<IndexMap<String, Spanned<Value>>, ShellError> {
crate fn config(span: impl Into<Span>) -> Result<IndexMap<String, Tagged<Value>>, ShellError> {
let span = span.into();
let location = app_root(AppDataType::UserConfig, &APP_INFO)
@ -47,7 +47,7 @@ crate fn config(span: impl Into<Span>) -> Result<IndexMap<String, Spanned<Value>
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)

View File

@ -9,7 +9,7 @@ use std::fmt;
#[derive(Debug, Default, Eq, PartialEq, Serialize, Deserialize, Clone, new)]
pub struct Dictionary {
pub entries: IndexMap<String, Spanned<Value>>,
pub entries: IndexMap<String, Tagged<Value>>,
}
impl PartialOrd for Dictionary {
@ -28,8 +28,8 @@ impl PartialOrd for Dictionary {
}
}
impl From<IndexMap<String, Spanned<Value>>> for Dictionary {
fn from(input: IndexMap<String, Spanned<Value>>) -> Dictionary {
impl From<IndexMap<String, Tagged<Value>>> for Dictionary {
fn from(input: IndexMap<String, Tagged<Value>>) -> 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<Value>> {
crate fn get_data_by_key(&self, name: &str) -> Option<&Tagged<Value>> {
match self
.entries
.iter()
@ -101,72 +101,71 @@ impl Dictionary {
}
}
pub struct SpannedListBuilder {
pub struct TaggedListBuilder {
span: Span,
list: Vec<Spanned<Value>>,
list: Vec<Tagged<Value>>,
}
impl SpannedListBuilder {
pub fn new(span: impl Into<Span>) -> SpannedListBuilder {
SpannedListBuilder {
impl TaggedListBuilder {
pub fn new(span: impl Into<Span>) -> TaggedListBuilder {
TaggedListBuilder {
span: span.into(),
list: vec![],
}
}
pub fn push(&mut self, value: impl Into<Value>) {
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<Spanned<Value>>) {
pub fn insert_tagged(&mut self, value: impl Into<Tagged<Value>>) {
self.list.push(value.into());
}
pub fn into_spanned_value(self) -> Spanned<Value> {
Value::List(self.list).spanned(self.span)
pub fn into_tagged_value(self) -> Tagged<Value> {
Value::List(self.list).tagged(self.span)
}
}
impl From<SpannedListBuilder> for Spanned<Value> {
fn from(input: SpannedListBuilder) -> Spanned<Value> {
input.into_spanned_value()
impl From<TaggedListBuilder> for Tagged<Value> {
fn from(input: TaggedListBuilder) -> Tagged<Value> {
input.into_tagged_value()
}
}
#[derive(Debug)]
pub struct SpannedDictBuilder {
pub struct TaggedDictBuilder {
span: Span,
dict: IndexMap<String, Spanned<Value>>,
dict: IndexMap<String, Tagged<Value>>,
}
impl SpannedDictBuilder {
pub fn new(span: impl Into<Span>) -> SpannedDictBuilder {
SpannedDictBuilder {
impl TaggedDictBuilder {
pub fn new(span: impl Into<Span>) -> TaggedDictBuilder {
TaggedDictBuilder {
span: span.into(),
dict: IndexMap::default(),
}
}
pub fn insert(&mut self, key: impl Into<String>, value: impl Into<Value>) {
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<String>, value: impl Into<Spanned<Value>>) {
pub fn insert_tagged(&mut self, key: impl Into<String>, value: impl Into<Tagged<Value>>) {
self.dict.insert(key.into(), value.into());
}
pub fn into_spanned_value(self) -> Spanned<Value> {
self.into_spanned_dict().map(Value::Object)
pub fn into_tagged_value(self) -> Tagged<Value> {
self.into_tagged_dict().map(Value::Object)
}
pub fn into_spanned_dict(self) -> Spanned<Dictionary> {
Dictionary { entries: self.dict }.spanned(self.span)
pub fn into_tagged_dict(self) -> Tagged<Dictionary> {
Dictionary { entries: self.dict }.tagged(self.span)
}
}
impl From<SpannedDictBuilder> for Spanned<Value> {
fn from(input: SpannedDictBuilder) -> Spanned<Value> {
input.into_spanned_value()
impl From<TaggedDictBuilder> for Tagged<Value> {
fn from(input: TaggedDictBuilder) -> Tagged<Value> {
input.into_tagged_value()
}
}

View File

@ -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<Span>,
) -> Result<Spanned<Value>, ShellError> {
let mut dict = SpannedDictBuilder::new(span);
) -> Result<Tagged<Value>, 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())
}

View File

@ -13,11 +13,10 @@ impl From<String> for Value {
}
}
impl<T: Into<Value>> Spanned<T> {
pub fn into_spanned_value(self) -> Spanned<Value> {
let Spanned { item, span } = self;
let value = item.into();
value.spanned(span)
impl<T: Into<Value>> Tagged<T> {
pub fn into_tagged_value(self) -> Tagged<Value> {
let value_span = self.span();
let value = self.item.into();
value.tagged(value_span)
}
}

View File

@ -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<Span>) -> Spanned<Value> {
let mut dict = SpannedDictBuilder::new(span);
crate fn process_dict(proc: &sysinfo::Process, span: impl Into<Span>) -> Tagged<Value> {
let mut dict = TaggedDictBuilder::new(span);
let cmd = proc.cmd();
@ -25,5 +25,5 @@ crate fn process_dict(proc: &sysinfo::Process, span: impl Into<Span>) -> Spanned
_ => dict.insert("name", cmd_value),
}
dict.into_spanned_value()
dict.into_tagged_value()
}

View File

@ -12,19 +12,19 @@ pub trait Type: std::fmt::Debug + Send {
}
pub trait ExtractType: Sized {
fn extract(value: &Spanned<Value>) -> Result<Self, ShellError>;
fn check(value: &'value Spanned<Value>) -> Result<&'value Spanned<Value>, ShellError>;
fn extract(value: &Tagged<Value>) -> Result<Self, ShellError>;
fn check(value: &'value Tagged<Value>) -> Result<&'value Tagged<Value>, ShellError>;
fn syntax_type() -> hir::SyntaxType {
hir::SyntaxType::Any
}
}
impl<T: ExtractType> ExtractType for Spanned<T> {
fn extract(value: &Spanned<Value>) -> Result<Spanned<T>, ShellError> {
Ok(T::extract(value)?.spanned(value.span))
impl<T: ExtractType> ExtractType for Tagged<T> {
fn extract(value: &Tagged<Value>) -> Result<Tagged<T>, ShellError> {
Ok(T::extract(value)?.tagged(value.span()))
}
fn check(value: &'value Spanned<Value>) -> Result<&'value Spanned<Value>, ShellError> {
fn check(value: &'value Tagged<Value>) -> Result<&'value Tagged<Value>, ShellError> {
T::check(value)
}
@ -37,19 +37,19 @@ impl<T: ExtractType> ExtractType for Spanned<T> {
pub struct Any;
impl Type for Any {
type Extractor = Spanned<Value>;
type Extractor = Tagged<Value>;
fn name(&self) -> &'static str {
"Any"
}
}
impl ExtractType for Spanned<Value> {
fn extract(value: &Spanned<Value>) -> Result<Self, ShellError> {
impl ExtractType for Tagged<Value> {
fn extract(value: &Tagged<Value>) -> Result<Self, ShellError> {
Ok(value.clone())
}
fn check(value: &'value Spanned<Value>) -> Result<&'value Spanned<Value>, ShellError> {
fn check(value: &'value Tagged<Value>) -> Result<&'value Tagged<Value>, ShellError> {
Ok(value)
}
}
@ -59,23 +59,23 @@ impl ExtractType for std::path::PathBuf {
hir::SyntaxType::Path
}
fn extract(value: &'a Spanned<Value>) -> Result<std::path::PathBuf, ShellError> {
fn extract(value: &'a Tagged<Value>) -> Result<std::path::PathBuf, ShellError> {
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<Value>) -> Result<&'value Spanned<Value>, ShellError> {
fn check(value: &'value Tagged<Value>) -> Result<&'value Tagged<Value>, 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<Value>) -> Result<i64, ShellError> {
fn extract(value: &Tagged<Value>) -> Result<i64, ShellError> {
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<Value>) -> Result<&'value Spanned<Value>, ShellError> {
fn check(value: &'value Tagged<Value>) -> Result<&'value Tagged<Value>, 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<Value>) -> Result<String, ShellError> {
fn extract(value: &Tagged<Value>) -> Result<String, ShellError> {
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<Value>) -> Result<&'value Spanned<Value>, ShellError> {
fn check(value: &'value Tagged<Value>) -> Result<&'value Tagged<Value>, 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<Value>) -> Result<&'value Spanned<Value>, ShellError> {
fn check(value: &'value Tagged<Value>) -> Result<&'value Tagged<Value>, 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<Value>) -> Result<value::Block, ShellError> {
fn extract(value: &Tagged<Value>) -> Result<value::Block, ShellError> {
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())),
}
}
}

View File

@ -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};

View File

@ -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<Expression>, tail: Vec<Spanned<impl Into<String>>>) -> Path {
pub fn path(head: impl Into<Expression>, tail: Vec<Tagged<impl Into<String>>>) -> Path {
Path::new(
head.into(),
tail.into_iter()
@ -58,48 +60,48 @@ impl RawExpression {
}
}
pub type Expression = Spanned<RawExpression>;
pub type Expression = Tagged<RawExpression>;
impl Expression {
fn int(i: impl Into<i64>, span: impl Into<Span>) -> 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<i64>, unit: impl Into<Unit>, span: impl Into<Span>) -> Expression {
Spanned::from_item(
Tagged::from_item(
RawExpression::Literal(Literal::Size(i.into(), unit.into())),
span,
)
}
fn string(inner: impl Into<Span>, outer: impl Into<Span>) -> Expression {
Spanned::from_item(
Tagged::from_item(
RawExpression::Literal(Literal::String(inner.into())),
outer.into(),
)
}
fn bare(span: impl Into<Span>) -> Expression {
Spanned::from_item(RawExpression::Literal(Literal::Bare), span.into())
Tagged::from_item(RawExpression::Literal(Literal::Bare), span.into())
}
fn variable(inner: impl Into<Span>, outer: impl Into<Span>) -> Expression {
Spanned::from_item(
Tagged::from_item(
RawExpression::Variable(Variable::Other(inner.into())),
outer.into(),
)
}
fn it_variable(inner: impl Into<Span>, outer: impl Into<Span>) -> Expression {
Spanned::from_item(
Tagged::from_item(
RawExpression::Variable(Variable::It(inner.into())),
outer.into(),
)
}
}
impl From<Spanned<Path>> for Expression {
fn from(path: Spanned<Path>) -> Expression {
impl From<Tagged<Path>> for Expression {
fn from(path: Tagged<Path>) -> Expression {
path.map(|p| RawExpression::Path(Box::new(p)))
}
}

View File

@ -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()),
}
}

View File

@ -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<hir::RawExpression> = match first {
Spanned {
let path: Tagged<hir::RawExpression> = 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<DelimitedNode>,
token: &Tagged<DelimitedNode>,
registry: &dyn CommandRegistry,
source: &Text,
) -> Result<hir::Expression, ShellError> {
@ -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<PathNode>,
token: &Tagged<PathNode>,
registry: &dyn CommandRegistry,
source: &Text,
) -> Result<hir::Expression, ShellError> {
@ -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)]

View File

@ -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<Operator>,
op: Tagged<Operator>,
right: Expression,
}

View File

@ -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;

View File

@ -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<Spanned<String>>,
tail: Vec<Tagged<String>>,
}

View File

@ -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;

View File

@ -1,4 +1,4 @@
use crate::parser::parse::span::Span;
use crate::Span;
use derive_new::new;
use language_reporting::{FileName, Location};

View File

@ -1,4 +1,4 @@
use crate::parser::Span;
use crate::Span;
use derive_new::new;
use getset::Getters;
use serde_derive::{Deserialize, Serialize};

View File

@ -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<NomSpan, Spanned<i64>> {
pub fn raw_integer(input: NomSpan) -> IResult<NomSpan, Tagged<i64>> {
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<NomSpan, Spanned<i64>> {
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<NomSpan, TokenNode> {
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<NomSpan, TokenNode> {
})
}
pub fn raw_unit(input: NomSpan) -> IResult<NomSpan, Spanned<Unit>> {
pub fn raw_unit(input: NomSpan) -> IResult<NomSpan, Tagged<Unit>> {
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<NomSpan, Spanned<Unit>> {
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<NomSpan, TokenNode> {
})
}
pub fn raw_call(input: NomSpan) -> IResult<NomSpan, Spanned<CallNode>> {
pub fn raw_call(input: NomSpan) -> IResult<NomSpan, Tagged<CallNode>> {
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<NomSpan, TokenNode> {
}
fn make_call_list(
head: Option<(Spanned<CallNode>, Option<NomSpan>, Option<NomSpan>)>,
head: Option<(Tagged<CallNode>, Option<NomSpan>, Option<NomSpan>)>,
items: Vec<(
Option<NomSpan>,
Spanned<CallNode>,
Tagged<CallNode>,
Option<NomSpan>,
Option<NomSpan>,
)>,
@ -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<T>(block: CurriedNode<T>) -> T {

View File

@ -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<Span>,
#[get = "crate"]
call: Spanned<CallNode>,
call: Tagged<CallNode>,
pub post_ws: Option<Span>,
pub post_pipe: Option<Span>,
}

View File

@ -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<T> {
pub span: Span,
pub item: T,
}
impl<T> Spanned<T> {
pub fn spanned(self, span: impl Into<Span>) -> Spanned<T> {
Spanned::from_item(self.item, span.into())
}
}
pub trait SpannedItem: Sized {
fn spanned(self, span: impl Into<Span>) -> Spanned<Self> {
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<Self> {
Spanned::from_item(self, (0, 0))
}
}
impl<T> SpannedItem for T {}
impl<T> std::ops::Deref for Spanned<T> {
type Target = T;
fn deref(&self) -> &T {
&self.item
}
}
impl<T> Spanned<T> {
crate fn from_item(item: T, span: impl Into<Span>) -> Spanned<T> {
Spanned {
span: span.into(),
item,
}
}
pub fn map<U>(self, input: impl FnOnce(T) -> U) -> Spanned<U> {
let Spanned { span, item } = self;
let mapped = input(item);
Spanned { span, item: mapped }
}
crate fn copy_span<U>(&self, output: U) -> Spanned<U> {
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<Uuid>,
}
impl From<Option<Span>> for Span {
fn from(input: Option<Span>) -> Span {
match input {
None => Span {
start: 0,
end: 0,
source: None,
},
Some(span) => span,
}
}
}
impl<T> From<&Spanned<T>> for Span {
fn from(input: &Spanned<T>) -> Span {
input.span
}
}
impl From<&Span> for Span {
fn from(input: &Span) -> Span {
*input
}
}
impl From<nom5_locate::LocatedSpan<&str>> for Span {
fn from(input: nom5_locate::LocatedSpan<&str>) -> Span {
Span {
start: input.offset,
end: input.offset + input.fragment.len(),
source: None,
}
}
}
impl<T> From<(nom5_locate::LocatedSpan<T>, nom5_locate::LocatedSpan<T>)> for Span {
fn from(input: (nom5_locate::LocatedSpan<T>, nom5_locate::LocatedSpan<T>)) -> 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<usize>> for Span {
fn from(input: &std::ops::Range<usize>) -> 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
}
}

View File

@ -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<CallNode>),
Delimited(Spanned<DelimitedNode>),
Pipeline(Spanned<Pipeline>),
Operator(Spanned<Operator>),
Flag(Spanned<Flag>),
Call(Tagged<CallNode>),
Delimited(Tagged<DelimitedNode>),
Pipeline(Tagged<Pipeline>),
Operator(Tagged<Operator>),
Flag(Tagged<Flag>),
Member(Span),
Whitespace(Span),
#[allow(unused)]
Error(Spanned<Box<ShellError>>),
Path(Spanned<PathNode>),
Error(Tagged<Box<ShellError>>),
Path(Tagged<PathNode>),
}
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<Spanned<Flag>> {
crate fn as_flag(&self, value: &str, source: &Text) -> Option<Tagged<Flag>> {
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<Pipeline, ShellError> {
match self {
TokenNode::Pipeline(Spanned { item, .. }) => Ok(item.clone()),
TokenNode::Pipeline(Tagged { item, .. }) => Ok(item.clone()),
_ => Err(ShellError::string("unimplemented")),
}
}

View File

@ -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<T> = Box<dyn FnOnce(&mut TokenTreeBuilder) -> T + 'static>;
pub type CurriedToken = Box<dyn FnOnce(&mut TokenTreeBuilder) -> TokenNode + 'static>;
pub type CurriedCall = Box<dyn FnOnce(&mut TokenTreeBuilder) -> Spanned<CallNode> + 'static>;
pub type CurriedCall = Box<dyn FnOnce(&mut TokenTreeBuilder) -> Tagged<CallNode> + 'static>;
#[allow(unused)]
impl TokenTreeBuilder {
@ -92,7 +92,7 @@ impl TokenTreeBuilder {
input: (Vec<PipelineElement>, Option<Span>),
span: impl Into<Span>,
) -> 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<Operator>, span: impl Into<Span>) -> 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<String>) -> CurriedToken {
@ -128,7 +128,7 @@ impl TokenTreeBuilder {
}
pub fn spanned_string(input: impl Into<Span>, span: impl Into<Span>) -> 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<Span>) -> 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<i64>) -> 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>) -> CurriedToken {
@ -206,7 +206,7 @@ impl TokenTreeBuilder {
}
pub fn spanned_path(input: (TokenNode, Vec<TokenNode>), span: impl Into<Span>) -> 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>, span: impl Into<Span>) -> 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>, span: impl Into<Span>) -> 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>, span: impl Into<Span>) -> 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<TokenNode>, span: impl Into<Span>) -> Spanned<CallNode> {
pub fn spanned_call(input: Vec<TokenNode>, span: impl Into<Span>) -> Tagged<CallNode> {
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>) -> CurriedToken {
@ -324,7 +324,7 @@ impl TokenTreeBuilder {
}
pub fn spanned_parens(input: impl Into<Vec<TokenNode>>, span: impl Into<Span>) -> 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<Vec<TokenNode>>, span: impl Into<Span>) -> 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<Vec<TokenNode>>, span: impl Into<Span>) -> TokenNode {
TokenNode::Delimited(Spanned::from_item(
TokenNode::Delimited(Tagged::from_item(
DelimitedNode::new(Delimiter::Brace, input.into()),
span,
))

View File

@ -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<RawToken>;
pub type Token = Tagged<RawToken>;
impl Token {
pub fn debug(&self, source: &'a Text) -> DebugToken<'a> {

View File

@ -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<CallNode>,
call: &Tagged<CallNode>,
source: &Text,
) -> Result<hir::Call, ShellError> {
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<hir::Expression, ShellError> {
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<Flag>), ShellError> {
) -> Result<(usize, Tagged<Flag>), 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<Flag>)>), ShellError> {
) -> Result<(Option<(usize, Tagged<Flag>)>), ShellError> {
let flag = tokens.extract(|t| t.as_flag(name, source));
match flag {

View File

@ -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<Vec<Spanned<Value>>>,
pub named: Option<IndexMap<String, Spanned<Value>>>,
pub positional: Option<Vec<Tagged<Value>>>,
pub named: Option<IndexMap<String, Tagged<Value>>>,
}
#[derive(new)]
pub struct DebugPositional<'a> {
positional: &'a Option<Vec<Spanned<Value>>>,
positional: &'a Option<Vec<Tagged<Value>>>,
}
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<IndexMap<String, Spanned<Value>>>,
named: &'a Option<IndexMap<String, Tagged<Value>>>,
}
impl fmt::Debug for DebugNamed<'a> {
@ -139,14 +139,14 @@ impl Args {
DebugArgs { args: self }
}
pub fn nth(&self, pos: usize) -> Option<&Spanned<Value>> {
pub fn nth(&self, pos: usize) -> Option<&Tagged<Value>> {
match &self.positional {
None => None,
Some(array) => array.iter().nth(pos),
}
}
pub fn expect_nth(&self, pos: usize) -> Result<&Spanned<Value>, ShellError> {
pub fn expect_nth(&self, pos: usize) -> Result<&Tagged<Value>, 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<Value>> {
pub fn get(&self, name: &str) -> Option<&Tagged<Value>> {
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<Value>>),
Array(std::slice::Iter<'a, Tagged<Value>>),
}
impl Iterator for PositionalIter<'a> {
type Item = &'a Spanned<Value>;
type Item = &'a Tagged<Value>;
fn next(&mut self) -> Option<Self::Item> {
match self {
@ -207,7 +207,7 @@ impl Iterator for PositionalIter<'a> {
impl CommandConfig {
crate fn evaluate_args(
&self,
call: &Spanned<CallNode>,
call: &Tagged<CallNode>,
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<Spanned<Value>> = vec![];
// let mut named: IndexMap<String, Value> = IndexMap::default();
// let mut args: Vec<TokenNode> = 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<Vec<Spanned<Value>>, _> = args
// .map(|i| evaluate_baseline_expr(&i, &Scope::empty(), source))
// .collect();
// positional.extend(rest?);
// } else {
// let rest: Vec<TokenNode> = 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<Option<IndexMap<String, Spanned<Value>>>, ShellError> = args
let named: Result<Option<IndexMap<String, Tagged<Value>>>, 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(

View File

@ -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<Value>) -> Result<Vec<ReturnValue>, ShellError> {
fn filter(&mut self, input: Tagged<Value>) -> Result<Vec<ReturnValue>, 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<Spanned<Value>>) {}
fn sink(&mut self, call_info: CallInfo, input: Vec<Tagged<Value>>) {}
fn quit(&mut self) {}
}
@ -138,11 +139,11 @@ pub enum NuCommand {
params: CallInfo,
},
filter {
params: Spanned<Value>,
params: Tagged<Value>,
},
end_filter,
sink {
params: (CallInfo, Vec<Spanned<Value>>),
params: (CallInfo, Vec<Tagged<Value>>),
},
quit,
}

View File

@ -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<Value>) -> Result<Spanned<Value>, ShellError> {
fn add(&self, value: Tagged<Value>) -> Result<Tagged<Value>, 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<Vec<ReturnValue>, 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<Value>) -> Result<Vec<ReturnValue>, ShellError> {
fn filter(&mut self, input: Tagged<Value>) -> Result<Vec<ReturnValue>, ShellError> {
Ok(vec![ReturnSuccess::value(self.add(input)?)])
}
}

View File

@ -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<Spanned<Value>>) {
fn sink(&mut self, call_info: CallInfo, input: Vec<Tagged<Value>>) {
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"));
}
_ => {}

View File

@ -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<Value>) -> Result<Spanned<Value>, ShellError> {
fn edit(&self, value: Tagged<Value>) -> Result<Tagged<Value>, 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<Vec<ReturnValue>, 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<Value>) -> Result<Vec<ReturnValue>, ShellError> {
fn filter(&mut self, input: Tagged<Value>) -> Result<Vec<ReturnValue>, ShellError> {
Ok(vec![ReturnSuccess::value(self.edit(input)?)])
}
}

View File

@ -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>,
value: Tagged<Value>,
field: &Option<String>,
) -> Result<Spanned<Value>, ShellError> {
) -> Result<Tagged<Value>, 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::<u64>() {
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<Value>) -> Result<Vec<ReturnValue>, ShellError> {
fn filter(&mut self, input: Tagged<Value>) -> Result<Vec<ReturnValue>, ShellError> {
Ok(vec![ReturnSuccess::value(self.inc(input, &self.field)?)])
}
}

View File

@ -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<Value>) -> Result<Vec<ReturnValue>, ShellError> {
fn filter(&mut self, input: Tagged<Value>) -> Result<Vec<ReturnValue>, ShellError> {
if self.skip_amount == 0 {
Ok(vec![ReturnSuccess::value(input)])
} else {

View File

@ -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>,
value: Tagged<Value>,
field: &Option<String>,
) -> Result<Spanned<Value>, ShellError> {
) -> Result<Tagged<Value>, 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<Value>) -> Result<Vec<ReturnValue>, ShellError> {
fn filter(&mut self, input: Tagged<Value>) -> Result<Vec<ReturnValue>, ShellError> {
Ok(vec![ReturnSuccess::value(
self.strutils(input, &self.field)?,
)])

View File

@ -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<Spanned<Value>>,
total: Option<Tagged<Value>>,
}
impl Sum {
fn new() -> Sum {
Sum { total: None }
}
fn sum(&mut self, value: Spanned<Value>) -> Result<(), ShellError> {
fn sum(&mut self, value: Tagged<Value>) -> 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<Value>) -> Result<Vec<ReturnValue>, ShellError> {
fn filter(&mut self, input: Tagged<Value>) -> Result<Vec<ReturnValue>, ShellError> {
self.sum(input)?;
Ok(vec![])
}

View File

@ -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<Spanned<Value>> {
async fn cpu(span: Span) -> Option<Tagged<Value>> {
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<Value> {
let mut dict = SpannedDictBuilder::new(span);
async fn mem(span: Span) -> Tagged<Value> {
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<Value> {
dict.insert("swap free", Value::bytes(swap.free().get()));
}
dict.into_spanned_value()
dict.into_tagged_value()
}
async fn host(span: Span) -> Spanned<Value> {
let mut dict = SpannedDictBuilder::new(span);
async fn host(span: Span) -> Tagged<Value> {
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<Value> {
// 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<Value> {
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<Value> {
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<Spanned<Value>> = vec![];
let mut v: Vec<Tagged<Value>> = 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<Value> {
async fn net(span: Span) -> Tagged<Value> {
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<Value> {
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<Spanned<Value>> {
let mut sysinfo = SpannedDictBuilder::new(span);
async fn sysinfo(span: Span) -> Vec<Tagged<Value>> {
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<Value>) -> Result<Vec<ReturnValue>, ShellError> {
fn filter(&mut self, _: Tagged<Value>) -> Result<Vec<ReturnValue>, ShellError> {
Ok(vec![])
}
}

View File

@ -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<Spanned<Value>>) {
fn sink(&mut self, call_info: CallInfo, input: Vec<Tagged<Value>>) {
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<Value>, 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<Value>, 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<String> = match source {

View File

@ -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<Spanned<Value>>) {
fn sink(&mut self, _call_info: CallInfo, input: Vec<Tagged<Value>>) {
if input.len() > 0 {
for i in input.iter() {
let view = TreeView::from_value(&i);

View File

@ -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<T> FromInputStream for T
where
T: Stream<Item = Spanned<Value>> + Send + 'static,
T: Stream<Item = Tagged<Value>> + Send + 'static,
{
fn from_input_stream(self) -> OutputStream {
OutputStream {

View File

@ -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)),

View File

@ -1,37 +1,37 @@
use crate::prelude::*;
pub struct InputStream {
crate values: BoxStream<'static, Spanned<Value>>,
crate values: BoxStream<'static, Tagged<Value>>,
}
impl InputStream {
pub fn into_vec(self) -> impl Future<Output = Vec<Spanned<Value>>> {
pub fn into_vec(self) -> impl Future<Output = Vec<Tagged<Value>>> {
self.values.collect()
}
pub fn from_stream(input: impl Stream<Item = Spanned<Value>> + Send + 'static) -> InputStream {
pub fn from_stream(input: impl Stream<Item = Tagged<Value>> + Send + 'static) -> InputStream {
InputStream {
values: input.boxed(),
}
}
}
impl From<BoxStream<'static, Spanned<Value>>> for InputStream {
fn from(input: BoxStream<'static, Spanned<Value>>) -> InputStream {
impl From<BoxStream<'static, Tagged<Value>>> for InputStream {
fn from(input: BoxStream<'static, Tagged<Value>>) -> InputStream {
InputStream { values: input }
}
}
impl From<VecDeque<Spanned<Value>>> for InputStream {
fn from(input: VecDeque<Spanned<Value>>) -> InputStream {
impl From<VecDeque<Tagged<Value>>> for InputStream {
fn from(input: VecDeque<Tagged<Value>>) -> InputStream {
InputStream {
values: input.boxed(),
}
}
}
impl From<Vec<Spanned<Value>>> for InputStream {
fn from(input: Vec<Spanned<Value>>) -> InputStream {
impl From<Vec<Tagged<Value>>> for InputStream {
fn from(input: Vec<Tagged<Value>>) -> InputStream {
let mut list = VecDeque::default();
list.extend(input);
@ -52,7 +52,7 @@ impl OutputStream {
v.into()
}
pub fn from_input(input: impl Stream<Item = Spanned<Value>> + Send + 'static) -> OutputStream {
pub fn from_input(input: impl Stream<Item = Tagged<Value>> + Send + 'static) -> OutputStream {
OutputStream {
values: input.map(ReturnSuccess::value).boxed(),
}
@ -67,8 +67,8 @@ impl From<InputStream> for OutputStream {
}
}
impl From<BoxStream<'static, Spanned<Value>>> for OutputStream {
fn from(input: BoxStream<'static, Spanned<Value>>) -> OutputStream {
impl From<BoxStream<'static, Tagged<Value>>> for OutputStream {
fn from(input: BoxStream<'static, Tagged<Value>>) -> OutputStream {
OutputStream {
values: input.map(ReturnSuccess::value).boxed(),
}
@ -89,8 +89,8 @@ impl From<VecDeque<ReturnValue>> for OutputStream {
}
}
impl From<VecDeque<Spanned<Value>>> for OutputStream {
fn from(input: VecDeque<Spanned<Value>>) -> OutputStream {
impl From<VecDeque<Tagged<Value>>> for OutputStream {
fn from(input: VecDeque<Tagged<Value>>) -> OutputStream {
OutputStream {
values: input
.into_iter()
@ -112,8 +112,8 @@ impl From<Vec<ReturnValue>> for OutputStream {
}
}
impl From<Vec<Spanned<Value>>> for OutputStream {
fn from(input: Vec<Spanned<Value>>) -> OutputStream {
impl From<Vec<Tagged<Value>>> for OutputStream {
fn from(input: Vec<Tagged<Value>>) -> OutputStream {
let mut list = VecDeque::default();
list.extend(input.into_iter().map(ReturnSuccess::value));