mirror of
https://github.com/nushell/nushell.git
synced 2025-04-16 17:28:19 +02:00
Restructure and streamline token expansion The purpose of this commit is to streamline the token expansion code, by removing aspects of the code that are no longer relevant, removing pointless duplication, and eliminating the need to pass the same arguments to `expand_syntax`. The first big-picture change in this commit is that instead of a handful of `expand_` functions, which take a TokensIterator and ExpandContext, a smaller number of methods on the `TokensIterator` do the same job. The second big-picture change in this commit is fully eliminating the coloring traits, making coloring a responsibility of the base expansion implementations. This also means that the coloring tracer is merged into the expansion tracer, so you can follow a single expansion and see how the expansion process produced colored tokens. One side effect of this change is that the expander itself is marginally more error-correcting. The error correction works by switching from structured expansion to `BackoffColoringMode` when an unexpected token is found, which guarantees that all spans of the source are colored, but may not be the most optimal error recovery strategy. That said, because `BackoffColoringMode` only extends as far as a closing delimiter (`)`, `]`, `}`) or pipe (`|`), it does result in fairly granular correction strategy. The current code still produces an `Err` (plus a complete list of colored shapes) from the parsing process if any errors are encountered, but this could easily be addressed now that the underlying expansion is error-correcting. This commit also colors any spans that are syntax errors in red, and causes the parser to include some additional information about what tokens were expected at any given point where an error was encountered, so that completions and hinting could be more robust in the future. Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com> Co-authored-by: Andrés N. Robalino <andres@androbtech.com>
176 lines
4.8 KiB
Rust
176 lines
4.8 KiB
Rust
use crate::parse::token_tree::Token;
|
|
use crate::{
|
|
hir::syntax_shape::{ExpandSyntax, FlatShape, MaybeSpaceShape},
|
|
TokensIterator,
|
|
};
|
|
use derive_new::new;
|
|
use nu_errors::ParseError;
|
|
use nu_protocol::SpannedTypeName;
|
|
use nu_source::{b, DebugDocBuilder, HasSpan, PrettyDebug, Span, Spanned, SpannedItem};
|
|
|
|
#[derive(Debug, Eq, PartialEq, Clone, new)]
|
|
pub struct ExternalTokensSyntax {
|
|
pub tokens: Spanned<Vec<Spanned<String>>>,
|
|
}
|
|
|
|
impl HasSpan for ExternalTokensSyntax {
|
|
fn span(&self) -> Span {
|
|
self.tokens.span
|
|
}
|
|
}
|
|
|
|
impl PrettyDebug for ExternalTokensSyntax {
|
|
fn pretty(&self) -> DebugDocBuilder {
|
|
b::intersperse(
|
|
self.tokens
|
|
.iter()
|
|
.map(|token| b::primitive(format!("{:?}", token.item))),
|
|
b::space(),
|
|
)
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
pub struct ExternalTokensShape;
|
|
|
|
impl ExpandSyntax for ExternalTokensShape {
|
|
type Output = ExternalTokensSyntax;
|
|
|
|
fn name(&self) -> &'static str {
|
|
"external tokens"
|
|
}
|
|
|
|
fn expand<'a, 'b>(&self, token_nodes: &'b mut TokensIterator<'a>) -> ExternalTokensSyntax {
|
|
let mut out: Vec<Spanned<String>> = vec![];
|
|
|
|
let start = token_nodes.span_at_cursor();
|
|
|
|
loop {
|
|
match token_nodes.expand_syntax(ExternalExpressionShape) {
|
|
Err(_) => break,
|
|
Ok(span) => out.push(span.spanned_string(&token_nodes.source())),
|
|
}
|
|
}
|
|
|
|
let end = token_nodes.span_at_cursor();
|
|
|
|
ExternalTokensSyntax {
|
|
tokens: out.spanned(start.until(end)),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
pub struct ExternalExpressionShape;
|
|
|
|
impl ExpandSyntax for ExternalExpressionShape {
|
|
type Output = Result<Span, ParseError>;
|
|
|
|
fn name(&self) -> &'static str {
|
|
"external expression"
|
|
}
|
|
|
|
fn expand<'a, 'b>(&self, token_nodes: &'b mut TokensIterator<'a>) -> Result<Span, ParseError> {
|
|
token_nodes.expand_infallible(MaybeSpaceShape);
|
|
|
|
let first = token_nodes.expand_syntax(ExternalStartToken)?;
|
|
let mut last = first;
|
|
|
|
loop {
|
|
let continuation = token_nodes.expand_syntax(ExternalStartToken);
|
|
|
|
if let Ok(continuation) = continuation {
|
|
last = continuation;
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
|
|
Ok(first.until(last))
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
struct ExternalStartToken;
|
|
|
|
impl ExpandSyntax for ExternalStartToken {
|
|
type Output = Result<Span, ParseError>;
|
|
|
|
fn name(&self) -> &'static str {
|
|
"external start token"
|
|
}
|
|
fn expand<'a, 'b>(&self, token_nodes: &'b mut TokensIterator<'a>) -> Result<Span, ParseError> {
|
|
token_nodes.atomic_parse(|token_nodes| {
|
|
let mut span: Option<Span> = None;
|
|
|
|
loop {
|
|
let boundary = token_nodes.expand_infallible(PeekExternalBoundary);
|
|
|
|
if boundary {
|
|
break;
|
|
}
|
|
|
|
let peeked = token_nodes.peek().not_eof("external start token")?;
|
|
let node = peeked.node;
|
|
|
|
let new_span = match node.unspanned() {
|
|
Token::Comment(_)
|
|
| Token::Separator
|
|
| Token::Whitespace
|
|
| Token::Pipeline(_) => {
|
|
return Err(ParseError::mismatch(
|
|
"external start token",
|
|
node.spanned_type_name(),
|
|
))
|
|
}
|
|
|
|
_ => {
|
|
let node = peeked.commit();
|
|
node.span()
|
|
}
|
|
};
|
|
|
|
span = match span {
|
|
None => Some(new_span),
|
|
Some(before) => Some(before.until(new_span)),
|
|
};
|
|
}
|
|
|
|
match span {
|
|
None => Err(token_nodes.err_next_token("external start token")),
|
|
Some(span) => {
|
|
token_nodes.color_shape(FlatShape::ExternalWord.spanned(span));
|
|
Ok(span)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
struct PeekExternalBoundary;
|
|
|
|
impl ExpandSyntax for PeekExternalBoundary {
|
|
type Output = bool;
|
|
|
|
fn name(&self) -> &'static str {
|
|
"external boundary"
|
|
}
|
|
|
|
fn expand<'a, 'b>(&self, token_nodes: &'b mut TokensIterator<'a>) -> Self::Output {
|
|
let next = token_nodes.peek();
|
|
|
|
match next.node {
|
|
None => true,
|
|
Some(node) => match node.unspanned() {
|
|
Token::Delimited(_) => true,
|
|
Token::Whitespace => true,
|
|
Token::Comment(_) => true,
|
|
Token::Separator => true,
|
|
Token::Call(_) => true,
|
|
_ => false,
|
|
},
|
|
}
|
|
}
|
|
}
|