Inheritance: IBfsDataBlock, IBfsConsumptionType
Ejemplo n.º 1
0
        public BinaryFileSchema GetBFSTree(PegNode rootnode)
        {
            //First pass below root. Expecting datablocks and byteOrder fields.
            PegNode node = rootnode.child_;
            do
            {
                PegNode field = node;
                EBinaryFileSchemaParser field_id = GetNodeId(field);
                bool isformat = false;
                BfsSourceRange formatrange = GetSourceRange(field);
                BfsSourceRange blocktyperange;

                //BYTEORDER
                if (GetNodeId(field) == EBinaryFileSchemaParser.byteorder)
                {
                    BfsByteOrder byteorder = new BfsByteOrder();
                    StoreSourceRange(field, byteorder);
                    if ( GetNodeId(field.child_) == EBinaryFileSchemaParser.littleendian)
                        byteorder.ByteOrder = BfsByteOrderEnum.LittleEndian;
                    else if (GetNodeId(field.child_) == EBinaryFileSchemaParser.bigendian)
                        byteorder.ByteOrder = BfsByteOrderEnum.BigEndian;
                    else
                        byteorder.ByteOrder = BfsByteOrderEnum.LanguageDefault;
                    schema.ByteOrder = byteorder;
                }
                else
                {
                    PegNode block_content = field.child_;

                    //If the first node is a 'format' flag, go to next sibling
                    if (GetNodeId(block_content) == EBinaryFileSchemaParser.formatspecifier)
                    {
                        isformat = true;
                        formatrange = GetSourceRange(block_content);
                        block_content = block_content.next_;
                    }

                    blocktyperange = GetSourceRange(block_content);
                    block_content = block_content.next_;

                    IBfsDataBlock block;
                    switch (field_id)
                    {
                        //STRUCT
                        case EBinaryFileSchemaParser.p_struct:
                            block = new BfsStruct();
                            StoreSourceRange(node, block);
                            block.IsFormat = isformat;
                            ConvertStructType(block_content, block as IBfsStructType);
                            schema.DatablockList.Add(block);
                            break;

                        //ABS_OFFSET
                        case EBinaryFileSchemaParser.abs_offset:
                            block = new BfsAbsOffset();
                            StoreSourceRange(node, block);
                            block.IsFormat = isformat;
                            ConvertStructType(block_content, block as IBfsStructType);
                            schema.DatablockList.Add(block);
                            break;

                        //REL_OFFSET
                        case EBinaryFileSchemaParser.rel_offset:
                            block = new BfsRelOffset();
                            StoreSourceRange(node, block);
                            block.IsFormat = isformat;
                            ConvertStructType(block_content, block as IBfsStructType);
                            schema.DatablockList.Add(block);
                            break;

                        //ENUM
                        case EBinaryFileSchemaParser.p_enum:
                            block = new BfsEnum();
                            StoreSourceRange(node, block);
                            block.IsFormat = isformat;
                            ConvertEnumType(block_content, block as BfsEnum);
                            schema.DatablockList.Add(block);
                            break;

                        //BITFIELD
                        case EBinaryFileSchemaParser.bitfield:
                            block = new BfsBitfield();
                            StoreSourceRange(node, block);
                            block.BlockTypeSourceRange = GetSourceRange(field);
                            block.IsFormat = isformat;
                            ConvertBitfieldType(block_content, block as BfsBitfield);
                            schema.DatablockList.Add(block);
                            break;

                        default:
                            throw new AstConvertException("Not a data-block: " + GetNodeId(block_content));
                    }

                    block.BlockTypeSourceRange = blocktyperange;
                    if (isformat)
                        block.FormatSourceRange = formatrange;
                }

            }
            while ((node = node.next_) != null);

            return schema;
        }
Ejemplo n.º 2
0
        private void ConvertEnumField(PegNode node, BfsEnum block)
        {
            BfsEnumField field = new BfsEnumField();
            StoreSourceRange(node, field);
            for (PegNode enode = node.child_; enode != null; enode = enode.next_)
            {
                switch (GetNodeId(enode))
                {
                    case EBinaryFileSchemaParser.number:
                        BfsEnumValue enumval = new BfsEnumValue();
                        StoreSourceRange(enode, enumval);
                        enumval.Value = long.Parse(GetNodeText(enode), CultureInfo.InvariantCulture);
                        field.EnumMatch = enumval;
                        break;

                    case EBinaryFileSchemaParser.enumrange:
                        BfsEnumRange enumrange = new BfsEnumRange();
                        StoreSourceRange(enode, enumrange);
                        enumrange.StartValue = long.Parse(GetNodeText(enode.child_.next_), CultureInfo.InvariantCulture);
                        enumrange.EndValue = long.Parse(GetNodeText(enode.child_.next_.next_), CultureInfo.InvariantCulture);
                        enumrange.StartInclusion =
                            (GetNodeText(enode.child_) == "[") ? BfsInclusionEnum.Included : BfsInclusionEnum.Excluded;
                        enumrange.EndInclusion =
                            (GetNodeText(enode.child_.next_.next_.next_) == "]") ? BfsInclusionEnum.Included : BfsInclusionEnum.Excluded;
                        field.EnumMatch = enumrange;
                        break;

                    case EBinaryFileSchemaParser.p_else:
                        field.EnumMatch = new BfsEnumElse();
                        StoreSourceRange(enode, field.EnumMatch);
                        break;

                    case EBinaryFileSchemaParser.action_list:
                        ConvertActionList(enode, field.Actions);
                        break;

                    case EBinaryFileSchemaParser.enumname:
                        field.Alias = GetNodeText(enode);
                        field.AliasSourceRange = GetSourceRange(enode);
                        break;
                }
            }
            block.EnumFields.Add(field);
        }
Ejemplo n.º 3
0
        private void ConvertEnumType(PegNode node, BfsEnum block)
        {
            if (block == null)
                throw new AstConvertException("Type wasn't an enum");

            StoreSourceRange(node, block);

            block.Name = GetNodeText(node);
            block.PrimitiveType = ConvertPrimitiveType(node.next_);

            //For all enum fields contained in the enum (values,ranges and 'else')
            for (PegNode enumfield = node.next_.next_; enumfield != null; enumfield = enumfield.next_)
            {
                if (GetNodeId(enumfield) == EBinaryFileSchemaParser.enumfield)
                    ConvertEnumField(enumfield, block);
                else if (GetNodeId(enumfield) == EBinaryFileSchemaParser.localfield)
                    ConvertLocalField(enumfield, block);
                else
                    throw new AstConvertException("Not an enum field!");
            }
        }
Ejemplo n.º 4
0
 private void ReadEnum(BfsEnum bfsenum, TreeNode parent)
 {
 }