2019-08-02 19:15:07 +00: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 02:30:48 +00:00
|
|
|
use nu_errors::{CoerceInto, ShellError};
|
2019-12-04 21:14:52 +00:00
|
|
|
use nu_protocol::{
|
2020-04-20 06:41:51 +00:00
|
|
|
hir::Block, CallInfo, ColumnPath, Primitive, RangeInclusion, ShellTypeName, UntaggedValue,
|
2020-04-13 07:59:57 +00:00
|
|
|
Value,
|
2019-12-04 21:14:52 +00:00
|
|
|
};
|
|
|
|
use nu_source::{HasSpan, Spanned, SpannedItem, Tagged, TaggedItem};
|
2019-12-09 18:52:01 +00:00
|
|
|
use nu_value_ext::ValueExt;
|
2019-09-01 23:02:23 +00:00
|
|
|
use serde::de;
|
2019-12-04 21:14:52 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2019-09-03 00:10:48 +00:00
|
|
|
use std::path::PathBuf;
|
2019-08-02 19:15:07 +00:00
|
|
|
|
2019-12-04 21:14:52 +00:00
|
|
|
#[derive(Copy, Clone, Deserialize, Serialize)]
|
|
|
|
pub struct NumericRange {
|
|
|
|
pub from: (Spanned<u64>, RangeInclusion),
|
|
|
|
pub to: (Spanned<u64>, RangeInclusion),
|
|
|
|
}
|
|
|
|
|
2019-08-02 19:15:07 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct DeserializerItem<'de> {
|
2019-09-02 00:43:07 +00:00
|
|
|
key_struct_field: Option<(String, &'de str)>,
|
2019-11-21 14:33:14 +00:00
|
|
|
val: Value,
|
2019-08-02 19:15:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ConfigDeserializer<'de> {
|
2019-08-17 03:53:39 +00:00
|
|
|
call: CallInfo,
|
2019-08-02 19:15:07 +00:00
|
|
|
stack: Vec<DeserializerItem<'de>>,
|
|
|
|
saw_root: bool,
|
|
|
|
position: usize,
|
|
|
|
}
|
|
|
|
|
2019-08-29 12:16:11 +00:00
|
|
|
impl<'de> ConfigDeserializer<'de> {
|
2019-08-17 03:53:39 +00:00
|
|
|
pub fn from_call_info(call: CallInfo) -> ConfigDeserializer<'de> {
|
2019-08-02 19:15:07 +00:00
|
|
|
ConfigDeserializer {
|
2019-08-17 03:53:39 +00:00
|
|
|
call,
|
2019-08-02 19:15:07 +00:00
|
|
|
stack: vec![],
|
|
|
|
saw_root: false,
|
|
|
|
position: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
pub fn push_val(&mut self, val: Value) {
|
2019-09-02 01:25:34 +00:00
|
|
|
self.stack.push(DeserializerItem {
|
|
|
|
key_struct_field: None,
|
|
|
|
val,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-02 19:15:07 +00:00
|
|
|
pub fn push(&mut self, name: &'static str) -> Result<(), ShellError> {
|
2019-11-21 14:33:14 +00:00
|
|
|
let value: Option<Value> = if name == "rest" {
|
2019-08-17 03:53:39 +00:00
|
|
|
let positional = self.call.args.slice_from(self.position);
|
2019-08-02 19:15:07 +00:00
|
|
|
self.position += positional.len();
|
2019-11-21 14:33:14 +00:00
|
|
|
Some(UntaggedValue::Table(positional).into_untagged_value()) // TODO: correct tag
|
2019-12-07 09:34:32 +00:00
|
|
|
} else if self.call.args.has(name) {
|
|
|
|
self.call.args.get(name).cloned()
|
2019-08-02 19:15:07 +00:00
|
|
|
} else {
|
2019-12-07 09:34:32 +00:00
|
|
|
let position = self.position;
|
|
|
|
self.position += 1;
|
|
|
|
self.call.args.nth(position).cloned()
|
2019-08-02 19:15:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
trace!("pushing {:?}", value);
|
|
|
|
|
|
|
|
self.stack.push(DeserializerItem {
|
2019-09-02 00:43:07 +00:00
|
|
|
key_struct_field: Some((name.to_string(), name)),
|
2019-12-04 19:52:31 +00:00
|
|
|
val: value.unwrap_or_else(|| UntaggedValue::nothing().into_value(&self.call.name_tag)),
|
2019-08-02 19:15:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-09-03 00:49:51 +00:00
|
|
|
|
2019-09-01 22:32:26 +00:00
|
|
|
pub fn top(&mut self) -> &DeserializerItem {
|
|
|
|
let value = self.stack.last();
|
|
|
|
trace!("inspecting top value :: {:?}", value);
|
2019-11-01 21:19:46 +00:00
|
|
|
value.expect("Can't get top element of an empty stack")
|
2019-09-01 22:32:26 +00:00
|
|
|
}
|
2019-08-02 19:15:07 +00:00
|
|
|
|
|
|
|
pub fn pop(&mut self) -> DeserializerItem {
|
|
|
|
let value = self.stack.pop();
|
|
|
|
trace!("popping value :: {:?}", value);
|
|
|
|
value.expect("Can't pop an empty stack")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use de::Visitor;
|
|
|
|
|
|
|
|
impl<'de, 'a> de::Deserializer<'de> for &'a mut ConfigDeserializer<'de> {
|
|
|
|
type Error = ShellError;
|
|
|
|
fn deserialize_any<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: Visitor<'de>,
|
|
|
|
{
|
2019-09-02 02:07:02 +00:00
|
|
|
unimplemented!("deserialize_any")
|
2019-08-02 19:15:07 +00:00
|
|
|
}
|
2019-09-01 23:02:23 +00:00
|
|
|
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: Visitor<'de>,
|
|
|
|
{
|
|
|
|
let value = self.pop();
|
|
|
|
trace!("Extracting {:?} for bool", value.val);
|
2019-08-02 19:15:07 +00:00
|
|
|
|
2019-09-01 23:02:23 +00:00
|
|
|
match &value.val {
|
2019-11-21 14:33:14 +00:00
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Primitive(Primitive::Boolean(b)),
|
2019-09-01 23:02:23 +00:00
|
|
|
..
|
|
|
|
} => visitor.visit_bool(*b),
|
2019-11-21 14:33:14 +00:00
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Primitive(Primitive::Nothing),
|
2019-09-01 23:02:23 +00:00
|
|
|
..
|
|
|
|
} => visitor.visit_bool(false),
|
2019-11-04 15:47:03 +00:00
|
|
|
other => Err(ShellError::type_error(
|
|
|
|
"Boolean",
|
|
|
|
other.type_name().spanned(other.span()),
|
|
|
|
)),
|
2019-09-01 23:02:23 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-02 19:15:07 +00:00
|
|
|
fn deserialize_i8<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: Visitor<'de>,
|
|
|
|
{
|
|
|
|
unimplemented!("deserialize_i8")
|
|
|
|
}
|
|
|
|
fn deserialize_i16<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: Visitor<'de>,
|
|
|
|
{
|
|
|
|
unimplemented!("deserialize_i16")
|
|
|
|
}
|
|
|
|
fn deserialize_i32<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: Visitor<'de>,
|
|
|
|
{
|
|
|
|
unimplemented!("deserialize_i32")
|
|
|
|
}
|
|
|
|
fn deserialize_i64<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: Visitor<'de>,
|
|
|
|
{
|
|
|
|
unimplemented!("deserialize_i64")
|
|
|
|
}
|
|
|
|
fn deserialize_u8<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: Visitor<'de>,
|
|
|
|
{
|
|
|
|
unimplemented!("deserialize_u8")
|
|
|
|
}
|
|
|
|
fn deserialize_u16<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: Visitor<'de>,
|
|
|
|
{
|
|
|
|
unimplemented!("deserialize_u16")
|
|
|
|
}
|
|
|
|
fn deserialize_u32<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: Visitor<'de>,
|
|
|
|
{
|
|
|
|
unimplemented!("deserialize_u32")
|
|
|
|
}
|
|
|
|
fn deserialize_u64<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: Visitor<'de>,
|
|
|
|
{
|
|
|
|
unimplemented!("deserialize_u64")
|
|
|
|
}
|
|
|
|
fn deserialize_f32<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: Visitor<'de>,
|
|
|
|
{
|
|
|
|
unimplemented!("deserialize_f32")
|
|
|
|
}
|
|
|
|
fn deserialize_f64<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: Visitor<'de>,
|
|
|
|
{
|
|
|
|
unimplemented!("deserialize_f64")
|
|
|
|
}
|
|
|
|
fn deserialize_char<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: Visitor<'de>,
|
|
|
|
{
|
|
|
|
unimplemented!("deserialize_char")
|
|
|
|
}
|
|
|
|
fn deserialize_str<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: Visitor<'de>,
|
|
|
|
{
|
|
|
|
unimplemented!("deserialize_str")
|
|
|
|
}
|
|
|
|
fn deserialize_string<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: Visitor<'de>,
|
|
|
|
{
|
|
|
|
unimplemented!("deserialize_string")
|
|
|
|
}
|
|
|
|
fn deserialize_bytes<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: Visitor<'de>,
|
|
|
|
{
|
|
|
|
unimplemented!("deserialize_bytes")
|
|
|
|
}
|
|
|
|
fn deserialize_byte_buf<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: Visitor<'de>,
|
|
|
|
{
|
|
|
|
unimplemented!("deserialize_byte_buf")
|
|
|
|
}
|
2019-09-03 00:49:51 +00:00
|
|
|
|
2019-09-01 22:32:26 +00:00
|
|
|
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: Visitor<'de>,
|
|
|
|
{
|
|
|
|
let value = self.top();
|
|
|
|
let name = std::any::type_name::<V::Value>();
|
|
|
|
trace!("<Option> Extracting {:?} for Option<{}>", value, name);
|
2019-11-21 14:33:14 +00:00
|
|
|
match &value.val.value {
|
|
|
|
UntaggedValue::Primitive(Primitive::Nothing) => visitor.visit_none(),
|
2019-09-01 22:32:26 +00:00
|
|
|
_ => visitor.visit_some(self),
|
|
|
|
}
|
|
|
|
}
|
2019-08-02 19:15:07 +00:00
|
|
|
|
|
|
|
fn deserialize_unit<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: Visitor<'de>,
|
|
|
|
{
|
|
|
|
unimplemented!("deserialize_unit")
|
|
|
|
}
|
|
|
|
fn deserialize_unit_struct<V>(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_visitor: V,
|
|
|
|
) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: Visitor<'de>,
|
|
|
|
{
|
|
|
|
unimplemented!("deserialize_unit_struct")
|
|
|
|
}
|
|
|
|
fn deserialize_newtype_struct<V>(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_visitor: V,
|
|
|
|
) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: Visitor<'de>,
|
|
|
|
{
|
|
|
|
unimplemented!("deserialize_newtype_struct")
|
|
|
|
}
|
2019-09-02 01:25:34 +00:00
|
|
|
fn deserialize_seq<V>(mut self, visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: Visitor<'de>,
|
|
|
|
{
|
|
|
|
let value = self.pop();
|
|
|
|
trace!("<Vec> Extracting {:?} for vec", value.val);
|
2019-08-02 19:15:07 +00:00
|
|
|
|
2019-09-02 01:25:34 +00:00
|
|
|
match value.val.into_parts() {
|
2019-11-21 14:33:14 +00:00
|
|
|
(UntaggedValue::Table(items), _) => {
|
2019-09-02 01:25:34 +00:00
|
|
|
let de = SeqDeserializer::new(&mut self, items.into_iter());
|
|
|
|
visitor.visit_seq(de)
|
|
|
|
}
|
2019-11-04 15:47:03 +00:00
|
|
|
(other, tag) => Err(ShellError::type_error(
|
|
|
|
"Vec",
|
|
|
|
other.type_name().spanned(tag),
|
|
|
|
)),
|
2019-09-02 01:25:34 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-02 01:37:01 +00:00
|
|
|
fn deserialize_tuple<V>(mut self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
|
2019-08-02 19:15:07 +00:00
|
|
|
where
|
|
|
|
V: Visitor<'de>,
|
|
|
|
{
|
2019-09-02 01:37:01 +00:00
|
|
|
let value = self.pop();
|
2019-09-05 16:23:42 +00:00
|
|
|
trace!(
|
|
|
|
"<Tuple> Extracting {:?} for tuple with {} elements",
|
|
|
|
value.val,
|
|
|
|
len
|
|
|
|
);
|
2019-09-02 01:37:01 +00:00
|
|
|
|
|
|
|
match value.val.into_parts() {
|
2019-11-21 14:33:14 +00:00
|
|
|
(UntaggedValue::Table(items), _) => {
|
2019-09-02 01:37:01 +00:00
|
|
|
let de = SeqDeserializer::new(&mut self, items.into_iter());
|
|
|
|
visitor.visit_seq(de)
|
|
|
|
}
|
|
|
|
(other, tag) => Err(ShellError::type_error(
|
|
|
|
"Tuple",
|
2019-11-04 15:47:03 +00:00
|
|
|
other.type_name().spanned(tag),
|
2019-09-02 01:37:01 +00:00
|
|
|
)),
|
|
|
|
}
|
2019-08-02 19:15:07 +00:00
|
|
|
}
|
|
|
|
fn deserialize_tuple_struct<V>(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_len: usize,
|
|
|
|
_visitor: V,
|
|
|
|
) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: Visitor<'de>,
|
|
|
|
{
|
|
|
|
unimplemented!("deserialize_tuple_struct")
|
|
|
|
}
|
|
|
|
fn deserialize_map<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: Visitor<'de>,
|
|
|
|
{
|
|
|
|
unimplemented!("deserialize_map")
|
|
|
|
}
|
|
|
|
fn deserialize_struct<V>(
|
|
|
|
mut self,
|
|
|
|
name: &'static str,
|
|
|
|
fields: &'static [&'static str],
|
|
|
|
visitor: V,
|
|
|
|
) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: Visitor<'de>,
|
|
|
|
{
|
2019-09-02 21:21:29 +00:00
|
|
|
fn visit<'de, T, V>(
|
|
|
|
val: T,
|
|
|
|
name: &'static str,
|
|
|
|
fields: &'static [&'static str],
|
2019-09-05 16:23:42 +00:00
|
|
|
visitor: V,
|
2019-09-02 21:21:29 +00:00
|
|
|
) -> Result<V::Value, ShellError>
|
|
|
|
where
|
|
|
|
T: serde::Serialize,
|
|
|
|
V: Visitor<'de>,
|
|
|
|
{
|
|
|
|
let json = serde_json::to_string(&val)?;
|
|
|
|
let json_cursor = std::io::Cursor::new(json.into_bytes());
|
|
|
|
let mut json_de = serde_json::Deserializer::from_reader(json_cursor);
|
|
|
|
let r = json_de.deserialize_struct(name, fields, visitor)?;
|
2019-12-06 15:28:26 +00:00
|
|
|
Ok(r)
|
2019-09-02 21:21:29 +00:00
|
|
|
}
|
2019-11-21 14:33:14 +00:00
|
|
|
|
2019-08-02 19:15:07 +00:00
|
|
|
trace!(
|
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-17 22:26:27 +00:00
|
|
|
"deserializing struct {:?} {:?} (saw_root={} stack={:?})",
|
2019-08-02 19:15:07 +00:00
|
|
|
name,
|
|
|
|
fields,
|
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-17 22:26:27 +00:00
|
|
|
self.saw_root,
|
2019-08-02 19:15:07 +00:00
|
|
|
self.stack
|
|
|
|
);
|
|
|
|
|
2019-09-02 20:06:46 +00:00
|
|
|
if !self.saw_root {
|
2019-08-02 19:15:07 +00:00
|
|
|
self.saw_root = true;
|
2019-09-02 20:06:46 +00:00
|
|
|
return visitor.visit_seq(StructDeserializer::new(&mut self, fields));
|
2019-08-02 19:15:07 +00:00
|
|
|
}
|
2019-09-02 20:06:46 +00:00
|
|
|
|
|
|
|
let value = self.pop();
|
2019-09-02 20:30:51 +00:00
|
|
|
|
2019-09-02 21:23:34 +00:00
|
|
|
let type_name = std::any::type_name::<V::Value>();
|
2019-11-21 14:33:14 +00:00
|
|
|
let tagged_val_name = std::any::type_name::<Value>();
|
2019-09-02 21:23:34 +00: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`
2019-09-17 22:26:27 +00:00
|
|
|
trace!(
|
2019-11-04 15:47:03 +00:00
|
|
|
"name={} type_name={} tagged_val_name={}",
|
|
|
|
name,
|
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-17 22:26:27 +00:00
|
|
|
type_name,
|
|
|
|
tagged_val_name
|
|
|
|
);
|
|
|
|
|
2019-09-09 11:02:25 +00:00
|
|
|
if type_name == tagged_val_name {
|
2019-11-21 14:33:14 +00:00
|
|
|
return visit::<Value, _>(value.val, name, fields, visitor);
|
2019-09-02 21:23:34 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 02:04:54 +00:00
|
|
|
if name == "Block" {
|
2019-09-02 20:30:51 +00:00
|
|
|
let block = match value.val {
|
2019-11-21 14:33:14 +00:00
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Block(block),
|
2019-09-02 20:30:51 +00:00
|
|
|
..
|
|
|
|
} => block,
|
2019-11-04 15:47:03 +00:00
|
|
|
other => {
|
|
|
|
return Err(ShellError::type_error(
|
|
|
|
"Block",
|
|
|
|
other.type_name().spanned(other.span()),
|
|
|
|
))
|
|
|
|
}
|
2019-09-02 20:30:51 +00:00
|
|
|
};
|
2020-04-20 06:41:51 +00:00
|
|
|
return visit::<Block, _>(block, name, fields, visitor);
|
2019-09-02 20:30:51 +00:00
|
|
|
}
|
|
|
|
|
2019-11-04 15:47:03 +00:00
|
|
|
if name == "ColumnPath" {
|
|
|
|
let path = match value.val {
|
2019-11-21 14:33:14 +00:00
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Primitive(Primitive::ColumnPath(path)),
|
2019-11-04 15:47:03 +00:00
|
|
|
..
|
|
|
|
} => path,
|
|
|
|
other => {
|
|
|
|
return Err(ShellError::type_error(
|
|
|
|
"column path",
|
|
|
|
other.type_name().spanned(other.span()),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return visit::<ColumnPath, _>(path, name, fields, visitor);
|
|
|
|
}
|
|
|
|
|
2019-09-02 21:23:34 +00:00
|
|
|
trace!("Extracting {:?} for {:?}", value.val, type_name);
|
2019-09-03 00:10:48 +00:00
|
|
|
|
|
|
|
let tag = value.val.tag();
|
|
|
|
match value.val {
|
2019-11-21 14:33:14 +00:00
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Primitive(Primitive::Boolean(b)),
|
2019-09-03 00:10:48 +00:00
|
|
|
..
|
|
|
|
} => visit::<Tagged<bool>, _>(b.tagged(tag), name, fields, visitor),
|
2019-11-21 14:33:14 +00:00
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Primitive(Primitive::Nothing),
|
2019-09-03 00:10:48 +00:00
|
|
|
..
|
|
|
|
} => visit::<Tagged<bool>, _>(false.tagged(tag), name, fields, visitor),
|
2019-11-21 14:33:14 +00:00
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Primitive(Primitive::Path(p)),
|
2019-09-03 00:10:48 +00:00
|
|
|
..
|
2019-12-31 07:36:08 +00:00
|
|
|
} => visit::<Tagged<PathBuf>, _>(p.tagged(tag), name, fields, visitor),
|
2019-11-21 14:33:14 +00:00
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Primitive(Primitive::Int(int)),
|
2019-09-03 00:10:48 +00:00
|
|
|
..
|
|
|
|
} => {
|
|
|
|
let i: i64 = int.tagged(value.val.tag).coerce_into("converting to i64")?;
|
|
|
|
visit::<Tagged<i64>, _>(i.tagged(tag), name, fields, visitor)
|
2019-09-05 16:23:42 +00:00
|
|
|
}
|
2019-11-21 14:33:14 +00:00
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Primitive(Primitive::String(string)),
|
2019-09-03 00:10:48 +00:00
|
|
|
..
|
|
|
|
} => visit::<Tagged<String>, _>(string.tagged(tag), name, fields, visitor),
|
2019-12-04 21:14:52 +00:00
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Primitive(Primitive::Range(range)),
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
let (left, left_inclusion) = range.from;
|
|
|
|
let (right, right_inclusion) = range.to;
|
|
|
|
let left_span = left.span;
|
|
|
|
let right_span = right.span;
|
|
|
|
|
|
|
|
let left = left.as_u64(left_span)?;
|
|
|
|
let right = right.as_u64(right_span)?;
|
|
|
|
|
|
|
|
let numeric_range = NumericRange {
|
|
|
|
from: (left.spanned(left_span), left_inclusion),
|
|
|
|
to: (right.spanned(right_span), right_inclusion),
|
|
|
|
};
|
|
|
|
|
|
|
|
visit::<Tagged<NumericRange>, _>(numeric_range.tagged(tag), name, fields, visitor)
|
|
|
|
}
|
2019-09-03 00:10:48 +00:00
|
|
|
|
2019-12-06 15:28:26 +00:00
|
|
|
other => Err(ShellError::type_error(
|
|
|
|
name,
|
|
|
|
other.type_name().spanned(other.span()),
|
|
|
|
)),
|
2019-09-03 00:10:48 +00:00
|
|
|
}
|
2019-08-02 19:15:07 +00:00
|
|
|
}
|
|
|
|
fn deserialize_enum<V>(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_variants: &'static [&'static str],
|
|
|
|
_visitor: V,
|
|
|
|
) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: Visitor<'de>,
|
|
|
|
{
|
|
|
|
unimplemented!("deserialize_enum")
|
|
|
|
}
|
|
|
|
fn deserialize_identifier<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: Visitor<'de>,
|
|
|
|
{
|
|
|
|
unimplemented!("deserialize_identifier")
|
|
|
|
}
|
|
|
|
fn deserialize_ignored_any<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: Visitor<'de>,
|
|
|
|
{
|
|
|
|
unimplemented!("deserialize_ignored_any")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
struct SeqDeserializer<'a, 'de: 'a, I: Iterator<Item = Value>> {
|
2019-09-02 01:25:34 +00:00
|
|
|
de: &'a mut ConfigDeserializer<'de>,
|
|
|
|
vals: I,
|
|
|
|
}
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
impl<'a, 'de: 'a, I: Iterator<Item = Value>> SeqDeserializer<'a, 'de, I> {
|
2019-09-02 01:25:34 +00:00
|
|
|
fn new(de: &'a mut ConfigDeserializer<'de>, vals: I) -> Self {
|
2019-09-05 16:23:42 +00:00
|
|
|
SeqDeserializer { de, vals }
|
2019-09-02 01:25:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
impl<'a, 'de: 'a, I: Iterator<Item = Value>> de::SeqAccess<'de> for SeqDeserializer<'a, 'de, I> {
|
2019-09-02 01:25:34 +00:00
|
|
|
type Error = ShellError;
|
|
|
|
|
|
|
|
fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
|
|
|
|
where
|
|
|
|
T: de::DeserializeSeed<'de>,
|
|
|
|
{
|
|
|
|
let next = if let Some(next) = self.vals.next() {
|
|
|
|
next
|
|
|
|
} else {
|
|
|
|
return Ok(None);
|
|
|
|
};
|
|
|
|
|
|
|
|
self.de.push_val(next);
|
|
|
|
seed.deserialize(&mut *self.de).map(Some)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn size_hint(&self) -> Option<usize> {
|
2019-12-06 15:28:26 +00:00
|
|
|
self.vals.size_hint().1
|
2019-09-02 01:25:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 19:15:07 +00:00
|
|
|
struct StructDeserializer<'a, 'de: 'a> {
|
|
|
|
de: &'a mut ConfigDeserializer<'de>,
|
|
|
|
fields: &'static [&'static str],
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'de: 'a> StructDeserializer<'a, 'de> {
|
|
|
|
fn new(de: &'a mut ConfigDeserializer<'de>, fields: &'static [&'static str]) -> Self {
|
2019-09-05 16:23:42 +00:00
|
|
|
StructDeserializer { de, fields }
|
2019-08-02 19:15:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'de: 'a> de::SeqAccess<'de> for StructDeserializer<'a, 'de> {
|
|
|
|
type Error = ShellError;
|
|
|
|
|
|
|
|
fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
|
|
|
|
where
|
|
|
|
T: de::DeserializeSeed<'de>,
|
|
|
|
{
|
2019-12-06 15:28:26 +00:00
|
|
|
if self.fields.is_empty() {
|
2019-08-02 19:15:07 +00:00
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
|
|
|
|
trace!("Processing {}", self.fields[0]);
|
|
|
|
|
|
|
|
self.de.push(self.fields[0])?;
|
|
|
|
self.fields = &self.fields[1..];
|
|
|
|
seed.deserialize(&mut *self.de).map(Some)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn size_hint(&self) -> Option<usize> {
|
2019-12-06 15:28:26 +00:00
|
|
|
Some(self.fields.len())
|
2019-08-02 19:15:07 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-09 11:39:43 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use std::any::type_name;
|
|
|
|
#[test]
|
|
|
|
fn check_type_name_properties() {
|
|
|
|
// This ensures that certain properties for the
|
|
|
|
// std::any::type_name function hold, that
|
|
|
|
// this code relies on. The type_name docs explicitly
|
|
|
|
// mention that the actual format of the output
|
|
|
|
// is unspecified and change is likely.
|
|
|
|
// This test makes sure that such change is detected
|
|
|
|
// by this test failing, and not things silently breaking.
|
2019-11-01 21:19:46 +00:00
|
|
|
// Specifically, we rely on this behavior further above
|
2019-11-21 14:33:14 +00:00
|
|
|
// in the file for the Value special case parsing.
|
2019-09-09 11:39:43 +00:00
|
|
|
let tuple = type_name::<()>();
|
|
|
|
let tagged_tuple = type_name::<Tagged<()>>();
|
2019-11-21 14:33:14 +00:00
|
|
|
let tagged_value = type_name::<Value>();
|
2019-09-09 11:39:43 +00:00
|
|
|
assert!(tuple != tagged_tuple);
|
|
|
|
assert!(tuple != tagged_value);
|
|
|
|
assert!(tagged_tuple != tagged_value);
|
|
|
|
}
|
|
|
|
}
|