mirror of
https://github.com/nushell/nushell.git
synced 2025-04-19 10:48:21 +02:00
This commit extracts five new crates: - nu-source, which contains the core source-code handling logic in Nu, including Text, Span, and also the pretty.rs-based debug logic - nu-parser, which is the parser and expander logic - nu-protocol, which is the bulk of the types and basic conveniences used by plugins - nu-errors, which contains ShellError, ParseError and error handling conveniences - nu-textview, which is the textview plugin extracted into a crate One of the major consequences of this refactor is that it's no longer possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so a lot of types became more concrete (Value became a concrete type instead of Spanned<Value>, for example). This also turned a number of inherent methods in the main nu crate into plain functions (impl Value {} became a bunch of functions in the `value` namespace in `crate::data::value`).
124 lines
3.6 KiB
Rust
124 lines
3.6 KiB
Rust
use crate::hir::syntax_shape::{
|
|
expand_atom, expand_variable, parse_single_node, AtomicToken, ExpandContext, ExpandExpression,
|
|
ExpansionRule, FallibleColorSyntax, FlatShape, TestSyntax, UnspannedAtomicToken,
|
|
};
|
|
use crate::hir::tokens_iterator::Peeked;
|
|
use crate::parse::tokens::UnspannedToken;
|
|
use crate::{hir, hir::TokensIterator};
|
|
use nu_errors::{ParseError, ShellError};
|
|
#[cfg(not(coloring_in_tokens))]
|
|
use nu_source::Spanned;
|
|
use nu_source::SpannedItem;
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
pub struct StringShape;
|
|
|
|
#[cfg(not(coloring_in_tokens))]
|
|
impl FallibleColorSyntax for StringShape {
|
|
type Info = ();
|
|
type Input = FlatShape;
|
|
|
|
fn color_syntax<'a, 'b>(
|
|
&self,
|
|
input: &FlatShape,
|
|
token_nodes: &'b mut TokensIterator<'a>,
|
|
context: &ExpandContext,
|
|
shapes: &mut Vec<Spanned<FlatShape>>,
|
|
) -> Result<(), ShellError> {
|
|
let atom = expand_atom(token_nodes, "string", context, ExpansionRule::permissive());
|
|
|
|
let atom = match atom {
|
|
Err(_) => return Ok(()),
|
|
Ok(atom) => atom,
|
|
};
|
|
|
|
match atom {
|
|
AtomicToken {
|
|
unspanned: UnspannedAtomicToken::String { .. },
|
|
span,
|
|
} => shapes.push((*input).spanned(span)),
|
|
other => other.color_tokens(shapes),
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
#[cfg(coloring_in_tokens)]
|
|
impl FallibleColorSyntax for StringShape {
|
|
type Info = ();
|
|
type Input = FlatShape;
|
|
|
|
fn name(&self) -> &'static str {
|
|
"StringShape"
|
|
}
|
|
|
|
fn color_syntax<'a, 'b>(
|
|
&self,
|
|
input: &FlatShape,
|
|
token_nodes: &'b mut TokensIterator<'a>,
|
|
context: &ExpandContext,
|
|
) -> Result<(), ShellError> {
|
|
let atom = expand_atom(token_nodes, "string", context, ExpansionRule::permissive());
|
|
|
|
let atom = match atom {
|
|
Err(_) => return Ok(()),
|
|
Ok(atom) => atom,
|
|
};
|
|
|
|
match atom {
|
|
AtomicToken {
|
|
unspanned: UnspannedAtomicToken::String { .. },
|
|
span,
|
|
} => token_nodes.color_shape((*input).spanned(span)),
|
|
atom => token_nodes.mutate_shapes(|shapes| atom.color_tokens(shapes)),
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
impl ExpandExpression for StringShape {
|
|
fn name(&self) -> &'static str {
|
|
"string"
|
|
}
|
|
|
|
fn expand_expr<'a, 'b>(
|
|
&self,
|
|
token_nodes: &mut TokensIterator<'_>,
|
|
context: &ExpandContext,
|
|
) -> Result<hir::Expression, ParseError> {
|
|
parse_single_node(token_nodes, "String", |token, token_span, err| {
|
|
Ok(match token {
|
|
UnspannedToken::GlobPattern
|
|
| UnspannedToken::Operator(..)
|
|
| UnspannedToken::ExternalWord => return Err(err.error()),
|
|
UnspannedToken::Variable(span) => {
|
|
expand_variable(span, token_span, &context.source)
|
|
}
|
|
UnspannedToken::ExternalCommand(span) => {
|
|
hir::Expression::external_command(span, token_span)
|
|
}
|
|
UnspannedToken::Number(_) => hir::Expression::bare(token_span),
|
|
UnspannedToken::Bare => hir::Expression::bare(token_span),
|
|
UnspannedToken::String(span) => hir::Expression::string(span, token_span),
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
impl TestSyntax for StringShape {
|
|
fn test<'a, 'b>(
|
|
&self,
|
|
token_nodes: &'b mut TokensIterator<'a>,
|
|
_context: &ExpandContext,
|
|
) -> Option<Peeked<'a, 'b>> {
|
|
let peeked = token_nodes.peek_any();
|
|
|
|
match peeked.node {
|
|
Some(token) if token.is_string() => Some(peeked),
|
|
_ => None,
|
|
}
|
|
}
|
|
}
|