コード例 #1
0
        /// <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);
        }
コード例 #2
0
        /// <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);
        }
コード例 #3
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());
        }
コード例 #4
0
        /// <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);
        }
コード例 #5
0
 /// <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();
 }
コード例 #6
0
        /// <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;
        }
コード例 #7
0
        /// <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());
        }
コード例 #8
0
        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());
        }
コード例 #9
0
        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));
        }
コード例 #10
0
 /// <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];
 }
コード例 #11
0
ファイル: CompiledGrammar.cs プロジェクト: gahadzikwa/GAPP
		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());
		}
コード例 #12
0
ファイル: CompiledGrammar.cs プロジェクト: gahadzikwa/GAPP
		/// <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());
		}
コード例 #13
0
ファイル: CompiledGrammar.cs プロジェクト: gahadzikwa/GAPP
		/// <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);
		}
コード例 #14
0
ファイル: CompiledGrammar.cs プロジェクト: gahadzikwa/GAPP
		/// <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();
		}
コード例 #15
0
ファイル: CompiledGrammar.cs プロジェクト: gahadzikwa/GAPP
		/// <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();
		}
コード例 #16
0
ファイル: CompiledGrammar.cs プロジェクト: gahadzikwa/GAPP
		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));
		}
コード例 #17
0
ファイル: CompiledGrammar.cs プロジェクト: gahadzikwa/GAPP
		/// <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);
		}
コード例 #18
0
ファイル: CompiledGrammar.cs プロジェクト: gahadzikwa/GAPP
		/// <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());
		}
コード例 #19
0
ファイル: CompiledGrammar.cs プロジェクト: gahadzikwa/GAPP
		/// <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];
		}
コード例 #20
0
 /// <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();
 }
コード例 #21
0
ファイル: CompiledGrammar.cs プロジェクト: gahadzikwa/GAPP
		/// <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());
		}
コード例 #22
0
        /// <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());
        }
コード例 #23
0
ファイル: CompiledGrammar.cs プロジェクト: gahadzikwa/GAPP
		/// <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);
		}
コード例 #24
0
ファイル: CompiledGrammar.cs プロジェクト: gahadzikwa/GAPP
		/// <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;
		}