Ejemplo n.º 1
0
        /// <summary>
        /// Runs the lexer's DFA to match a terminal in the input ahead
        /// </summary>
        /// <param name="index">The current start index in the input text</param>
        /// <returns>The matching DFA state and length</returns>
        internal TokenMatch RunDFA(int index)
        {
            if (text.IsEnd(index))
            {
                // At the end of input
                // The only terminal matched at state index 0 is $
                return(new TokenMatch(0, 0));
            }

            TokenMatch result = new TokenMatch(0);
            int        state  = 0;
            int        i      = index;

            while (state != Automaton.DEAD_STATE)
            {
                AutomatonState stateData = automaton.GetState(state);
                // Is this state a matching state ?
                if (stateData.TerminalsCount != 0)
                {
                    result = new TokenMatch(state, i - index);
                }
                // No further transition => exit
                if (stateData.IsDeadEnd)
                {
                    break;
                }
                // At the end of the buffer
                if (text.IsEnd(i))
                {
                    break;
                }
                char current = text.GetValue(i);
                i++;
                // Try to find a transition from this state with the read character
                state = stateData.GetTargetBy(current);
            }
            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Runs this matcher
        /// </summary>
        /// <returns>The solution</returns>
        public TokenMatch Run()
        {
            heads           = new List <Head>();
            insertions      = new int[16];
            insertionsCount = 0;
            matchHead       = new Head(0);
            matchLength     = 0;
            int  offset  = 0;
            bool atEnd   = text.IsEnd(originIndex + offset);
            char current = atEnd ? '\0' : text.GetValue(originIndex + offset);

            if (atEnd)
            {
                InspectAtEnd(matchHead, offset);
            }
            else
            {
                Inspect(matchHead, offset, current);
            }
            while (heads.Count != 0)
            {
                offset++;
                atEnd   = text.IsEnd(originIndex + offset);
                current = atEnd ? '\0' : text.GetValue(originIndex + offset);
                List <Head> temp = new List <Head>(heads);
                heads.Clear();
                foreach (Head head in temp)
                {
                    if (atEnd)
                    {
                        InspectAtEnd(head, offset);
                    }
                    else
                    {
                        Inspect(head, offset, current);
                    }
                }
            }
            return(matchLength == 0 ? OnFailure() : OnSuccess());
        }