Ejemplo n.º 1
0
        /// This method implements the DFA algorithm and returns a token
        /// to the LALR state machine.
        private Token RetrieveToken()
        {
            Token   result;
            int     currentPos      = 0;
            int     lastAcceptState = -1;
            int     lastAcceptPos   = -1;
            FAState currentState    = m_DfaStates[m_initDfaState];

            try
            {
                while (true)
                {
                    // This code searches all the branches of the current DFA state for the next
                    // character in the input LookaheadStream. If found the target state is returned.
                    // The InStr() function searches the string pCharacterSetTable.Member(CharSetIndex)
                    // starting at position 1 for ch.  The pCompareMode variable determines whether
                    // the search is case sensitive.
                    int  target = -1;
                    char ch     = FixCase(m_source.LookAhead(currentPos));

                    foreach (FAEdge edge in currentState.Edges)
                    {
                        String chars = edge.Characters;
                        if (chars.IndexOf(ch) != -1)
                        {
                            target = edge.TargetIndex;
                            break;
                        }
                    }

                    // This block-if statement checks whether an edge was found from the current state.
                    // If so, the state and current position advance. Otherwise it is time to exit the main loop
                    // and report the token found (if there was it fact one). If the LastAcceptState is -1,
                    // then we never found a match and the Error Token is created. Otherwise, a new token
                    // is created using the Symbol in the Accept State and all the characters that
                    // comprise it.
                    if (target != -1)
                    {
                        // This code checks whether the target state accepts a token. If so, it sets the
                        // appropiate variables so when the algorithm is done, it can return the proper
                        // token and number of characters.
                        if (m_DfaStates[target].AcceptSymbol != -1)
                        {
                            lastAcceptState = target;
                            lastAcceptPos   = currentPos;
                        }

                        currentState = m_DfaStates[target];
                        currentPos++;
                    }
                    else
                    {
                        if (lastAcceptState == -1)
                        {
                            result      = new Token(m_errorSymbol);
                            result.Data = m_source.Read(1);
                        }
                        else
                        {
                            Symbol symbol = m_symbols[m_DfaStates[lastAcceptState].AcceptSymbol];
                            result      = new Token(symbol);
                            result.Data = m_source.Read(lastAcceptPos + 1);
                        }
                        break;
                    }
                }
            }
            catch (EndOfStreamException)
            {
                result      = new Token(m_endSymbol);
                result.Data = "";
            }

            UpdateLineNumber((String)result.Data);

            return(result);
        }