Example #1
0
        public bool LoadTables(BinaryReader reader)
        {
            var egt = new EGTReader();
            bool success;

            try
            {
                egt.Open(reader);

                this.Restart();
                success = true;
                while (!(egt.EndOfFile() || success == false))
                {
                    egt.GetNextRecord();

                    var recType = (EGTRecord)egt.RetrieveByte();

                    switch (recType)
                    {
                        case EGTRecord.Property:
                        {
                            // Index, Name, Value
                            var index = egt.RetrieveInt16();
                            egt.RetrieveString();

                            // Just discard
                            this.m_Grammar.SetValue(index, egt.RetrieveString());

                            break;
                        }

                        case EGTRecord.TableCounts:
                        {
                            // Symbol, CharacterSet, Rule, DFA, LALR
                            this.m_SymbolTable = new SymbolList(egt.RetrieveInt16());
                            this.m_CharSetTable = new CharacterSetList(egt.RetrieveInt16());
                            this.m_ProductionTable = new ProductionList(egt.RetrieveInt16());
                            this.m_DFA = new FAStateList(egt.RetrieveInt16());
                            this.m_LRStates = new LRStateList(egt.RetrieveInt16());
                            this.m_GroupTable = new GroupList(egt.RetrieveInt16());

                            break;
                        }

                        case EGTRecord.InitialStates:
                        {
                            // DFA, LALR
                            this.m_DFA.InitialState = (short)egt.RetrieveInt16();
                            this.m_LRStates.InitialState = (short)egt.RetrieveInt16();

                            break;
                        }

                        case EGTRecord.Symbol:
                        {
                            // #, Name, Kind
                            var index = (short)egt.RetrieveInt16();
                            var name = egt.RetrieveString();
                            var type = (SymbolType)egt.RetrieveInt16();

                            this.m_SymbolTable[index] = new Symbol(name, type, index);

                            break;
                        }

                        case EGTRecord.Group:
                        {
                            // #, Name, Container#, Start#, End#, Tokenized, Open Ended, Reserved, Count, (Nested Group #...)
                            var g = new Group();
                            int n;

                            var with1 = g;
                            var index = egt.RetrieveInt16();

                            // #
                            with1.Name = egt.RetrieveString();
                            with1.Container = this.SymbolTable()[egt.RetrieveInt16()];
                            with1.Start = this.SymbolTable()[egt.RetrieveInt16()];
                            with1.End = this.SymbolTable()[egt.RetrieveInt16()];

                            with1.Advance = (Group.AdvanceMode)egt.RetrieveInt16();
                            with1.Ending = (Group.EndingMode)egt.RetrieveInt16();
                            egt.RetrieveEntry();

                            // Reserved
                            var count = egt.RetrieveInt16();
                            for (n = 1; n <= count; n++)
                            {
                                with1.Nesting.Add(egt.RetrieveInt16());
                            }

                            // === Link back
                            g.Container.Group = g;
                            g.Start.Group = g;
                            g.End.Group = g;

                            this.m_GroupTable[index] = g;

                            break;
                        }

                        case EGTRecord.CharRanges:
                        {
                            // #, Total Sets, RESERVED, (Start#, End#  ...)
                            var index = egt.RetrieveInt16();
                            egt.RetrieveInt16();

                            // Codepage
                            egt.RetrieveInt16();
                            egt.RetrieveEntry();

                            // Reserved
                            this.m_CharSetTable[index] = new CharacterSet();
                            while (!egt.RecordComplete())
                            {
                                this.m_CharSetTable[index].Add(
                                    new CharacterRange((ushort)egt.RetrieveInt16(), (ushort)egt.RetrieveInt16()));
                            }

                            break;
                        }

                        case EGTRecord.Production:
                        {
                            // #, ID#, Reserved, (Symbol#,  ...)
                            var index = egt.RetrieveInt16();
                            var headIndex = egt.RetrieveInt16();
                            egt.RetrieveEntry();

                            // Reserved
                            this.m_ProductionTable[index] = new Production(this.m_SymbolTable[headIndex], (short)index);

                            while (!egt.RecordComplete())
                            {
                                var symIndex = egt.RetrieveInt16();
                                this.m_ProductionTable[index].Handle().Add(this.m_SymbolTable[symIndex]);
                            }

                            break;
                        }

                        case EGTRecord.DFAState:
                        {
                            // #, Accept?, Accept#, Reserved (CharSet#, Target#, Reserved)...
                            var index = egt.RetrieveInt16();
                            var accept = egt.RetrieveBoolean();
                            var acceptIndex = egt.RetrieveInt16();
                            egt.RetrieveEntry();

                            // Reserved
                            if (accept)
                            {
                                this.m_DFA[index] = new FAState(this.m_SymbolTable[acceptIndex]);
                            }
                            else
                            {
                                this.m_DFA[index] = new FAState();
                            }

                            // (Edge chars, Target#, Reserved)...
                            while (!egt.RecordComplete())
                            {
                                var setIndex = egt.RetrieveInt16();

                                // Char table index
                                var target = egt.RetrieveInt16();

                                // Target
                                egt.RetrieveEntry();

                                // Reserved
                                this.m_DFA[index].Edges.Add(new FAEdge(this.m_CharSetTable[setIndex], target));
                            }

                            break;
                        }

                        case EGTRecord.LRState:
                        {
                            // #, Reserved (Symbol#, Action, Target#, Reserved)...
                            var index = egt.RetrieveInt16();
                            egt.RetrieveEntry();

                            // Reserved
                            this.m_LRStates[index] = new LRState();

                            // (Symbol#, Action, Target#, Reserved)...
                            while (!egt.RecordComplete())
                            {
                                var symIndex = egt.RetrieveInt16();
                                var action = egt.RetrieveInt16();
                                var target = egt.RetrieveInt16();
                                egt.RetrieveEntry();

                                // Reserved
                                this.m_LRStates[index].Add(
                                    new LRAction(this.m_SymbolTable[symIndex], (LRActionType)action, (short)target));
                            }

                            break;
                        }

                        default:

                            // RecordIDComment
                            throw new ParserException(
                                "File Error. A record of type '" + (char)recType
                                + "' was read. This is not a valid code.");
                    }
                }

                egt.Close();
            }
            catch (Exception ex)
            {
                throw new ParserException(ex.Message, ex, "LoadTables");
            }

            this.m_TablesLoaded = success;

            return success;
        }
Example #2
0
        public bool LoadTables(BinaryReader reader)
        {
            var  egt = new EGTReader();
            bool success;

            try
            {
                egt.Open(reader);

                this.Restart();
                success = true;
                while (!(egt.EndOfFile() || success == false))
                {
                    egt.GetNextRecord();

                    var recType = (EGTRecord)egt.RetrieveByte();

                    switch (recType)
                    {
                    case EGTRecord.Property:
                    {
                        // Index, Name, Value
                        var index = egt.RetrieveInt16();
                        egt.RetrieveString();

                        // Just discard
                        this.m_Grammar.SetValue(index, egt.RetrieveString());

                        break;
                    }

                    case EGTRecord.TableCounts:
                    {
                        // Symbol, CharacterSet, Rule, DFA, LALR
                        this.m_SymbolTable     = new SymbolList(egt.RetrieveInt16());
                        this.m_CharSetTable    = new CharacterSetList(egt.RetrieveInt16());
                        this.m_ProductionTable = new ProductionList(egt.RetrieveInt16());
                        this.m_DFA             = new FAStateList(egt.RetrieveInt16());
                        this.m_LRStates        = new LRStateList(egt.RetrieveInt16());
                        this.m_GroupTable      = new GroupList(egt.RetrieveInt16());

                        break;
                    }

                    case EGTRecord.InitialStates:
                    {
                        // DFA, LALR
                        this.m_DFA.InitialState      = (short)egt.RetrieveInt16();
                        this.m_LRStates.InitialState = (short)egt.RetrieveInt16();

                        break;
                    }

                    case EGTRecord.Symbol:
                    {
                        // #, Name, Kind
                        var index = (short)egt.RetrieveInt16();
                        var name  = egt.RetrieveString();
                        var type  = (SymbolType)egt.RetrieveInt16();

                        this.m_SymbolTable[index] = new Symbol(name, type, index);

                        break;
                    }

                    case EGTRecord.Group:
                    {
                        // #, Name, Container#, Start#, End#, Tokenized, Open Ended, Reserved, Count, (Nested Group #...)
                        var g = new Group();
                        int n;

                        var with1 = g;
                        var index = egt.RetrieveInt16();

                        // #
                        with1.Name      = egt.RetrieveString();
                        with1.Container = this.SymbolTable()[egt.RetrieveInt16()];
                        with1.Start     = this.SymbolTable()[egt.RetrieveInt16()];
                        with1.End       = this.SymbolTable()[egt.RetrieveInt16()];

                        with1.Advance = (Group.AdvanceMode)egt.RetrieveInt16();
                        with1.Ending  = (Group.EndingMode)egt.RetrieveInt16();
                        egt.RetrieveEntry();

                        // Reserved
                        var count = egt.RetrieveInt16();
                        for (n = 1; n <= count; n++)
                        {
                            with1.Nesting.Add(egt.RetrieveInt16());
                        }

                        // === Link back
                        g.Container.Group = g;
                        g.Start.Group     = g;
                        g.End.Group       = g;

                        this.m_GroupTable[index] = g;

                        break;
                    }

                    case EGTRecord.CharRanges:
                    {
                        // #, Total Sets, RESERVED, (Start#, End#  ...)
                        var index = egt.RetrieveInt16();
                        egt.RetrieveInt16();

                        // Codepage
                        egt.RetrieveInt16();
                        egt.RetrieveEntry();

                        // Reserved
                        this.m_CharSetTable[index] = new CharacterSet();
                        while (!egt.RecordComplete())
                        {
                            this.m_CharSetTable[index].Add(
                                new CharacterRange((ushort)egt.RetrieveInt16(), (ushort)egt.RetrieveInt16()));
                        }

                        break;
                    }

                    case EGTRecord.Production:
                    {
                        // #, ID#, Reserved, (Symbol#,  ...)
                        var index     = egt.RetrieveInt16();
                        var headIndex = egt.RetrieveInt16();
                        egt.RetrieveEntry();

                        // Reserved
                        this.m_ProductionTable[index] = new Production(this.m_SymbolTable[headIndex], (short)index);

                        while (!egt.RecordComplete())
                        {
                            var symIndex = egt.RetrieveInt16();
                            this.m_ProductionTable[index].Handle().Add(this.m_SymbolTable[symIndex]);
                        }

                        break;
                    }

                    case EGTRecord.DFAState:
                    {
                        // #, Accept?, Accept#, Reserved (CharSet#, Target#, Reserved)...
                        var index       = egt.RetrieveInt16();
                        var accept      = egt.RetrieveBoolean();
                        var acceptIndex = egt.RetrieveInt16();
                        egt.RetrieveEntry();

                        // Reserved
                        if (accept)
                        {
                            this.m_DFA[index] = new FAState(this.m_SymbolTable[acceptIndex]);
                        }
                        else
                        {
                            this.m_DFA[index] = new FAState();
                        }

                        // (Edge chars, Target#, Reserved)...
                        while (!egt.RecordComplete())
                        {
                            var setIndex = egt.RetrieveInt16();

                            // Char table index
                            var target = egt.RetrieveInt16();

                            // Target
                            egt.RetrieveEntry();

                            // Reserved
                            this.m_DFA[index].Edges.Add(new FAEdge(this.m_CharSetTable[setIndex], target));
                        }

                        break;
                    }

                    case EGTRecord.LRState:
                    {
                        // #, Reserved (Symbol#, Action, Target#, Reserved)...
                        var index = egt.RetrieveInt16();
                        egt.RetrieveEntry();

                        // Reserved
                        this.m_LRStates[index] = new LRState();

                        // (Symbol#, Action, Target#, Reserved)...
                        while (!egt.RecordComplete())
                        {
                            var symIndex = egt.RetrieveInt16();
                            var action   = egt.RetrieveInt16();
                            var target   = egt.RetrieveInt16();
                            egt.RetrieveEntry();

                            // Reserved
                            this.m_LRStates[index].Add(
                                new LRAction(this.m_SymbolTable[symIndex], (LRActionType)action, (short)target));
                        }

                        break;
                    }

                    default:

                        // RecordIDComment
                        throw new ParserException(
                                  "File Error. A record of type '" + (char)recType
                                  + "' was read. This is not a valid code.");
                    }
                }

                egt.Close();
            }
            catch (Exception ex)
            {
                throw new ParserException(ex.Message, ex, "LoadTables");
            }

            this.m_TablesLoaded = success;

            return(success);
        }