Use string interpolation.

This commit is contained in:
Stephen Chung 2022-09-26 22:12:28 +08:00
parent 8d1310c0f3
commit 8f1cb4aef7
2 changed files with 79 additions and 95 deletions

View File

@ -126,47 +126,45 @@ impl Error for EvalAltResult {}
impl fmt::Display for EvalAltResult {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ErrorSystem(s, err) => match s.as_str() {
"" => write!(f, "{}", err),
s => write!(f, "{}: {}", s, err),
}?,
Self::ErrorSystem(s, err) if s.is_empty() => write!(f, "{err}")?,
Self::ErrorSystem(s, err) => write!(f, "{s}: {err}")?,
Self::ErrorParsing(p, ..) => write!(f, "Syntax error: {}", p)?,
Self::ErrorParsing(p, ..) => write!(f, "Syntax error: {p}")?,
#[cfg(not(feature = "no_function"))]
Self::ErrorInFunctionCall(s, src, err, ..) if crate::parser::is_anonymous_fn(s) => {
write!(f, "{} in call to closure", err)?;
write!(f, "{err} in call to closure")?;
if !src.is_empty() {
write!(f, " @ '{}'", src)?;
write!(f, " @ '{src}'")?;
}
}
Self::ErrorInFunctionCall(s, src, err, ..) => {
write!(f, "{} in call to function {}", err, s)?;
write!(f, "{err} in call to function {s}")?;
if !src.is_empty() {
write!(f, " @ '{}'", src)?;
write!(f, " @ '{src}'")?;
}
}
Self::ErrorInModule(s, err, ..) if s.is_empty() => {
write!(f, "Error in module > {}", err)?
write!(f, "Error in module > {err}")?
}
Self::ErrorInModule(s, err, ..) => write!(f, "Error in module '{}' > {}", s, err)?,
Self::ErrorInModule(s, err, ..) => write!(f, "Error in module '{s}' > {err}")?,
Self::ErrorVariableExists(s, ..) => write!(f, "Variable already defined: {}", s)?,
Self::ErrorForbiddenVariable(s, ..) => write!(f, "Forbidden variable name: {}", s)?,
Self::ErrorVariableNotFound(s, ..) => write!(f, "Variable not found: {}", s)?,
Self::ErrorPropertyNotFound(s, ..) => write!(f, "Property not found: {}", s)?,
Self::ErrorIndexNotFound(s, ..) => write!(f, "Invalid index: {}", s)?,
Self::ErrorFunctionNotFound(s, ..) => write!(f, "Function not found: {}", s)?,
Self::ErrorModuleNotFound(s, ..) => write!(f, "Module not found: {}", s)?,
Self::ErrorVariableExists(s, ..) => write!(f, "Variable already defined: {s}")?,
Self::ErrorForbiddenVariable(s, ..) => write!(f, "Forbidden variable name: {s}")?,
Self::ErrorVariableNotFound(s, ..) => write!(f, "Variable not found: {s}")?,
Self::ErrorPropertyNotFound(s, ..) => write!(f, "Property not found: {s}")?,
Self::ErrorIndexNotFound(s, ..) => write!(f, "Invalid index: {s}")?,
Self::ErrorFunctionNotFound(s, ..) => write!(f, "Function not found: {s}")?,
Self::ErrorModuleNotFound(s, ..) => write!(f, "Module not found: {s}")?,
Self::ErrorDataRace(s, ..) => {
write!(f, "Data race detected when accessing variable: {}", s)?
write!(f, "Data race detected when accessing variable: {s}")?
}
Self::ErrorDotExpr(s, ..) => match s.as_str() {
"" => f.write_str("Malformed dot expression"),
s => f.write_str(s),
}?,
Self::ErrorIndexingType(s, ..) => write!(f, "Indexer unavailable: {}", s)?,
Self::ErrorDotExpr(s, ..) if s.is_empty() => f.write_str("Malformed dot expression")?,
Self::ErrorDotExpr(s, ..) => f.write_str(s)?,
Self::ErrorIndexingType(s, ..) => write!(f, "Indexer unavailable: {s}")?,
Self::ErrorUnboundThis(..) => f.write_str("'this' not bound")?,
Self::ErrorFor(..) => f.write_str("For loop expects an iterable type")?,
Self::ErrorTooManyOperations(..) => f.write_str("Too many operations")?,
@ -181,63 +179,57 @@ impl fmt::Display for EvalAltResult {
{
write!(f, "Runtime error")?
}
Self::ErrorRuntime(d, ..) => write!(f, "Runtime error: {}", d)?,
Self::ErrorRuntime(d, ..) => write!(f, "Runtime error: {d}")?,
Self::ErrorAssignmentToConstant(s, ..) => write!(f, "Cannot modify constant: {}", s)?,
Self::ErrorAssignmentToConstant(s, ..) => write!(f, "Cannot modify constant: {s}")?,
Self::ErrorMismatchOutputType(e, a, ..) => match (a.as_str(), e.as_str()) {
("", e) => write!(f, "Output type incorrect, expecting {}", e),
(a, "") => write!(f, "Output type incorrect: {}", a),
(a, e) => write!(f, "Output type incorrect: {} (expecting {})", a, e),
("", e) => write!(f, "Output type incorrect, expecting {e}"),
(a, "") => write!(f, "Output type incorrect: {a}"),
(a, e) => write!(f, "Output type incorrect: {a} (expecting {e})"),
}?,
Self::ErrorMismatchDataType(e, a, ..) => match (a.as_str(), e.as_str()) {
("", e) => write!(f, "Data type incorrect, expecting {}", e),
(a, "") => write!(f, "Data type incorrect: {}", a),
(a, e) => write!(f, "Data type incorrect: {} (expecting {})", a, e),
}?,
Self::ErrorArithmetic(s, ..) => match s.as_str() {
"" => f.write_str("Arithmetic error"),
s => f.write_str(s),
("", e) => write!(f, "Data type incorrect, expecting {e}"),
(a, "") => write!(f, "Data type incorrect: {a}"),
(a, e) => write!(f, "Data type incorrect: {a} (expecting {e})"),
}?,
Self::ErrorArithmetic(s, ..) if s.is_empty() => f.write_str("Arithmetic error")?,
Self::ErrorArithmetic(s, ..) => f.write_str(s)?,
Self::LoopBreak(true, ..) => f.write_str("'break' not inside a loop")?,
Self::LoopBreak(false, ..) => f.write_str("'continue' not inside a loop")?,
Self::Return(..) => f.write_str("NOT AN ERROR - function returns value")?,
Self::ErrorArrayBounds(max, index, ..) => match max {
0 => write!(f, "Array index {} out of bounds: array is empty", index),
0 => write!(f, "Array index {index} out of bounds: array is empty"),
1 => write!(
f,
"Array index {} out of bounds: only 1 element in array",
index
"Array index {index} out of bounds: only 1 element in array",
),
_ => write!(
f,
"Array index {} out of bounds: only {} elements in array",
index, max
"Array index {index} out of bounds: only {max} elements in array",
),
}?,
Self::ErrorStringBounds(max, index, ..) => match max {
0 => write!(f, "String index {} out of bounds: string is empty", index),
0 => write!(f, "String index {index} out of bounds: string is empty"),
1 => write!(
f,
"String index {} out of bounds: only 1 character in string",
index
"String index {index} out of bounds: only 1 character in string",
),
_ => write!(
f,
"String index {} out of bounds: only {} characters in string",
index, max
"String index {index} out of bounds: only {max} characters in string",
),
}?,
Self::ErrorBitFieldBounds(max, index, ..) => write!(
f,
"Bit-field index {} out of bounds: only {} bits in bit-field",
index, max
"Bit-field index {index} out of bounds: only {max} bits in bit-field",
)?,
Self::ErrorDataTooLarge(typ, ..) => write!(f, "{} exceeds maximum limit", typ)?,
Self::ErrorDataTooLarge(typ, ..) => write!(f, "{typ} exceeds maximum limit")?,
Self::ErrorCustomSyntax(s, tokens, ..) => write!(f, "{}: {}", s, tokens.join(" "))?,
Self::ErrorCustomSyntax(s, tokens, ..) => write!(f, "{s}: {}", tokens.join(" "))?,
}
// Do not write any position if None

View File

@ -184,77 +184,69 @@ impl ParseErrorType {
impl fmt::Display for ParseErrorType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::BadInput(err) => write!(f, "{}", err),
Self::BadInput(err) => write!(f, "{err}"),
Self::UnknownOperator(s) => write!(f, "Unknown operator: '{}'", s),
Self::UnknownOperator(s) => write!(f, "Unknown operator: '{s}'"),
Self::MalformedCallExpr(s) => match s.as_str() {
"" => f.write_str("Invalid expression in function call arguments"),
s => f.write_str(s)
},
Self::MalformedIndexExpr(s) => match s.as_str() {
"" => f.write_str("Invalid index in indexing expression"),
s => f.write_str(s)
},
Self::MalformedInExpr(s) => match s.as_str() {
"" => f.write_str("Invalid 'in' expression"),
s => f.write_str(s)
},
Self::MalformedCapture(s) => match s.as_str() {
"" => f.write_str("Invalid capturing"),
s => f.write_str(s)
},
Self::MalformedCallExpr(s) if s.is_empty() => f.write_str(s),
Self::MalformedCallExpr(..) => f.write_str("Invalid expression in function call arguments"),
Self::MalformedIndexExpr(s) if s.is_empty() => f.write_str("Invalid index in indexing expression"),
Self::MalformedIndexExpr(s) => f.write_str(s),
Self::MalformedInExpr(s) if s.is_empty() => f.write_str("Invalid 'in' expression"),
Self::MalformedInExpr(s) => f.write_str(s),
Self::MalformedCapture(s) if s.is_empty() => f.write_str("Invalid capturing"),
Self::MalformedCapture(s) => f.write_str(s),
Self::FnDuplicatedDefinition(s, n) => {
write!(f, "Function {} with ", s)?;
write!(f, "Function {s} with ")?;
match n {
0 => f.write_str("no parameters already exists"),
1 => f.write_str("1 parameter already exists"),
_ => write!(f, "{} parameters already exists", n),
_ => write!(f, "{n} parameters already exists"),
}
}
Self::FnMissingBody(s) => match s.as_str() {
"" => f.write_str("Expecting body statement block for anonymous function"),
s => write!(f, "Expecting body statement block for function {}", s)
},
Self::FnMissingParams(s) => write!(f, "Expecting parameters for function {}", s),
Self::FnDuplicatedParam(s, arg) => write!(f, "Duplicated parameter {} for function {}", arg, s),
Self::DuplicatedProperty(s) => write!(f, "Duplicated property for object map literal: {}", s),
Self::FnMissingBody(s) if s.is_empty() => f.write_str("Expecting body statement block for anonymous function"),
Self::FnMissingBody(s) => write!(f, "Expecting body statement block for function {s}"),
Self::FnMissingParams(s) => write!(f, "Expecting parameters for function {s}"),
Self::FnDuplicatedParam(s, arg) => write!(f, "Duplicated parameter {arg} for function {s}"),
Self::DuplicatedProperty(s) => write!(f, "Duplicated property for object map literal: {s}"),
#[allow(deprecated)]
Self::DuplicatedSwitchCase => f.write_str("Duplicated switch case"),
Self::DuplicatedVariable(s) => write!(f, "Duplicated variable name: {}", s),
Self::DuplicatedVariable(s) => write!(f, "Duplicated variable name: {s}"),
Self::VariableExists(s) => write!(f, "Variable already defined: {}", s),
Self::VariableUndefined(s) => write!(f, "Undefined variable: {}", s),
Self::ModuleUndefined(s) => write!(f, "Undefined module: {}", s),
Self::VariableExists(s) => write!(f, "Variable already defined: {s}"),
Self::VariableUndefined(s) => write!(f, "Undefined variable: {s}"),
Self::ModuleUndefined(s) => write!(f, "Undefined module: {s}"),
Self::MismatchedType(r, a) => write!(f, "Expecting {}, not {}", r, a),
Self::MismatchedType(r, a) => write!(f, "Expecting {r}, not {a}"),
Self::ExprExpected(s) => write!(f, "Expecting {} expression", s),
Self::MissingToken(token, s) => write!(f, "Expecting '{}' {}", token, s),
Self::MissingToken(token, s) => write!(f, "Expecting '{token}' {s}"),
Self::MissingSymbol(s) if s.is_empty() => f.write_str("Expecting a symbol"),
Self::MissingSymbol(s) => f.write_str(s),
Self::AssignmentToConstant(s) => match s.as_str() {
"" => f.write_str("Cannot assign to a constant value"),
s => write!(f, "Cannot assign to constant {}", s)
},
Self::AssignmentToInvalidLHS(s) => match s.as_str() {
"" => f.write_str("Expression cannot be assigned to"),
s => f.write_str(s)
},
Self::AssignmentToConstant(s) if s.is_empty() => f.write_str("Cannot assign to a constant value"),
Self::AssignmentToConstant(s) => write!(f, "Cannot assign to constant {s}"),
Self::LiteralTooLarge(typ, max) => write!(f, "{} exceeds the maximum limit ({})", typ, max),
Self::Reserved(s) if is_valid_identifier(s.chars()) => write!(f, "'{}' is a reserved keyword", s),
Self::Reserved(s) => write!(f, "'{}' is a reserved symbol", s),
Self::AssignmentToInvalidLHS(s) if s.is_empty() => f.write_str("Expression cannot be assigned to"),
Self::AssignmentToInvalidLHS(s) => f.write_str(s),
Self::LiteralTooLarge(typ, max) => write!(f, "{typ} exceeds the maximum limit ({max})"),
Self::Reserved(s) if is_valid_identifier(s.chars()) => write!(f, "'{s}' is a reserved keyword"),
Self::Reserved(s) => write!(f, "'{s}' is a reserved symbol"),
Self::UnexpectedEOF => f.write_str("Script is incomplete"),
Self::WrongSwitchIntegerCase => f.write_str("Integer switch case cannot follow a range case"),
Self::WrongSwitchDefaultCase => f.write_str("Default switch case must be the last"),
Self::WrongSwitchCaseCondition => f.write_str("This switch case cannot have a condition"),
Self::PropertyExpected => f.write_str("Expecting name of a property"),
Self::VariableExpected => f.write_str("Expecting name of a variable"),
Self::ForbiddenVariable(s) => write!(f, "Forbidden variable name: {}", s),
Self::ForbiddenVariable(s) => write!(f, "Forbidden variable name: {s}"),
Self::WrongFnDefinition => f.write_str("Function definitions must be at global level and cannot be inside a block or another function"),
Self::FnMissingName => f.write_str("Expecting function name in function declaration"),
Self::WrongDocComment => f.write_str("Doc-comment must be followed immediately by a function definition"),