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.
120 lines
3.2 KiB
Rust
120 lines
3.2 KiB
Rust
use crate::parser::hir::syntax_shape::expression::atom::{expand_atom, AtomicToken, ExpansionRule};
|
|
use crate::parser::hir::syntax_shape::{
|
|
expression::expand_file_path, ExpandContext, ExpandExpression, FallibleColorSyntax, FlatShape,
|
|
ParseError,
|
|
};
|
|
use crate::parser::{hir, hir::TokensIterator};
|
|
use crate::prelude::*;
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
pub struct FilePathShape;
|
|
|
|
#[cfg(not(coloring_in_tokens))]
|
|
impl FallibleColorSyntax for FilePathShape {
|
|
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> {
|
|
let atom = expand_atom(
|
|
token_nodes,
|
|
"file path",
|
|
context,
|
|
ExpansionRule::permissive(),
|
|
);
|
|
|
|
let atom = match atom {
|
|
Err(_) => return Ok(()),
|
|
Ok(atom) => atom,
|
|
};
|
|
|
|
match atom.item {
|
|
AtomicToken::Word { .. }
|
|
| AtomicToken::String { .. }
|
|
| AtomicToken::Number { .. }
|
|
| AtomicToken::Size { .. } => {
|
|
shapes.push(FlatShape::Path.spanned(atom.span));
|
|
}
|
|
|
|
_ => atom.color_tokens(shapes),
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
#[cfg(coloring_in_tokens)]
|
|
impl FallibleColorSyntax for FilePathShape {
|
|
type Info = ();
|
|
type Input = ();
|
|
|
|
fn name(&self) -> &'static str {
|
|
"FilePathShape"
|
|
}
|
|
|
|
fn color_syntax<'a, 'b>(
|
|
&self,
|
|
_input: &(),
|
|
token_nodes: &'b mut TokensIterator<'a>,
|
|
context: &ExpandContext,
|
|
) -> Result<(), ShellError> {
|
|
let atom = expand_atom(
|
|
token_nodes,
|
|
"file path",
|
|
context,
|
|
ExpansionRule::permissive(),
|
|
);
|
|
|
|
let atom = match atom {
|
|
Err(_) => return Ok(()),
|
|
Ok(atom) => atom,
|
|
};
|
|
|
|
match atom.item {
|
|
AtomicToken::Word { .. }
|
|
| AtomicToken::String { .. }
|
|
| AtomicToken::Number { .. }
|
|
| AtomicToken::Size { .. } => {
|
|
token_nodes.color_shape(FlatShape::Path.spanned(atom.span));
|
|
}
|
|
|
|
_ => token_nodes.mutate_shapes(|shapes| atom.color_tokens(shapes)),
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
impl ExpandExpression for FilePathShape {
|
|
fn name(&self) -> &'static str {
|
|
"file path"
|
|
}
|
|
|
|
fn expand_expr<'a, 'b>(
|
|
&self,
|
|
token_nodes: &mut TokensIterator<'_>,
|
|
context: &ExpandContext,
|
|
) -> Result<hir::Expression, ParseError> {
|
|
let atom = expand_atom(token_nodes, "file path", context, ExpansionRule::new())?;
|
|
|
|
match atom.item {
|
|
AtomicToken::Word { text: body } | AtomicToken::String { body } => {
|
|
let path = expand_file_path(body.slice(context.source), context);
|
|
return Ok(hir::Expression::file_path(path, atom.span));
|
|
}
|
|
|
|
AtomicToken::Number { .. } | AtomicToken::Size { .. } => {
|
|
let path = atom.span.slice(context.source);
|
|
return Ok(hir::Expression::file_path(path, atom.span));
|
|
}
|
|
|
|
_ => return atom.into_hir(context, "file path"),
|
|
}
|
|
}
|
|
}
|