/// <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 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); }
/// <summary> /// Read char set information. /// </summary> /// <param name="context"></param> private void ReadPackedCharset(LoadContext context) { int index = context.ReadIntegerEntry(); char sequenceStart = (char)context.ReadIntegerEntry(); char sequenceEnd = (char)context.ReadIntegerEntry(); StringBuilder result = new StringBuilder(context.ReadStringEntry()); result.Capacity = result.Length + (sequenceEnd - sequenceStart) + 1; for (char c = sequenceStart; c <= sequenceEnd; c++) { result.Append(c); } 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> /// Reads grammar header information. /// </summary> /// <param name="context"></param> private void ReadHeader(LoadContext context) { properties.Add(NameProperty, context.ReadStringEntry()); properties.Add(VersionProperty, context.ReadStringEntry()); properties.Add(AuthorProperty, context.ReadStringEntry()); properties.Add(AboutProperty, context.ReadStringEntry()); caseSensitive = context.ReadBoolEntry(); context.StartSymbolIndex = context.ReadIntegerEntry(); }
/// <summary> /// Read symbol information. /// </summary> /// <param name="context"></param> private void ReadSymbol(LoadContext context) { int index = context.ReadIntegerEntry(); string name = context.ReadStringEntry(); SymbolKind symbolKind = (SymbolKind)context.ReadIntegerEntry(); Symbol symbol = new Symbol(this, index, name, symbolKind); switch (symbolKind) { case SymbolKind.Error: errorSymbol = symbol; break; case SymbolKind.End: endSymbol = symbol; break; } symbolTable[index] = symbol; }
/// <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()); }
private void ReadProperty(LoadContext context) { int index = context.ReadIntegerEntry(); string name = context.ReadStringEntry(); // override the name for the well-known constants, so that we have the right name for sure switch (index) { case 0: name = NameProperty; break; case 1: name = VersionProperty; break; case 2: name = AuthorProperty; break; case 3: name = AboutProperty; break; case 4: name = CharacterSetProperty; break; case 5: name = CharacterMappingProperty; break; case 6: name = GeneratedByProperty; break; case 7: name = GeneratedDateProperty; break; } properties.Add(name, context.ReadStringEntry()); }
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> /// Reads table record counts and initializes tables. /// </summary> /// <param name="context"></param> private void ReadTableCounts(LoadContext context, bool readGroups) { // Initialize tables symbolTable = new Symbol[context.ReadIntegerEntry()]; charSetTable = new DfaCharset[context.ReadIntegerEntry()]; ruleTable = new Rule[context.ReadIntegerEntry()]; for (int i = 0; i < ruleTable.Length; i++) { ruleTable[i] = new Rule(this, i); } dfaStateTable = new DfaState[context.ReadIntegerEntry()]; for (int i = 0; i < dfaStateTable.Length; i++) { dfaStateTable[i] = new DfaState(this, i); } lalrStateTable = new LalrState[context.ReadIntegerEntry()]; for (int i = 0; i < lalrStateTable.Length; i++) { lalrStateTable[i] = new LalrState(this, i); } groupTable = new Group[readGroups ? context.ReadIntegerEntry() : 0]; }
/// <summary> /// Read char set information. /// </summary> /// <param name="context"></param> private void ReadPackedCharset(LoadContext context) { int index = context.ReadIntegerEntry(); char sequenceStart = (char)context.ReadIntegerEntry(); char sequenceEnd = (char)context.ReadIntegerEntry(); StringBuilder result = new StringBuilder(context.ReadStringEntry()); result.Capacity = result.Length+(sequenceEnd-sequenceStart)+1; for (char c = sequenceStart; c <= sequenceEnd; c++) { result.Append(c); } 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 initial DFA and LR states. /// </summary> /// <param name="context"></param> private void ReadInitialStates(LoadContext context) { context.DfaInitialStateIndex = context.ReadIntegerEntry(); context.LrInitialState = context.ReadIntegerEntry(); }
/// <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 char set information. /// </summary> /// <param name="context"></param> private void ReadCharset(LoadContext context) { int index = context.ReadIntegerEntry(); charSetTable[index] = new DfaCharset(this, index, context.ReadStringEntry()); }
/// <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()); }