Add error messages where the errors are generated

This removes an awkward hack from ParseError::describe_with_prefix,
where it added errors for two error codes.

andor_in_pipeline was already there, so we just need bare_variable_assignment.
This commit is contained in:
Fabian Boehm 2024-01-04 18:05:03 +01:00
parent ee837f254b
commit 3213883510
2 changed files with 15 additions and 35 deletions

View File

@ -14,7 +14,8 @@ use crate::flog::FLOG;
use crate::parse_constants::{
token_type_user_presentable_description, ParseError, ParseErrorCode, ParseErrorList,
ParseErrorListFfi, ParseKeyword, ParseTokenType, ParseTreeFlags, SourceRange,
StatementDecoration, INVALID_PIPELINE_CMD_ERR_MSG, SOURCE_OFFSET_INVALID,
StatementDecoration, ERROR_BAD_COMMAND_ASSIGN_ERR_MSG, INVALID_PIPELINE_CMD_ERR_MSG,
SOURCE_OFFSET_INVALID,
};
use crate::parse_tree::ParseToken;
use crate::tokenizer::{
@ -3569,11 +3570,19 @@ impl<'s> Populator<'s> {
// Here we have a variable assignment which we chose to not parse as a variable
// assignment because there was no string after it.
// Ensure we consume the token, so we don't get back here again at the same place.
let token = &self.consume_any_token();
let text = &self.tokens.src
[token.source_start()..token.source_start() + token.source_length()];
let equals_pos = variable_assignment_equals_pos(text).unwrap();
let variable = &text[..equals_pos];
let value = &text[equals_pos + 1..];
parse_error!(
self,
self.consume_any_token(),
token,
ParseErrorCode::bare_variable_assignment,
""
ERROR_BAD_COMMAND_ASSIGN_ERR_MSG,
variable,
value
);
return got_error(self);
}

View File

@ -2,7 +2,6 @@
use crate::fallback::{fish_wcswidth, fish_wcwidth};
use crate::ffi::wcharz_t;
use crate::tokenizer::variable_assignment_equals_pos;
use crate::wchar::prelude::*;
use crate::wchar_ffi::{AsWstr, WCharFromFFI, WCharToFFI};
use bitflags::bitflags;
@ -385,38 +384,10 @@ impl ParseError {
skip_caret: bool,
) -> WString {
let mut result = prefix.to_owned();
// Some errors don't have their message passed in, so we construct them here.
// This affects e.g. `eval "a=(foo)"`
match self.code {
ParseErrorCode::andor_in_pipeline => {
let context = wstr::from_char_slice(
&src.as_char_slice()[self.source_start..self.source_start + self.source_length],
);
result += wstr::from_char_slice(
wgettext_fmt!(INVALID_PIPELINE_CMD_ERR_MSG, context).as_char_slice(),
);
}
ParseErrorCode::bare_variable_assignment => {
let context = wstr::from_char_slice(
&src.as_char_slice()[self.source_start..self.source_start + self.source_length],
);
let assignment_src = context;
#[allow(clippy::explicit_auto_deref)]
let equals_pos = variable_assignment_equals_pos(assignment_src).unwrap();
let variable = &assignment_src[..equals_pos];
let value = &assignment_src[equals_pos + 1..];
result += wstr::from_char_slice(
wgettext_fmt!(ERROR_BAD_COMMAND_ASSIGN_ERR_MSG, variable, value)
.as_char_slice(),
);
}
_ => {
if skip_caret && self.text.is_empty() {
return L!("").to_owned();
}
result += wstr::from_char_slice(self.text.as_char_slice());
}
if skip_caret && self.text.is_empty() {
return L!("").to_owned();
}
result += wstr::from_char_slice(self.text.as_char_slice());
let mut start = self.source_start;
let mut len = self.source_length;