Skip to content

Commit

Permalink
Improvement and standardization of error messages #286
Browse files Browse the repository at this point in the history
  • Loading branch information
mariuszgromada committed Dec 10, 2022
1 parent d25e875 commit c9c2ff4
Show file tree
Hide file tree
Showing 26 changed files with 2,381 additions and 879 deletions.
3 changes: 2 additions & 1 deletion CURRENT/c-sharp/1-Current-Dev.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@
<Compile Include="src\org\mariuszgromada\math\mxparser\syntaxchecker\SyntaxCheckerTokenManager.cs" />
<Compile Include="src\org\mariuszgromada\math\mxparser\syntaxchecker\Token.cs" />
<Compile Include="src\org\mariuszgromada\math\mxparser\syntaxchecker\TokenMgrError.cs" />
<Compile Include="src\org\mariuszgromada\math\mxparser\Tutorial.cs" />
<Compile Include="src\org\mariuszgromada\math\mxparser\SerializationUtils.cs" />
<Compile Include="src\org\mariuszgromada\math\mxparser\StringResources.cs" />
<Compile Include="src\org\mariuszgromada\math\mxparser\Tutorial.cs" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.0,Profile=Client">
Expand Down
85 changes: 50 additions & 35 deletions CURRENT/c-sharp/src/org/mariuszgromada/math/mxparser/Argument.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* @(#)Argument.cs 5.1.0 2022-11-11
* @(#)Argument.cs 5.2.0 2022-12-09
*
* MathParser.org-mXparser DUAL LICENSE AGREEMENT as of date 2022-05-22
* The most up-to-date license is available at the below link:
Expand Down Expand Up @@ -226,7 +226,7 @@ namespace org.mariuszgromada.math.mxparser {
* <a href="https://play.google.com/store/apps/details?id=org.mathparser.scalar.pro" target="_blank">Scalar Pro</a><br>
* <a href="https://mathspace.pl" target="_blank">MathSpace.pl</a><br>
*
* @version 5.1.0
* @version 5.2.0
*
* @see RecursiveArgument
* @see Expression
Expand All @@ -248,7 +248,7 @@ public class Argument : PrimitiveElement {
*/
public const double ARGUMENT_INITIAL_VALUE = Double.NaN;
/**
* When argument was not not found
* When argument was not found
*/
public const int NOT_FOUND = Expression.NOT_FOUND;
/**
Expand All @@ -267,8 +267,8 @@ public class Argument : PrimitiveElement {
* Argument type id for the definition of key words
* known by the parser.
*/
public const int TYPE_ID = 101;
public const String TYPE_DESC = "User defined argument";
public const int TYPE_ID = 101;
public static readonly String TYPE_DESC = StringResources.USER_DEFINED_ARGUMENT;
/**
* Argument with body based on the value or expression string.
*
Expand Down Expand Up @@ -300,7 +300,7 @@ public class Argument : PrimitiveElement {
/**
* Description of the argument.
*/
private String description;
private String description = "";
/**
* Argument expression for dependent and recursive
* arguments.
Expand All @@ -309,7 +309,7 @@ public class Argument : PrimitiveElement {
/**
* Argument name (x, y, arg1, my_argument, etc...)
*/
private String argumentName;
private String argumentName = "";
/**
* Argument type (free, dependent)
*/
Expand All @@ -330,6 +330,12 @@ public class Argument : PrimitiveElement {
*
*=================================================
*/
private static String buildErrorMessageInvalidArgumentName(String argumentName) {
return StringResources.buildErrorMessagePatternDoesNotMatchWithExamples(argumentName, StringResources.INVALID_ARGUMENT_NAME, StringInvariant.ARGUMENT_NAME_EXAMPLES);
}
private static String buildErrorMessageInvalidArgumentDefinition(String argumentDefinitionString) {
return StringResources.buildErrorMessagePatternDoesNotMatchWithExamples(argumentDefinitionString, StringResources.INVALID_ARGUMENT_DEFINITION, StringInvariant.ARGUMENT_DEFINITION_EXAMPLES);
}
/**
* Default constructor - creates argument based on the argument definition string.
*
Expand All @@ -343,30 +349,34 @@ public class Argument : PrimitiveElement {
*
* @param elements Optional parameters (comma separated) such as Arguments, Constants, Functions
*/
public Argument(String argumentDefinitionString, params PrimitiveElement[] elements) : base(Argument.TYPE_ID)
{
public Argument(String argumentDefinitionString, params PrimitiveElement[] elements) : base(Argument.TYPE_ID) {
if (mXparser.regexMatch(argumentDefinitionString, ParserSymbol.nameOnlyTokenRegExp)) {
argumentName = argumentDefinitionString;
argumentValue = ARGUMENT_INITIAL_VALUE;
argumentType = FREE_ARGUMENT;
argumentExpression = new Expression(elements);
}
argumentExpression.setDescription(argumentName);
}
else if (mXparser.regexMatch(argumentDefinitionString, ParserSymbol.constArgDefStrRegExp)) {
HeadEqBody headEqBody = new HeadEqBody(argumentDefinitionString);
argumentName = headEqBody.headTokens[0].tokenStr;
Expression bodyExpr = new Expression(headEqBody.bodyStr);
double bodyValue = bodyExpr.calculate();
bodyExpr.setDescription(StringInvariant.INTERNAL);
bodyExpr.setForwardErrorMessage(false);
double bodyValue = bodyExpr.calculate();
if ((bodyExpr.getSyntaxStatus() == Expression.NO_SYNTAX_ERRORS) && (bodyValue != Double.NaN)) {
argumentExpression = new Expression();
argumentValue = bodyValue;
argumentType = FREE_ARGUMENT;
}
else {
argumentExpression = bodyExpr;
addDefinitions(elements);
bodyExpr.setForwardErrorMessage(true);
addDefinitions(elements);
argumentType = DEPENDENT_ARGUMENT;
}
}
argumentExpression.setDescription(argumentName);
}
else if (mXparser.regexMatch(argumentDefinitionString, ParserSymbol.functionDefStrRegExp)) {
HeadEqBody headEqBody = new HeadEqBody(argumentDefinitionString);
argumentName = headEqBody.headTokens[0].tokenStr;
Expand All @@ -380,8 +390,8 @@ public Argument(String argumentDefinitionString, params PrimitiveElement[] eleme
argumentValue = ARGUMENT_INITIAL_VALUE;
argumentType = FREE_ARGUMENT;
argumentExpression = new Expression();
argumentExpression.setSyntaxStatus(SYNTAX_ERROR_OR_STATUS_UNKNOWN, "[" + argumentDefinitionString + "] " + "Invalid argument definition (patterns: 'x', 'x=5', 'x=5+3/2', 'x=2*y').");
}
argumentExpression.setSyntaxStatus(SYNTAX_ERROR_OR_STATUS_UNKNOWN, buildErrorMessageInvalidArgumentDefinition(argumentDefinitionString));
}
argumentBodyType = BODY_RUNTIME;
setSilentMode();
description = "";
Expand All @@ -406,7 +416,8 @@ public Argument(String argumentDefinitionString, bool forceDependent, params Pri
argumentValue = ARGUMENT_INITIAL_VALUE;
argumentType = FREE_ARGUMENT;
argumentExpression = new Expression(elements);
} else if ( mXparser.regexMatch(argumentDefinitionString, ParserSymbol.constArgDefStrRegExp) ) {
argumentExpression.setDescription(argumentName);
} else if ( mXparser.regexMatch(argumentDefinitionString, ParserSymbol.constArgDefStrRegExp) ) {
HeadEqBody headEqBody = new HeadEqBody(argumentDefinitionString);
argumentName = headEqBody.headTokens[0].tokenStr;
Expression bodyExpr = new Expression(headEqBody.bodyStr);
Expand All @@ -415,18 +426,22 @@ public Argument(String argumentDefinitionString, bool forceDependent, params Pri
addDefinitions(elements);
argumentType = DEPENDENT_ARGUMENT;
} else {
double bodyValue = bodyExpr.calculate();
bodyExpr.setDescription(StringInvariant.INTERNAL);
bodyExpr.setForwardErrorMessage(false);
double bodyValue = bodyExpr.calculate();
if ( (bodyExpr.getSyntaxStatus() == Expression.NO_SYNTAX_ERRORS) && (bodyValue != Double.NaN) ) {
argumentExpression = new Expression();
argumentValue = bodyValue;
argumentType = FREE_ARGUMENT;
} else {
argumentExpression = bodyExpr;
addDefinitions(elements);
bodyExpr.setForwardErrorMessage(true);
addDefinitions(elements);
argumentType = DEPENDENT_ARGUMENT;
}
}
} else if ( mXparser.regexMatch(argumentDefinitionString, ParserSymbol.functionDefStrRegExp) ) {
argumentExpression.setDescription(headEqBody.headStr);
} else if ( mXparser.regexMatch(argumentDefinitionString, ParserSymbol.functionDefStrRegExp) ) {
HeadEqBody headEqBody = new HeadEqBody(argumentDefinitionString);
argumentName = headEqBody.headTokens[0].tokenStr;
argumentExpression = new Expression(headEqBody.bodyStr, elements);
Expand All @@ -438,8 +453,8 @@ public Argument(String argumentDefinitionString, bool forceDependent, params Pri
argumentValue = ARGUMENT_INITIAL_VALUE;
argumentType = FREE_ARGUMENT;
argumentExpression = new Expression();
argumentExpression.setSyntaxStatus(SYNTAX_ERROR_OR_STATUS_UNKNOWN, "[" + argumentDefinitionString + "] " + "Invalid argument definition (patterns: 'x', 'x=5', 'x=5+3/2', 'x=2*y').");
}
argumentExpression.setSyntaxStatus(SYNTAX_ERROR_OR_STATUS_UNKNOWN, buildErrorMessageInvalidArgumentDefinition(argumentDefinitionString));
}
argumentBodyType = BODY_RUNTIME;
setSilentMode();
description = "";
Expand All @@ -453,14 +468,14 @@ public Argument(String argumentDefinitionString, bool forceDependent, params Pri
public Argument(String argumentName, double argumentValue) : base(Argument.TYPE_ID) {
argumentExpression = new Expression();
if (mXparser.regexMatch(argumentName, ParserSymbol.nameOnlyTokenRegExp)) {
this.argumentName = "" + argumentName;
this.argumentName = argumentName;
this.argumentValue = argumentValue;
argumentType = FREE_ARGUMENT;
}
else {
this.argumentValue = ARGUMENT_INITIAL_VALUE;
argumentExpression.setSyntaxStatus(SYNTAX_ERROR_OR_STATUS_UNKNOWN, "[" + argumentName + "] " + "Invalid argument name, pattern not match: " + ParserSymbol.nameOnlyTokenRegExp);
}
argumentExpression.setSyntaxStatus(SYNTAX_ERROR_OR_STATUS_UNKNOWN, buildErrorMessageInvalidArgumentName(argumentName));
}
argumentBodyType = BODY_RUNTIME;
setSilentMode();
description = "";
Expand All @@ -476,15 +491,15 @@ public Argument(String argumentName, double argumentValue) : base(Argument.TYPE_
public Argument(String argumentName, ArgumentExtension argumentExtension) : base(Argument.TYPE_ID) {
argumentExpression = new Expression();
if (mXparser.regexMatch(argumentName, ParserSymbol.nameOnlyTokenRegExp)) {
this.argumentName = "" + argumentName;
this.argumentName = argumentName;
this.argumentExtension = argumentExtension;
argumentType = FREE_ARGUMENT;
argumentBodyType = BODY_EXTENDED;
}
else {
this.argumentValue = ARGUMENT_INITIAL_VALUE;
argumentExpression.setSyntaxStatus(SYNTAX_ERROR_OR_STATUS_UNKNOWN, "[" + argumentName + "] " + "Invalid argument name, pattern not match: " + ParserSymbol.nameOnlyTokenRegExp);
argumentBodyType = BODY_RUNTIME;
argumentExpression.setSyntaxStatus(SYNTAX_ERROR_OR_STATUS_UNKNOWN, buildErrorMessageInvalidArgumentName(argumentName));
argumentBodyType = BODY_RUNTIME;
}
setSilentMode();
description = "";
Expand All @@ -503,17 +518,17 @@ public Argument(String argumentName, ArgumentExtension argumentExtension) : base
*/
public Argument(String argumentName, String argumentExpressionString, params PrimitiveElement[] elements) : base(Argument.TYPE_ID) {
if (mXparser.regexMatch(argumentName, ParserSymbol.nameOnlyTokenRegExp)) {
this.argumentName="" + argumentName;
argumentValue=ARGUMENT_INITIAL_VALUE;
this.argumentName = argumentName;
argumentValue = ARGUMENT_INITIAL_VALUE;
argumentExpression = new Expression(argumentExpressionString, elements);
argumentExpression.setDescription(argumentName);
argumentType = DEPENDENT_ARGUMENT;
}
else {
this.argumentValue = ARGUMENT_INITIAL_VALUE;
argumentExpression = new Expression();
argumentExpression.setSyntaxStatus(SYNTAX_ERROR_OR_STATUS_UNKNOWN, "[" + argumentName + "] " + "Invalid argument name, pattern not match: " + ParserSymbol.nameOnlyTokenRegExp);
}
argumentExpression.setSyntaxStatus(SYNTAX_ERROR_OR_STATUS_UNKNOWN, buildErrorMessageInvalidArgumentName(argumentName));
}
argumentBodyType = BODY_RUNTIME;
setSilentMode();
description = "";
Expand Down Expand Up @@ -586,8 +601,8 @@ public void setArgumentName(String argumentName) {
setExpressionModifiedFlags();
}
else if (argumentExpression != null)
argumentExpression.setSyntaxStatus(SYNTAX_ERROR_OR_STATUS_UNKNOWN, "[" + argumentName + "] " + "Invalid argument name, pattern not match: " + ParserSymbol.nameOnlyTokenRegExp);
}
argumentExpression.setSyntaxStatus(SYNTAX_ERROR_OR_STATUS_UNKNOWN, buildErrorMessageInvalidArgumentName(argumentName));
}
/**
* Sets argument expression string.
* Each expression / function / dependent argument associated
Expand Down Expand Up @@ -767,7 +782,7 @@ public void defineArguments(params String[] argumentsNames) {
* based on the argument name and the argument value.
*
* @param argumentName the argument name
* @param argumentValue the the argument value
* @param argumentValue the argument value
*
* @see Argument
* @see RecursiveArgument
Expand Down Expand Up @@ -1134,7 +1149,7 @@ internal void setExpressionModifiedFlags() {
argumentExpression.setExpressionModifiedFlag();
}
/**
* Creates cloned object of the this argument.''
* Creates cloned object of this argument.
*
* @return clone of the argument.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* @(#)CalcStepsRegister.cs 5.1.0 2022-11-11
* @(#)CalcStepsRegister.cs 5.2.0 2022-12-09
*
* MathParser.org-mXparser DUAL LICENSE AGREEMENT as of date 2022-05-22
* The most up-to-date license is available at the below link:
Expand Down Expand Up @@ -197,7 +197,7 @@ namespace org.mariuszgromada.math.mxparser {
* <a href="https://play.google.com/store/apps/details?id=org.mathparser.scalar.pro" target="_blank">Scalar Pro</a><br>
* <a href="https://mathspace.pl" target="_blank">MathSpace.pl</a><br>
*
* @version 5.1.0
* @version 5.2.0
*
* @see CalcStepRecord
* @see Expression
Expand Down Expand Up @@ -247,35 +247,36 @@ public class CalcStepsRegister {
*/
public void consolePrint() {
if (calcStepRecords.Count == 0) {
mXparser.consolePrintln("CalcStepsRegister is empty");
mXparser.consolePrintln(StringResources.CALC_STEPS_REGISTER_IS_EMPTY);
return;
}
mXparser.consolePrint("CalcStepsRegister for ");
mXparser.consolePrint(StringResources.CALC_STEPS_REGISTER_FOR + StringInvariant.SPACE);
bool toPrintEq = false;
if (argumentNameStart.Length > 0) {
mXparser.consolePrint("Argument " + argumentNameStart);
mXparser.consolePrint(StringResources.ARGUMENT + StringInvariant.SPACE + argumentNameStart);
toPrintEq = true;
}
if (functionNameStart.Length > 0) {
mXparser.consolePrint("Function " + functionNameStart);
mXparser.consolePrint(StringResources.FUNCTION + StringInvariant.SPACE + functionNameStart);
toPrintEq = true;
}
if (toPrintEq)
mXparser.consolePrint(" = ");
mXparser.consolePrint(StringInvariant.SPACE_EQUAL_SPACE);
else
mXparser.consolePrint("Expression ");
mXparser.consolePrint(StringResources.EXPRESSION + StringInvariant.SPACE);
mXparser.consolePrintln(expressionStringStart + ", result = " + result);
mXparser.consolePrintln(expressionStringStart + StringInvariant.COMMA_SPACE + StringResources.RESULT + StringInvariant.SPACE_EQUAL_SPACE + result);
foreach (CalcStepRecord stepRecord in calcStepRecords)
mXparser.consolePrintln(
"gr = " + stepRecord.numberGroup
+ ", nr = " + stepRecord.numberGroupWithin
+ ", first = " + stepRecord.firstInGroup
+ ", last = " + stepRecord.lastInGroup
+ ", type = " + stepRecord.type
+ ", descr = " + stepRecord.description
+ ", step = " + stepRecord.content
StringResources.GROUP_SHORT + StringInvariant.SPACE_EQUAL_SPACE + stepRecord.numberGroup
+ StringInvariant.COMMA_SPACE + StringResources.NUMBER_SHORT + StringInvariant.SPACE_EQUAL_SPACE + stepRecord.numberGroupWithin
+ StringInvariant.COMMA_SPACE + StringResources.FIRST + StringInvariant.SPACE_EQUAL_SPACE + stepRecord.firstInGroup
+ StringInvariant.COMMA_SPACE + StringResources.LAST + StringInvariant.SPACE_EQUAL_SPACE + stepRecord.lastInGroup
+ StringInvariant.COMMA_SPACE + StringResources.TYPE + StringInvariant.SPACE_EQUAL_SPACE + stepRecord.type
+ StringInvariant.COMMA_SPACE + StringResources.DESCRIPTION_SHORT + StringInvariant.SPACE_EQUAL_SPACE + stepRecord.description
+ StringInvariant.COMMA_SPACE + StringResources.STEP + StringInvariant.SPACE_EQUAL_SPACE + stepRecord.content
);
mXparser.consolePrintln("Computing time = " + computingTime + " s.");
mXparser.consolePrintln(StringResources.COMPUTING_TIME + StringInvariant.SPACE_EQUAL_SPACE + computingTime + " s.");
}
internal bool isStartSet = false;
internal int stepNumberGroup = 0;
Expand Down
Loading

0 comments on commit c9c2ff4

Please sign in to comment.