Commit Graph

85 Commits

Author SHA1 Message Date
Yehuda Katz
f20f3f56c7 Start moving coloring into the token stream
The benefit of this is that coloring can be made atomic alongside token
stream forwarding.

I put the feature behind a flag so I can continue to iterate on it
without possibly regressing existing functionality. It's a lot of places
where the flags have to go, but I expect it to be a short-lived flag,
and the flags are fully contained in the parser.
2019-10-14 16:11:00 -07:00
Jason Gedge
341cc1ea63 Ignore errors in ls.
`std::fs::metadata` will attempt to follow symlinks, which results in a
"No such file or directory" error if the path pointed to by the symlink
does not exist. This shouldn't prevent `ls` from succeeding, so we
ignore errors.

Also, switching to use of `symlink_metadata` means we get stat info on
the symlink itself, not what it points to. This means `ls` will now
include broken symlinks in its listing.
2019-10-13 12:26:31 -04:00
Jonathan Turner
193b00764b
Stream support (#812)
* Moves off of draining between filters. Instead, the sink will pull on the stream, and will drain element-wise. This moves the whole stream to being lazy.
* Adds ctrl-c support and connects it into some of the key points where we pull on the stream. If a ctrl-c is detect, we immediately halt pulling on the stream and return to the prompt.
* Moves away from having a SourceMap where anchor locations are stored. Now AnchorLocation is kept directly in the Tag.
* To make this possible, split tag and span. Span is largely used in the parser and is copyable. Tag is now no longer copyable.
2019-10-13 17:12:43 +13:00
Yehuda Katz
c2c10e2bc0 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-10 19:30:04 -07:00
Yehuda Katz
1ad9d6f199 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-10-10 08:27:51 -07:00
Jonathan Turner
caed87c125 Rename origin to anchor 2019-09-29 18:13:56 +13:00
Andrés N. Robalino
837d12decd Filesystem shell can't cd into files. Ever. 2019-09-24 15:34:30 -05:00
Jonathan Turner
88c1b1dc6f Improve default features and don't precompute ls 2019-09-15 13:51:19 +12:00
Yehuda Katz
ab915f1c44 Revert "Revert "Migrate most uses of the Span concept to Tag""
This reverts commit bee7c5639c.
2019-09-14 11:30:24 -05:00
Maximilian Roos
127381497c
run rustfmt 2019-09-11 10:36:50 -04:00
Yehuda Katz
b15bb2c667 Added glob patterns to the syntax shapes
Bare words now represent literal file names, and globs are a different
syntax shape called "Pattern". This allows commands like `cp` to ask for
a pattern as a source and a literal file as a target.

This also means that attempting to pass a glob to a command that expects
a literal path will produce an error.
2019-09-10 09:00:50 -07:00
Yehuda Katz
4d3e7efe25 Close a bunch of holes in external command args
Previously, there was a single parsing rule for "bare words" that
applied to both internal and external commands.

This meant that, because `cargo +nightly` needed to work, we needed to
add `+` as a valid character in bare words.

The number of characters continued to grow, and the situation was
becoming untenable. The current strategy would eventually eat up all
syntax and make it impossible to add syntax like `@foo` to internal
commands.

This patch significantly restricts bare words and introduces a new token
type (`ExternalWord`). An `ExternalWord` expands to an error in the
internal syntax, but expands to a bare word in the external syntax.

`ExternalWords` are highlighted in grey in the shell.
2019-09-09 10:43:10 -07:00
Andrés N. Robalino
f770409a60 cd '-' valueshell implementation and valueshell refactorings. 2019-09-08 05:40:47 -05:00
Andrés N. Robalino
77c2e4200e Filesystem cd refactor/cleanup. 2019-09-08 04:55:49 -05:00
Andrés N. Robalino
eecda3ecba
Merge pull request #363 from twe4ked/cd-back
Implement `cd -` to return to the previous directory
2019-09-08 01:00:32 -05:00
Odin Dutton
159cf27e39 Implement cd - to return to the last path for the FilesystemShell 2019-09-08 15:10:46 +10:00
Jonathan Rothberg
7913ae76f8 Expand pwd command
Expand functionality of the pwd command to better handle the different
types of shells (e.g. FilesystemShell, ValueShell, etc.).
2019-09-07 15:31:16 -07:00
Patrick Meredith
ea24571c22 Remove added newline 2019-09-06 23:24:29 -04:00
Patrick Meredith
1b2fdf7c1e Fix bug with ls globbing a single directory 2019-09-06 23:20:13 -04:00
Jonathan Turner
b84c77d23a
Merge pull request #603 from jonathandturner/oop_to_table
Move internal terminology to tables/rows
2019-09-06 05:22:24 +12:00
Jonathan Turner
dcd97b6346 Move internal terminology to tables/rows 2019-09-06 04:23:42 +12:00
George Pollard
6034de641a
Improve parsing of pipelines, require pipes
At the moment the pipeline parser does not enforce
that there must be a pipe between each part of the pipeline,
which can lead to confusing behaviour or misleading errors.
2019-09-05 03:30:51 +12:00
Andrés N. Robalino
3256b7adb3 if path to ls given that does not exist, report the error. 2019-09-03 05:24:04 -05:00
Andrés N. Robalino
1d0ed7e957 ls lists contents of value entered with or without path given. 2019-09-03 05:17:44 -05:00
Andrés N. Robalino
a449d2c005 If path to cd given. Report the error with the path given. 2019-09-03 03:49:20 -05:00
Andrés N. Robalino
b0a02518f9 cd can be awared inside a value entered. 2019-09-03 02:43:37 -05:00
Jonathan Turner
4a00887e9d
Merge pull request #578 from jonathandturner/fix_577
Fix line completion for extended chars
2019-09-03 07:09:26 +12:00
Jonathan Turner
95feb1ff16 Fix line completion for extended chars 2019-09-03 06:06:25 +12:00
Yehuda Katz
7fa09f59c2 Remove unused code
Closes #467
2019-09-01 23:11:05 -07:00
Andrés N. Robalino
ca0c6eaf58 This commit introduces a basic help feature. We can go to it
with the `help` command to explore and list all commands available.

Enter will also try to see if the location to be entered is an existing
Nu command, if it is it will let you inspect the command under `help`.

This provides baseline needed so we can iterate on it.
2019-08-31 19:06:11 -05:00
Yehuda Katz
138b5af82b Basic support for decimal numbers
This commit is more substantial than it looks: there was basically no
real support for decimals before, and that impacted values all the way
through.

I also made Size contain a decimal instead of an integer (`1.6kb` is a
reasonable thing to type), which impacted a bunch of code.

The biggest impact of this commit is that it creates many more possible
ways for valid nu types to fail to serialize as toml, json, etc. which
typically can't support the full range of Decimal (or Bigint, which I
also think we should support). This commit makes to-toml fallible, and a
similar effort is necessary for the rest of the serializations.

We also need to figure out how to clearly communicate to users what has
happened, but failing to serialize to toml seems clearly superior to me
than weird errors in basic math operations.
2019-08-30 21:05:32 -07:00
est31
c87fa14fc8 Replace crate visibility identifier with pub(crate)
Result of running:

find src -name *.rs -exec sed -i 's/crate /pub(crate) /g' {} \;
2019-08-29 13:09:09 +02:00
Jonathan Turner
570a0ac275 Fix path-related parts of value shell 2019-08-27 17:15:05 +12:00
Yehuda Katz
34292b282a 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:03:24 -07:00
Jonathan Turner
721a7b159d switch from reqwest to surf 2019-08-25 07:36:19 +12:00
Andrés N. Robalino
92ce31ac46 rmwrapped cd - ls 2019-08-22 23:51:43 -05:00
Jonathan Turner
03c29b9a6f Remove an unwrap 2019-08-23 04:44:31 +12:00
Jonathan Turner
399b411a9c
Merge pull request #332 from jonathandturner/fix_abs_ls
Fix absolute ls
2019-08-22 20:18:21 +12:00
Jonathan Turner
effbc56ec8 Fix absolute ls 2019-08-22 19:52:57 +12:00
Jonathan Turner
b9b462ffeb Remove dep import 2019-08-22 17:15:14 +12:00
Jonathan Turner
87930ba35a Fix tests 2019-08-22 16:23:57 +12:00
Jonathan Turner
303d27d4b6 Finish adding support for protecting value shells 2019-08-22 16:13:40 +12:00
Jonathan Turner
78ca297e47 WIP 2019-08-22 07:38:09 +12:00
Jonathan Turner
73e3402e2f Merge master 2019-08-22 05:09:23 +12:00
Jonathan Turner
e012e04da0 Disallow fileops on values 2019-08-22 05:03:59 +12:00
Jonathan Turner
953d28ef74 Fix push/pop for shell manager 2019-08-19 20:07:55 +12:00
Jonathan Turner
fef447a659 Fix shell-ring to rotate as expected 2019-08-19 19:29:27 +12:00
Yehuda Katz
5bfb96447a Reduce unwraps
Remove a number of unwraps. In some cases, a `?` just worked as is. I also made it possible to use `?` to go from Result<OutputStream, ShellError> to OutputStream. Finally, started updating PerItemCommand to be able to use the signature deserialization logic, which substantially reduces unwraps.

This is still in-progress work, but tests pass and it should be clear to merge and keep iterating on master.
2019-08-16 20:53:39 -07:00
Yehuda Katz
0dc4b2b686 Add support for external escape valve (^dir)
This commit makes it possible to force nu to treat a command as an external command by prefixing it with `^`. For example `^dir` will force `dir` to run an external command, even if `dir` is also a registered nu command.

This ensures that users don't need to leave nu just because we happened to use a command they need.

This commit adds a new token type for external commands, which, among other things, makes it pretty straight forward to syntax highlight external commands uniquely, and generally to treat them as special.
2019-08-15 15:18:18 -07:00
Jonathan Turner
dd18122a24 WIP 2019-08-15 17:02:02 +12:00