private TypeParser GetArrayElement(TypeParser array) { TypeParser size = array.Children[0]; if (size.TypeName != nameof(BasicType.@int)) { throw new Exception($"Unsupported size's type {size.TypeName}"); } if (size.VarName != "size") { throw new Exception($"Unsupported size's name {size.VarName}"); } TypeParser data = array.Children[1]; if (data.VarName != "data") { throw new Exception($"Unsupported data's name {data.VarName}"); } return(data); }
private TypeParser GetArrayType() { if (Children.Count != 1) { throw new Exception($"Root contains {Children.Count} children"); } TypeParser array = Children[0]; if (array.Children.Count != 2) { throw new Exception($"Array contains {Children.Count} children"); } if (array.TypeName != "Array") { throw new Exception($"Array has unsupported type {array.TypeName}"); } if (array.VarName != "Array") { throw new Exception($"Array has unsupported name {array.VarName}"); } TypeParser size = array.Children[0]; if (size.TypeName != nameof(BaseType.@int)) { throw new Exception($"Size has unsupported type {size.TypeName}"); } if (size.VarName != "size") { throw new Exception($"Size has unsupported name {size.VarName}"); } TypeParser data = array.Children[1]; if (data.VarName != "data") { throw new Exception($"Data has unsupported name {data.VarName}"); } return(data); }
private TypeParser GetElement() { if (Children.Count != 1) { throw new Exception($"Root contains {Children.Count} children"); } TypeParser array = Children[0]; if (array.Children.Count != 2) { throw new Exception($"Array contains {Children.Count} children"); } if (array.TypeName != "Array") { throw new Exception($"Unsupported array's type {array.TypeName}"); } if (array.VarName != "Array") { throw new Exception($"Unsupported array's name {array.VarName}"); } return(GetArrayElement(array)); }
public TypeDefinition GenerateType(AssemblyDefinition assembly, string baseName, int version) { string name = GetTypeDefinitionName(); TypeDefinition type = assembly.FindType(name); if (type == null) { FieldDefinition[] fields = new FieldDefinition[Children.Count]; for (int i = 0; i < Children.Count; i++) { TypeParser child = Children[i]; FieldDefinition field = GenerateField(assembly, child); fields[i] = field; } TypeDefinition newType = new TypeDefinition(name, baseName, version, fields); assembly.Types.Add(newType); return(newType); } else { return(type); } }
public TreeParser(TreeReader reader) : base(reader) { m_header = new TreeHeaderParser(reader); m_type = new TypeParser(reader, 0); }
private string GetTypeDefinitionName() { switch (TypeName) { case "Array": { TypeParser arrayType = GetArrayElement(); return($"{arrayType.GetTypeDefinitionName()}[]"); } case "vector": { TypeParser vectorType = GetElement(); return($"vector<{vectorType.GetTypeDefinitionName()}>"); } case "set": { TypeParser setType = GetElement(); return($"set<{setType.GetTypeDefinitionName()}>"); } case "map": { TypeParser pair = GetElement(); if (pair.TypeName != "pair") { throw new Exception($"Pair has unsupported type {pair.TypeName}"); } if (pair.Children.Count != 2) { throw new Exception($"Pair contains {pair.Children.Count} children"); } TypeParser key = pair.Children[0]; if (key.VarName != "first") { throw new Exception($"First has unsupported name {key.VarName}"); } TypeParser value = pair.Children[1]; if (value.VarName != "second") { throw new Exception($"Second has unsupported name {value.VarName}"); } return($"map<{key.GetTypeDefinitionName()}, {value.GetTypeDefinitionName()}>"); } case "pair": { if (Children.Count != 2) { throw new Exception($"Pair contains {Children.Count} children"); } TypeParser key = Children[0]; if (key.VarName != "first") { throw new Exception($"First has unsupported name {key.VarName}"); } TypeParser value = Children[1]; if (value.VarName != "second") { throw new Exception($"Second has unsupported name {value.VarName}"); } return($"pair<{key.GetTypeDefinitionName()}, {value.GetTypeDefinitionName()}>"); } default: return(TypeName); } }
public void Parse() { VarParser vreader = new VarParser(m_reader, true); Validate(); ReadType(); VarName = string.Empty; FindWord(); StringBuilder sb = new StringBuilder(); while (true) { char symb = (char)m_reader.PeekChar(); if (symb == '/') { while (sb[sb.Length - 1] == ' ') { sb.Length--; } break; } sb.Append(m_reader.ReadChar()); } VarName = sb.ToString(); if (VarName == string.Empty) { throw CreateException("Can't find variable name"); } FindValidateWord("//"); vreader.Parse("ByteSize"); Size = vreader.Value; FindValidateSymbol(','); vreader.Parse("Index"); Index = vreader.Value; FindValidateSymbol(','); vreader.Parse("Version"); Version = vreader.Value; FindValidateSymbol(','); vreader.Parse("IsArray"); IsArray = vreader.Value != 0; FindValidateSymbol(','); vreader.Parse("MetaFlag"); MetaFlag = (TypeMetaFlag)vreader.Value; int childIndent = m_indent + 1; while (true) { int indent = CheckIndent(); if (indent < childIndent) { return; } if (indent > childIndent) { throw CreateException($"Unsupported indent {indent}"); } FindNextLine(); TypeParser child = new TypeParser(m_reader, childIndent); child.Parse(); m_children.Add(child); } }