Overhaul the coloring system
This commit replaces the previous naive coloring system with a coloring
system that is more aligned with the parser.
The main benefit of this change is that it allows us to use parsing
rules to decide how to color tokens.
For example, consider the following syntax:
```
$ ps | where cpu > 10
```
Ideally, we could color `cpu` like a column name and not a string,
because `cpu > 10` is a shorthand block syntax that expands to
`{ $it.cpu > 10 }`.
The way that we know that it's a shorthand block is that the `where`
command declares that its first parameter is a `SyntaxShape::Block`,
which allows the shorthand block form.
In order to accomplish this, we need to color the tokens in a way that
corresponds to their expanded semantics, which means that high-fidelity
coloring requires expansion.
This commit adds a `ColorSyntax` trait that corresponds to the
`ExpandExpression` trait. The semantics are fairly similar, with a few
differences.
First `ExpandExpression` consumes N tokens and returns a single
`hir::Expression`. `ColorSyntax` consumes N tokens and writes M
`FlatShape` tokens to the output.
Concretely, for syntax like `[1 2 3]`
- `ExpandExpression` takes a single token node and produces a single
`hir::Expression`
- `ColorSyntax` takes the same token node and emits 7 `FlatShape`s
(open delimiter, int, whitespace, int, whitespace, int, close
delimiter)
Second, `ColorSyntax` is more willing to plow through failures than
`ExpandExpression`.
In particular, consider syntax like
```
$ ps | where cpu >
```
In this case
- `ExpandExpression` will see that the `where` command is expecting a
block, see that it's not a literal block and try to parse it as a
shorthand block. It will successfully find a member followed by an
infix operator, but not a following expression. That means that the
entire pipeline part fails to parse and is a syntax error.
- `ColorSyntax` will also try to parse it as a shorthand block and
ultimately fail, but it will fall back to "backoff coloring mode",
which parsing any unidentified tokens in an unfallible, simple way. In
this case, `cpu` will color as a string and `>` will color as an
operator.
Finally, it's very important that coloring a pipeline infallibly colors
the entire string, doesn't fail, and doesn't get stuck in an infinite
loop.
In order to accomplish this, this PR separates `ColorSyntax`, which is
infallible from `FallibleColorSyntax`, which might fail. This allows the
type system to let us know if our coloring rules bottom out at at an
infallible rule.
It's not perfect: it's still possible for the coloring process to get
stuck or consume tokens non-atomically. I intend to reduce the
opportunity for those problems in a future commit. In the meantime, the
current system catches a number of mistakes (like trying to use a
fallible coloring rule in a loop without thinking about the possibility
that it will never terminate).
2019-10-06 22:22:50 +02:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! return_err {
|
|
|
|
($expr:expr) => {
|
|
|
|
match $expr {
|
|
|
|
Err(_) => return,
|
|
|
|
Ok(expr) => expr,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-07-03 22:31:15 +02:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! stream {
|
|
|
|
($($expr:expr),*) => {{
|
|
|
|
let mut v = VecDeque::new();
|
|
|
|
|
|
|
|
$(
|
|
|
|
v.push_back($expr);
|
|
|
|
)*
|
|
|
|
|
|
|
|
v
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! trace_stream {
|
2019-11-21 15:33:14 +01:00
|
|
|
(target: $target:tt, $desc:tt = $expr:expr) => {{
|
2019-07-12 21:22:08 +02:00
|
|
|
if log::log_enabled!(target: $target, log::Level::Trace) {
|
2019-07-03 22:31:15 +02:00
|
|
|
use futures::stream::StreamExt;
|
|
|
|
|
2019-11-04 16:47:03 +01:00
|
|
|
let objects = $expr.values.inspect(move |o| {
|
2019-11-21 15:33:14 +01:00
|
|
|
trace!(
|
|
|
|
target: $target,
|
|
|
|
"{} = {}",
|
|
|
|
$desc,
|
|
|
|
nu_source::PrettyDebug::plain_string(o, 70)
|
|
|
|
);
|
2019-07-03 22:31:15 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$crate::stream::InputStream::from_stream(objects.boxed())
|
|
|
|
} else {
|
|
|
|
$expr
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
Add support for ~ expansion
This ended up being a bit of a yak shave. The basic idea in this commit is to
expand `~` in paths, but only in paths.
The way this is accomplished is by doing the expansion inside of the code that
parses literal syntax for `SyntaxType::Path`.
As a quick refresher: every command is entitled to expand its arguments in a
custom way. While this could in theory be used for general-purpose macros,
today the expansion facility is limited to syntactic hints.
For example, the syntax `where cpu > 0` expands under the hood to
`where { $it.cpu > 0 }`. This happens because the first argument to `where`
is defined as a `SyntaxType::Block`, and the parser coerces binary expressions
whose left-hand-side looks like a member into a block when the command is
expecting one.
This is mildly more magical than what most programming languages would do,
but we believe that it makes sense to allow commands to fine-tune the syntax
because of the domain nushell is in (command-line shells).
The syntactic expansions supported by this facility are relatively limited.
For example, we don't allow `$it` to become a bare word, simply because the
command asks for a string in the relevant position. That would quickly
become more confusing than it's worth.
This PR adds a new `SyntaxType` rule: `SyntaxType::Path`. When a command
declares a parameter as a `SyntaxType::Path`, string literals and bare
words passed as an argument to that parameter are processed using the
path expansion rules. Right now, that only means that `~` is expanded into
the home directory, but additional rules are possible in the future.
By restricting this expansion to a syntactic expansion when passed as an
argument to a command expecting a path, we avoid making `~` a generally
reserved character. This will also allow us to give good tab completion
for paths with `~` characters in them when a command is expecting a path.
In order to accomplish the above, this commit changes the parsing functions
to take a `Context` instead of just a `CommandRegistry`. From the perspective
of macro expansion, you can think of the `CommandRegistry` as a dictionary
of in-scope macros, and the `Context` as the compile-time state used in
expansion. This could gain additional functionality over time as we find
more uses for the expansion system.
2019-08-26 21:21:03 +02:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! trace_out_stream {
|
2019-11-21 15:33:14 +01:00
|
|
|
(target: $target:tt, $desc:tt = $expr:expr) => {{
|
Add support for ~ expansion
This ended up being a bit of a yak shave. The basic idea in this commit is to
expand `~` in paths, but only in paths.
The way this is accomplished is by doing the expansion inside of the code that
parses literal syntax for `SyntaxType::Path`.
As a quick refresher: every command is entitled to expand its arguments in a
custom way. While this could in theory be used for general-purpose macros,
today the expansion facility is limited to syntactic hints.
For example, the syntax `where cpu > 0` expands under the hood to
`where { $it.cpu > 0 }`. This happens because the first argument to `where`
is defined as a `SyntaxType::Block`, and the parser coerces binary expressions
whose left-hand-side looks like a member into a block when the command is
expecting one.
This is mildly more magical than what most programming languages would do,
but we believe that it makes sense to allow commands to fine-tune the syntax
because of the domain nushell is in (command-line shells).
The syntactic expansions supported by this facility are relatively limited.
For example, we don't allow `$it` to become a bare word, simply because the
command asks for a string in the relevant position. That would quickly
become more confusing than it's worth.
This PR adds a new `SyntaxType` rule: `SyntaxType::Path`. When a command
declares a parameter as a `SyntaxType::Path`, string literals and bare
words passed as an argument to that parameter are processed using the
path expansion rules. Right now, that only means that `~` is expanded into
the home directory, but additional rules are possible in the future.
By restricting this expansion to a syntactic expansion when passed as an
argument to a command expecting a path, we avoid making `~` a generally
reserved character. This will also allow us to give good tab completion
for paths with `~` characters in them when a command is expecting a path.
In order to accomplish the above, this commit changes the parsing functions
to take a `Context` instead of just a `CommandRegistry`. From the perspective
of macro expansion, you can think of the `CommandRegistry` as a dictionary
of in-scope macros, and the `Context` as the compile-time state used in
expansion. This could gain additional functionality over time as we find
more uses for the expansion system.
2019-08-26 21:21:03 +02:00
|
|
|
if log::log_enabled!(target: $target, log::Level::Trace) {
|
|
|
|
use futures::stream::StreamExt;
|
|
|
|
|
|
|
|
let objects = $expr.values.inspect(move |o| {
|
2019-11-21 15:33:14 +01:00
|
|
|
trace!(
|
|
|
|
target: $target,
|
|
|
|
"{} = {}",
|
|
|
|
$desc,
|
|
|
|
match o {
|
|
|
|
Err(err) => format!("{:?}", err),
|
|
|
|
Ok(value) => value.display(),
|
|
|
|
}
|
|
|
|
);
|
Add support for ~ expansion
This ended up being a bit of a yak shave. The basic idea in this commit is to
expand `~` in paths, but only in paths.
The way this is accomplished is by doing the expansion inside of the code that
parses literal syntax for `SyntaxType::Path`.
As a quick refresher: every command is entitled to expand its arguments in a
custom way. While this could in theory be used for general-purpose macros,
today the expansion facility is limited to syntactic hints.
For example, the syntax `where cpu > 0` expands under the hood to
`where { $it.cpu > 0 }`. This happens because the first argument to `where`
is defined as a `SyntaxType::Block`, and the parser coerces binary expressions
whose left-hand-side looks like a member into a block when the command is
expecting one.
This is mildly more magical than what most programming languages would do,
but we believe that it makes sense to allow commands to fine-tune the syntax
because of the domain nushell is in (command-line shells).
The syntactic expansions supported by this facility are relatively limited.
For example, we don't allow `$it` to become a bare word, simply because the
command asks for a string in the relevant position. That would quickly
become more confusing than it's worth.
This PR adds a new `SyntaxType` rule: `SyntaxType::Path`. When a command
declares a parameter as a `SyntaxType::Path`, string literals and bare
words passed as an argument to that parameter are processed using the
path expansion rules. Right now, that only means that `~` is expanded into
the home directory, but additional rules are possible in the future.
By restricting this expansion to a syntactic expansion when passed as an
argument to a command expecting a path, we avoid making `~` a generally
reserved character. This will also allow us to give good tab completion
for paths with `~` characters in them when a command is expecting a path.
In order to accomplish the above, this commit changes the parsing functions
to take a `Context` instead of just a `CommandRegistry`. From the perspective
of macro expansion, you can think of the `CommandRegistry` as a dictionary
of in-scope macros, and the `Context` as the compile-time state used in
expansion. This could gain additional functionality over time as we find
more uses for the expansion system.
2019-08-26 21:21:03 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$crate::stream::OutputStream::new(objects)
|
|
|
|
} else {
|
|
|
|
$expr
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2019-11-04 16:47:03 +01:00
|
|
|
// These macros exist to differentiate between intentional writing to stdout
|
|
|
|
// and stray printlns left by accident
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! outln {
|
|
|
|
($($tokens:tt)*) => { println!($($tokens)*) }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! errln {
|
|
|
|
($($tokens:tt)*) => { eprintln!($($tokens)*) }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! dict {
|
|
|
|
($( $key:expr => $value:expr ),*) => {
|
|
|
|
$crate::data::dict::TaggedDictBuilder::build(Tag::unknown(), |d| {
|
|
|
|
$(
|
2019-11-21 15:33:14 +01:00
|
|
|
d.insert_untagged($key, $value);
|
2019-11-04 16:47:03 +01:00
|
|
|
)*
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
([tag] => $tag:expr, $( $key:expr => $value:expr ),*) => {
|
|
|
|
$crate::data::dict::TaggedDictBuilder::build($tag, |d| {
|
|
|
|
$(
|
2019-11-21 15:33:14 +01:00
|
|
|
d.insert_untagged($key, $value);
|
2019-11-04 16:47:03 +01:00
|
|
|
)*
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-29 13:08:28 +02:00
|
|
|
pub(crate) use crate::cli::MaybeOwned;
|
|
|
|
pub(crate) use crate::commands::command::{
|
2019-08-15 07:02:02 +02:00
|
|
|
CallInfo, CommandAction, CommandArgs, ReturnSuccess, ReturnValue, RunnableContext,
|
2019-07-03 22:31:15 +02:00
|
|
|
};
|
2019-08-29 13:08:28 +02:00
|
|
|
pub(crate) use crate::commands::PerItemCommand;
|
2019-08-31 00:13:09 +02:00
|
|
|
pub(crate) use crate::commands::RawCommandArgs;
|
2019-08-29 13:08:28 +02:00
|
|
|
pub(crate) use crate::context::CommandRegistry;
|
2019-11-21 15:33:14 +01:00
|
|
|
pub(crate) use crate::context::Context;
|
|
|
|
pub(crate) use crate::data::base::{UntaggedValue, Value};
|
2019-09-05 18:23:42 +02:00
|
|
|
pub(crate) use crate::data::types::ExtractType;
|
2019-11-21 15:33:14 +01:00
|
|
|
pub(crate) use crate::data::Primitive;
|
2019-08-29 13:08:28 +02:00
|
|
|
pub(crate) use crate::env::host::handle_unexpected;
|
|
|
|
pub(crate) use crate::env::Host;
|
2019-10-28 15:46:50 +01:00
|
|
|
pub(crate) use crate::errors::{CoerceInto, ParseError, ShellError};
|
2019-09-14 18:30:24 +02:00
|
|
|
pub(crate) use crate::parser::hir::SyntaxShape;
|
2019-08-30 19:29:04 +02:00
|
|
|
pub(crate) use crate::parser::parse::parser::Number;
|
2019-08-29 13:08:28 +02:00
|
|
|
pub(crate) use crate::parser::registry::Signature;
|
|
|
|
pub(crate) use crate::shell::filesystem_shell::FilesystemShell;
|
2019-09-05 18:23:42 +02:00
|
|
|
pub(crate) use crate::shell::help_shell::HelpShell;
|
2019-08-29 13:08:28 +02:00
|
|
|
pub(crate) use crate::shell::shell_manager::ShellManager;
|
|
|
|
pub(crate) use crate::shell::value_shell::ValueShell;
|
|
|
|
pub(crate) use crate::stream::{InputStream, OutputStream};
|
2019-11-21 15:33:14 +01:00
|
|
|
pub(crate) use crate::traits::{ShellTypeName, SpannedTypeName};
|
2019-09-26 02:22:17 +02:00
|
|
|
pub(crate) use async_stream::stream as async_stream;
|
2019-09-01 18:20:31 +02:00
|
|
|
pub(crate) use bigdecimal::BigDecimal;
|
2019-08-29 13:08:28 +02:00
|
|
|
pub(crate) use futures::stream::BoxStream;
|
|
|
|
pub(crate) use futures::{FutureExt, Stream, StreamExt};
|
2019-11-21 15:33:14 +01:00
|
|
|
pub(crate) use nu_source::{
|
|
|
|
b, AnchorLocation, DebugDocBuilder, HasFallibleSpan, HasSpan, PrettyDebug,
|
|
|
|
PrettyDebugWithSource, Span, SpannedItem, Tag, TaggedItem, Text,
|
|
|
|
};
|
2019-09-01 18:20:31 +02:00
|
|
|
pub(crate) use num_bigint::BigInt;
|
2019-08-30 19:29:04 +02:00
|
|
|
pub(crate) use num_traits::cast::{FromPrimitive, ToPrimitive};
|
2019-09-01 18:20:31 +02:00
|
|
|
pub(crate) use num_traits::identities::Zero;
|
|
|
|
pub(crate) use serde::Deserialize;
|
2019-08-29 13:08:28 +02:00
|
|
|
pub(crate) use std::collections::VecDeque;
|
|
|
|
pub(crate) use std::future::Future;
|
|
|
|
pub(crate) use std::sync::{Arc, Mutex};
|
2019-07-03 22:31:15 +02:00
|
|
|
|
2019-10-28 15:46:50 +01:00
|
|
|
pub(crate) use itertools::Itertools;
|
|
|
|
|
2019-07-03 22:31:15 +02:00
|
|
|
pub trait FromInputStream {
|
|
|
|
fn from_input_stream(self) -> OutputStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> FromInputStream for T
|
|
|
|
where
|
2019-11-21 15:33:14 +01:00
|
|
|
T: Stream<Item = Value> + Send + 'static,
|
2019-07-03 22:31:15 +02:00
|
|
|
{
|
|
|
|
fn from_input_stream(self) -> OutputStream {
|
|
|
|
OutputStream {
|
|
|
|
values: self.map(ReturnSuccess::value).boxed(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-13 06:12:43 +02:00
|
|
|
pub trait ToInputStream {
|
|
|
|
fn to_input_stream(self) -> InputStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, U> ToInputStream for T
|
|
|
|
where
|
|
|
|
T: Stream<Item = U> + Send + 'static,
|
2019-11-21 15:33:14 +01:00
|
|
|
U: Into<Result<Value, ShellError>>,
|
2019-10-13 06:12:43 +02:00
|
|
|
{
|
|
|
|
fn to_input_stream(self) -> InputStream {
|
|
|
|
InputStream {
|
|
|
|
values: self.map(|item| item.into().unwrap()).boxed(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-03 22:31:15 +02:00
|
|
|
pub trait ToOutputStream {
|
|
|
|
fn to_output_stream(self) -> OutputStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, U> ToOutputStream for T
|
|
|
|
where
|
|
|
|
T: Stream<Item = U> + Send + 'static,
|
|
|
|
U: Into<ReturnValue>,
|
|
|
|
{
|
|
|
|
fn to_output_stream(self) -> OutputStream {
|
|
|
|
OutputStream {
|
|
|
|
values: self.map(|item| item.into()).boxed(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|