/// <summary> /// Read DFA state information. /// </summary> /// <param name="context"></param> private void ReadDfaState(LoadContext context) { DfaState dfaState = GetDfaState(context.ReadIntegerEntry()); Symbol acceptSymbol = null; bool acceptState = context.ReadBoolEntry(); if (acceptState) { acceptSymbol = symbolTable[context.ReadIntegerEntry()]; } else { context.ReadIntegerEntry(); // Skip the entry. } context.ReadEmptyEntry(); // Read DFA edges DfaEdge[] edges = new DfaEdge[context.EntryCount / 3]; for (int i = 0; i < edges.Length; i++) { edges[i] = new DfaEdge(context.ReadIntegerEntry(), context.ReadIntegerEntry()); context.ReadEmptyEntry(); } // Create DFA state and store it in DFA state table dfaState.Initialize(acceptSymbol, edges); }
/// <summary> /// Read LR state information. /// </summary> /// <param name="context"></param> private void ReadLRState(LoadContext context) { int index = context.ReadIntegerEntry(); context.ReadEmptyEntry(); LalrAction[] table = new LalrAction[context.EntryCount / 4]; for (int i = 0; i < table.Length; i++) { Symbol symbol = symbolTable[context.ReadIntegerEntry()]; LalrActionType actionType = (LalrActionType)context.ReadIntegerEntry(); int targetIndex = context.ReadIntegerEntry(); context.ReadEmptyEntry(); LalrAction action; switch (actionType) { case LalrActionType.Accept: action = new LalrActionAccept(i, symbol); break; case LalrActionType.Goto: action = new LalrActionGoto(i, symbol, GetLalrState(targetIndex)); break; case LalrActionType.Reduce: action = new LalrActionReduce(i, symbol, GetRule(targetIndex)); break; case LalrActionType.Shift: action = new LalrActionShift(i, symbol, GetLalrState(targetIndex)); break; default: throw new InvalidOperationException("Invalid table action data"); } table[i] = action; } // Create the transition vector LalrAction[] transitionVector = new LalrAction[symbolTable.Length]; for (int i = 0; i < transitionVector.Length; i++) { transitionVector[i] = null; } for (int i = 0; i < table.Length; i++) { transitionVector[table[i].Symbol.Index] = table[i]; } GetLalrState(index).Initialize(table, transitionVector); }
/// <summary> /// Read rule information. /// </summary> /// <param name="context"></param> private void ReadRule(LoadContext context) { int index = context.ReadIntegerEntry(); Symbol nonterminal = symbolTable[context.ReadIntegerEntry()]; context.ReadEmptyEntry(); Symbol[] symbols = new Symbol[context.EntryCount]; for (int i = 0; i < symbols.Length; i++) { symbols[i] = symbolTable[context.ReadIntegerEntry()]; } GetRule(index).Initialize(nonterminal, symbols); }
private void ReadGroup(LoadContext context) { int index = context.ReadIntegerEntry(); string name = context.ReadStringEntry(); int containerIndex = context.ReadIntegerEntry(); int startIndex = context.ReadIntegerEntry(); int endIndex = context.ReadIntegerEntry(); GroupAdvanceMode advanceMode = (GroupAdvanceMode)context.ReadIntegerEntry(); GroupEndingMode endingMode = (GroupEndingMode)context.ReadIntegerEntry(); context.ReadEmptyEntry(); int nestingCount = context.ReadIntegerEntry(); Debug.Assert(nestingCount == context.EntryCount); int[] nesting = new int[nestingCount]; for (int i = 0; i < nesting.Length; i++) { nesting[i] = context.ReadIntegerEntry(); } groupTable[index] = new Group(this, index, name, advanceMode, endingMode); context.GroupInfos.Add(index, new GroupInfo(containerIndex, startIndex, endIndex, nesting)); }
/// <summary> /// Read char set information. /// </summary> /// <param name="context"></param> private void ReadRangeCharset(LoadContext context) { int index = context.ReadIntegerEntry(); context.ReadIntegerEntry(); // unicode plane int rangeCount = context.ReadIntegerEntry(); context.ReadEmptyEntry(); // reserved Debug.Assert(rangeCount == context.EntryCount / 2); StringBuilder result = new StringBuilder(); while (context.EntryCount > 0) { char ch = (char)context.ReadIntegerEntry(); char end = (char)context.ReadIntegerEntry(); while (ch <= end) { result.Append(ch++); } } charSetTable[index] = new DfaCharset(this, index, result.ToString()); }
/// <summary> /// Read char set information. /// </summary> /// <param name="context"></param> private void ReadRangeCharset(LoadContext context) { int index = context.ReadIntegerEntry(); context.ReadIntegerEntry(); // unicode plane int rangeCount = context.ReadIntegerEntry(); context.ReadEmptyEntry(); // reserved Debug.Assert(rangeCount == context.EntryCount/2); StringBuilder result = new StringBuilder(); while (context.EntryCount > 0) { char ch = (char)context.ReadIntegerEntry(); char end = (char)context.ReadIntegerEntry(); while (ch <= end) { result.Append(ch++); } } charSetTable[index] = new DfaCharset(this, index, result.ToString()); }
/// <summary> /// Read LR state information. /// </summary> /// <param name="context"></param> private void ReadLRState(LoadContext context) { int index = context.ReadIntegerEntry(); context.ReadEmptyEntry(); LalrAction[] table = new LalrAction[context.EntryCount/4]; for (int i = 0; i < table.Length; i++) { Symbol symbol = symbolTable[context.ReadIntegerEntry()]; LalrActionType actionType = (LalrActionType)context.ReadIntegerEntry(); int targetIndex = context.ReadIntegerEntry(); context.ReadEmptyEntry(); LalrAction action; switch (actionType) { case LalrActionType.Accept: action = new LalrActionAccept(i, symbol); break; case LalrActionType.Goto: action = new LalrActionGoto(i, symbol, GetLalrState(targetIndex)); break; case LalrActionType.Reduce: action = new LalrActionReduce(i, symbol, GetRule(targetIndex)); break; case LalrActionType.Shift: action = new LalrActionShift(i, symbol, GetLalrState(targetIndex)); break; default: throw new InvalidOperationException("Invalid table action data"); } table[i] = action; } // Create the transition vector LalrAction[] transitionVector = new LalrAction[symbolTable.Length]; for (int i = 0; i < transitionVector.Length; i++) { transitionVector[i] = null; } for (int i = 0; i < table.Length; i++) { transitionVector[table[i].Symbol.Index] = table[i]; } GetLalrState(index).Initialize(table, transitionVector); }
/// <summary> /// Read DFA state information. /// </summary> /// <param name="context"></param> private void ReadDfaState(LoadContext context) { DfaState dfaState = GetDfaState(context.ReadIntegerEntry()); Symbol acceptSymbol = null; bool acceptState = context.ReadBoolEntry(); if (acceptState) { acceptSymbol = symbolTable[context.ReadIntegerEntry()]; } else { context.ReadIntegerEntry(); // Skip the entry. } context.ReadEmptyEntry(); // Read DFA edges DfaEdge[] edges = new DfaEdge[context.EntryCount/3]; for (int i = 0; i < edges.Length; i++) { edges[i] = new DfaEdge(context.ReadIntegerEntry(), context.ReadIntegerEntry()); context.ReadEmptyEntry(); } // Create DFA state and store it in DFA state table dfaState.Initialize(acceptSymbol, edges); }