2019-11-21 15:33:14 +01:00
|
|
|
use crate::pretty::{b, DebugDocBuilder, PrettyDebugWithSource};
|
|
|
|
use crate::text::Text;
|
|
|
|
use crate::tracable::TracableContext;
|
|
|
|
|
2019-06-11 07:53:04 +02:00
|
|
|
use derive_new::new;
|
2019-06-22 03:36:57 +02:00
|
|
|
use getset::Getters;
|
2019-08-02 21:15:07 +02:00
|
|
|
use serde::Deserialize;
|
2019-07-13 04:18:02 +02:00
|
|
|
use serde::Serialize;
|
2019-09-14 18:30:24 +02:00
|
|
|
use std::path::{Path, PathBuf};
|
2019-06-11 07:53:04 +02:00
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
pub enum AnchorLocation {
|
|
|
|
Url(String),
|
|
|
|
File(String),
|
|
|
|
Source(Text),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait HasTag {
|
|
|
|
fn tag(&self) -> Tag;
|
|
|
|
}
|
|
|
|
|
2019-08-05 10:54:29 +02:00
|
|
|
#[derive(new, Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize, Hash)]
|
2019-10-13 06:12:43 +02:00
|
|
|
pub struct Spanned<T> {
|
|
|
|
pub span: Span,
|
|
|
|
pub item: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Spanned<T> {
|
|
|
|
pub fn map<U>(self, input: impl FnOnce(T) -> U) -> Spanned<U> {
|
|
|
|
let span = self.span;
|
|
|
|
|
|
|
|
let mapped = input(self.item);
|
|
|
|
mapped.spanned(span)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-04 16:47:03 +01:00
|
|
|
impl Spanned<String> {
|
|
|
|
pub fn items<'a, U>(
|
|
|
|
items: impl Iterator<Item = &'a Spanned<String>>,
|
|
|
|
) -> impl Iterator<Item = &'a str> {
|
2019-12-06 16:28:26 +01:00
|
|
|
items.map(|item| &item.item[..])
|
2019-11-04 16:47:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Spanned<String> {
|
|
|
|
pub fn borrow_spanned(&self) -> Spanned<&str> {
|
|
|
|
let span = self.span;
|
|
|
|
self.item[..].spanned(span)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-13 06:12:43 +02:00
|
|
|
pub trait SpannedItem: Sized {
|
|
|
|
fn spanned(self, span: impl Into<Span>) -> Spanned<Self> {
|
|
|
|
Spanned {
|
|
|
|
item: self,
|
|
|
|
span: span.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn spanned_unknown(self) -> Spanned<Self> {
|
|
|
|
Spanned {
|
|
|
|
item: self,
|
|
|
|
span: Span::unknown(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl<T> SpannedItem for T {}
|
|
|
|
|
|
|
|
impl<T> std::ops::Deref for Spanned<T> {
|
|
|
|
type Target = T;
|
|
|
|
|
|
|
|
fn deref(&self) -> &T {
|
|
|
|
&self.item
|
|
|
|
}
|
|
|
|
}
|
2019-11-21 15:33:14 +01:00
|
|
|
|
2019-10-13 06:12:43 +02:00
|
|
|
#[derive(new, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize, Hash)]
|
2019-08-01 05:25:59 +02:00
|
|
|
pub struct Tagged<T> {
|
|
|
|
pub tag: Tag,
|
2019-06-27 06:56:48 +02:00
|
|
|
pub item: T,
|
2019-06-11 07:53:04 +02:00
|
|
|
}
|
|
|
|
|
2019-11-04 16:47:03 +01:00
|
|
|
impl Tagged<String> {
|
|
|
|
pub fn borrow_spanned(&self) -> Spanned<&str> {
|
|
|
|
let span = self.tag.span;
|
|
|
|
self.item[..].spanned(span)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn borrow_tagged(&self) -> Tagged<&str> {
|
|
|
|
self.item[..].tagged(self.tag.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
impl<T> Tagged<Vec<T>> {
|
|
|
|
pub fn items(&self) -> impl Iterator<Item = &T> {
|
|
|
|
self.item.iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-14 18:30:24 +02:00
|
|
|
impl<T> HasTag for Tagged<T> {
|
|
|
|
fn tag(&self) -> Tag {
|
2019-10-13 06:12:43 +02:00
|
|
|
self.tag.clone()
|
2019-09-14 18:30:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<Path> for Tagged<PathBuf> {
|
|
|
|
fn as_ref(&self) -> &Path {
|
|
|
|
self.item.as_ref()
|
2019-07-24 00:22:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 05:25:59 +02:00
|
|
|
pub trait TaggedItem: Sized {
|
2019-08-05 10:54:29 +02:00
|
|
|
fn tagged(self, tag: impl Into<Tag>) -> Tagged<Self> {
|
2019-10-13 06:12:43 +02:00
|
|
|
Tagged {
|
|
|
|
item: self,
|
|
|
|
tag: tag.into(),
|
|
|
|
}
|
2019-07-09 06:31:26 +02:00
|
|
|
}
|
|
|
|
|
2019-07-08 18:44:53 +02:00
|
|
|
// For now, this is a temporary facility. In many cases, there are other useful spans that we
|
|
|
|
// could be using, such as the original source spans of JSON or Toml files, but we don't yet
|
|
|
|
// have the infrastructure to make that work.
|
2019-08-01 05:25:59 +02:00
|
|
|
fn tagged_unknown(self) -> Tagged<Self> {
|
2019-10-13 06:12:43 +02:00
|
|
|
Tagged {
|
|
|
|
item: self,
|
|
|
|
tag: Tag {
|
2019-08-05 10:54:29 +02:00
|
|
|
span: Span::unknown(),
|
2019-10-13 06:12:43 +02:00
|
|
|
anchor: None,
|
2019-08-05 10:54:29 +02:00
|
|
|
},
|
2019-10-13 06:12:43 +02:00
|
|
|
}
|
2019-07-08 18:44:53 +02:00
|
|
|
}
|
2019-06-29 10:55:42 +02:00
|
|
|
}
|
|
|
|
|
2019-08-01 05:25:59 +02:00
|
|
|
impl<T> TaggedItem for T {}
|
2019-06-29 10:55:42 +02:00
|
|
|
|
2019-08-01 05:25:59 +02:00
|
|
|
impl<T> std::ops::Deref for Tagged<T> {
|
2019-06-11 07:53:04 +02:00
|
|
|
type Target = T;
|
|
|
|
|
|
|
|
fn deref(&self) -> &T {
|
|
|
|
&self.item
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 05:25:59 +02:00
|
|
|
impl<T> Tagged<T> {
|
|
|
|
pub fn map<U>(self, input: impl FnOnce(T) -> U) -> Tagged<U> {
|
2019-08-05 10:54:29 +02:00
|
|
|
let tag = self.tag();
|
2019-06-11 07:53:04 +02:00
|
|
|
|
2019-08-01 05:25:59 +02:00
|
|
|
let mapped = input(self.item);
|
2019-10-13 06:12:43 +02:00
|
|
|
mapped.tagged(tag)
|
2019-08-01 05:25:59 +02:00
|
|
|
}
|
2019-06-11 07:53:04 +02:00
|
|
|
|
2019-11-03 09:49:06 +01:00
|
|
|
pub fn map_anchored(self, anchor: &Option<AnchorLocation>) -> Tagged<T> {
|
|
|
|
let mut tag = self.tag;
|
|
|
|
|
|
|
|
tag.anchor = anchor.clone();
|
|
|
|
|
|
|
|
Tagged {
|
|
|
|
item: self.item,
|
2019-12-06 16:28:26 +01:00
|
|
|
tag,
|
2019-11-03 09:49:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-04 16:47:03 +01:00
|
|
|
pub fn transpose(&self) -> Tagged<&T> {
|
|
|
|
Tagged {
|
|
|
|
item: &self.item,
|
|
|
|
tag: self.tag.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-05 10:54:29 +02:00
|
|
|
pub fn tag(&self) -> Tag {
|
2019-10-13 06:12:43 +02:00
|
|
|
self.tag.clone()
|
2019-08-05 10:54:29 +02:00
|
|
|
}
|
|
|
|
|
2019-09-18 08:37:04 +02:00
|
|
|
pub fn span(&self) -> Span {
|
|
|
|
self.tag.span
|
|
|
|
}
|
|
|
|
|
2019-10-13 06:12:43 +02:00
|
|
|
pub fn anchor(&self) -> Option<AnchorLocation> {
|
|
|
|
self.tag.anchor.clone()
|
2019-08-05 10:54:29 +02:00
|
|
|
}
|
|
|
|
|
2019-10-13 06:12:43 +02:00
|
|
|
pub fn anchor_name(&self) -> Option<String> {
|
|
|
|
match self.tag.anchor {
|
|
|
|
Some(AnchorLocation::File(ref file)) => Some(file.clone()),
|
|
|
|
Some(AnchorLocation::Url(ref url)) => Some(url.clone()),
|
2019-08-10 22:18:14 +02:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-05 10:54:29 +02:00
|
|
|
pub fn item(&self) -> &T {
|
|
|
|
&self.item
|
2019-07-08 18:44:53 +02:00
|
|
|
}
|
2019-08-16 00:18:18 +02:00
|
|
|
|
|
|
|
pub fn into_parts(self) -> (T, Tag) {
|
|
|
|
(self.item, self.tag)
|
|
|
|
}
|
2019-07-08 18:44:53 +02:00
|
|
|
}
|
|
|
|
|
2019-09-14 18:30:24 +02:00
|
|
|
impl From<&Tag> for Tag {
|
|
|
|
fn from(input: &Tag) -> Tag {
|
2019-10-13 06:12:43 +02:00
|
|
|
input.clone()
|
2019-06-22 03:36:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
impl<T> From<nom_locate::LocatedSpanEx<&str, T>> for Span {
|
|
|
|
fn from(input: nom_locate::LocatedSpanEx<&str, T>) -> Span {
|
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-18 00:26:27 +02:00
|
|
|
Span::new(input.offset, input.offset + input.fragment.len())
|
2019-06-22 03:36:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-14 18:30:24 +02:00
|
|
|
impl<T>
|
|
|
|
From<(
|
2019-10-13 06:12:43 +02:00
|
|
|
nom_locate::LocatedSpanEx<T, u64>,
|
|
|
|
nom_locate::LocatedSpanEx<T, u64>,
|
2019-09-14 18:30:24 +02:00
|
|
|
)> for Span
|
|
|
|
{
|
|
|
|
fn from(
|
|
|
|
input: (
|
2019-10-13 06:12:43 +02:00
|
|
|
nom_locate::LocatedSpanEx<T, u64>,
|
|
|
|
nom_locate::LocatedSpanEx<T, u64>,
|
2019-09-14 18:30:24 +02:00
|
|
|
),
|
|
|
|
) -> Span {
|
2019-12-04 22:14:52 +01:00
|
|
|
Span::new(input.0.offset, input.1.offset)
|
2019-06-11 07:53:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<(usize, usize)> for Span {
|
|
|
|
fn from(input: (usize, usize)) -> Span {
|
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-18 00:26:27 +02:00
|
|
|
Span::new(input.0, input.1)
|
2019-06-11 07:53:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&std::ops::Range<usize>> for Span {
|
|
|
|
fn from(input: &std::ops::Range<usize>) -> Span {
|
2019-12-04 22:14:52 +01:00
|
|
|
Span::new(input.start, input.end)
|
2019-08-01 05:25:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(
|
2019-10-13 06:12:43 +02:00
|
|
|
Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Serialize, Deserialize, Hash, Getters, new,
|
2019-08-01 05:25:59 +02:00
|
|
|
)]
|
|
|
|
pub struct Tag {
|
2019-10-13 06:12:43 +02:00
|
|
|
pub anchor: Option<AnchorLocation>,
|
2019-08-01 05:25:59 +02:00
|
|
|
pub span: Span,
|
|
|
|
}
|
|
|
|
|
2019-08-16 00:18:18 +02:00
|
|
|
impl From<Span> for Tag {
|
|
|
|
fn from(span: Span) -> Self {
|
2019-10-13 06:12:43 +02:00
|
|
|
Tag { anchor: None, span }
|
2019-08-16 00:18:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&Span> for Tag {
|
|
|
|
fn from(span: &Span) -> Self {
|
|
|
|
Tag {
|
2019-10-13 06:12:43 +02:00
|
|
|
anchor: None,
|
2019-08-16 00:18:18 +02:00
|
|
|
span: *span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-18 00:26:27 +02:00
|
|
|
impl From<(usize, usize, TracableContext)> for Tag {
|
2019-10-13 06:12:43 +02:00
|
|
|
fn from((start, end, _context): (usize, usize, TracableContext)) -> Self {
|
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-18 00:26:27 +02:00
|
|
|
Tag {
|
2019-10-13 06:12:43 +02:00
|
|
|
anchor: None,
|
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-18 00:26:27 +02:00
|
|
|
span: Span::new(start, end),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-13 06:12:43 +02:00
|
|
|
impl From<(usize, usize, AnchorLocation)> for Tag {
|
|
|
|
fn from((start, end, anchor): (usize, usize, AnchorLocation)) -> Self {
|
2019-09-14 18:30:24 +02:00
|
|
|
Tag {
|
2019-10-13 06:12:43 +02:00
|
|
|
anchor: Some(anchor),
|
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-18 00:26:27 +02:00
|
|
|
span: Span::new(start, end),
|
2019-09-14 18:30:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-13 06:12:43 +02:00
|
|
|
impl From<(usize, usize, Option<AnchorLocation>)> for Tag {
|
|
|
|
fn from((start, end, anchor): (usize, usize, Option<AnchorLocation>)) -> Self {
|
2019-09-14 18:30:24 +02:00
|
|
|
Tag {
|
2019-10-13 06:12:43 +02:00
|
|
|
anchor,
|
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-18 00:26:27 +02:00
|
|
|
span: Span::new(start, end),
|
2019-09-14 18:30:24 +02: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-18 00:26:27 +02:00
|
|
|
impl From<nom_locate::LocatedSpanEx<&str, TracableContext>> for Tag {
|
|
|
|
fn from(input: nom_locate::LocatedSpanEx<&str, TracableContext>) -> Tag {
|
2019-09-14 18:30:24 +02:00
|
|
|
Tag {
|
2019-10-13 06:12:43 +02:00
|
|
|
anchor: None,
|
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-18 00:26:27 +02:00
|
|
|
span: Span::new(input.offset, input.offset + input.fragment.len()),
|
2019-09-14 18:30:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-17 05:53:39 +02:00
|
|
|
impl From<Tag> for Span {
|
|
|
|
fn from(tag: Tag) -> Self {
|
|
|
|
tag.span
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&Tag> for Span {
|
|
|
|
fn from(tag: &Tag) -> Self {
|
|
|
|
tag.span
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-05 10:54:29 +02:00
|
|
|
impl Tag {
|
2019-09-29 07:13:56 +02:00
|
|
|
pub fn unknown_anchor(span: Span) -> Tag {
|
2019-10-13 06:12:43 +02:00
|
|
|
Tag { anchor: None, span }
|
2019-08-05 10:54:29 +02:00
|
|
|
}
|
|
|
|
|
2019-10-13 06:12:43 +02:00
|
|
|
pub fn for_char(pos: usize, anchor: AnchorLocation) -> Tag {
|
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
|
|
|
Tag {
|
2019-10-13 06:12:43 +02:00
|
|
|
anchor: Some(anchor),
|
2019-12-04 22:14:52 +01:00
|
|
|
span: Span::new(pos, pos + 1),
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-13 06:12:43 +02:00
|
|
|
pub fn unknown_span(anchor: AnchorLocation) -> Tag {
|
2019-09-14 18:30:24 +02:00
|
|
|
Tag {
|
2019-10-13 06:12:43 +02:00
|
|
|
anchor: Some(anchor),
|
2019-09-14 18:30:24 +02:00
|
|
|
span: Span::unknown(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-05 10:54:29 +02:00
|
|
|
pub fn unknown() -> Tag {
|
|
|
|
Tag {
|
2019-10-13 06:12:43 +02:00
|
|
|
anchor: None,
|
2019-08-05 10:54:29 +02:00
|
|
|
span: Span::unknown(),
|
|
|
|
}
|
|
|
|
}
|
2019-09-14 18:30:24 +02:00
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
pub fn anchor(&self) -> Option<AnchorLocation> {
|
|
|
|
self.anchor.clone()
|
|
|
|
}
|
|
|
|
|
2019-09-14 18:30:24 +02:00
|
|
|
pub fn until(&self, other: impl Into<Tag>) -> Tag {
|
|
|
|
let other = other.into();
|
2019-09-14 19:16:52 +02:00
|
|
|
debug_assert!(
|
2019-09-29 07:13:56 +02:00
|
|
|
self.anchor == other.anchor,
|
|
|
|
"Can only merge two tags with the same anchor"
|
2019-09-14 19:16:52 +02:00
|
|
|
);
|
2019-09-14 18:30:24 +02:00
|
|
|
|
|
|
|
Tag {
|
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-18 00:26:27 +02:00
|
|
|
span: Span::new(self.span.start, other.span.end),
|
2019-10-13 06:12:43 +02:00
|
|
|
anchor: self.anchor.clone(),
|
2019-09-14 18:30:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
pub fn until_option(&self, other: Option<impl Into<Tag>>) -> Tag {
|
|
|
|
match other {
|
|
|
|
Some(other) => {
|
|
|
|
let other = other.into();
|
|
|
|
debug_assert!(
|
|
|
|
self.anchor == other.anchor,
|
|
|
|
"Can only merge two tags with the same anchor"
|
|
|
|
);
|
|
|
|
|
|
|
|
Tag {
|
|
|
|
span: Span::new(self.span.start, other.span.end),
|
2019-10-13 06:12:43 +02:00
|
|
|
anchor: self.anchor.clone(),
|
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
|
|
|
}
|
|
|
|
}
|
2019-10-13 06:12:43 +02:00
|
|
|
None => self.clone(),
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-14 18:30:24 +02:00
|
|
|
pub fn slice<'a>(&self, source: &'a str) -> &'a str {
|
|
|
|
self.span.slice(source)
|
|
|
|
}
|
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-18 00:26:27 +02:00
|
|
|
|
|
|
|
pub fn string<'a>(&self, source: &'a str) -> String {
|
|
|
|
self.span.slice(source).to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tagged_slice<'a>(&self, source: &'a str) -> Tagged<&'a str> {
|
|
|
|
self.span.slice(source).tagged(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tagged_string<'a>(&self, source: &'a str) -> Tagged<String> {
|
|
|
|
self.span.slice(source).to_string().tagged(self)
|
|
|
|
}
|
2019-11-21 15:33:14 +01:00
|
|
|
|
|
|
|
pub fn anchor_name(&self) -> Option<String> {
|
|
|
|
match self.anchor {
|
|
|
|
Some(AnchorLocation::File(ref file)) => Some(file.clone()),
|
|
|
|
Some(AnchorLocation::Url(ref url)) => Some(url.clone()),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
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-18 00:26:27 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
#[allow(unused)]
|
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-18 00:26:27 +02:00
|
|
|
pub fn tag_for_tagged_list(mut iter: impl Iterator<Item = Tag>) -> Tag {
|
|
|
|
let first = iter.next();
|
|
|
|
|
|
|
|
let first = match first {
|
|
|
|
None => return Tag::unknown(),
|
|
|
|
Some(first) => first,
|
|
|
|
};
|
|
|
|
|
|
|
|
let last = iter.last();
|
|
|
|
|
2019-11-04 16:47:03 +01:00
|
|
|
match last {
|
|
|
|
None => first,
|
|
|
|
Some(last) => first.until(last),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(unused)]
|
|
|
|
pub fn span_for_spanned_list(mut iter: impl Iterator<Item = Span>) -> Span {
|
|
|
|
let first = iter.next();
|
|
|
|
|
|
|
|
let first = match first {
|
|
|
|
None => return Span::unknown(),
|
|
|
|
Some(first) => first,
|
|
|
|
};
|
|
|
|
|
|
|
|
let last = iter.last();
|
|
|
|
|
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-18 00:26:27 +02:00
|
|
|
match last {
|
|
|
|
None => first,
|
|
|
|
Some(last) => first.until(last),
|
|
|
|
}
|
2019-08-05 10:54:29 +02:00
|
|
|
}
|
|
|
|
|
2019-08-01 05:25:59 +02:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Serialize, Deserialize, Hash)]
|
|
|
|
pub struct Span {
|
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-18 00:26:27 +02:00
|
|
|
start: usize,
|
|
|
|
end: usize,
|
2019-08-01 05:25:59 +02:00
|
|
|
}
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
impl From<&Span> for Span {
|
|
|
|
fn from(span: &Span) -> Span {
|
|
|
|
*span
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 05:25:59 +02:00
|
|
|
impl From<Option<Span>> for Span {
|
|
|
|
fn from(input: Option<Span>) -> Span {
|
|
|
|
match input {
|
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-18 00:26:27 +02:00
|
|
|
None => Span::new(0, 0),
|
2019-08-01 05:25:59 +02:00
|
|
|
Some(span) => span,
|
2019-06-11 07:53:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Span {
|
2019-07-08 18:44:53 +02:00
|
|
|
pub fn unknown() -> Span {
|
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-18 00:26:27 +02:00
|
|
|
Span::new(0, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new(start: usize, end: usize) -> Span {
|
|
|
|
assert!(
|
|
|
|
end >= start,
|
|
|
|
"Can't create a Span whose end < start, start={}, end={}",
|
|
|
|
start,
|
|
|
|
end
|
|
|
|
);
|
|
|
|
|
|
|
|
Span { start, end }
|
2019-07-19 21:48:14 +02:00
|
|
|
}
|
|
|
|
|
2019-10-13 06:12:43 +02:00
|
|
|
pub fn for_char(pos: usize) -> Span {
|
|
|
|
Span {
|
|
|
|
start: pos,
|
|
|
|
end: pos + 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn until(&self, other: impl Into<Span>) -> Span {
|
|
|
|
let other = other.into();
|
|
|
|
|
|
|
|
Span::new(self.start, other.end)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn until_option(&self, other: Option<impl Into<Span>>) -> Span {
|
|
|
|
match other {
|
|
|
|
Some(other) => {
|
|
|
|
let other = other.into();
|
|
|
|
|
|
|
|
Span::new(self.start, other.end)
|
|
|
|
}
|
|
|
|
None => *self,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn string<'a>(&self, source: &'a str) -> String {
|
|
|
|
self.slice(source).to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn spanned_slice<'a>(&self, source: &'a str) -> Spanned<&'a str> {
|
|
|
|
self.slice(source).spanned(*self)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn spanned_string<'a>(&self, source: &'a str) -> Spanned<String> {
|
|
|
|
self.slice(source).to_string().spanned(*self)
|
|
|
|
}
|
|
|
|
|
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-18 00:26:27 +02:00
|
|
|
pub fn start(&self) -> usize {
|
|
|
|
self.start
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn end(&self) -> usize {
|
|
|
|
self.end
|
|
|
|
}
|
|
|
|
|
2019-07-08 18:44:53 +02:00
|
|
|
pub fn is_unknown(&self) -> bool {
|
|
|
|
self.start == 0 && self.end == 0
|
|
|
|
}
|
|
|
|
|
2019-08-29 14:16:11 +02:00
|
|
|
pub fn slice<'a>(&self, source: &'a str) -> &'a str {
|
2019-06-22 03:36:57 +02:00
|
|
|
&source[self.start..self.end]
|
2019-06-11 07:53:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl language_reporting::ReportingSpan for Span {
|
|
|
|
fn with_start(&self, start: usize) -> Self {
|
2019-12-04 22:14:52 +01:00
|
|
|
if self.end < start {
|
|
|
|
Span::new(start, start)
|
|
|
|
} else {
|
|
|
|
Span::new(start, self.end)
|
|
|
|
}
|
2019-06-11 07:53:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn with_end(&self, end: usize) -> Self {
|
2019-12-04 22:14:52 +01:00
|
|
|
if end < self.start {
|
|
|
|
Span::new(end, end)
|
|
|
|
} else {
|
|
|
|
Span::new(self.start, end)
|
|
|
|
}
|
2019-06-11 07:53:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn start(&self) -> usize {
|
|
|
|
self.start
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end(&self) -> usize {
|
|
|
|
self.end
|
|
|
|
}
|
|
|
|
}
|
2019-10-28 15:46:50 +01:00
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
pub trait HasSpan: PrettyDebugWithSource {
|
2019-10-28 15:46:50 +01:00
|
|
|
fn span(&self) -> Span;
|
|
|
|
}
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
pub trait HasFallibleSpan: PrettyDebugWithSource {
|
2019-10-28 15:46:50 +01:00
|
|
|
fn maybe_span(&self) -> Option<Span>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: HasSpan> HasFallibleSpan for T {
|
|
|
|
fn maybe_span(&self) -> Option<Span> {
|
|
|
|
Some(HasSpan::span(self))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> HasSpan for Spanned<T>
|
|
|
|
where
|
2019-11-21 15:33:14 +01:00
|
|
|
Spanned<T>: PrettyDebugWithSource,
|
2019-10-28 15:46:50 +01:00
|
|
|
{
|
|
|
|
fn span(&self) -> Span {
|
|
|
|
self.span
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
impl PrettyDebugWithSource for Option<Span> {
|
|
|
|
fn pretty_debug(&self, source: &str) -> DebugDocBuilder {
|
|
|
|
match self {
|
|
|
|
None => b::description("no span"),
|
|
|
|
Some(span) => span.pretty_debug(source),
|
|
|
|
}
|
2019-10-28 15:46:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
impl HasFallibleSpan for Option<Span> {
|
|
|
|
fn maybe_span(&self) -> Option<Span> {
|
|
|
|
*self
|
2019-10-28 15:46:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
impl PrettyDebugWithSource for Span {
|
|
|
|
fn pretty_debug(&self, source: &str) -> DebugDocBuilder {
|
|
|
|
b::typed(
|
|
|
|
"spanned",
|
|
|
|
b::keyword("for") + b::space() + b::description(format!("{:?}", source)),
|
|
|
|
)
|
2019-10-28 15:46:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HasSpan for Span {
|
|
|
|
fn span(&self) -> Span {
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
impl<T> PrettyDebugWithSource for Option<Spanned<T>>
|
2019-10-28 15:46:50 +01:00
|
|
|
where
|
2019-11-21 15:33:14 +01:00
|
|
|
Spanned<T>: PrettyDebugWithSource,
|
2019-10-28 15:46:50 +01:00
|
|
|
{
|
2019-11-21 15:33:14 +01:00
|
|
|
fn pretty_debug(&self, source: &str) -> DebugDocBuilder {
|
2019-10-28 15:46:50 +01:00
|
|
|
match self {
|
2019-11-21 15:33:14 +01:00
|
|
|
None => b::description("nothing"),
|
|
|
|
Some(v) => v.pretty_debug(source),
|
2019-10-28 15:46:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> HasFallibleSpan for Option<Spanned<T>>
|
|
|
|
where
|
2019-11-21 15:33:14 +01:00
|
|
|
Spanned<T>: PrettyDebugWithSource,
|
2019-10-28 15:46:50 +01:00
|
|
|
{
|
|
|
|
fn maybe_span(&self) -> Option<Span> {
|
|
|
|
match self {
|
|
|
|
None => None,
|
|
|
|
Some(value) => Some(value.span),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
impl<T> PrettyDebugWithSource for Option<Tagged<T>>
|
2019-10-28 15:46:50 +01:00
|
|
|
where
|
2019-11-21 15:33:14 +01:00
|
|
|
Tagged<T>: PrettyDebugWithSource,
|
2019-10-28 15:46:50 +01:00
|
|
|
{
|
2019-11-21 15:33:14 +01:00
|
|
|
fn pretty_debug(&self, source: &str) -> DebugDocBuilder {
|
2019-10-28 15:46:50 +01:00
|
|
|
match self {
|
2019-11-21 15:33:14 +01:00
|
|
|
None => b::description("nothing"),
|
|
|
|
Some(d) => d.pretty_debug(source),
|
2019-10-28 15:46:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> HasFallibleSpan for Option<Tagged<T>>
|
|
|
|
where
|
2019-11-21 15:33:14 +01:00
|
|
|
Tagged<T>: PrettyDebugWithSource,
|
2019-10-28 15:46:50 +01:00
|
|
|
{
|
|
|
|
fn maybe_span(&self) -> Option<Span> {
|
|
|
|
match self {
|
|
|
|
None => None,
|
|
|
|
Some(value) => Some(value.tag.span),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> HasSpan for Tagged<T>
|
|
|
|
where
|
2019-11-21 15:33:14 +01:00
|
|
|
Tagged<T>: PrettyDebugWithSource,
|
2019-10-28 15:46:50 +01:00
|
|
|
{
|
|
|
|
fn span(&self) -> Span {
|
|
|
|
self.tag.span
|
|
|
|
}
|
|
|
|
}
|