Commit Graph

161 Commits

Author SHA1 Message Date
Andrés N. Robalino
540cc4016e Expand tilde in patterns. 2019-10-27 03:55:30 -05:00
Andrés N. Robalino
2706ae076d Move out tags when parsing and building tree nodes. 2019-10-25 18:31:25 -05:00
Jonathan Turner
d160e834eb rustyline git and add plus for filenames 2019-10-26 05:43:31 +13:00
Andrés N. Robalino
16751b5dee color escaped external command. 2019-10-22 19:29:45 -05:00
Yehuda Katz
6a7c00eaef Finish the job of moving shapes into the stream
This commit should finish the `coloring_in_tokens` feature, which moves
the shape accumulator into the token stream. This allows rollbacks of
the token stream to also roll back any shapes that were added.

This commit also adds a much nicer syntax highlighter trace, which shows
all of the paths the highlighter took to arrive at a particular coloring
output. This change is fairly substantial, but really improves the
understandability of the flow. I intend to update the normal parser with
a similar tracing view.

In general, this change also fleshes out the concept of "atomic" token
stream operations.

A good next step would be to try to make the parser more
error-correcting, using the coloring infrastructure. A follow-up step
would involve merging the parser and highlighter shapes themselves.
2019-10-22 16:19:22 -07:00
Jonathan Turner
f3c41bbdf1
Merge pull request #851 from t-hart/pr/remove-unwrap-unit
Deletes impl From<&str> for Unit
2019-10-20 07:29:07 +13:00
Thomas Hartmann
e913e26c01 Deletes impl From<&str>
The code still compiles, so this doesn't seem to break anything. That also means
it's not critical to fix it, but having dead code around isn't great either.
2019-10-18 20:02:24 +02:00
Antti Keränen
321629a693 Fix size comparison in 'where size'
Fixes #840
2019-10-17 22:57:02 +03:00
Yehuda Katz
d1ebc55ed7
Merge pull request #831 from nushell/coloring_in_tokens
Start moving coloring into the token stream
2019-10-14 18:31:21 -07:00
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
Andrés N. Robalino
43cf52275b Color escaped externals. 2019-10-14 14:09:44 -05:00
Jonathan Turner
2716bb020f
Fix #811 (#813) 2019-10-13 17:53:58 +13: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
Jonathan Turner
72e6222992 Switch to using Uuid::nil() and fix test 2019-09-18 19:05:33 +12:00
Yehuda Katz
17d2a27350 Fixed lints 2019-09-14 12:16:52 -05: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
Andrés N. Robalino
f47349c1a0
Merge pull request #632 from nushell/improve-external-words
Close a bunch of holes in external command args
2019-09-10 12:37:43 -05:00
Andrés N. Robalino
540e93aa3a question mark character can also be in glob patterns. 2019-09-10 12:26:56 -05: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
est31
1d3483b590 Add a test 2019-09-09 13:39:43 +02:00
est31
1277bfe0fb Fix setting configuration params
Fixes #627

Fixes a regression caused by #579, specifically commit cc8872b4ee .

The code was intended to perform a comparison between the wanted
output type and "Tagged<Value>" in order to be able to provide a
special-cased path for Tagged<Value>. When I wrote the code, I
used "name" as a variable name and only later realized that it
shadowed the "name" param to the function, so I renamed it to
type_name, but forgot to change the comparison.
This broke the special-casing, as the name param only contains
the name of the struct without generics (like "Tagged"), while
`std::any::type_name` (in the current implementation) contains
the full paths of the struct including all generic params
(like "nu::object::meta::Tagged<nu::object::base::Value>").
2019-09-09 13:22:18 +02: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
60212611e5
Allow leading space before head of pipeline 2019-09-05 04:13:07 +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
Jonathan Turner
7bd2fa1bfc
Merge pull request #580 from est31/trailing_spaces
Trim trailing whitespace and set it in editorconfig
2019-09-03 16:25:41 +12:00
est31
cf0efb811e Trim trailing whitespace and set it in editorconfig 2019-09-03 02:52:52 +02:00
est31
225ef8e75d Use serde to deserialize the remaining things 2019-09-03 02:10:48 +02:00
est31
cc8872b4ee Use serde to deserialize Tagged<Value> 2019-09-03 01:41:26 +02:00
est31
9ba2e75ac1 Move code into separate visit function 2019-09-03 01:32:54 +02:00
est31
e8880a1a57 Deserialize Block using serde 2019-09-02 22:30:51 +02:00
est31
9b3a561e83 Small refactor 2019-09-02 22:06:46 +02:00
Yehuda Katz
7fa09f59c2 Remove unused code
Closes #467
2019-09-01 23:11:05 -07:00
Yehuda Katz
246c9c06dc
Merge pull request #569 from est31/serde_instead_of_specialization
Remove use of ExtractType in deserialize_any
2019-09-01 22:39:13 -07:00
Yehuda Katz
3d5e31c55d
Merge pull request #571 from nushell/bigint
Migrated numerics to BigInt/BigDecimal
2019-09-01 22:08:48 -07:00
Yehuda Katz
8a29c9e6ab Migrated numerics to BigInt/BigDecimal
This commit migrates Value's numeric types to BigInt and BigDecimal. The
basic idea is that overflow errors aren't great in a shell environment,
and not really necessary.

The main immediate consequence is that new errors can occur when
serializing Nu values to other formats. You can see this in changes to
the various serialization formats (JSON, TOML, etc.). There's a new
`CoerceInto` trait that uses the `ToPrimitive` trait from `num_traits`
to attempt to coerce a `BigNum` or `BigDecimal` into a target type, and
produces a `RangeError` (kind of `ShellError`) if the coercion fails.

Another possible future consequence is that certain performance-critical
numeric operations might be too slow. If that happens, we can introduce
specialized numeric types to help improve the performance of those
situations, based on the real-world experience.
2019-09-01 21:00:30 -07:00
est31
113c2c380f deserialize_any isn't used any more 2019-09-02 04:07:02 +02:00
est31
bbde86c20d Use serde to deserialize bare bools
There are still tagged bools in use so we can't
remove the ExtractType implementation.
2019-09-02 03:45:00 +02:00
est31
a69a0bc5ee Use serde to deserialize options 2019-09-02 03:40:21 +02:00
est31
e8bbd330e0 Deserialize tuples with serde 2019-09-02 03:40:18 +02:00
est31
79a779dbea Deserialize vecs with serde 2019-09-02 03:37:30 +02:00
est31
5491b54859 Make key and struct_field optional in DeserializerItem
The main point of this struct seems to be debugging,
as key_struct_field is unused except for debugging.
2019-09-02 03:31:11 +02:00
Jonathan Turner
f9d54c2f25 Allow % in bare words 2019-09-02 12:32:15 +12:00
Jonathan Turner
6e0cb6b809
Merge pull request #563 from est31/field_shorthand
Adopt field init shorthand in a few places
2019-09-02 11:45:32 +12:00
est31
8504c7a8e6 Adopt field init shorthand in a few places
Found by running 'egrep "(\b[a-zA-Z]+): \1\b" -R src'
2019-09-01 23:39:59 +02:00