public void ConvertStructType(PegNode blocknamenode, IBfsStructType block) { if (block == null) throw new AstConvertException("Type wasn't struct-like"); block.SourceRange = GetSourceRange(blocknamenode); block.Name = GetNodeText(blocknamenode); PegNode nextNode = blocknamenode.next_; //If the structure has a Compression Method defined if ( nextNode != null && GetNodeId(nextNode) == EBinaryFileSchemaParser.compressionmethod) { block.CompressionMethod = GetNodeText(nextNode); block.CompressionRange = GetSourceRange(nextNode); nextNode = nextNode.next_; } //If a primitive type is present, then it is a comsumeable struct type if (nextNode != null && GetNodeId(nextNode) == EBinaryFileSchemaParser.primitivetype) { IBfsConsumptionType special = block as IBfsConsumptionType; special.PrimitiveType = ConvertPrimitiveType(nextNode); nextNode = nextNode.next_; } //For each of the fields the struct-type contains for(PegNode field = nextNode; field != null; field = field.next_) { if (GetNodeId(field) == EBinaryFileSchemaParser.field) { BfsStructField fielditem = new BfsStructField(); fielditem.SourceRange = GetSourceRange(field); ConvertStructField(field, fielditem); block.StructFieldList.Add(fielditem); } else if (GetNodeId(field) == EBinaryFileSchemaParser.localfield) ConvertLocalField(field, block); else throw new AstConvertException("Node wasn't a field: " + GetNodeId(field)); } }
private void ConvertStructField(PegNode fieldNode, BfsStructField field) { PegNode node = fieldNode.child_; if (GetNodeId(node) == EBinaryFileSchemaParser.skip) { field.Skip = true; field.SkipSourceRange = GetSourceRange(node); node = node.next_; } if (GetNodeId(node) == EBinaryFileSchemaParser.conditional) { field.Conditional = ConvertExpression(node.child_); field.ConditionalSourceRange = GetSourceRange(node); node = node.next_; } if (GetNodeId(node) == EBinaryFileSchemaParser.fieldname) { field.Name = GetNodeText(node); node = node.next_; } //else throw new AstConvertException("Expected a fieldname node"); if (GetNodeId(node) == EBinaryFileSchemaParser.type) field.FieldType = ConvertType(node); //else throw new AstConvertException("Type expected"); }
private void ReadStructField(BfsStructField field, TreeNode parent) { if (field.FieldType is BfsFunctionType) { BfsFunctionType functionType = field.FieldType as BfsFunctionType; if (functionType.FunctionName == "ascii") AddFileLine(field.Name + " " + reader.ReadASCIIString(functionType.FunctionArgument), field.FieldType, parent); } if (field.FieldType is BfsPrimitiveType) { BfsPrimitiveType primitiveType = field.FieldType as BfsPrimitiveType; switch (primitiveType.PrimitiveType) { case BfsPrimitiveTypeEnum.Bool: AddFileLine(field.Name + " " + reader.ReadBool(), field.FieldType, parent); break; case BfsPrimitiveTypeEnum.Sbyte: AddFileLine(field.Name + " " + reader.ReadSbyte(), field.FieldType, parent); break; case BfsPrimitiveTypeEnum.Ubyte: AddFileLine(field.Name + " " + reader.ReadUbyte(), field.FieldType, parent); break; case BfsPrimitiveTypeEnum.Int: AddFileLine(field.Name + " " + reader.ReadInt(), field.FieldType, parent); break; case BfsPrimitiveTypeEnum.Uint: AddFileLine(field.Name + " " + reader.ReadUint(), field.FieldType, parent); break; case BfsPrimitiveTypeEnum.Short: AddFileLine(field.Name + " " + reader.ReadShort(), field.FieldType, parent); break; case BfsPrimitiveTypeEnum.Ushort: AddFileLine(field.Name + " " + reader.ReadUshort(), field.FieldType, parent); break; case BfsPrimitiveTypeEnum.Long: AddFileLine(field.Name + " " + reader.ReadLong(), field.FieldType, parent); break; case BfsPrimitiveTypeEnum.Ulong: AddFileLine(field.Name + " " + reader.ReadUlong(), field.FieldType, parent); break; } } if (field.FieldType is BfsNamedType) { BfsNamedType namedType = field.FieldType as BfsNamedType; if (namedType.DataBlock is BfsStruct) { BfsStruct subStruct = namedType.DataBlock as BfsStruct; TreeNode newNode = new TreeNode(subStruct.Name); parent.Nodes.Add(newNode); ReadDataBlock(namedType.DataBlock, newNode); } } }