mirror of
https://github.com/nushell/nushell.git
synced 2025-08-13 10:37:40 +02:00
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`
This commit is contained in:
@ -1,10 +1,13 @@
|
||||
use itertools::Itertools;
|
||||
use nu::{
|
||||
serve_plugin, CallInfo, Plugin, Primitive, ReturnSuccess, ReturnValue, ShellError, Signature,
|
||||
SyntaxShape, Tagged, Value,
|
||||
serve_plugin, CallInfo, Plugin, ReturnSuccess, ReturnValue, ShellError, Signature, SyntaxShape,
|
||||
Tagged, Value,
|
||||
};
|
||||
|
||||
pub type ColumnPath = Vec<Tagged<String>>;
|
||||
|
||||
struct Add {
|
||||
field: Option<String>,
|
||||
field: Option<ColumnPath>,
|
||||
value: Option<Value>,
|
||||
}
|
||||
impl Add {
|
||||
@ -19,12 +22,13 @@ impl Add {
|
||||
let value_tag = value.tag();
|
||||
match (value.item, self.value.clone()) {
|
||||
(obj @ Value::Row(_), Some(v)) => match &self.field {
|
||||
Some(f) => match obj.insert_data_at_path(value_tag, &f, v) {
|
||||
Some(f) => match obj.insert_data_at_column_path(value_tag, &f, v) {
|
||||
Some(v) => return Ok(v),
|
||||
None => {
|
||||
return Err(ShellError::string(format!(
|
||||
"add could not find place to insert field {:?} {}",
|
||||
obj, f
|
||||
obj,
|
||||
f.iter().map(|i| &i.item).join(".")
|
||||
)))
|
||||
}
|
||||
},
|
||||
@ -44,7 +48,7 @@ impl Plugin for Add {
|
||||
fn config(&mut self) -> Result<Signature, ShellError> {
|
||||
Ok(Signature::build("add")
|
||||
.desc("Add a new field to the table.")
|
||||
.required("Field", SyntaxShape::String)
|
||||
.required("Field", SyntaxShape::ColumnPath)
|
||||
.required("Value", SyntaxShape::String)
|
||||
.rest(SyntaxShape::String)
|
||||
.filter())
|
||||
@ -53,12 +57,13 @@ 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] {
|
||||
Tagged {
|
||||
item: Value::Primitive(Primitive::String(s)),
|
||||
table @ Tagged {
|
||||
item: Value::Table(_),
|
||||
..
|
||||
} => {
|
||||
self.field = Some(s.clone());
|
||||
self.field = Some(table.as_column_path()?.item);
|
||||
}
|
||||
|
||||
_ => {
|
||||
return Err(ShellError::string(format!(
|
||||
"Unrecognized type in params: {:?}",
|
||||
|
@ -1,10 +1,12 @@
|
||||
use nu::{
|
||||
serve_plugin, CallInfo, Plugin, Primitive, ReturnSuccess, ReturnValue, ShellError, Signature,
|
||||
SyntaxShape, Tagged, Value,
|
||||
serve_plugin, CallInfo, Plugin, ReturnSuccess, ReturnValue, ShellError, Signature, SyntaxShape,
|
||||
Tagged, Value,
|
||||
};
|
||||
|
||||
pub type ColumnPath = Vec<Tagged<String>>;
|
||||
|
||||
struct Edit {
|
||||
field: Option<String>,
|
||||
field: Option<ColumnPath>,
|
||||
value: Option<Value>,
|
||||
}
|
||||
impl Edit {
|
||||
@ -19,7 +21,7 @@ impl Edit {
|
||||
let value_tag = value.tag();
|
||||
match (value.item, self.value.clone()) {
|
||||
(obj @ Value::Row(_), Some(v)) => match &self.field {
|
||||
Some(f) => match obj.replace_data_at_path(value_tag, &f, v) {
|
||||
Some(f) => match obj.replace_data_at_column_path(value_tag, &f, v) {
|
||||
Some(v) => return Ok(v),
|
||||
None => {
|
||||
return Err(ShellError::string(
|
||||
@ -43,7 +45,7 @@ impl Plugin for Edit {
|
||||
fn config(&mut self) -> Result<Signature, ShellError> {
|
||||
Ok(Signature::build("edit")
|
||||
.desc("Edit an existing column to have a new value.")
|
||||
.required("Field", SyntaxShape::String)
|
||||
.required("Field", SyntaxShape::ColumnPath)
|
||||
.required("Value", SyntaxShape::String)
|
||||
.filter())
|
||||
}
|
||||
@ -51,11 +53,11 @@ 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] {
|
||||
Tagged {
|
||||
item: Value::Primitive(Primitive::String(s)),
|
||||
table @ Tagged {
|
||||
item: Value::Table(_),
|
||||
..
|
||||
} => {
|
||||
self.field = Some(s.clone());
|
||||
self.field = Some(table.as_column_path()?.item);
|
||||
}
|
||||
_ => {
|
||||
return Err(ShellError::string(format!(
|
||||
|
@ -14,8 +14,10 @@ pub enum SemVerAction {
|
||||
Patch,
|
||||
}
|
||||
|
||||
pub type ColumnPath = Vec<Tagged<String>>;
|
||||
|
||||
struct Inc {
|
||||
field: Option<String>,
|
||||
field: Option<ColumnPath>,
|
||||
error: Option<String>,
|
||||
action: Option<Action>,
|
||||
}
|
||||
@ -85,16 +87,17 @@ impl Inc {
|
||||
}
|
||||
Value::Row(_) => match self.field {
|
||||
Some(ref f) => {
|
||||
let replacement = match value.item.get_data_by_path(value.tag(), f) {
|
||||
let replacement = match value.item.get_data_by_column_path(value.tag(), f) {
|
||||
Some(result) => self.inc(result.map(|x| x.clone()))?,
|
||||
None => {
|
||||
return Err(ShellError::string("inc could not find field to replace"))
|
||||
}
|
||||
};
|
||||
match value
|
||||
.item
|
||||
.replace_data_at_path(value.tag(), f, replacement.item.clone())
|
||||
{
|
||||
match value.item.replace_data_at_column_path(
|
||||
value.tag(),
|
||||
f,
|
||||
replacement.item.clone(),
|
||||
) {
|
||||
Some(v) => return Ok(v),
|
||||
None => {
|
||||
return Err(ShellError::string("inc could not find field to replace"))
|
||||
@ -120,7 +123,7 @@ impl Plugin for Inc {
|
||||
.switch("major")
|
||||
.switch("minor")
|
||||
.switch("patch")
|
||||
.rest(SyntaxShape::String)
|
||||
.rest(SyntaxShape::ColumnPath)
|
||||
.filter())
|
||||
}
|
||||
|
||||
@ -138,11 +141,11 @@ impl Plugin for Inc {
|
||||
if let Some(args) = call_info.args.positional {
|
||||
for arg in args {
|
||||
match arg {
|
||||
Tagged {
|
||||
item: Value::Primitive(Primitive::String(s)),
|
||||
table @ Tagged {
|
||||
item: Value::Table(_),
|
||||
..
|
||||
} => {
|
||||
self.field = Some(s);
|
||||
self.field = Some(table.as_column_path()?.item);
|
||||
}
|
||||
_ => {
|
||||
return Err(ShellError::string(format!(
|
||||
@ -209,8 +212,13 @@ mod tests {
|
||||
}
|
||||
|
||||
fn with_parameter(&mut self, name: &str) -> &mut Self {
|
||||
let fields: Vec<Tagged<Value>> = name
|
||||
.split(".")
|
||||
.map(|s| Value::string(s.to_string()).tagged(Tag::unknown_span(self.anchor)))
|
||||
.collect();
|
||||
|
||||
self.positionals
|
||||
.push(Value::string(name.to_string()).tagged(Tag::unknown_span(self.anchor)));
|
||||
.push(Value::Table(fields).tagged(Tag::unknown_span(self.anchor)));
|
||||
self
|
||||
}
|
||||
|
||||
@ -297,7 +305,12 @@ mod tests {
|
||||
)
|
||||
.is_ok());
|
||||
|
||||
assert_eq!(plugin.field, Some("package.version".to_string()));
|
||||
assert_eq!(
|
||||
plugin
|
||||
.field
|
||||
.map(|f| f.into_iter().map(|f| f.item).collect()),
|
||||
Some(vec!["package".to_string(), "version".to_string()])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -1,6 +1,6 @@
|
||||
use nu::{
|
||||
serve_plugin, CallInfo, Plugin, Primitive, ReturnSuccess, ReturnValue, ShellError, Signature,
|
||||
SyntaxShape, Tagged, Value,
|
||||
SyntaxShape, Tagged, TaggedItem, Value,
|
||||
};
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
@ -10,8 +10,10 @@ enum Action {
|
||||
ToInteger,
|
||||
}
|
||||
|
||||
pub type ColumnPath = Vec<Tagged<String>>;
|
||||
|
||||
struct Str {
|
||||
field: Option<String>,
|
||||
field: Option<ColumnPath>,
|
||||
params: Option<Vec<String>>,
|
||||
error: Option<String>,
|
||||
action: Option<Action>,
|
||||
@ -43,8 +45,8 @@ impl Str {
|
||||
Ok(applied)
|
||||
}
|
||||
|
||||
fn for_field(&mut self, field: &str) {
|
||||
self.field = Some(String::from(field));
|
||||
fn for_field(&mut self, column_path: ColumnPath) {
|
||||
self.field = Some(column_path);
|
||||
}
|
||||
|
||||
fn permit(&mut self) -> bool {
|
||||
@ -92,14 +94,15 @@ impl Str {
|
||||
}
|
||||
Value::Row(_) => match self.field {
|
||||
Some(ref f) => {
|
||||
let replacement = match value.item.get_data_by_path(value.tag(), f) {
|
||||
let replacement = match value.item.get_data_by_column_path(value.tag(), f) {
|
||||
Some(result) => self.strutils(result.map(|x| x.clone()))?,
|
||||
None => return Ok(Tagged::from_item(Value::nothing(), value.tag)),
|
||||
};
|
||||
match value
|
||||
.item
|
||||
.replace_data_at_path(value.tag(), f, replacement.item.clone())
|
||||
{
|
||||
match value.item.replace_data_at_column_path(
|
||||
value.tag(),
|
||||
f,
|
||||
replacement.item.clone(),
|
||||
) {
|
||||
Some(v) => return Ok(v),
|
||||
None => {
|
||||
return Err(ShellError::string("str could not find field to replace"))
|
||||
@ -127,7 +130,7 @@ impl Plugin for Str {
|
||||
.switch("downcase")
|
||||
.switch("upcase")
|
||||
.switch("to-int")
|
||||
.rest(SyntaxShape::Member)
|
||||
.rest(SyntaxShape::ColumnPath)
|
||||
.filter())
|
||||
}
|
||||
|
||||
@ -148,15 +151,21 @@ impl Plugin for Str {
|
||||
match possible_field {
|
||||
Tagged {
|
||||
item: Value::Primitive(Primitive::String(s)),
|
||||
..
|
||||
tag,
|
||||
} => match self.action {
|
||||
Some(Action::Downcase)
|
||||
| Some(Action::Upcase)
|
||||
| Some(Action::ToInteger)
|
||||
| None => {
|
||||
self.for_field(&s);
|
||||
self.for_field(vec![s.clone().tagged(tag)]);
|
||||
}
|
||||
},
|
||||
table @ Tagged {
|
||||
item: Value::Table(_),
|
||||
..
|
||||
} => {
|
||||
self.field = Some(table.as_column_path()?.item);
|
||||
}
|
||||
_ => {
|
||||
return Err(ShellError::string(format!(
|
||||
"Unrecognized type in params: {:?}",
|
||||
@ -227,8 +236,13 @@ mod tests {
|
||||
}
|
||||
|
||||
fn with_parameter(&mut self, name: &str) -> &mut Self {
|
||||
let fields: Vec<Tagged<Value>> = name
|
||||
.split(".")
|
||||
.map(|s| Value::string(s.to_string()).tagged(Tag::unknown_span(self.anchor)))
|
||||
.collect();
|
||||
|
||||
self.positionals
|
||||
.push(Value::string(name.to_string()).tagged(Tag::unknown()));
|
||||
.push(Value::Table(fields).tagged(Tag::unknown_span(self.anchor)));
|
||||
self
|
||||
}
|
||||
|
||||
@ -303,7 +317,12 @@ mod tests {
|
||||
)
|
||||
.is_ok());
|
||||
|
||||
assert_eq!(plugin.field, Some("package.description".to_string()));
|
||||
assert_eq!(
|
||||
plugin
|
||||
.field
|
||||
.map(|f| f.into_iter().map(|f| f.item).collect()),
|
||||
Some(vec!["package".to_string(), "description".to_string()])
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
Reference in New Issue
Block a user