2023-05-02 15:17:14 +00:00
use std ::{
fmt ::Display ,
str ::{ from_utf8 , Utf8Error } ,
} ;
use crate ::{ did_you_mean , Span , Type } ;
2021-09-20 21:37:26 +00:00
use miette ::Diagnostic ;
2023-04-26 13:15:42 +00:00
use serde ::{ Deserialize , Serialize } ;
2021-09-20 21:37:26 +00:00
use thiserror ::Error ;
2021-08-10 18:51:08 +00:00
2023-04-26 13:15:42 +00:00
#[ derive(Clone, Debug, Error, Diagnostic, Serialize, Deserialize) ]
2021-08-16 23:00:00 +00:00
pub enum ParseError {
2021-09-20 21:37:26 +00:00
/// The parser encountered unexpected tokens, when the code should have
/// finished. You should remove these or finish adding what you intended
/// to add.
#[ error( " Extra tokens in code. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::extra_tokens), help( " Try removing them. " )) ]
2021-09-20 21:37:26 +00:00
ExtraTokens ( #[ label = " extra tokens " ] Span ) ,
#[ error( " Extra positional argument. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::extra_positional), help( " Usage: {0} " )) ]
2022-01-03 23:14:33 +00:00
ExtraPositional ( String , #[ label = " extra positional argument " ] Span ) ,
2021-09-20 21:37:26 +00:00
2022-12-31 11:18:53 +00:00
#[ error( " Required positional parameter after optional parameter " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::required_after_optional)) ]
2022-03-07 20:08:56 +00:00
RequiredAfterOptional (
String ,
#[ label = " required parameter {0} after optional parameter " ] Span ,
) ,
2021-09-20 21:37:26 +00:00
#[ error( " Unexpected end of code. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::unexpected_eof)) ]
2021-09-22 05:29:53 +00:00
UnexpectedEof ( String , #[ label( " expected closing {0} " ) ] Span ) ,
2021-09-20 21:37:26 +00:00
#[ error( " Unclosed delimiter. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::unclosed_delimiter)) ]
2021-09-20 21:37:26 +00:00
Unclosed ( String , #[ label( " unclosed {0} " ) ] Span ) ,
2023-01-24 08:05:46 +00:00
#[ error( " Unbalanced delimiter. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::unbalanced_delimiter)) ]
2023-01-24 08:05:46 +00:00
Unbalanced ( String , String , #[ label( " unbalanced {0} and {1} " ) ] Span ) ,
2021-09-20 21:37:26 +00:00
#[ error( " Parse mismatch during operation. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::parse_mismatch)) ]
2023-05-24 19:53:57 +00:00
Expected ( & 'static str , #[ label( " expected {0} " ) ] Span ) ,
#[ error( " Parse mismatch during operation. " ) ]
#[ diagnostic(code(nu::parser::parse_mismatch_with_full_string_msg)) ]
ExpectedWithStringMsg ( String , #[ label( " expected {0} " ) ] Span ) ,
2021-09-20 21:37:26 +00:00
2023-07-14 03:20:35 +00:00
#[ error( " Command does not support {0} input. " ) ]
#[ diagnostic(code(nu::parser::input_type_mismatch)) ]
InputMismatch ( Type , #[ label( " command doesn't support {0} input " ) ] Span ) ,
#[ error( " Command output doesn't match {0}. " ) ]
#[ diagnostic(code(nu::parser::output_type_mismatch)) ]
OutputMismatch ( Type , #[ label( " command doesn't output {0} " ) ] Span ) ,
2021-09-20 21:37:26 +00:00
#[ error( " Type mismatch during operation. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::type_mismatch)) ]
2021-09-20 21:37:26 +00:00
Mismatch ( String , String , #[ label( " expected {0}, found {1} " ) ] Span ) , // expected, found, span
2022-12-07 23:02:11 +00:00
#[ error( " The '&&' operator is not supported in Nushell " ) ]
#[ diagnostic(
code ( nu ::parser ::shell_andand ) ,
help ( " use ';' instead of the shell '&&', or 'and' instead of the boolean '&&' " )
) ]
ShellAndAnd ( #[ label( " instead of '&&', use ';' or 'and' " ) ] Span ) ,
#[ error( " The '||' operator is not supported in Nushell " ) ]
#[ diagnostic(
code ( nu ::parser ::shell_oror ) ,
help ( " use 'try' instead of the shell '||', or 'or' instead of the boolean '||' " )
) ]
ShellOrOr ( #[ label( " instead of '||', use 'try' or 'or' " ) ] Span ) ,
#[ error( " The '2>' shell operation is 'err>' in Nushell. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::shell_err)) ]
2022-12-07 23:02:11 +00:00
ShellErrRedirect ( #[ label( " use 'err>' instead of '2>' in Nushell " ) ] Span ) ,
#[ error( " The '2>&1' shell operation is 'out+err>' in Nushell. " ) ]
#[ diagnostic(
code ( nu ::parser ::shell_outerr ) ,
help ( " Nushell redirection will write all of stdout before stderr. " )
) ]
ShellOutErrRedirect ( #[ label( " use 'out+err>' instead of '2>&1' in Nushell " ) ] Span ) ,
2023-04-07 21:32:44 +00:00
#[ error( " {0} is not supported on values of type {3} " ) ]
#[ diagnostic(code(nu::parser::unsupported_operation)) ]
UnsupportedOperationLHS (
String ,
#[ label = " doesn't support this value " ] Span ,
#[ label( " {3} " ) ] Span ,
Type ,
) ,
#[ error( " {0} is not supported between {3} and {5}. " ) ]
#[ diagnostic(code(nu::parser::unsupported_operation)) ]
UnsupportedOperationRHS (
String ,
#[ label = " doesn't support these values " ] Span ,
#[ label( " {3} " ) ] Span ,
2021-09-20 21:37:26 +00:00
Type ,
2023-04-07 21:32:44 +00:00
#[ label( " {5} " ) ] Span ,
2021-09-20 21:37:26 +00:00
Type ,
) ,
2022-11-11 06:51:08 +00:00
#[ error( " Capture of mutable variable. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::expected_keyword)) ]
2022-11-11 06:51:08 +00:00
CaptureOfMutableVar ( #[ label( " capture of mutable variable " ) ] Span ) ,
2021-09-20 21:37:26 +00:00
#[ error( " Expected keyword. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::expected_keyword)) ]
2021-09-20 21:37:26 +00:00
ExpectedKeyword ( String , #[ label( " expected {0} " ) ] Span ) ,
2021-09-28 18:12:46 +00:00
#[ error( " Unexpected keyword. " ) ]
#[ diagnostic(
code ( nu ::parser ::unexpected_keyword ) ,
2022-01-15 15:26:52 +00:00
help ( " '{0}' keyword is allowed only in a module. " )
2021-09-28 18:12:46 +00:00
) ]
UnexpectedKeyword ( String , #[ label( " unexpected {0} " ) ] Span ) ,
2023-03-10 21:20:31 +00:00
#[ error( " Can't create alias to parser keyword. " ) ]
#[ diagnostic(
code ( nu ::parser ::cant_alias_keyword ) ,
help ( " Only the following keywords can be aliased: {0}. " )
) ]
CantAliasKeyword ( String , #[ label( " not supported in alias " ) ] Span ) ,
2023-04-06 21:40:53 +00:00
#[ error( " Can't create alias to expression. " ) ]
#[ diagnostic(
code ( nu ::parser ::cant_alias_expression ) ,
help ( " Only command calls can be aliased. " )
) ]
CantAliasExpression ( String , #[ label( " aliasing {0} is not supported " ) ] Span ) ,
2022-11-26 21:59:43 +00:00
#[ error( " Unknown operator " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::unknown_operator), help( " {1} " )) ]
2022-11-26 21:59:43 +00:00
UnknownOperator (
& 'static str ,
& 'static str ,
#[ label( " Operator '{0}' not supported " ) ] Span ,
) ,
2022-01-15 15:26:52 +00:00
#[ error( " Statement used in pipeline. " ) ]
#[ diagnostic(
code ( nu ::parser ::unexpected_keyword ) ,
help (
" '{0}' keyword is not allowed in pipeline. Use '{0}' by itself, outside of a pipeline. "
)
) ]
2022-02-15 19:31:14 +00:00
BuiltinCommandInPipeline ( String , #[ label( " not allowed in pipeline " ) ] Span ) ,
2022-01-15 15:26:52 +00:00
2023-02-22 12:14:20 +00:00
#[ error( " {0} statement used in pipeline. " ) ]
2022-05-26 00:13:14 +00:00
#[ diagnostic(
code ( nu ::parser ::unexpected_keyword ) ,
help (
2023-02-22 12:14:20 +00:00
" Assigning '{1}' to '{2}' does not produce a value to be piped. If the pipeline result is meant to be assigned to '{2}', use '{0} {2} = ({1} | ...)'. "
2022-05-26 00:13:14 +00:00
)
) ]
2023-02-22 12:14:20 +00:00
AssignInPipeline ( String , String , String , #[ label( " '{0}' in pipeline " ) ] Span ) ,
2022-11-11 06:51:08 +00:00
2023-04-20 17:44:31 +00:00
#[ error( " `{0}` used as variable name. " ) ]
2022-06-24 21:55:25 +00:00
#[ diagnostic(
2023-04-20 17:44:31 +00:00
code ( nu ::parser ::name_is_builtin_var ) ,
help (
" '{0}' is the name of a builtin Nushell variable and cannot be used as a variable name "
)
2022-11-11 06:51:08 +00:00
) ]
2023-04-20 17:44:31 +00:00
NameIsBuiltinVar ( String , #[ label( " already a builtin variable " ) ] Span ) ,
2022-11-11 06:51:08 +00:00
2021-12-12 11:50:35 +00:00
#[ error( " Incorrect value " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::incorrect_value), help( " {2} " )) ]
2021-12-12 11:50:35 +00:00
IncorrectValue ( String , #[ label( " unexpected {0} " ) ] Span , String ) ,
2021-09-20 21:37:26 +00:00
#[ error( " Multiple rest params. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::multiple_rest_params)) ]
2021-09-20 21:37:26 +00:00
MultipleRestParams ( #[ label = " multiple rest params " ] Span ) ,
#[ error( " Variable not found. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::variable_not_found)) ]
2023-05-02 15:17:14 +00:00
VariableNotFound ( DidYouMean , #[ label = " variable not found. {0} " ] Span ) ,
2021-09-20 21:37:26 +00:00
2023-06-25 17:59:56 +00:00
#[ error( " Use $env.{0} instead of ${0}. " ) ]
#[ diagnostic(code(nu::parser::env_var_not_var)) ]
EnvVarNotVar ( String , #[ label = " use $env.{0} instead of ${0} " ] Span ) ,
2021-10-12 05:08:55 +00:00
#[ error( " Variable name not supported. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::variable_not_valid)) ]
2021-10-12 05:08:55 +00:00
VariableNotValid ( #[ label = " variable name can't contain spaces or quotes " ] Span ) ,
2022-09-28 22:08:38 +00:00
#[ error( " Alias name not supported. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::variable_not_valid)) ]
2022-12-27 23:00:44 +00:00
AliasNotValid (
#[ label = " alias name can't be a number, a filesize, or contain a hash # or caret ^ " ] Span ,
) ,
2022-12-22 20:31:34 +00:00
#[ error( " Command name not supported. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::variable_not_valid)) ]
2022-12-22 20:31:34 +00:00
CommandDefNotValid (
2022-12-27 23:00:44 +00:00
#[ label = " command name can't be a number, a filesize, or contain a hash # or caret ^ " ]
Span ,
2022-12-22 20:31:34 +00:00
) ,
2022-09-28 22:08:38 +00:00
2021-09-26 10:25:52 +00:00
#[ error( " Module not found. " ) ]
2022-03-25 21:43:46 +00:00
#[ diagnostic(
code ( nu ::parser ::module_not_found ) ,
2022-05-17 11:22:31 +00:00
help ( " module files and their paths must be available before your script is run as parsing occurs before anything is evaluated " )
2022-03-25 21:43:46 +00:00
) ]
2021-09-26 10:25:52 +00:00
ModuleNotFound ( #[ label = " module not found " ] Span ) ,
2023-05-12 22:20:33 +00:00
#[ error( " Missing mod.nu file. " ) ]
#[ diagnostic(
code ( nu ::parser ::module_missing_mod_nu_file ) ,
2023-09-23 13:30:03 +00:00
help ( " Directory {0} is missing a mod.nu file. \n \n When importing a directory as a Nushell module, it needs to contain a mod.nu file (can be empty). Alternatively, you can use .nu files in the directory as modules individually. " )
2023-05-12 22:20:33 +00:00
) ]
2023-09-23 13:30:03 +00:00
ModuleMissingModNuFile (
String ,
#[ label = " module directory is missing a mod.nu file " ] Span ,
) ,
2023-05-12 22:20:33 +00:00
2022-09-04 20:19:20 +00:00
#[ error( " Cyclical module import. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::cyclical_module_import), help( " {0} " )) ]
2022-09-04 20:19:20 +00:00
CyclicalModuleImport ( String , #[ label = " detected cyclical module import " ] Span ) ,
2023-01-22 19:34:15 +00:00
#[ error( " Can't export {0} named same as the module. " ) ]
#[ diagnostic(
code ( nu ::parser ::named_as_module ) ,
2023-05-06 18:39:54 +00:00
help ( " Module {1} can't export {0} named the same as the module. Either change the module name, or export `{2}` {0}. " )
2023-01-22 19:34:15 +00:00
) ]
NamedAsModule (
2023-05-06 18:39:54 +00:00
String ,
2023-01-22 19:34:15 +00:00
String ,
String ,
#[ label = " can't export from module {1} " ] Span ,
) ,
2023-05-06 18:39:54 +00:00
#[ error( " Module already contains 'main' command. " ) ]
#[ diagnostic(
code ( nu ::parser ::module_double_main ) ,
help ( " Tried to add 'main' command to module '{0}' but it has already been added. " )
) ]
ModuleDoubleMain (
String ,
#[ label = " module '{0}' already contains 'main' " ] Span ,
) ,
#[ error( " Invalid module file name " ) ]
#[ diagnostic(
code ( nu ::parser ::invalid_module_file_name ) ,
help ( " File {0} resolves to module name {1} which is the same as the parent module. Either rename the file or, save it as 'mod.nu' to define the parent module. " )
) ]
InvalidModuleFileName (
String ,
String ,
#[ label = " submodule can't have the same name as the parent module " ] Span ,
) ,
2023-01-22 19:34:15 +00:00
#[ error( " Can't export alias defined as 'main'. " ) ]
#[ diagnostic(
code ( nu ::parser ::export_main_alias_not_allowed ) ,
help ( " Exporting aliases as 'main' is not allowed. Either rename the alias or convert it to a custom command. " )
) ]
ExportMainAliasNotAllowed ( #[ label = " can't export from module " ] Span ) ,
2022-05-07 19:39:22 +00:00
#[ error( " Active overlay not found. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::active_overlay_not_found)) ]
2022-05-07 19:39:22 +00:00
ActiveOverlayNotFound ( #[ label = " not an active overlay " ] Span ) ,
2022-08-12 18:06:51 +00:00
#[ error( " Overlay prefix mismatch. " ) ]
#[ diagnostic(
code ( nu ::parser ::overlay_prefix_mismatch ) ,
help ( " Overlay {0} already exists {1} a prefix. To add it again, do it {1} the --prefix flag. " )
) ]
OverlayPrefixMismatch (
String ,
String ,
#[ label = " already exists {1} a prefix " ] Span ,
) ,
2022-05-07 19:39:22 +00:00
#[ error( " Module or overlay not found. " ) ]
#[ diagnostic(
code ( nu ::parser ::module_or_overlay_not_found ) ,
help ( " Requires either an existing overlay, a module, or an import pattern defining a module. " )
) ]
ModuleOrOverlayNotFound ( #[ label = " not a module or an overlay " ] Span ) ,
#[ error( " Cannot remove the last overlay. " ) ]
#[ diagnostic(
code ( nu ::parser ::cant_remove_last_overlay ) ,
help ( " At least one overlay must always be active. " )
) ]
CantRemoveLastOverlay ( #[ label = " this is the last overlay, can't remove it " ] Span ) ,
2022-08-21 14:27:56 +00:00
#[ error( " Cannot hide default overlay. " ) ]
2022-05-07 19:39:22 +00:00
#[ diagnostic(
2022-08-21 14:27:56 +00:00
code ( nu ::parser ::cant_hide_default_overlay ) ,
help ( " '{0}' is a default overlay. Default overlays cannot be hidden. " )
2022-05-07 19:39:22 +00:00
) ]
2022-08-21 14:27:56 +00:00
CantHideDefaultOverlay ( String , #[ label = " can't hide overlay " ] Span ) ,
2022-05-07 19:39:22 +00:00
2022-08-13 14:28:18 +00:00
#[ error( " Cannot add overlay. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::cant_add_overlay_help), help( " {0} " )) ]
2022-08-13 14:28:18 +00:00
CantAddOverlayHelp ( String , #[ label = " cannot add this overlay " ] Span ) ,
2021-11-15 23:16:06 +00:00
#[ error( " Not found. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::not_found)) ]
2021-11-15 23:16:06 +00:00
NotFound ( #[ label = " did not find anything under this name " ] Span ) ,
2021-10-01 20:16:27 +00:00
#[ error( " Duplicate command definition within a block. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::duplicate_command_def)) ]
2021-10-01 20:16:27 +00:00
DuplicateCommandDef ( #[ label = " defined more than once " ] Span ) ,
2021-09-20 21:37:26 +00:00
#[ error( " Unknown command. " ) ]
#[ diagnostic(
code ( nu ::parser ::unknown_command ) ,
2021-10-12 17:44:23 +00:00
// TODO: actual suggestions like "Did you mean `foo`?"
2021-09-20 21:37:26 +00:00
) ]
UnknownCommand ( #[ label = " unknown command " ] Span ) ,
2021-10-31 15:53:53 +00:00
#[ error( " Non-UTF8 string. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::non_utf8)) ]
2021-10-31 15:53:53 +00:00
NonUtf8 ( #[ label = " non-UTF8 string " ] Span ) ,
2021-09-20 21:37:26 +00:00
2021-09-21 04:03:06 +00:00
#[ error( " The `{0}` command doesn't have flag `{1}`. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::unknown_flag), help( " {3} " )) ]
2022-12-13 12:45:33 +00:00
UnknownFlag ( String , String , #[ label = " unknown flag " ] Span , String ) ,
2021-09-20 21:37:26 +00:00
#[ error( " Unknown type. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::unknown_type)) ]
2021-09-20 21:37:26 +00:00
UnknownType ( #[ label = " unknown type " ] Span ) ,
2022-01-03 23:14:33 +00:00
#[ error( " Missing flag argument. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::missing_flag_param)) ]
2022-01-03 23:14:33 +00:00
MissingFlagParam ( String , #[ label = " flag missing {0} argument " ] Span ) ,
2021-09-20 21:37:26 +00:00
2023-04-15 08:24:51 +00:00
#[ error( " Only the last flag in a short flag batch can take an argument. " ) ]
#[ diagnostic(code(nu::parser::only_last_flag_in_batch_can_take_arg)) ]
OnlyLastFlagInBatchCanTakeArg ( #[ label = " only the last flag can take args " ] Span ) ,
2021-09-20 21:37:26 +00:00
#[ error( " Missing required positional argument. " ) ]
2023-11-04 17:24:21 +00:00
#[ diagnostic(
code ( nu ::parser ::missing_positional ) ,
help ( " Usage: {2}. Use `--help` for more information. " )
) ]
2022-01-03 23:14:33 +00:00
MissingPositional ( String , #[ label( " missing {0} " ) ] Span , String ) ,
2021-09-20 21:37:26 +00:00
2022-01-03 23:14:33 +00:00
#[ error( " Missing argument to `{1}`. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::keyword_missing_arg)) ]
2022-01-03 23:14:33 +00:00
KeywordMissingArgument (
String ,
String ,
#[ label( " missing {0} value that follows {1} " ) ] Span ,
) ,
2021-09-20 21:37:26 +00:00
#[ error( " Missing type. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::missing_type)) ]
2021-09-20 21:37:26 +00:00
MissingType ( #[ label = " expected type " ] Span ) ,
#[ error( " Type mismatch. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::type_mismatch)) ]
2023-03-28 21:23:10 +00:00
TypeMismatch ( Type , Type , #[ label( " expected {0}, found {1} " ) ] Span ) , // expected, found, span
2021-09-20 21:37:26 +00:00
2023-10-02 18:13:31 +00:00
#[ error( " Type mismatch. " ) ]
#[ diagnostic(code(nu::parser::type_mismatch_help), help( " {3} " )) ]
TypeMismatchHelp ( Type , Type , #[ label( " expected {0}, found {1} " ) ] Span , String ) , // expected, found, span, help
2021-09-20 21:37:26 +00:00
#[ error( " Missing required flag. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::missing_required_flag)) ]
2021-09-20 21:37:26 +00:00
MissingRequiredFlag ( String , #[ label( " missing required flag {0} " ) ] Span ) ,
#[ error( " Incomplete math expression. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::incomplete_math_expression)) ]
2021-09-20 21:37:26 +00:00
IncompleteMathExpression ( #[ label = " incomplete math expression " ] Span ) ,
#[ error( " Unknown state. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::unknown_state)) ]
2021-09-20 21:37:26 +00:00
UnknownState ( String , #[ label( " {0} " ) ] Span ) ,
2021-11-15 23:16:06 +00:00
#[ error( " Internal error. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::unknown_state)) ]
2021-11-15 23:16:06 +00:00
InternalError ( String , #[ label( " {0} " ) ] Span ) ,
2021-09-20 21:37:26 +00:00
#[ error( " Parser incomplete. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::parser_incomplete)) ]
2021-09-20 21:37:26 +00:00
IncompleteParser ( #[ label = " parser support missing for this expression " ] Span ) ,
#[ error( " Rest parameter needs a name. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::rest_needs_name)) ]
2021-09-20 21:37:26 +00:00
RestNeedsName ( #[ label = " needs a parameter name " ] Span ) ,
2022-02-17 11:40:24 +00:00
#[ error( " Parameter not correct type. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::parameter_mismatch_type)) ]
2022-02-17 11:40:24 +00:00
ParameterMismatchType (
String ,
String ,
String ,
#[ label = " parameter {0} needs to be '{1}' instead of '{2}' " ] Span ,
) ,
2023-04-26 14:14:02 +00:00
#[ error( " Default values should be constant expressions. " ) ]
#[ diagnostic(code(nu::parser::non_constant_default_value)) ]
NonConstantDefaultValue ( #[ label = " expected a constant value " ] Span ) ,
2021-09-20 21:37:26 +00:00
#[ error( " Extra columns. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::extra_columns)) ]
2021-09-20 21:37:26 +00:00
ExtraColumns (
usize ,
#[ label( " expected {0} column{} " , if *.0 == 1 { " " } else { " s " }) ] Span ,
) ,
#[ error( " Missing columns. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::missing_columns)) ]
2021-09-20 21:37:26 +00:00
MissingColumns (
usize ,
#[ label( " expected {0} column{} " , if *.0 == 1 { " " } else { " s " }) ] Span ,
) ,
#[ error( " {0} " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::assignment_mismatch)) ]
2021-09-20 21:37:26 +00:00
AssignmentMismatch ( String , String , #[ label( " {1} " ) ] Span ) ,
2021-09-26 18:39:19 +00:00
#[ error( " Missing import pattern. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::missing_import_pattern)) ]
2021-09-26 18:39:19 +00:00
MissingImportPattern ( #[ label = " needs an import pattern " ] Span ) ,
2021-10-19 20:38:49 +00:00
#[ error( " Wrong import pattern structure. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::missing_import_pattern)) ]
2023-05-06 18:39:54 +00:00
WrongImportPattern ( String , #[ label = " {0} " ] Span ) ,
2021-10-19 20:38:49 +00:00
2021-11-15 23:16:06 +00:00
#[ error( " Export not found. " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::export_not_found)) ]
2021-09-26 18:39:19 +00:00
ExportNotFound ( #[ label = " could not find imports " ] Span ) ,
2021-10-31 08:17:01 +00:00
2022-03-25 21:43:46 +00:00
#[ error( " File not found " ) ]
#[ diagnostic(
code ( nu ::parser ::sourced_file_not_found ) ,
help ( " sourced files need to be available before your script is run " )
) ]
SourcedFileNotFound ( String , #[ label( " File not found: {0} " ) ] Span ) ,
#[ error( " File not found " ) ]
#[ diagnostic(
code ( nu ::parser ::registered_file_not_found ) ,
help ( " registered files need to be available before your script is run " )
) ]
RegisteredFileNotFound ( String , #[ label( " File not found: {0} " ) ] Span ) ,
2021-12-12 11:50:35 +00:00
#[ error( " File not found " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::parser::file_not_found)) ]
2021-12-12 11:50:35 +00:00
FileNotFound ( String , #[ label( " File not found: {0} " ) ] Span ) ,
2021-10-31 08:17:01 +00:00
2022-05-07 19:39:22 +00:00
/// Error while trying to read a file
///
/// ## Resolution
///
/// The error will show the result from a file operation
#[ error( " Error trying to read file " ) ]
2023-02-24 17:26:31 +00:00
#[ diagnostic(code(nu::shell::error_reading_file)) ]
2022-05-07 19:39:22 +00:00
ReadingFile ( String , #[ label( " {0} " ) ] Span ) ,
Syntax errors for string and int (#7952)
# Description
Added a few syntax errors in ints and strings, changed parser to stop
and show that error rather than continue trying to parse those tokens as
some other shape. However, I don't see how to push this direction much
further, and most of the classic confusing errors can't be changed.
Flagged as WIP for the moment, but passes all checks and works better
than current release:
1. I have yet to figure out how to make these errors refer back to the
book, as I see some other errors do.
2. How to give syntax error when malformed int is first token in line?
Currently parsed as external command, user gets confusing error message.
3. Would like to be more strict with *decimal* int literals (lacking,
e.g, `0x' prefix). Need to tinker more with the order of parse shape
calls, currently, float is tried after int, so '1.4' has to be passed.
_(Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.)_
```bash
〉"\z"
Error:
╭─[entry #3:1:1]
1 │ "\z"
· ─┬─
· ╰── Syntax error in string, unrecognized character after escape '\'.
╰────
```
Canonic presentation of a syntax error.
```bash
〉" \u{01ffbogus}"
Error:
× Invalid syntax
╭─[entry #2:1:1]
1 │ " \u{01ffbogus}"
· ───────┬──────
· ╰── Syntax error in string, expecting 1 to 6 hex digits in unicode escape '\u{X...}', max value 10FFFF.
╰────
```
Malformed unicode escape in string, flagged as error.
String parse can be opinionated, it's the last shape tried.
```bash
〉0x22bogus
Error: nu::shell::external_command (link)
× External command failed
╭─[entry #4:1:1]
1 │ 0x22bogus
· ────┬────
· ╰── executable was not found
╰────
help: No such file or directory (os error 2)
```
A *correct* number in first token would be evaluated, but an *incorrect*
one is treated as external command? Confusing to users.
```bash
〉0 + 0x22bogus
Error:
× Invalid syntax
╭─[entry #5:1:1]
1 │ 0 + 0x22bogus
· ────┬────
· ╰── Syntax error in int, invalid digits in radix 16 int.
╰────
```
Can give syntax error if token is unambiguously int literal. e.g has 0b
or 0x prefix, could not be a float.
```bash
〉0 + 098bogus
Error: nu::parser::unsupported_operation (link)
× Types mismatched for operation.
╭─[entry #6:1:1]
1 │ 0 + 098bogus
· ┬ ┬ ────┬───
· │ │ ╰── string
· │ ╰── doesn't support these values.
· ╰── int
╰────
help: Change int or string to be the right types and try again.
```
But *decimal* literal (no prefix) can't be too strict. Parser is going
to try float later. So '1.4' must be passed.
# User-Facing Changes
First and foremost, more specific error messages for typos in string and
int literals. Probably improves interactive user experience.
But a script that was causing and then checking for specific error might
notice a different error message.
_(List of all changes that impact the user experience here. This helps
us keep track of breaking changes.)_
# Tests + Formatting
Added (positive and negative unit tests in `cargo test -p nu-parser`.
Didn't add integration tests.
Make sure you've run and fixed any issues with these commands:
- [x] `cargo fmt --all -- --check` to check standard code formatting
(`cargo fmt --all` applies these changes)
- [x] `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- [x] `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
---------
Co-authored-by: Stefan Holderbach <sholderbach@users.noreply.github.com>
2023-02-13 16:09:50 +00:00
#[ error( " Invalid literal " ) ] // <problem> in <entity>.
#[ diagnostic() ]
InvalidLiteral ( String , String , #[ label( " {0} in {1} " ) ] Span ) ,
2021-12-02 05:42:56 +00:00
#[ error( " {0} " ) ]
#[ diagnostic() ]
LabeledError ( String , String , #[ label( " {1} " ) ] Span ) ,
2023-05-12 14:10:40 +00:00
#[ error( " {error} " ) ]
#[ diagnostic(help( " {help} " )) ]
LabeledErrorWithHelp {
error : String ,
label : String ,
help : String ,
#[ label( " {label} " ) ]
span : Span ,
} ,
2023-09-04 00:21:45 +00:00
#[ error( " Redirection can not be used with let/mut. " ) ]
#[ diagnostic() ]
RedirectionInLetMut (
#[ label( " Not allowed here " ) ] Span ,
#[ label( " ...and here " ) ] Option < Span > ,
) ,
2021-08-16 23:00:00 +00:00
}
2022-02-21 03:31:50 +00:00
impl ParseError {
pub fn span ( & self ) -> Span {
match self {
ParseError ::ExtraTokens ( s ) = > * s ,
ParseError ::ExtraPositional ( _ , s ) = > * s ,
ParseError ::UnexpectedEof ( _ , s ) = > * s ,
ParseError ::Unclosed ( _ , s ) = > * s ,
2023-01-24 08:05:46 +00:00
ParseError ::Unbalanced ( _ , _ , s ) = > * s ,
2022-02-21 03:31:50 +00:00
ParseError ::Expected ( _ , s ) = > * s ,
2023-05-24 19:53:57 +00:00
ParseError ::ExpectedWithStringMsg ( _ , s ) = > * s ,
2022-02-21 03:31:50 +00:00
ParseError ::Mismatch ( _ , _ , s ) = > * s ,
2023-04-07 21:32:44 +00:00
ParseError ::UnsupportedOperationLHS ( _ , _ , s , _ ) = > * s ,
ParseError ::UnsupportedOperationRHS ( _ , _ , _ , _ , s , _ ) = > * s ,
2022-02-21 03:31:50 +00:00
ParseError ::ExpectedKeyword ( _ , s ) = > * s ,
ParseError ::UnexpectedKeyword ( _ , s ) = > * s ,
2023-03-10 21:20:31 +00:00
ParseError ::CantAliasKeyword ( _ , s ) = > * s ,
2023-04-06 21:40:53 +00:00
ParseError ::CantAliasExpression ( _ , s ) = > * s ,
2022-02-21 03:31:50 +00:00
ParseError ::BuiltinCommandInPipeline ( _ , s ) = > * s ,
2023-02-22 12:14:20 +00:00
ParseError ::AssignInPipeline ( _ , _ , _ , s ) = > * s ,
2023-04-20 17:44:31 +00:00
ParseError ::NameIsBuiltinVar ( _ , s ) = > * s ,
2022-11-11 06:51:08 +00:00
ParseError ::CaptureOfMutableVar ( s ) = > * s ,
2022-02-21 03:31:50 +00:00
ParseError ::IncorrectValue ( _ , s , _ ) = > * s ,
ParseError ::MultipleRestParams ( s ) = > * s ,
2023-05-02 15:17:14 +00:00
ParseError ::VariableNotFound ( _ , s ) = > * s ,
2023-06-25 17:59:56 +00:00
ParseError ::EnvVarNotVar ( _ , s ) = > * s ,
2022-02-21 03:31:50 +00:00
ParseError ::VariableNotValid ( s ) = > * s ,
2022-09-28 22:08:38 +00:00
ParseError ::AliasNotValid ( s ) = > * s ,
2022-12-22 20:31:34 +00:00
ParseError ::CommandDefNotValid ( s ) = > * s ,
2022-02-21 03:31:50 +00:00
ParseError ::ModuleNotFound ( s ) = > * s ,
2023-09-23 13:30:03 +00:00
ParseError ::ModuleMissingModNuFile ( _ , s ) = > * s ,
2023-05-06 18:39:54 +00:00
ParseError ::NamedAsModule ( _ , _ , _ , s ) = > * s ,
ParseError ::ModuleDoubleMain ( _ , s ) = > * s ,
ParseError ::InvalidModuleFileName ( _ , _ , s ) = > * s ,
2023-01-22 19:34:15 +00:00
ParseError ::ExportMainAliasNotAllowed ( s ) = > * s ,
2022-09-04 20:19:20 +00:00
ParseError ::CyclicalModuleImport ( _ , s ) = > * s ,
2022-05-07 19:39:22 +00:00
ParseError ::ModuleOrOverlayNotFound ( s ) = > * s ,
ParseError ::ActiveOverlayNotFound ( s ) = > * s ,
2022-08-12 18:06:51 +00:00
ParseError ::OverlayPrefixMismatch ( _ , _ , s ) = > * s ,
2022-05-07 19:39:22 +00:00
ParseError ::CantRemoveLastOverlay ( s ) = > * s ,
2022-08-21 14:27:56 +00:00
ParseError ::CantHideDefaultOverlay ( _ , s ) = > * s ,
2022-08-13 14:28:18 +00:00
ParseError ::CantAddOverlayHelp ( _ , s ) = > * s ,
2022-02-21 03:31:50 +00:00
ParseError ::NotFound ( s ) = > * s ,
ParseError ::DuplicateCommandDef ( s ) = > * s ,
ParseError ::UnknownCommand ( s ) = > * s ,
ParseError ::NonUtf8 ( s ) = > * s ,
2022-12-13 12:45:33 +00:00
ParseError ::UnknownFlag ( _ , _ , s , _ ) = > * s ,
2022-03-07 20:08:56 +00:00
ParseError ::RequiredAfterOptional ( _ , s ) = > * s ,
2022-02-21 03:31:50 +00:00
ParseError ::UnknownType ( s ) = > * s ,
ParseError ::MissingFlagParam ( _ , s ) = > * s ,
2023-04-15 08:24:51 +00:00
ParseError ::OnlyLastFlagInBatchCanTakeArg ( s ) = > * s ,
2022-02-21 03:31:50 +00:00
ParseError ::MissingPositional ( _ , s , _ ) = > * s ,
ParseError ::KeywordMissingArgument ( _ , _ , s ) = > * s ,
ParseError ::MissingType ( s ) = > * s ,
ParseError ::TypeMismatch ( _ , _ , s ) = > * s ,
2023-10-02 18:13:31 +00:00
ParseError ::TypeMismatchHelp ( _ , _ , s , _ ) = > * s ,
2023-07-14 03:20:35 +00:00
ParseError ::InputMismatch ( _ , s ) = > * s ,
ParseError ::OutputMismatch ( _ , s ) = > * s ,
2022-02-21 03:31:50 +00:00
ParseError ::MissingRequiredFlag ( _ , s ) = > * s ,
ParseError ::IncompleteMathExpression ( s ) = > * s ,
ParseError ::UnknownState ( _ , s ) = > * s ,
ParseError ::InternalError ( _ , s ) = > * s ,
ParseError ::IncompleteParser ( s ) = > * s ,
ParseError ::RestNeedsName ( s ) = > * s ,
ParseError ::ParameterMismatchType ( _ , _ , _ , s ) = > * s ,
2023-04-26 14:14:02 +00:00
ParseError ::NonConstantDefaultValue ( s ) = > * s ,
2022-02-21 03:31:50 +00:00
ParseError ::ExtraColumns ( _ , s ) = > * s ,
ParseError ::MissingColumns ( _ , s ) = > * s ,
ParseError ::AssignmentMismatch ( _ , _ , s ) = > * s ,
ParseError ::MissingImportPattern ( s ) = > * s ,
2023-05-06 18:39:54 +00:00
ParseError ::WrongImportPattern ( _ , s ) = > * s ,
2022-02-21 03:31:50 +00:00
ParseError ::ExportNotFound ( s ) = > * s ,
2022-03-25 21:43:46 +00:00
ParseError ::SourcedFileNotFound ( _ , s ) = > * s ,
ParseError ::RegisteredFileNotFound ( _ , s ) = > * s ,
2022-02-21 03:31:50 +00:00
ParseError ::FileNotFound ( _ , s ) = > * s ,
2022-05-07 19:39:22 +00:00
ParseError ::ReadingFile ( _ , s ) = > * s ,
2022-02-21 03:31:50 +00:00
ParseError ::LabeledError ( _ , _ , s ) = > * s ,
2022-12-07 23:02:11 +00:00
ParseError ::ShellAndAnd ( s ) = > * s ,
ParseError ::ShellOrOr ( s ) = > * s ,
ParseError ::ShellErrRedirect ( s ) = > * s ,
ParseError ::ShellOutErrRedirect ( s ) = > * s ,
2022-11-26 21:59:43 +00:00
ParseError ::UnknownOperator ( _ , _ , s ) = > * s ,
Syntax errors for string and int (#7952)
# Description
Added a few syntax errors in ints and strings, changed parser to stop
and show that error rather than continue trying to parse those tokens as
some other shape. However, I don't see how to push this direction much
further, and most of the classic confusing errors can't be changed.
Flagged as WIP for the moment, but passes all checks and works better
than current release:
1. I have yet to figure out how to make these errors refer back to the
book, as I see some other errors do.
2. How to give syntax error when malformed int is first token in line?
Currently parsed as external command, user gets confusing error message.
3. Would like to be more strict with *decimal* int literals (lacking,
e.g, `0x' prefix). Need to tinker more with the order of parse shape
calls, currently, float is tried after int, so '1.4' has to be passed.
_(Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.)_
```bash
〉"\z"
Error:
╭─[entry #3:1:1]
1 │ "\z"
· ─┬─
· ╰── Syntax error in string, unrecognized character after escape '\'.
╰────
```
Canonic presentation of a syntax error.
```bash
〉" \u{01ffbogus}"
Error:
× Invalid syntax
╭─[entry #2:1:1]
1 │ " \u{01ffbogus}"
· ───────┬──────
· ╰── Syntax error in string, expecting 1 to 6 hex digits in unicode escape '\u{X...}', max value 10FFFF.
╰────
```
Malformed unicode escape in string, flagged as error.
String parse can be opinionated, it's the last shape tried.
```bash
〉0x22bogus
Error: nu::shell::external_command (link)
× External command failed
╭─[entry #4:1:1]
1 │ 0x22bogus
· ────┬────
· ╰── executable was not found
╰────
help: No such file or directory (os error 2)
```
A *correct* number in first token would be evaluated, but an *incorrect*
one is treated as external command? Confusing to users.
```bash
〉0 + 0x22bogus
Error:
× Invalid syntax
╭─[entry #5:1:1]
1 │ 0 + 0x22bogus
· ────┬────
· ╰── Syntax error in int, invalid digits in radix 16 int.
╰────
```
Can give syntax error if token is unambiguously int literal. e.g has 0b
or 0x prefix, could not be a float.
```bash
〉0 + 098bogus
Error: nu::parser::unsupported_operation (link)
× Types mismatched for operation.
╭─[entry #6:1:1]
1 │ 0 + 098bogus
· ┬ ┬ ────┬───
· │ │ ╰── string
· │ ╰── doesn't support these values.
· ╰── int
╰────
help: Change int or string to be the right types and try again.
```
But *decimal* literal (no prefix) can't be too strict. Parser is going
to try float later. So '1.4' must be passed.
# User-Facing Changes
First and foremost, more specific error messages for typos in string and
int literals. Probably improves interactive user experience.
But a script that was causing and then checking for specific error might
notice a different error message.
_(List of all changes that impact the user experience here. This helps
us keep track of breaking changes.)_
# Tests + Formatting
Added (positive and negative unit tests in `cargo test -p nu-parser`.
Didn't add integration tests.
Make sure you've run and fixed any issues with these commands:
- [x] `cargo fmt --all -- --check` to check standard code formatting
(`cargo fmt --all` applies these changes)
- [x] `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- [x] `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
---------
Co-authored-by: Stefan Holderbach <sholderbach@users.noreply.github.com>
2023-02-13 16:09:50 +00:00
ParseError ::InvalidLiteral ( _ , _ , s ) = > * s ,
2023-05-12 14:10:40 +00:00
ParseError ::LabeledErrorWithHelp { span : s , .. } = > * s ,
2023-09-04 00:21:45 +00:00
ParseError ::RedirectionInLetMut ( s , _ ) = > * s ,
2022-02-21 03:31:50 +00:00
}
}
}
2023-05-02 15:17:14 +00:00
#[ derive(Clone, Debug, Serialize, Deserialize) ]
pub struct DidYouMean ( Option < String > ) ;
fn did_you_mean_impl ( possibilities_bytes : & [ & [ u8 ] ] , input_bytes : & [ u8 ] ) -> Option < String > {
let input = from_utf8 ( input_bytes ) . ok ( ) ? ;
let possibilities = possibilities_bytes
. iter ( )
. map ( | p | from_utf8 ( p ) )
. collect ::< Result < Vec < & str > , Utf8Error > > ( )
. ok ( ) ? ;
did_you_mean ( & possibilities , input )
}
impl DidYouMean {
pub fn new ( possibilities_bytes : & [ & [ u8 ] ] , input_bytes : & [ u8 ] ) -> DidYouMean {
DidYouMean ( did_you_mean_impl ( possibilities_bytes , input_bytes ) )
}
}
impl From < Option < String > > for DidYouMean {
fn from ( value : Option < String > ) -> Self {
Self ( value )
}
}
impl Display for DidYouMean {
fn fmt ( & self , f : & mut std ::fmt ::Formatter < '_ > ) -> std ::fmt ::Result {
if let Some ( suggestion ) = & self . 0 {
write! ( f , " Did you mean '{}'? " , suggestion )
} else {
write! ( f , " " )
}
}
}