mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 22:50:14 +02:00
@ -24,6 +24,10 @@ pub enum ParseErrorReason {
|
||||
expected: &'static str,
|
||||
actual: Spanned<String>,
|
||||
},
|
||||
|
||||
/// An unexpected internal error has occurred
|
||||
InternalError { message: Spanned<String> },
|
||||
|
||||
/// The parser tried to parse an argument for a command, but it failed for
|
||||
/// some reason
|
||||
ArgumentError {
|
||||
@ -69,6 +73,15 @@ impl ParseError {
|
||||
}
|
||||
}
|
||||
|
||||
/// Construct a [ParseErrorReason::InternalError](ParseErrorReason::InternalError)
|
||||
pub fn internal_error(message: Spanned<impl Into<String>>) -> ParseError {
|
||||
ParseError {
|
||||
reason: ParseErrorReason::InternalError {
|
||||
message: message.item.into().spanned(message.span),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Construct a [ParseErrorReason::ArgumentError](ParseErrorReason::ArgumentError)
|
||||
pub fn argument_error(command: Spanned<impl Into<String>>, kind: ArgumentError) -> ParseError {
|
||||
ParseError {
|
||||
@ -89,6 +102,11 @@ impl From<ParseError> for ShellError {
|
||||
ParseErrorReason::Mismatch { actual, expected } => {
|
||||
ShellError::type_error(expected, actual)
|
||||
}
|
||||
ParseErrorReason::InternalError { message } => ShellError::labeled_error(
|
||||
format!("Internal error: {}", message.item),
|
||||
&message.item,
|
||||
&message.span,
|
||||
),
|
||||
ParseErrorReason::ArgumentError { command, error } => {
|
||||
ShellError::argument_error(command, error)
|
||||
}
|
||||
|
@ -676,7 +676,13 @@ impl FallibleColorSyntax for CommandHeadShape {
|
||||
if context.registry.has(name) {
|
||||
// If the registry has the command, color it as an internal command
|
||||
token_nodes.color_shape(FlatShape::InternalCommand.spanned(text));
|
||||
let signature = context.registry.get(name).unwrap();
|
||||
let signature = context.registry.get(name).ok_or_else(|| {
|
||||
ShellError::labeled_error(
|
||||
"Internal error: could not load signature from registry",
|
||||
"could not load from registry",
|
||||
text,
|
||||
)
|
||||
})?;
|
||||
Ok(CommandHeadKind::Internal(signature))
|
||||
} else {
|
||||
// Otherwise, color it as an external command
|
||||
@ -716,7 +722,9 @@ impl ExpandSyntax for CommandHeadShape {
|
||||
UnspannedToken::Bare => {
|
||||
let name = token_span.slice(context.source);
|
||||
if context.registry.has(name) {
|
||||
let signature = context.registry.get(name).unwrap();
|
||||
let signature = context.registry.get(name).ok_or_else(|| {
|
||||
ParseError::internal_error(name.spanned(token_span))
|
||||
})?;
|
||||
CommandSignature::Internal(signature.spanned(token_span))
|
||||
} else {
|
||||
CommandSignature::External(token_span)
|
||||
|
@ -683,7 +683,11 @@ impl ExpandSyntax for IntMemberShape {
|
||||
UnspannedAtomicToken::Number {
|
||||
number: RawNumber::Int(int),
|
||||
} => Ok(Member::Int(
|
||||
BigInt::from_str(int.slice(context.source)).unwrap(),
|
||||
BigInt::from_str(int.slice(context.source)).map_err(|_| {
|
||||
ParseError::internal_error(
|
||||
"can't convert from string to big int".spanned(int),
|
||||
)
|
||||
})?,
|
||||
int,
|
||||
)),
|
||||
|
||||
@ -732,7 +736,9 @@ impl ExpandSyntax for MemberShape {
|
||||
|
||||
if let Some(peeked) = number {
|
||||
let node = peeked.not_eof("column")?.commit();
|
||||
let (n, span) = node.as_number().unwrap();
|
||||
let (n, span) = node.as_number().ok_or_else(|| {
|
||||
ParseError::internal_error("can't convert node to number".spanned(node.span()))
|
||||
})?;
|
||||
|
||||
return Ok(Member::Number(n, span))
|
||||
}*/
|
||||
@ -741,7 +747,9 @@ impl ExpandSyntax for MemberShape {
|
||||
|
||||
if let Some(peeked) = string {
|
||||
let node = peeked.not_eof("column")?.commit();
|
||||
let (outer, inner) = node.as_string().unwrap();
|
||||
let (outer, inner) = node.as_string().ok_or_else(|| {
|
||||
ParseError::internal_error("can't convert node to string".spanned(node.span()))
|
||||
})?;
|
||||
|
||||
return Ok(Member::String(outer, inner));
|
||||
}
|
||||
|
@ -67,8 +67,8 @@ impl<'content, 'me> std::ops::Drop for Checkpoint<'content, 'me> {
|
||||
pub struct Peeked<'content, 'me> {
|
||||
pub(crate) node: Option<&'content TokenNode>,
|
||||
iterator: &'me mut TokensIterator<'content>,
|
||||
from: usize,
|
||||
to: usize,
|
||||
pub from: usize,
|
||||
pub to: usize,
|
||||
}
|
||||
|
||||
impl<'content, 'me> Peeked<'content, 'me> {
|
||||
|
Reference in New Issue
Block a user