public override void EnterObject(IdfParser.ObjectContext context) { string typeName = context.ALPHA().GetText(); if (!IdfObjectList.ContainsKey(typeName)) { errors.Add(new ObjectTypeNotFoundError(context.ALPHA().Symbol, typeName)); // Return early and don't check any of the fields if we don't know what it is. return; } IdfObjects.AddSafe(typeName, context); }
public override void EnterObject(IdfParser.ObjectContext context) { string typeName = context.ALPHA().GetText(); var fields = context.fields(); var myFields = fields.field(); Console.WriteLine($"{typeName},"); var paddedFields = string.Join(",\n", myFields.ToList().Select(f => $" {f.GetText()}")) + ";\n"; Console.WriteLine(paddedFields); }
/// <summary> /// Exit a parse tree produced by <see cref="IdfParser.object"/>. /// <para>The default implementation does nothing.</para> /// </summary> /// <param name="context">The parse tree.</param> public virtual void ExitObject([NotNull] IdfParser.ObjectContext context) { }
public List <IdfError> FieldChecks(IdfParser.ObjectContext actualIdfObject) { List <IdfError> errors = new List <IdfError>(); var fields = actualIdfObject.fields().field(); // Check for minimum number of fields if (MinNumberOfFields != null && fields.Length < MinNumberOfFields) { errors.Add(new MinNumberOfFieldsError(actualIdfObject.Start, Name, MinNumberOfFields.Value, fields.Length)); } if (fields.Length > TotalNumberOfDefinedFields) { errors.Add(new TooManyFieldsProvidedError(actualIdfObject.Start, Name, TotalNumberOfDefinedFields, fields.Count())); } List <(IdfParser.FieldContext ActualField, IdfField ExpectedField)> zippedFields = fields .Zip(Fields, (context, field) => (ActualField: context, ExpectedField: field)).ToList(); foreach ((IdfParser.FieldContext actualField, IdfField expectedField) in zippedFields) { // Check for matching one of the key values for a field var trimmedFieldValue = actualField.GetText().Trim(); if (expectedField.Keys.Any()) { if (!expectedField.Keys.Contains(trimmedFieldValue) && !(string.IsNullOrWhiteSpace(trimmedFieldValue) && (expectedField.HasDefault || !expectedField.Required))) { errors.Add(new FieldNotInChoiceError(actualField.Start, expectedField.Name, expectedField.Keys, trimmedFieldValue)); } } if (expectedField.AlphaNumeric == IdfFieldAlphaNumeric.Numeric) { bool properlyAutocalculatable = (string.Equals(trimmedFieldValue, "autocalculate", StringComparison.OrdinalIgnoreCase) && expectedField.AutoCalculatable); bool parsesAsDouble = double.TryParse(trimmedFieldValue, out double value); var isBlankAndNotRequired = (string.IsNullOrWhiteSpace(trimmedFieldValue) && (expectedField.HasDefault || !expectedField.Required)); bool success = parsesAsDouble || properlyAutocalculatable || isBlankAndNotRequired; if (!success) { errors.Add(new NumericFieldNotNumericError(actualField.Start, expectedField.Name, trimmedFieldValue)); } else if (parsesAsDouble) { if (expectedField.MinType == IdfFieldMinMaxType.Inclusive && value < expectedField.Minimum) { errors.Add(new NumericFieldOutOfRangeError(actualField.Start, MinMax.Minimum, expectedField.MinType, actualField.GetText(), expectedField.Minimum, expectedField.Name)); } else if (expectedField.MinType == IdfFieldMinMaxType.Exclusive && value <= expectedField.Minimum) { errors.Add(new NumericFieldOutOfRangeError(actualField.Start, MinMax.Minimum, expectedField.MinType, actualField.GetText(), expectedField.Minimum, expectedField.Name)); } else if (expectedField.MaxType == IdfFieldMinMaxType.Inclusive && value > expectedField.Maximum) { errors.Add(new NumericFieldOutOfRangeError(actualField.Start, MinMax.Maximum, expectedField.MaxType, actualField.GetText(), expectedField.Maximum, expectedField.Name)); } else if (expectedField.MaxType == IdfFieldMinMaxType.Exclusive && value >= expectedField.Maximum) { errors.Add(new NumericFieldOutOfRangeError(actualField.Start, MinMax.Maximum, expectedField.MaxType, actualField.GetText(), expectedField.Maximum, expectedField.Name)); } } } } return(errors); }
public static List <string> Fields(this IdfParser.ObjectContext objectContext) => objectContext.fields().field().Select(context => context.GetText().Trim()).ToList();