Commit Graph

39 Commits

Author SHA1 Message Date
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
c720cc00e3 More 'did you mean?' errors 2019-09-24 08:24:51 +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
Jonathan Turner
b11a4535bd Bump compiler 2019-09-13 13:54:17 +12:00
Jonathan Turner
bee7c5639c
Revert "Migrate most uses of the Span concept to Tag" 2019-09-11 19:53:05 +12:00
Yehuda Katz
58b7800172 Migrate most uses of the Span concept to Tag
Also migrate mv, rm and commands like that to taking a
SyntaxType::Pattern instead of a SyntaxType::Path for their first
argument.
2019-09-10 20:41:03 -07: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
Jonathan Turner
dcd97b6346 Move internal terminology to tables/rows 2019-09-06 04:23:42 +12: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
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
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
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
aadacc2d36 Merge master 2019-08-09 16:51:21 +12:00
Jonathan Turner
99671b8ffc Move more parts to tags and away from spans 2019-08-05 20:54:29 +12:00
Yehuda Katz
fc173c46d8 Restructuring 2019-08-02 12:15:07 -07:00
Jonathan Turner
462f783fac initial change to Tagged<Value> 2019-08-01 13:58:42 +12:00
Yehuda Katz
5a8e041a48 Tests pass! 2019-07-23 15:22:11 -07:00
Yehuda Katz
2da12aed56 Tests pass 2019-07-12 19:20:26 -07:00
Yehuda Katz
7b68739b52 WIP 2019-07-12 19:20:26 -07:00
Yehuda Katz
3379c23a49 Support evaluating most expressions
Blocks, paths, and others

Plus a bunch of other infra improvements
2019-06-29 01:55:42 -07:00
Jonathan Turner
160074346f Fix units and add test 2019-06-24 19:59:23 +12:00
Yehuda Katz
7957fc502f Fix a bunch of bugs 2019-06-23 18:55:31 -06:00
Yehuda Katz
4036bf1ffd &str -> Text 2019-06-22 16:46:16 -04:00
Yehuda Katz
3b35dcb619 Finish nom upgrade 2019-06-22 10:08:53 -04:00
Yehuda Katz
e981129f1f Things work 2019-06-21 21:36:57 -04:00
Jonathan Turner
e94d1d2758 Add pretty errors to commands 2019-06-08 10:35:07 +12:00
Yehuda Katz
324f7915be Span all the things
Also set up builder infra for more consistent AST creation.
2019-06-05 23:34:59 -07:00
Yehuda Katz
f3bb4a03c2 Improve parser 2019-06-04 14:42:31 -07:00
Yehuda Katz
a8574abbf2 Improve errors 2019-06-02 22:11:21 -07:00
Jonathan Turner
2997d24f16
Merge branch 'master' into testing 2019-06-03 05:51:11 +12:00
Yehuda Katz
b9159f033b Parsing tests 2019-06-02 09:28:40 -07:00
Jonathan Turner
a62de9356c Add support for magic open 2019-06-02 07:20:48 +12:00
Yehuda Katz
69effbc9e7 Improve signature infrastructure
The `config` command uses different kinds of named arguments, which
illustrates how it works.
2019-05-31 22:54:15 -07:00
Yehuda Katz
9e9c0b9811 Fix bare word .
Addresses `git add .`
2019-05-30 22:45:57 -07:00
Yehuda Katz
b7d15c2afd Better errors and more fleshed out semantics 2019-05-29 21:19:46 -07:00
Jonathan Turner
8e00cd43a8 Add select command which opens cell 2019-05-29 16:02:36 +12:00
Yehuda Katz
d5255f6dbf
Evaluator MVP (#39)
Evaluator, MVP
2019-05-27 23:45:18 -07:00