コード例 #1
0
        public static bool Scan(this ITdfaSimulation automaton, int[] input, out State action)
        {
            State state          = automaton.Start;
            State?acceptingState = null;

            foreach (var item in input)
            {
                State nextState;

                if (automaton.TryNext(state, item, out nextState))
                {
                    state = nextState;
                }
                else if (automaton.Tunnel(state, out nextState))
                {
                    state = nextState;
                }
                else
                {
                    break;
                }

                if (automaton.IsAccepting(state))
                {
                    acceptingState = state;
                }
            }

            if (!acceptingState.HasValue)
            {
                action = -1;
                return(false);
            }

            var optAction = automaton.GetAction(acceptingState.Value);

            action = optAction.GetValueOrDefault(-1);
            return(true);
        }
コード例 #2
0
        public IReceiver <Msg> Accept(IReceiver <Msg> visitor)
        {
            int start = 0, pos = 0;

            int length = text.Length;

            while (pos != length)
            {
                int state          = tdfa.Start;
                int?acceptingState = null;

                // Find longest token
                for (; pos != length;)
                {
                    int item = text[pos];

                    int next;
                    if (tdfa.TryNext(state, item, out next))
                    {
                        state = next;
                        if (tdfa.IsAccepting(state))
                        {
                            acceptingState = state;
                        }

                        ++pos;
                    }
                    else if (tdfa.Tunnel(state, out next))
                    {
                        state = next;
                    }
                    else
                    {
                        break;
                    }
                }

                // Fail
                if (!acceptingState.HasValue)
                {
                    throw new Exception("Unexpected character at pos " + pos);
                }

                int ruleIndex   = tdfa.GetAction(acceptingState.Value) ?? -1;
                int tokenLength = pos - start;

                TokenFactoryDelegate tokenFactory = tokenFactories[ruleIndex];
                if (tokenFactory != null)
                {
                    var prod = descriptor.Matchers[ruleIndex];

                    // Emit next token
                    visitor = visitor.Next(
                        new Msg(
                            prod.Outcome.Index,
                            tokenFactory(text.Substring(start, tokenLength)),
                            new Loc(Loc.MemoryString, start, pos)));
                }

                start = pos;
            }

            return(visitor.Done());
        }