Exemplo n.º 1
0
        public TdfaSimulationLexer(TextReader textSource, ScannerDescriptor descriptor)
        {
            this.text       = textSource.ReadToEnd();
            this.descriptor = descriptor;

            Dictionary <string, int> literalToAction = new Dictionary <string, int>();
            var root      = descriptor.MakeAst(literalToAction);
            var regTree   = new RegularTree(root);
            var algorithm = new RegularToTdfaAlgorithm(regTree, literalToAction);

            DescribeTdfa(algorithm.Data);
            this.tdfa = new TdfaSimulation(algorithm.Data);

            int count = descriptor.Matchers.Count;

            this.tokenFactories = new TokenFactoryDelegate[count];

            for (int i = 0; i != count; ++i)
            {
                if (descriptor.Matchers[i].Outcome != null)
                {
                    tokenFactories[i] = BuildTokenFactory(descriptor.Matchers[i]);
                }
            }
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        public static bool Match(this ITdfaSimulation automaton, IEnumerable <int> input)
        {
            State state = automaton.Start;

            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;
                }
            }

            return(automaton.IsAccepting(state));
        }
Exemplo n.º 4
0
        public static bool MatchBeginning(this ITdfaSimulation automaton, int[] input)
        {
            int action;

            return(automaton.Scan(input, out action));
        }