Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve error messages for invalid parameters #441

Merged
merged 3 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Source/Parser/Expressions/ComparisonExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ public override bool ReplaceVariables(InterpreterScope scope, out ExpressionBase
}

if (funcRefLeft != null)
result = new ErrorExpression(string.Format("Cannot compare function reference and {0}", right.Type), this.Location);
result = new ErrorExpression(string.Format("Cannot compare function reference and {0}", right.Type.ToLowerString()), this.Location);
else
result = new ErrorExpression(string.Format("Cannot compare {0} and function reference", left.Type), this.Location);
result = new ErrorExpression(string.Format("Cannot compare {0} and function reference", left.Type.ToLowerString()), this.Location);
return false;
}

Expand Down
24 changes: 24 additions & 0 deletions Source/Parser/Expressions/ErrorExpression.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Jamiras.Components;
using System;
using System.Text;

namespace RATools.Parser.Expressions
Expand Down Expand Up @@ -116,4 +117,27 @@ public UnknownVariableParseErrorExpression(string message, ExpressionBase expres
{
}
}

internal class ConversionErrorExpression : ErrorExpression
{
public ConversionErrorExpression(ExpressionBase value, ExpressionType expectedType)
: this(value, expectedType.ToLowerString(), value.Location)
{
}

public ConversionErrorExpression(ExpressionBase value, string expectedType, TextRange location)
: base(String.Format("Cannot convert {0} to {1}", value.Type.ToLowerString(), expectedType), location)
{
}

public ConversionErrorExpression(ExpressionBase value, ExpressionType expectedType, TextRange location, string parameterName)
: this(value, expectedType.ToLowerString(), location, parameterName)
{
}

public ConversionErrorExpression(ExpressionBase value, string expectedType, TextRange location, string parameterName)
: base(String.Format("{0}: Cannot convert {1} to {2}", parameterName, value.Type.ToLowerString(), expectedType), location)
{
}
}
}
121 changes: 0 additions & 121 deletions Source/Parser/Expressions/ExpressionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1107,125 +1107,4 @@ public virtual bool ReplaceVariables(InterpreterScope scope, out ExpressionBase
return null;
}
}

/// <summary>
/// The supported expression types.
/// </summary>
public enum ExpressionType
{
/// <summary>
/// Unspecified
/// </summary>
None = 0,

/// <summary>
/// A variable definition.
/// </summary>
Variable,

/// <summary>
/// An integer constant.
/// </summary>
IntegerConstant,

/// <summary>
/// A string constant.
/// </summary>
StringConstant,

/// <summary>
/// A boolean constant.
/// </summary>
BooleanConstant,

/// <summary>
/// A floating point constant.
/// </summary>
FloatConstant,

/// <summary>
/// A function call.
/// </summary>
FunctionCall,

/// <summary>
/// A mathematic equation.
/// </summary>
Mathematic,

/// <summary>
/// A comparison.
/// </summary>
Comparison,

/// <summary>
/// The conditional
/// </summary>
Conditional,

/// <summary>
/// An assignment.
/// </summary>
Assignment,

/// <summary>
/// A function definition.
/// </summary>
FunctionDefinition,

/// <summary>
/// A return statement.
/// </summary>
Return,

/// <summary>
/// A dictionary.
/// </summary>
Dictionary,

/// <summary>
/// An array.
/// </summary>
Array,

/// <summary>
/// A for loop.
/// </summary>
For,

/// <summary>
/// An if statement.
/// </summary>
If,

/// <summary>
/// A comment.
/// </summary>
Comment,

/// <summary>
/// A keyword.
/// </summary>
Keyword,

/// <summary>
/// An error.
/// </summary>
Error,

/// <summary>
/// A reference to a variable.
/// </summary>
VariableReference,

/// <summary>
/// A memory accessor.
/// </summary>
MemoryAccessor,

/// <summary>
/// A comparison of MemoryValues with a possible hit target.
/// </summary>
Requirement,
}
}
174 changes: 174 additions & 0 deletions Source/Parser/Expressions/ExpressionType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
namespace RATools.Parser.Expressions
{
/// <summary>
/// The supported expression types.
/// </summary>
public enum ExpressionType
{
/// <summary>
/// Unspecified
/// </summary>
None = 0,

/// <summary>
/// A variable definition.
/// </summary>
Variable,

/// <summary>
/// An integer constant.
/// </summary>
IntegerConstant,

/// <summary>
/// A string constant.
/// </summary>
StringConstant,

/// <summary>
/// A boolean constant.
/// </summary>
BooleanConstant,

/// <summary>
/// A floating point constant.
/// </summary>
FloatConstant,

/// <summary>
/// A function call.
/// </summary>
FunctionCall,

/// <summary>
/// A mathematic equation.
/// </summary>
Mathematic,

/// <summary>
/// A comparison.
/// </summary>
Comparison,

/// <summary>
/// The conditional
/// </summary>
Conditional,

/// <summary>
/// An assignment.
/// </summary>
Assignment,

/// <summary>
/// A function definition.
/// </summary>
FunctionDefinition,

/// <summary>
/// A return statement.
/// </summary>
Return,

/// <summary>
/// A dictionary.
/// </summary>
Dictionary,

/// <summary>
/// An array.
/// </summary>
Array,

/// <summary>
/// A for loop.
/// </summary>
For,

/// <summary>
/// An if statement.
/// </summary>
If,

/// <summary>
/// A comment.
/// </summary>
Comment,

/// <summary>
/// A keyword.
/// </summary>
Keyword,

/// <summary>
/// An error.
/// </summary>
Error,

/// <summary>
/// A reference to a variable.
/// </summary>
VariableReference,

/// <summary>
/// A memory accessor.
/// </summary>
MemoryAccessor,

/// <summary>
/// A comparison of MemoryValues with a possible hit target.
/// </summary>
Requirement,
}

internal static class ExpressionTypeExtension
{
public static string ToLowerString(this ExpressionType expressionType)
{
switch (expressionType)
{
case ExpressionType.Array: return "array";
case ExpressionType.Assignment: return "assignment";
case ExpressionType.BooleanConstant: return "boolean";
case ExpressionType.Comment: return "comment";
case ExpressionType.Comparison: return "comparison";
case ExpressionType.Conditional: return "conditional expression";
case ExpressionType.Dictionary: return "dictionary";
case ExpressionType.Error: return "error";
case ExpressionType.FloatConstant: return "decimal number";
case ExpressionType.For: return "for expression";
case ExpressionType.FunctionCall: return "function call";
case ExpressionType.FunctionDefinition: return "function definition";
case ExpressionType.If: return "if statement";
case ExpressionType.IntegerConstant: return "integer";
case ExpressionType.Keyword: return "keyword";
case ExpressionType.Mathematic: return "mathematic expression";
case ExpressionType.MemoryAccessor: return "memory accessor";
case ExpressionType.Requirement: return "requirement";
case ExpressionType.Return: return "return statement";
case ExpressionType.StringConstant: return "string";
case ExpressionType.Variable: return "variable";
case ExpressionType.VariableReference: return "variable reference";
default:
return expressionType.ToString().ToLower();
}
}

public static string ToArticleString(this ExpressionType expressionType)
{
var lowerString = ToLowerString(expressionType);
switch (lowerString[0])
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
return "an " + lowerString;

default:
return "a " + lowerString;
}
}
}
}
2 changes: 1 addition & 1 deletion Source/Parser/Expressions/FloatConstantExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public static ExpressionBase ConvertFrom(ExpressionBase expression)
break;

default:
return new ErrorExpression("Cannot convert to float", expression);
return new ConversionErrorExpression(expression, ExpressionType.FloatConstant);
}

expression.CopyLocation(floatExpression);
Expand Down
23 changes: 20 additions & 3 deletions Source/Parser/Expressions/FunctionCallExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,33 @@ private bool Evaluate(InterpreterScope scope, bool inAssignment, out ExpressionB
functionDefinition.Evaluate(functionParametersScope, out result);
}

if (result != null && result.Location.Start.Line == 0)
CopyLocation(result);

var error = result as ErrorExpression;
if (error != null)
{
if (error.Location.End.Line == 0)
CopyLocation(error);

result = ErrorExpression.WrapError(error, FunctionName.Name + " call failed", FunctionName);
return false;
}

if (result != null)
{
if (!result.IsReadOnly || result.Location.End.Line == 0)
{
CopyLocation(result);
}
else if (!result.Location.RangeEquals(Location))
{
var cloneable = result as ICloneableExpression;
if (cloneable != null)
{
result = cloneable.Clone();
CopyLocation(result);
}
}
}

var functionCall = result as FunctionCallExpression;
if (functionCall != null)
{
Expand Down
Loading
Loading