public void AddEnumOrFlagDefinition(EnumOrFlagsDefinition definition) { if (enumOrFlagDefinitions == null) { enumOrFlagDefinitions = new List <EnumOrFlagsDefinition>(); } enumOrFlagDefinitions.Add(definition); }
// returns definition key public void AddEnumOrFlagsDefinition(EnumOrFlagsDefinition enumOrFlagsDefinition) { if (enumOrFlagsDefinitionMap.ContainsKey(enumOrFlagsDefinition.globalReferenceNameLowerInvariant)) { throw new InvalidOperationException(String.Format("Found enum type key '{0}' twice", enumOrFlagsDefinition.globalReferenceNameLowerInvariant)); } enumOrFlagsDefinitionMap.Add(enumOrFlagsDefinition.globalReferenceNameLowerInvariant, enumOrFlagsDefinition); if (enumOrFlagsDefinition.isFlagsDefinition) { flagsDefinitions.Add(enumOrFlagsDefinition); } else { enumDefinitions.Add(enumOrFlagsDefinition); } }
static void ParseEnumOrFlagsDefinition(BinaryFormatFile file, LfdLineReader reader, NamedObjectDefinition currentObjectDefinition, LfdLine enumDefinitionLine, out LfdLine nextLine, Boolean isFlagsDefinition) { String enumOrFlagsString = isFlagsDefinition ? "Flags" : "Enum"; VerifyFieldCount(enumDefinitionLine, 2); String underlyingIntegerTypeString = enumDefinitionLine.fields[0]; BinaryFormatType underlyingIntegerType = BinaryFormatTypeExtensions.ParseIntegerType(underlyingIntegerTypeString); EnumOrFlagsDefinition definition; try { definition = new EnumOrFlagsDefinition(file, isFlagsDefinition, currentObjectDefinition, underlyingIntegerType, enumDefinitionLine.fields[1]); } catch (FormatException e) { throw new ParseException(enumDefinitionLine, e.Message); } Debug(" Entering {0} '{1}' (IntegerType={2})", enumOrFlagsString, enumDefinitionLine.id, definition.underlyingIntegerType); // // Read enum values // nextLine = reader.ReadLineIgnoreComments(); while (nextLine != null && nextLine.parent == enumDefinitionLine) { LfdLine enumValueLine = nextLine; VerifyFieldCount(enumValueLine, 1); Debug(" {0} {1} {2}", enumOrFlagsString, enumValueLine.id, enumValueLine.fields[0]); if (isFlagsDefinition) { definition.Add(new FlagsValueDefinition(enumValueLine.fields[0], Byte.Parse(enumValueLine.id))); } else { definition.Add(new EnumValueDefinition(enumValueLine.id, enumValueLine.fields[0])); } nextLine = reader.ReadLineIgnoreComments(); } Debug(" Exiting {0} '{1}'", enumOrFlagsString, definition.typeName); }
static void GenerateEnumDefinition(String rootNamespace, TextWriter writer, UInt32 tabs, EnumOrFlagsDefinition definition) { String typeName = definition.typeName; Boolean subNamespace = false; Int32 lastDotIndex = definition.typeName.LastIndexOf('.'); if (lastDotIndex >= 0) { typeName = definition.typeName.Substring(lastDotIndex + 1); subNamespace = true; writer.WriteLine("}"); writer.WriteLine("namespace {0}", definition.typeName.Remove(lastDotIndex)); writer.WriteLine("{"); tabs++; } if (definition.isFlagsDefinition) { writer.WriteLine(tabs * 4, "[Flags]"); } writer.WriteLine(tabs * 4, "public enum {0} {{", typeName); if (definition.isFlagsDefinition) { foreach (FlagsValueDefinition flagValues in definition.flagValues) { writer.WriteLine(tabs * 4, " {0} = {1},", flagValues.name, 0x01 << flagValues.bit); } } else { foreach (EnumValueDefinition enumValues in definition.enumValues) { writer.WriteLine(tabs * 4, " {0} = {1},", enumValues.name, enumValues.value); } } writer.WriteLine(tabs * 4, "}"); if (subNamespace) { tabs--; writer.WriteLine("}"); writer.WriteLine("namespace {0}", rootNamespace); writer.WriteLine("{"); } }
public EnumOrFlagsTypeReference(String relativeEnumReferenceTypeString, EnumOrFlagsDefinition definition, BinaryFormatArrayType arrayType) : base(relativeEnumReferenceTypeString, definition.isFlagsDefinition ? BinaryFormatType.Flags : BinaryFormatType.Enum, arrayType) { this.relativeEnumReferenceTypeString = relativeEnumReferenceTypeString; this.definition = definition; }
public static void ParseObjectFieldLine(BinaryFormatFile file, LfdLineReader reader, NamedObjectDefinition containingNamedObject, IFieldContainer containingObject, LfdLine fieldLine, out LfdLine nextLine) { // // Check if it is only a definition (enum or flag) // if (fieldLine.id.Equals("enum", StringComparison.OrdinalIgnoreCase)) { ParseEnumOrFlagsDefinition(file, reader, containingNamedObject, fieldLine, out nextLine, false); return; } if (fieldLine.id.Equals("flags", StringComparison.OrdinalIgnoreCase)) { ParseEnumOrFlagsDefinition(file, reader, containingNamedObject, fieldLine, out nextLine, true); return; } String typeString = fieldLine.id; // // The rest of the fields can have arrayParse the Array Size Type // BinaryFormatArrayType arrayType; int indexOfOpenBracket = typeString.IndexOf('['); if (indexOfOpenBracket < 0) { arrayType = null; } else { String arraySizeTypeString = typeString.Substring(indexOfOpenBracket + 1); typeString = typeString.Remove(indexOfOpenBracket); int indexOfCloseBracket = arraySizeTypeString.IndexOf(']'); if (indexOfCloseBracket < 0) { throw new ParseException(fieldLine, "Found an opening bracket '[' without a closing bracket"); } if (indexOfCloseBracket != arraySizeTypeString.Length - 1) { throw new ParseException(fieldLine, "The array size type '{0}' had a closing bracket, but the closing bracket was not the last character", arraySizeTypeString); } arraySizeTypeString = arraySizeTypeString.Remove(indexOfCloseBracket); arrayType = BinaryFormatArrayType.Parse(fieldLine, arraySizeTypeString); } // // Parse object inline definition // if (typeString.Equals("object", StringComparison.OrdinalIgnoreCase)) { VerifyFieldCount(fieldLine, 1); String objectDefinitionAndFieldName = fieldLine.fields[0]; NamedObjectDefinition fieldObjectDefinition = ParseObjectDefinition(file, reader, fieldLine, containingNamedObject, objectDefinitionAndFieldName, out nextLine); containingObject.AddField(new ObjectDefinitionField( new ObjectTypeReference(objectDefinitionAndFieldName, fieldObjectDefinition, arrayType), objectDefinitionAndFieldName)); return; } // // Check if it is a serializer // if (fieldLine.id.Equals("serializer", StringComparison.OrdinalIgnoreCase)) { VerifyFieldCount(fieldLine, 2); String serializerLengthTypeString = fieldLine.fields[0]; String serializerFieldName = fieldLine.fields[1]; BinaryFormatArrayType serializerLengthType = BinaryFormatArrayType.Parse(fieldLine, serializerLengthTypeString); containingObject.AddField(new ObjectDefinitionField(new SerializerTypeReference(serializerLengthType, arrayType), serializerFieldName)); nextLine = reader.ReadLineIgnoreComments(); return; } // // Check if it is an 'if' type // if (typeString.Equals("if", StringComparison.OrdinalIgnoreCase)) { VerifyFieldCount(fieldLine, 1); IfTypeReference ifType = ParseIf(file, reader, fieldLine, containingNamedObject, out nextLine); containingObject.AddField(new ObjectDefinitionField(ifType, null)); return; } // // Check if it is a switch type // if (typeString.Equals("switch", StringComparison.OrdinalIgnoreCase)) { VerifyFieldCount(fieldLine, 1); SwitchTypeReference switchType = ParseSwitch(file, reader, fieldLine, containingNamedObject, out nextLine); containingObject.AddField(new ObjectDefinitionField(switchType, null)); return; } // // The field is only one line, so read the next line now for the caller // nextLine = reader.ReadLineIgnoreComments(); EnumOrFlagsDefinition enumDefinition = file.TryGetEnumOrFlagsDefinition(containingNamedObject, typeString); if (enumDefinition != null) { VerifyFieldCount(fieldLine, 1); containingObject.AddField(new ObjectDefinitionField(new EnumOrFlagsTypeReference(typeString, enumDefinition, arrayType), fieldLine.fields[0])); return; } // Check if it is an object type NamedObjectDefinition objectDefinition = file.TryGetObjectDefinition(containingNamedObject, typeString); if (objectDefinition != null) { if (fieldLine.fields == null || fieldLine.fields.Length <= 0) { // // Add each field from the object definition to the current object definition // List <ObjectDefinitionField> objectDefinitionFields = objectDefinition.Fields; for (int i = 0; i < objectDefinitionFields.Count; i++) { ObjectDefinitionField fieldDefinition = objectDefinitionFields[i]; containingObject.AddField(fieldDefinition); } } else if (fieldLine.fields.Length == 1) { containingObject.AddField(new ObjectDefinitionField( new ObjectTypeReference(typeString, objectDefinition, arrayType), fieldLine.fields[0])); } else { throw new ParseException(fieldLine, "Expected line to have 0 or 1 fields but had {0}", fieldLine.fields.Length); } return; } // // Check if it a string type // if (typeString.Equals("ascii", StringComparison.OrdinalIgnoreCase)) { VerifyFieldCount(fieldLine, 1); containingObject.AddField(new ObjectDefinitionField( new AsciiTypeReference(typeString, arrayType), fieldLine.fields[0])); return; } // // It must be an integer type // VerifyFieldCount(fieldLine, 1); BinaryFormatType type; try { type = (BinaryFormatType)Enum.Parse(typeof(BinaryFormatType), typeString, true); } catch (ArgumentException) { throw new FormatException(String.Format("Unknown Type '{0}'", typeString)); } if (!type.IsIntegerType()) { throw new InvalidOperationException(String.Format("Unhandled type '{0}'", type)); } containingObject.AddField(new ObjectDefinitionField(new IntegerTypeReference(type, arrayType), fieldLine.fields[0])); }