2019-08-15 07:02:02 +02:00
|
|
|
use crate::commands::WholeStreamCommand;
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
use crate::data::base::property_get::get_data_by_column_path;
|
2019-11-04 16:47:03 +01:00
|
|
|
use crate::data::base::shape::Shapes;
|
2019-05-29 06:02:36 +02:00
|
|
|
use crate::prelude::*;
|
2019-11-04 16:47:03 +01:00
|
|
|
use futures_util::pin_mut;
|
Overhaul the expansion system
The main thrust of this (very large) commit is an overhaul of the
expansion system.
The parsing pipeline is:
- Lightly parse the source file for atoms, basic delimiters and pipeline
structure into a token tree
- Expand the token tree into a HIR (high-level intermediate
representation) based upon the baseline syntax rules for expressions
and the syntactic shape of commands.
Somewhat non-traditionally, nu doesn't have an AST at all. It goes
directly from the token tree, which doesn't represent many important
distinctions (like the difference between `hello` and `5KB`) directly
into a high-level representation that doesn't have a direct
correspondence to the source code.
At a high level, nu commands work like macros, in the sense that the
syntactic shape of the invocation of a command depends on the
definition of a command.
However, commands do not have the ability to perform unrestricted
expansions of the token tree. Instead, they describe their arguments in
terms of syntactic shapes, and the expander expands the token tree into
HIR based upon that definition.
For example, the `where` command says that it takes a block as its first
required argument, and the description of the block syntactic shape
expands the syntax `cpu > 10` into HIR that represents
`{ $it.cpu > 10 }`.
This commit overhauls that system so that the syntactic shapes are
described in terms of a few new traits (`ExpandSyntax` and
`ExpandExpression` are the primary ones) that are more composable than
the previous system.
The first big win of this new system is the addition of the `ColumnPath`
shape, which looks like `cpu."max ghz"` or `package.version`.
Previously, while a variable path could look like `$it.cpu."max ghz"`,
the tail of a variable path could not be easily reused in other
contexts. Now, that tail is its own syntactic shape, and it can be used
as part of a command's signature.
This cleans up commands like `inc`, `add` and `edit` as well as
shorthand blocks, which can now look like `| where cpu."max ghz" > 10`
2019-09-18 00:26:27 +02:00
|
|
|
use log::trace;
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
use nu_errors::ShellError;
|
|
|
|
use nu_protocol::{
|
2019-12-09 19:39:51 +01:00
|
|
|
did_you_mean, ColumnPath, ReturnSuccess, ReturnValue, Signature, SyntaxShape, UntaggedValue,
|
|
|
|
Value,
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
};
|
2019-11-21 15:33:14 +01:00
|
|
|
use nu_source::{span_for_spanned_list, PrettyDebug};
|
2019-05-29 06:02:36 +02:00
|
|
|
|
2019-08-02 21:15:07 +02:00
|
|
|
pub struct Get;
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct GetArgs {
|
Overhaul the expansion system
The main thrust of this (very large) commit is an overhaul of the
expansion system.
The parsing pipeline is:
- Lightly parse the source file for atoms, basic delimiters and pipeline
structure into a token tree
- Expand the token tree into a HIR (high-level intermediate
representation) based upon the baseline syntax rules for expressions
and the syntactic shape of commands.
Somewhat non-traditionally, nu doesn't have an AST at all. It goes
directly from the token tree, which doesn't represent many important
distinctions (like the difference between `hello` and `5KB`) directly
into a high-level representation that doesn't have a direct
correspondence to the source code.
At a high level, nu commands work like macros, in the sense that the
syntactic shape of the invocation of a command depends on the
definition of a command.
However, commands do not have the ability to perform unrestricted
expansions of the token tree. Instead, they describe their arguments in
terms of syntactic shapes, and the expander expands the token tree into
HIR based upon that definition.
For example, the `where` command says that it takes a block as its first
required argument, and the description of the block syntactic shape
expands the syntax `cpu > 10` into HIR that represents
`{ $it.cpu > 10 }`.
This commit overhauls that system so that the syntactic shapes are
described in terms of a few new traits (`ExpandSyntax` and
`ExpandExpression` are the primary ones) that are more composable than
the previous system.
The first big win of this new system is the addition of the `ColumnPath`
shape, which looks like `cpu."max ghz"` or `package.version`.
Previously, while a variable path could look like `$it.cpu."max ghz"`,
the tail of a variable path could not be easily reused in other
contexts. Now, that tail is its own syntactic shape, and it can be used
as part of a command's signature.
This cleans up commands like `inc`, `add` and `edit` as well as
shorthand blocks, which can now look like `| where cpu."max ghz" > 10`
2019-09-18 00:26:27 +02:00
|
|
|
rest: Vec<ColumnPath>,
|
2019-08-02 21:15:07 +02:00
|
|
|
}
|
|
|
|
|
2019-08-15 07:02:02 +02:00
|
|
|
impl WholeStreamCommand for Get {
|
2019-08-02 21:15:07 +02:00
|
|
|
fn name(&self) -> &str {
|
|
|
|
"get"
|
|
|
|
}
|
|
|
|
|
2019-08-30 00:52:32 +02:00
|
|
|
fn signature(&self) -> Signature {
|
2019-11-04 16:47:03 +01:00
|
|
|
Signature::build("get").rest(
|
|
|
|
SyntaxShape::ColumnPath,
|
|
|
|
"optionally return additional data by path",
|
|
|
|
)
|
2019-08-30 00:52:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Open given cells as text."
|
|
|
|
}
|
|
|
|
|
2019-08-02 21:15:07 +02:00
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
args.process(registry, get)?.run()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
pub fn get_column_path(path: &ColumnPath, obj: &Value) -> Result<Value, ShellError> {
|
2019-10-30 23:46:40 +01:00
|
|
|
let fields = path.clone();
|
|
|
|
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
get_data_by_column_path(
|
|
|
|
obj,
|
2019-10-30 23:46:40 +01:00
|
|
|
path,
|
2019-11-04 16:47:03 +01:00
|
|
|
Box::new(move |(obj_source, column_path_tried, error)| {
|
2019-12-07 10:34:32 +01:00
|
|
|
if let UntaggedValue::Table(rows) = &obj_source.value {
|
|
|
|
let total = rows.len();
|
|
|
|
let end_tag = match fields
|
|
|
|
.members()
|
|
|
|
.iter()
|
|
|
|
.nth_back(if fields.members().len() > 2 { 1 } else { 0 })
|
|
|
|
{
|
|
|
|
Some(last_field) => last_field.span,
|
|
|
|
None => column_path_tried.span,
|
|
|
|
};
|
2019-10-31 10:36:08 +01:00
|
|
|
|
2019-12-07 10:34:32 +01:00
|
|
|
let primary_label = format!(
|
|
|
|
"There isn't a row indexed at {}",
|
|
|
|
column_path_tried.display()
|
|
|
|
);
|
|
|
|
|
|
|
|
let secondary_label = if total == 1 {
|
|
|
|
"The table only has 1 row".to_owned()
|
|
|
|
} else {
|
|
|
|
format!("The table only has {} rows (0 to {})", total, total - 1)
|
|
|
|
};
|
|
|
|
|
|
|
|
return ShellError::labeled_error_with_secondary(
|
|
|
|
"Row not found",
|
|
|
|
primary_label,
|
|
|
|
column_path_tried.span,
|
|
|
|
secondary_label,
|
|
|
|
end_tag,
|
|
|
|
);
|
2019-10-31 10:36:08 +01:00
|
|
|
}
|
|
|
|
|
2019-12-07 10:34:32 +01:00
|
|
|
if let Some(suggestions) = did_you_mean(&obj_source, column_path_tried) {
|
|
|
|
return ShellError::labeled_error(
|
|
|
|
"Unknown column",
|
|
|
|
format!("did you mean '{}'?", suggestions[0].1),
|
|
|
|
span_for_spanned_list(fields.members().iter().map(|p| p.span)),
|
|
|
|
);
|
2019-05-31 21:15:29 +02:00
|
|
|
}
|
2019-11-04 16:47:03 +01:00
|
|
|
|
2019-12-06 16:28:26 +01:00
|
|
|
error
|
2019-10-30 23:46:40 +01:00
|
|
|
}),
|
2019-11-21 15:33:14 +01:00
|
|
|
)
|
2019-05-31 21:15:29 +02:00
|
|
|
}
|
|
|
|
|
2019-08-02 21:15:07 +02:00
|
|
|
pub fn get(
|
2019-11-04 16:47:03 +01:00
|
|
|
GetArgs { rest: mut fields }: GetArgs,
|
2019-08-02 21:15:07 +02:00
|
|
|
RunnableContext { input, .. }: RunnableContext,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
2019-12-06 16:28:26 +01:00
|
|
|
if fields.is_empty() {
|
2019-11-04 16:47:03 +01:00
|
|
|
let stream = async_stream! {
|
|
|
|
let values = input.values;
|
|
|
|
pin_mut!(values);
|
|
|
|
|
|
|
|
let mut shapes = Shapes::new();
|
|
|
|
let mut index = 0;
|
|
|
|
|
|
|
|
while let Some(row) = values.next().await {
|
2019-11-21 15:33:14 +01:00
|
|
|
shapes.add(&row, index);
|
2019-11-04 16:47:03 +01:00
|
|
|
index += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for row in shapes.to_values() {
|
|
|
|
yield ReturnSuccess::value(row);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let stream: BoxStream<'static, ReturnValue> = stream.boxed();
|
|
|
|
|
|
|
|
Ok(stream.to_output_stream())
|
|
|
|
} else {
|
|
|
|
let member = fields.remove(0);
|
|
|
|
trace!("get {:?} {:?}", member, fields);
|
|
|
|
let stream = input
|
|
|
|
.values
|
|
|
|
.map(move |item| {
|
|
|
|
let mut result = VecDeque::new();
|
|
|
|
|
|
|
|
let member = vec![member.clone()];
|
|
|
|
|
|
|
|
let column_paths = vec![&member, &fields]
|
|
|
|
.into_iter()
|
|
|
|
.flatten()
|
|
|
|
.collect::<Vec<&ColumnPath>>();
|
|
|
|
|
|
|
|
for path in column_paths {
|
|
|
|
let res = get_column_path(&path, &item);
|
|
|
|
|
|
|
|
match res {
|
|
|
|
Ok(got) => match got {
|
2019-11-21 15:33:14 +01:00
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Table(rows),
|
2019-11-04 16:47:03 +01:00
|
|
|
..
|
|
|
|
} => {
|
|
|
|
for item in rows {
|
|
|
|
result.push_back(ReturnSuccess::value(item.clone()));
|
|
|
|
}
|
2019-10-30 23:46:40 +01:00
|
|
|
}
|
2019-11-04 16:47:03 +01:00
|
|
|
other => result.push_back(ReturnSuccess::value(other.clone())),
|
|
|
|
},
|
2019-11-21 15:33:14 +01:00
|
|
|
Err(reason) => result.push_back(ReturnSuccess::value(
|
|
|
|
UntaggedValue::Error(reason).into_untagged_value(),
|
|
|
|
)),
|
2019-11-04 16:47:03 +01:00
|
|
|
}
|
2019-05-29 06:02:36 +02:00
|
|
|
}
|
|
|
|
|
2019-11-04 16:47:03 +01:00
|
|
|
result
|
|
|
|
})
|
|
|
|
.flatten();
|
|
|
|
|
|
|
|
Ok(stream.to_output_stream())
|
|
|
|
}
|
2019-05-29 06:02:36 +02:00
|
|
|
}
|