mirror of
https://github.com/nushell/nushell.git
synced 2025-05-19 06:04:35 +00:00
The original purpose of this PR was to modernize the external parser to use the new Shape system. This commit does include some of that change, but a more important aspect of this change is an improvement to the expansion trace. Previous commit 6a7c00ea adding trace infrastructure to the syntax coloring feature. This commit adds tracing to the expander. The bulk of that work, in addition to the tree builder logic, was an overhaul of the formatter traits to make them more general purpose, and more structured. Some highlights: - `ToDebug` was split into two traits (`ToDebug` and `DebugFormat`) because implementations needed to become objects, but a convenience method on `ToDebug` didn't qualify - `DebugFormat`'s `fmt_debug` method now takes a `DebugFormatter` rather than a standard formatter, and `DebugFormatter` has a new (but still limited) facility for structured formatting. - Implementations of `ExpandSyntax` need to produce output that implements `DebugFormat`. Unlike the highlighter changes, these changes are fairly focused in the trace output, so these changes aren't behind a flag.
125 lines
3.6 KiB
Rust
125 lines
3.6 KiB
Rust
use crate::parser::hir::syntax_shape::{
|
|
expand_atom, expand_bare, expression::expand_file_path, AtomicToken, ExpandContext,
|
|
ExpandExpression, ExpandSyntax, ExpansionRule, FallibleColorSyntax, FlatShape, ParseError,
|
|
};
|
|
use crate::parser::{hir, hir::TokensIterator, Operator, RawToken, TokenNode};
|
|
use crate::prelude::*;
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
pub struct PatternShape;
|
|
|
|
#[cfg(not(coloring_in_tokens))]
|
|
impl FallibleColorSyntax for PatternShape {
|
|
type Info = ();
|
|
type Input = ();
|
|
|
|
fn color_syntax<'a, 'b>(
|
|
&self,
|
|
_input: &(),
|
|
token_nodes: &'b mut TokensIterator<'a>,
|
|
context: &ExpandContext,
|
|
shapes: &mut Vec<Spanned<FlatShape>>,
|
|
) -> Result<(), ShellError> {
|
|
token_nodes.atomic(|token_nodes| {
|
|
let atom = expand_atom(token_nodes, "pattern", context, ExpansionRule::permissive())?;
|
|
|
|
match &atom.item {
|
|
AtomicToken::GlobPattern { .. } | AtomicToken::Word { .. } => {
|
|
shapes.push(FlatShape::GlobPattern.spanned(atom.span));
|
|
Ok(())
|
|
}
|
|
|
|
_ => Err(ShellError::type_error("pattern", atom.tagged_type_name())),
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
#[cfg(coloring_in_tokens)]
|
|
impl FallibleColorSyntax for PatternShape {
|
|
type Info = ();
|
|
type Input = ();
|
|
|
|
fn name(&self) -> &'static str {
|
|
"PatternShape"
|
|
}
|
|
|
|
fn color_syntax<'a, 'b>(
|
|
&self,
|
|
_input: &(),
|
|
token_nodes: &'b mut TokensIterator<'a>,
|
|
context: &ExpandContext,
|
|
) -> Result<(), ShellError> {
|
|
token_nodes.atomic(|token_nodes| {
|
|
let atom = expand_atom(token_nodes, "pattern", context, ExpansionRule::permissive())?;
|
|
|
|
match &atom.item {
|
|
AtomicToken::GlobPattern { .. } | AtomicToken::Word { .. } => {
|
|
token_nodes.color_shape(FlatShape::GlobPattern.spanned(atom.span));
|
|
Ok(())
|
|
}
|
|
|
|
_ => Err(ShellError::type_error("pattern", atom.tagged_type_name())),
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
impl ExpandExpression for PatternShape {
|
|
fn name(&self) -> &'static str {
|
|
"glob pattern"
|
|
}
|
|
|
|
fn expand_expr<'a, 'b>(
|
|
&self,
|
|
token_nodes: &mut TokensIterator<'_>,
|
|
context: &ExpandContext,
|
|
) -> Result<hir::Expression, ParseError> {
|
|
let atom = expand_atom(token_nodes, "pattern", context, ExpansionRule::new())?;
|
|
|
|
match atom.item {
|
|
AtomicToken::Word { text: body }
|
|
| AtomicToken::String { body }
|
|
| AtomicToken::GlobPattern { pattern: body } => {
|
|
let path = expand_file_path(body.slice(context.source), context);
|
|
return Ok(hir::Expression::pattern(path.to_string_lossy(), atom.span));
|
|
}
|
|
_ => return atom.into_hir(context, "pattern"),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
pub struct BarePatternShape;
|
|
|
|
impl ExpandSyntax for BarePatternShape {
|
|
type Output = Span;
|
|
|
|
fn name(&self) -> &'static str {
|
|
"bare pattern"
|
|
}
|
|
|
|
fn expand_syntax<'a, 'b>(
|
|
&self,
|
|
token_nodes: &'b mut TokensIterator<'a>,
|
|
context: &ExpandContext,
|
|
) -> Result<Span, ParseError> {
|
|
expand_bare(token_nodes, context, |token| match token {
|
|
TokenNode::Token(Spanned {
|
|
item: RawToken::Bare,
|
|
..
|
|
})
|
|
| TokenNode::Token(Spanned {
|
|
item: RawToken::Operator(Operator::Dot),
|
|
..
|
|
})
|
|
| TokenNode::Token(Spanned {
|
|
item: RawToken::GlobPattern,
|
|
..
|
|
}) => true,
|
|
|
|
_ => false,
|
|
})
|
|
}
|
|
}
|