internal static void ReportMissingFile(Parser.Expression expression, string missingFile) { ReportMissingFile(expression.File, expression.Row, expression.Column, expression.OriginalCommand, missingFile); }
// --- check expression --- /// <summary>Checks if sufficient indices and arguments are provided to an expression, and reports messages to the host application if the amount of indices or arguments is outside of the specified range.</summary> /// <param name="expression">The expression to check.</param> /// <param name="indicesMinimum">The minimum number of indices the expression is allowed to have.</param> /// <param name="indicesMaximum">The maximum number of indices the expression is allowed to have.</param> /// <param name="acceptSuffix">Whether the expression is allowed to have a suffix.</param> /// <param name="argumentsMinimum">The minimum number of arguments the expression is allowed to have.</param> /// <param name="argumentsMaximum">The maximum number of arguments the expression is allowed tohave.</param> /// <returns>Whether the expression has at least the specified minimum number of indices and arguments.</returns> internal static bool CheckExpression(Parser.Expression expression, int indicesMinimum, int indicesMaximum, bool acceptSuffix, int argumentsMinimum, int argumentsMaximum) { int indicesProvided = expression.Indices != null ? expression.Indices.Length : 0; int argumentsProvided = expression.Arguments != null ? expression.Arguments.Length : 0; if ( indicesProvided >= indicesMinimum & indicesProvided <= indicesMaximum && (expression.Suffix == null | acceptSuffix) && argumentsProvided >= argumentsMinimum & argumentsProvided <= argumentsMaximum ) { return true; } else { /* * Check and report indices. * */ if (indicesProvided < indicesMinimum | indicesProvided > indicesMaximum) { string description; if (indicesMinimum == 0) { string provided = indicesProvided == 1 ? "1 index was" : indicesProvided.ToString(InvariantCulture) + " indices were"; string expected = indicesMaximum == 1 ? "1 index is" : indicesMaximum.ToString(InvariantCulture) + " indices are"; description = provided + " provided, but at most " + expected + " expected."; } else if (indicesMaximum == int.MaxValue) { string provided = indicesProvided == 1 ? "1 index was" : indicesProvided.ToString(InvariantCulture) + " indices were"; string expected = indicesMinimum == 1 ? "1 index is" : indicesMinimum.ToString(InvariantCulture) + " indices are"; description = provided + " provided, but at least " + expected + " expected."; } else if (indicesMinimum == indicesMaximum) { string provided = indicesProvided == 1 ? "1 index was" : indicesProvided.ToString(InvariantCulture) + " indices were"; string expected = indicesMinimum == 1 ? "1 index is" : indicesMinimum.ToString(InvariantCulture) + " indices are"; description = provided + " provided, but exactly " + expected + " expected."; } else { string provided = indicesProvided == 1 ? "1 index was" : indicesProvided.ToString(InvariantCulture) + " indices were"; string minimum = indicesMinimum.ToString(InvariantCulture); string maximum = indicesMaximum.ToString(InvariantCulture); description = provided + " provided, but between " + minimum + " and " + maximum + " indices are expected."; } Interfaces.Host.Report(OpenBveApi.General.ReportType.InvalidData, new OpenBveApi.General.ReportKeyValuePair(OpenBveApi.General.ReportKey.SourcePath, expression.File), new OpenBveApi.General.ReportKeyValuePair(OpenBveApi.General.ReportKey.Row, expression.Row + 1), new OpenBveApi.General.ReportKeyValuePair(OpenBveApi.General.ReportKey.Column, expression.Column + 1), new OpenBveApi.General.ReportKeyValuePair(OpenBveApi.General.ReportKey.Location, expression.OriginalCommand), new OpenBveApi.General.ReportKeyValuePair(OpenBveApi.General.ReportKey.Description, description) ); } /* * Check and report the suffix. * */ if (expression.Suffix != null & !acceptSuffix) { string description = "A suffix was provided, but the command does not take a suffix."; Interfaces.Host.Report(OpenBveApi.General.ReportType.InvalidData, new OpenBveApi.General.ReportKeyValuePair(OpenBveApi.General.ReportKey.SourcePath, expression.File), new OpenBveApi.General.ReportKeyValuePair(OpenBveApi.General.ReportKey.Row, expression.Row + 1), new OpenBveApi.General.ReportKeyValuePair(OpenBveApi.General.ReportKey.Column, expression.Column + 1), new OpenBveApi.General.ReportKeyValuePair(OpenBveApi.General.ReportKey.Location, expression.OriginalCommand), new OpenBveApi.General.ReportKeyValuePair(OpenBveApi.General.ReportKey.Description, description) ); } /* * Check and report arguments. * */ if (argumentsProvided < argumentsMinimum | argumentsProvided > argumentsMaximum) { string description; if (argumentsMinimum == 0) { string provided = argumentsProvided == 1 ? "1 argument was" : argumentsProvided.ToString(InvariantCulture) + " arguments were"; string expected = argumentsMaximum == 1 ? "1 argument is" : argumentsMaximum.ToString(InvariantCulture) + " arguments are"; description = provided + " provided, but at most " + expected + " expected."; } else if (argumentsMaximum == int.MaxValue) { string provided = argumentsProvided == 1 ? "1 argument was" : argumentsProvided.ToString(InvariantCulture) + " arguments were"; string expected = argumentsMinimum == 1 ? "1 argument is" : argumentsMinimum.ToString(InvariantCulture) + " arguments are"; description = provided + " provided, but at least " + expected + " expected."; } else if (argumentsMinimum == argumentsMaximum) { string provided = argumentsProvided == 1 ? "1 argument was" : argumentsProvided.ToString(InvariantCulture) + " arguments were"; string expected = argumentsMinimum == 1 ? "1 argument is" : argumentsMinimum.ToString(InvariantCulture) + " arguments are"; description = provided + " provided, but exactly " + expected + " expected."; } else { string provided = argumentsProvided == 1 ? "1 argument was" : argumentsProvided.ToString(InvariantCulture) + " arguments were"; string minimum = argumentsMinimum.ToString(InvariantCulture); string maximum = argumentsMaximum.ToString(InvariantCulture); description = provided + " provided, but between " + minimum + " and " + maximum + " arguments are expected."; } Interfaces.Host.Report(OpenBveApi.General.ReportType.InvalidData, new OpenBveApi.General.ReportKeyValuePair(OpenBveApi.General.ReportKey.SourcePath, expression.File), new OpenBveApi.General.ReportKeyValuePair(OpenBveApi.General.ReportKey.Row, expression.Row + 1), new OpenBveApi.General.ReportKeyValuePair(OpenBveApi.General.ReportKey.Column, expression.Column + 1), new OpenBveApi.General.ReportKeyValuePair(OpenBveApi.General.ReportKey.Location, expression.OriginalCommand), new OpenBveApi.General.ReportKeyValuePair(OpenBveApi.General.ReportKey.Description, description) ); } /* * Check for success. * */ if (indicesProvided < indicesMinimum | argumentsProvided < argumentsMinimum) { return false; } else { return true; } } }
internal static void ReportInvalidData(Parser.Expression expression, string description) { string location; if (expression.Position >= 0.0) { location = expression.OriginalCommand + " at " + expression.Position.ToString("0.0", InvariantCulture); } else { location = expression.OriginalCommand; } Interfaces.Host.Report( new OpenBveApi.General.ReportSourcePath(expression.File), new OpenBveApi.General.ReportTextRow(expression.Row + 1), new OpenBveApi.General.ReportTextColumn(expression.Column + 1), new OpenBveApi.General.ReportAdditionalLocation(location), new OpenBveApi.General.ReportDescription(description) ); }
// --- suffix --- internal static void ReportInvalidSuffix(Parser.Expression expression) { const string description = "The suffix is invalid."; Interfaces.Host.Report(OpenBveApi.General.ReportType.InvalidData, new OpenBveApi.General.ReportKeyValuePair(OpenBveApi.General.ReportKey.SourcePath, expression.File), new OpenBveApi.General.ReportKeyValuePair(OpenBveApi.General.ReportKey.Row, expression.Row + 1), new OpenBveApi.General.ReportKeyValuePair(OpenBveApi.General.ReportKey.Column, expression.Column + 1), new OpenBveApi.General.ReportKeyValuePair(OpenBveApi.General.ReportKey.Location, expression.OriginalCommand), new OpenBveApi.General.ReportKeyValuePair(OpenBveApi.General.ReportKey.Description, description) ); }
internal static void ReportInvalidArgument(Parser.Expression expression, int argumentIndex, string argumentName, string description) { ReportInvalidArgument(expression.File, expression.Row, expression.Column, expression.OriginalCommand, argumentIndex, argumentName, description); }
internal static bool ParseTrackPosition(Parser.Expression expression, double[] factors, out double value) { string text = expression.CsvEquivalentCommand; int colon = text.IndexOf(':'); if (colon >= 0) { string[] parts = text.Split(':'); value = 0.0; for (int i = 0; i < parts.Length; i++) { double number; if (double.TryParse(parts[i], NumberStyles.Float, CultureInfo.InvariantCulture, out number)) { int index = factors.Length - parts.Length + i; if (index >= 0) { value += number * factors[index]; } } else { string description = "The track position is invalid."; Interfaces.Host.Report(OpenBveApi.General.ReportType.InvalidData, new OpenBveApi.General.ReportKeyValuePair(OpenBveApi.General.ReportKey.SourcePath, expression.File), new OpenBveApi.General.ReportKeyValuePair(OpenBveApi.General.ReportKey.Row, expression.Row + 1), new OpenBveApi.General.ReportKeyValuePair(OpenBveApi.General.ReportKey.Column, expression.Column + 1), new OpenBveApi.General.ReportKeyValuePair(OpenBveApi.General.ReportKey.Description, description) ); value = 0.0; return false; } } return true; } else { if (double.TryParse(text, NumberStyles.Float, CultureInfo.InvariantCulture, out value)) { value *= factors[factors.Length - 1]; return true; } else { value = 0.0; return false; } } }
// --- parse int --- internal static bool ParseIntFromIndex(Parser.Expression expression, int indexIndex, string indexName, int minimumValue, int maximumValue, int defaultValue, out int value) { return ParseInt(expression.File, expression.Row, expression.Column, expression.OriginalCommand, expression.Indices, indexIndex, indexName, minimumValue, maximumValue, defaultValue, out value); }
internal static bool ParseIntFromArgument(Parser.Expression expression, int argumentIndex, string argumentName, int minimumValue, int maximumValue, int defaultValue, out int value) { return ParseInt(expression.File, expression.Row, expression.Column, expression.OriginalCommand, expression.Arguments, argumentIndex, argumentName, minimumValue, maximumValue, defaultValue, out value); }
// --- parse double extended --- internal static bool ParseDoubleFromArgumentExtended(Parser.Expression expression, int argumentIndex, string argumentName, double defaultValue, double[] factors, out double value) { if (expression.Arguments != null && argumentIndex < expression.Arguments.Length) { string text = expression.Arguments[argumentIndex]; int colon = text.IndexOf(':'); if (colon >= 0) { string[] parts = text.Split(':'); value = 0.0; for (int i = 0; i < parts.Length; i++) { double number; if (double.TryParse(parts[i], NumberStyles.Float, CultureInfo.InvariantCulture, out number)) { int index = factors.Length - parts.Length + i; if (index >= 0) { value += number * factors[index]; } } else { string location = "Argument " + (argumentIndex + 1).ToString(CultureInfo.InvariantCulture) + " in " + expression.OriginalCommand; string description = "The argument is not a valid floating-point number."; Interfaces.Host.Report(OpenBveApi.General.ReportType.InvalidData, new OpenBveApi.General.ReportKeyValuePair(OpenBveApi.General.ReportKey.SourcePath, expression.File), new OpenBveApi.General.ReportKeyValuePair(OpenBveApi.General.ReportKey.Row, expression.Row + 1), new OpenBveApi.General.ReportKeyValuePair(OpenBveApi.General.ReportKey.Column, expression.Column + 1), new OpenBveApi.General.ReportKeyValuePair(OpenBveApi.General.ReportKey.Location, location), new OpenBveApi.General.ReportKeyValuePair(OpenBveApi.General.ReportKey.Description, description) ); value = defaultValue; return false; } } return true; } else { if (ParseDouble(text, out value)) { value *= factors[factors.Length - 1]; return true; } else { value = defaultValue; return false; } } } else { value = defaultValue; return false; } }