コード例 #1
0
        public bool LoadTables(BinaryReader reader)
        {
            EGTReader egt = new EGTReader();
            bool      success;

            try
            {
                egt.Open(reader);
                Restart();
                success = true;
                while (!egt.IsEndOfFile || !success)
                {
                    egt.GetNextRecord();
                    EGTRecord recType = (EGTRecord)egt.RetrieveByte();

                    switch (recType)
                    {
                    case EGTRecord.DFAState:
                        int  index       = egt.RetrieveInt16();
                        bool accept      = egt.RetrieveBoolean();
                        int  acceptIndex = egt.RetrieveInt16();
                        egt.RetrieveEntry();     // Reserved
                        dfa[index] = !accept ? new FAState() : new FAState(SymbolTable[acceptIndex]);
                        while (!egt.IsReadComplete)
                        {
                            int setIndex = egt.RetrieveInt16();
                            int target   = egt.RetrieveInt16();
                            egt.RetrieveEntry();     // Reserved
                            dfa[index].Edges.Add(new FAEdge(this.charSetTable[setIndex], target));
                        }
                        break;

                    case EGTRecord.InitialStates:
                        dfa.InitialState      = egt.RetrieveInt16();
                        lrStates.InitialState = egt.RetrieveInt16();
                        break;

                    case EGTRecord.LRState:
                        index = egt.RetrieveInt16();
                        egt.RetrieveEntry();
                        lrStates[index] = new LRState();
                        while (!egt.IsReadComplete)
                        {
                            int symIndex = egt.RetrieveInt16();
                            int action   = egt.RetrieveInt16();
                            int target   = egt.RetrieveInt16();
                            egt.RetrieveEntry();
                            lrStates[index].Add(new LRAction(SymbolTable[symIndex], (LRActionType)action, (short)target));
                        }
                        break;

                    case EGTRecord.Production:
                        index = egt.RetrieveInt16();
                        int headIndex = egt.RetrieveInt16();
                        egt.RetrieveEntry();
                        ProductionTable[index] = new Production(SymbolTable[headIndex], (short)index);
                        while (!egt.IsReadComplete)
                        {
                            int symIndex = egt.RetrieveInt16();
                            ProductionTable[index].Handle.Add(SymbolTable[symIndex]);
                        }
                        break;

                    case EGTRecord.Symbol:
                        index = egt.RetrieveInt16();
                        string     name = egt.RetrieveString();
                        SymbolType type = (SymbolType)egt.RetrieveInt16();
                        SymbolTable[index] = new Symbol(name, type, (short)index);
                        break;

                    case EGTRecord.CharRanges:
                        index = egt.RetrieveInt16();
                        egt.RetrieveInt16();     // Codepage
                        int total = egt.RetrieveInt16();
                        egt.RetrieveEntry();     // Reserveds
                        charSetTable[index] = new CharacterSet();
                        while (!egt.IsReadComplete)
                        {
                            charSetTable[index].Add(new CharacterRange((ushort)egt.RetrieveInt16(), (ushort)egt.RetrieveInt16()));
                        }
                        break;

                    case EGTRecord.Group:
                        Group g = new Group();
                        index       = egt.RetrieveInt16();
                        g.Name      = egt.RetrieveString();
                        g.Container = SymbolTable[egt.RetrieveInt16()];
                        g.Start     = SymbolTable[egt.RetrieveInt16()];
                        g.End       = SymbolTable[egt.RetrieveInt16()];
                        g.Advance   = (Group.AdvanceMode)egt.RetrieveInt16();
                        g.Ending    = (Group.EndingMode)egt.RetrieveInt16();
                        egt.RetrieveEntry();

                        int count = egt.RetrieveInt16();
                        for (int i = 0; i < count; i++)
                        {
                            g.Nesting.Add(egt.RetrieveInt16());
                        }

                        g.Container.Group = g;
                        g.Start.Group     = g;
                        g.End.Group       = g;
                        groupTable[index] = g;
                        break;

                    case EGTRecord.Property:
                        index = egt.RetrieveInt16();
                        name  = egt.RetrieveString();    // Just discard
                        Grammar.SetValue(index, egt.RetrieveString());
                        break;

                    case EGTRecord.TableCounts:
                        SymbolTable     = new SymbolList(egt.RetrieveInt16());
                        charSetTable    = new CharacterSetList(egt.RetrieveInt16());
                        ProductionTable = new ProductionList(egt.RetrieveInt16());
                        dfa             = new FAStateList(egt.RetrieveInt16());
                        lrStates        = new LRStateList(egt.RetrieveInt16());
                        groupTable      = new GroupList(egt.RetrieveInt16());
                        break;

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

                egt.Dispose();
            }
            catch (Exception ex)
            {
                throw new ParserException(ex.Message, ex, nameof(LoadTables));
            }
            IsTablesLoaded = success;
            return(success);
        }
コード例 #2
0
        internal EGT(BinaryReader Reader)
        {
            EGTReader EGT     = new EGTReader(Reader);
            EGTRecord RecType = default(EGTRecord);

            try
            {
                while (!EGT.EndOfFile())
                {
                    EGT.GetNextRecord();

                    RecType = (EGTRecord)EGT.RetrieveByte();

                    switch (RecType)
                    {
                    case EGTRecord.Property:
                    {
                        //Index, Name, Value
                        int    Index = 0;
                        string Name  = null;

                        Index = EGT.RetrieveInt16();
                        Name  = EGT.RetrieveString();
                        //Just discard
                        m_Grammar.SetValue(Index, EGT.RetrieveString());
                    }
                    break;

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

                        break;

                    case EGTRecord.InitialStates:
                        //DFA, LALR
                        m_DFA.InitialState      = EGT.RetrieveInt16();
                        m_LRStates.InitialState = EGT.RetrieveInt16();

                        break;

                    case EGTRecord.Symbol:
                    {
                        //#, Name, Kind
                        short      Index = 0;
                        string     Name  = null;
                        SymbolType Type  = default(SymbolType);

                        Index = EGT.RetrieveInt16();
                        Name  = EGT.RetrieveString();
                        Type  = (SymbolType)EGT.RetrieveInt16();

                        m_SymbolTable[Index] = new Symbol(Name, Type, Index);
                    }
                    break;

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

                        G.TableIndex = EGT.RetrieveInt16();
                        //#

                        G.Name      = EGT.RetrieveString();
                        G.Container = m_SymbolTable[EGT.RetrieveInt16()];
                        G.Start     = m_SymbolTable[EGT.RetrieveInt16()];
                        G.End       = m_SymbolTable[EGT.RetrieveInt16()];

                        G.Advance = (Group.AdvanceMode)EGT.RetrieveInt16();
                        G.Ending  = (Group.EndingMode)EGT.RetrieveInt16();
                        EGT.RetrieveEntry();
                        //Reserved

                        int Count = EGT.RetrieveInt16();
                        for (int n = 1; n <= Count; n++)
                        {
                            G.Nesting.Add(EGT.RetrieveInt16());
                        }

                        //=== Link back
                        m_GroupStart.Add(G.Start, G);
                        m_GroupTable[G.TableIndex] = G;
                    }
                    break;

                    case EGTRecord.CharRanges:
                        //#, Total Sets, RESERVED, (Start#, End#  ...)
                    {
                        int Index = 0;
                        int Total = 0;

                        Index = EGT.RetrieveInt16();
                        EGT.RetrieveInt16();
                        //Codepage
                        Total = EGT.RetrieveInt16();
                        EGT.RetrieveEntry();
                        //Reserved

                        m_CharSetTable[Index] = new CharacterSet();
                        while (!(EGT.RecordComplete()))
                        {
                            m_CharSetTable[Index].Add(new CharacterRange(EGT.RetrieveUInt16(), EGT.RetrieveUInt16()));
                        }
                    }
                    break;

                    case EGTRecord.Production:
                        //#, ID#, Reserved, (Symbol#,  ...)
                    {
                        short Index     = 0;
                        int   HeadIndex = 0;
                        int   SymIndex  = 0;

                        Index     = EGT.RetrieveInt16();
                        HeadIndex = EGT.RetrieveInt16();
                        EGT.RetrieveEntry();
                        //Reserved

                        List <Symbol> symbols = new List <Symbol>();
                        while (!(EGT.RecordComplete()))
                        {
                            SymIndex = EGT.RetrieveInt16();
                            //m_ProductionTable[Index].Handle().Add(m_SymbolTable[SymIndex]);
                            symbols.Add(m_SymbolTable[SymIndex]);
                        }
                        SymbolList symbolList = new SymbolList(symbols);
                        m_ProductionTable[Index] = new Production(m_SymbolTable[HeadIndex], Index, symbolList);
                    }
                    break;

                    case EGTRecord.DFAState:
                        //#, Accept?, Accept#, Reserved (CharSet#, Target#, Reserved)...
                    {
                        int   Index       = 0;
                        bool  Accept      = false;
                        int   AcceptIndex = 0;
                        int   SetIndex    = 0;
                        short Target      = 0;

                        Index       = EGT.RetrieveInt16();
                        Accept      = EGT.RetrieveBoolean();
                        AcceptIndex = EGT.RetrieveInt16();
                        EGT.RetrieveEntry();
                        //Reserved

                        if (Accept)
                        {
                            m_DFA[Index] = new FAState(m_SymbolTable[AcceptIndex]);
                        }
                        else
                        {
                            m_DFA[Index] = new FAState();
                        }

                        //(Edge chars, Target#, Reserved)...
                        while (!(EGT.RecordComplete()))
                        {
                            SetIndex = EGT.RetrieveInt16();
                            //Char table index
                            Target = EGT.RetrieveInt16();
                            //Target
                            EGT.RetrieveEntry();
                            //Reserved

                            m_DFA[Index].Edges.Add(new FAEdge(m_CharSetTable[SetIndex], Target));
                        }
                    }
                    break;

                    case EGTRecord.LRState:
                        //#, Reserved (Symbol#, Action, Target#, Reserved)...
                    {
                        int          Index    = 0;
                        int          SymIndex = 0;
                        LRActionType Action   = 0;
                        short        Target   = 0;

                        Index = EGT.RetrieveInt16();
                        EGT.RetrieveEntry();
                        //Reserved

                        m_LRStates[Index] = new LRState();

                        //(Symbol#, Action, Target#, Reserved)...
                        while (!EGT.RecordComplete())
                        {
                            SymIndex = EGT.RetrieveInt16();
                            Action   = (LRActionType)EGT.RetrieveInt16();
                            Target   = EGT.RetrieveInt16();
                            EGT.RetrieveEntry();
                            //Reserved

                            m_LRStates[Index].Add(new LRAction(m_SymbolTable[SymIndex], Action, Target));
                        }
                    }
                    break;

                    default:
                        //RecordIDComment
                        throw new ParserException("File Error. A record of type '" + (char)RecType + "' was read. This is not a valid code.");
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ParserException(ex.Message, ex, "LoadTables");
            }
        }
コード例 #3
0
ファイル: ParseTables.cs プロジェクト: tvandijck/Frustel
        private bool LoadVer5(SimpleDB.Reader EGT)
        {
            bool flag2;

            try
            {
                flag2 = true;
                while (!(EGT.EndOfFile() | !flag2))
                {
                    Group        group;
                    int          num3;
                    int          num4;
                    CharacterSet set;
                    int          num5;
                    int          num7;
                    int          num8;
                    int          num10;
                    int          num14;
                    int          num15;
                    Group        group2;
                    int          num17;
                    EGT.GetNextRecord();
                    EGTRecord record = (EGTRecord)EGT.RetrieveByte();
                    switch (((byte)(((int)record) - 0x44)))
                    {
                    case 0:
                    {
                        num10 = EGT.RetrieveInt16();
                        bool flag3 = EGT.RetrieveBoolean();
                        int  num9  = EGT.RetrieveInt16();
                        EGT.RetrieveEntry();
                        if (!flag3)
                        {
                            goto Label_041A;
                        }
                        this.m_DFA[num10] = new FAState(this.m_Symbol[num9]);
                        goto Label_0475;
                    }

                    case 5:
                    {
                        this.m_DFA.InitialState  = (short)EGT.RetrieveInt16();
                        this.m_LALR.InitialState = (short)EGT.RetrieveInt16();
                        continue;
                    }

                    case 8:
                        num14 = EGT.RetrieveInt16();
                        EGT.RetrieveEntry();
                        this.m_LALR[num14] = new LRState();
                        goto Label_04F8;

                    case 14:
                    {
                        num7 = EGT.RetrieveInt16();
                        int num6 = EGT.RetrieveInt16();
                        EGT.RetrieveEntry();
                        this.m_Production[num7] = new Production(this.m_Symbol[num6], (short)num7);
                        goto Label_03BC;
                    }

                    case 15:
                    {
                        int        num  = EGT.RetrieveInt16();
                        string     name = EGT.RetrieveString();
                        SymbolType type = (SymbolType)EGT.RetrieveInt16();
                        this.m_Symbol[num] = new Symbol(name, type, (short)num);
                        continue;
                    }

                    case 0x1f:
                        set  = new CharacterSet();
                        num5 = EGT.RetrieveInt16();
                        EGT.RetrieveInt16();
                        EGT.RetrieveInt16();
                        EGT.RetrieveEntry();
                        goto Label_0324;

                    case 0x23:
                        group            = new Group();
                        group2           = group;
                        num3             = EGT.RetrieveInt16();
                        group2.Name      = EGT.RetrieveString();
                        group2.Container = this.m_Symbol[EGT.RetrieveInt16()];
                        group2.Start     = this.m_Symbol[EGT.RetrieveInt16()];
                        group2.End       = this.m_Symbol[EGT.RetrieveInt16()];
                        group2.Advance   = (AdvanceMode)EGT.RetrieveInt16();
                        group2.Ending    = (EndingMode)EGT.RetrieveInt16();
                        EGT.RetrieveEntry();
                        num17 = EGT.RetrieveInt16();
                        num4  = 1;
                        goto Label_029C;

                    case 0x2c:
                    {
                        EGT.RetrieveInt16();
                        string str  = EGT.RetrieveString();
                        string str2 = EGT.RetrieveString();
                        this.m_Properties.Add(str, str2);
                        continue;
                    }

                    case 0x30:
                    {
                        this.m_Symbol     = new SymbolList(EGT.RetrieveInt16());
                        this.m_CharSet    = new CharacterSetList(EGT.RetrieveInt16());
                        this.m_Production = new ProductionList(EGT.RetrieveInt16());
                        this.m_DFA        = new FAStateList(EGT.RetrieveInt16());
                        this.m_LALR       = new LRStateList(EGT.RetrieveInt16());
                        this.m_Group      = new GroupList(EGT.RetrieveInt16());
                        continue;
                    }

                    default:
                        goto Label_0509;
                    }
Label_0282:
                    group2.Nesting.Add(EGT.RetrieveInt16());
                    num4++;
Label_029C:
                    if (num4 <= num17)
                    {
                        goto Label_0282;
                    }
                    group2 = null;
                    group.Container.Group = group;
                    group.Start.Group     = group;
                    group.End.Group       = group;
                    this.m_Group[num3]    = group;
                    continue;
Label_030F:
                    set.AddRange(EGT.RetrieveInt16(), EGT.RetrieveInt16());
Label_0324:
                    if (!EGT.RecordComplete())
                    {
                        goto Label_030F;
                    }
                    set.TableIndex       = num5;
                    this.m_CharSet[num5] = set;
                    continue;
Label_038E:
                    num8 = EGT.RetrieveInt16();
                    this.m_Production[num7].Handle().Add(this.m_Symbol[num8]);
Label_03BC:
                    if (!EGT.RecordComplete())
                    {
                        goto Label_038E;
                    }
                    continue;
Label_041A:
                    this.m_DFA[num10] = new FAState();
Label_0475:
                    while (!EGT.RecordComplete())
                    {
                        int num11  = EGT.RetrieveInt16();
                        int target = EGT.RetrieveInt16();
                        EGT.RetrieveEntry();
                        this.m_DFA[num10].Edges().Add(new FAEdge(this.m_CharSet[num11], target));
                    }
                    continue;
Label_04AE:
                    num15 = EGT.RetrieveInt16();
                    int num13 = EGT.RetrieveInt16();
                    int num16 = EGT.RetrieveInt16();
                    EGT.RetrieveEntry();
                    this.m_LALR[num14].Add(new LRAction(this.m_Symbol[num15], (LRActionType)num13, (short)num16));
Label_04F8:
                    if (!EGT.RecordComplete())
                    {
                        goto Label_04AE;
                    }
                    continue;
Label_0509:
                    flag2 = false;
                    throw new Exception("File Error. A record of type '" + Conversions.ToString(Strings.ChrW((int)record)) + "' was read. This is not a valid code.");
                }
                EGT.Close();
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                throw exception;
            }
            return(flag2);
        }