示例#1
0
        private void CreateDfa()
        {
            var dfa = new RegularToDfaAlgorithm(regularTree);

            this.simulation    = new DfaSimulation(dfa.Data);
            this.serialization = new DfaSerialization(dfa.Data);

            // DEBUG: Console.WriteLine(dfa);
        }
示例#2
0
        public SRegex(string pattern, SRegexOptions options = SRegexOptions.Default)
        {
            var ast = Language.Parse(new SreSyntax(), pattern).Result.Node;

            switch (options)
            {
            case SRegexOptions.ByteCodeCompilation:
                var compiler = new NfaVMBytecodeBackend(ast);
                matcher = (int[] input) =>
                {
                    var vm = new PikeNfaVM(compiler.Code.ToArray());
                    vm.Feed(input.Select(ch => (int)ch)).Done();
                    return(vm.HasMatch);
                };
                break;

            case SRegexOptions.ILCompilation:
                string methodName = "MatchSrePattern" + PatternID++;

                var builder = new CachedMethod <MatchDelegate>(
                    methodName,
                    (emit, args) => EmitAst(emit, ast, args[0]));

                matcher = builder.Delegate;
                break;

            case SRegexOptions.NfaCompilation:
                var nfa = new Nfa(ast);
                matcher = nfa.Match;
                break;

            case SRegexOptions.DfaCompilation:
                var dfa        = new RegularToDfaAlgorithm(new RegularTree(ast));
                var simulation = new DfaSimulation(dfa.Data);
                matcher = input => simulation.Match(input);
                break;
            }
        }
        public DfaSimulationLexer(TextReader textSource, ScannerDescriptor descriptor)
        {
            this.text       = textSource.ReadToEnd();
            this.descriptor = descriptor;

            var root      = descriptor.MakeAst();
            var regTree   = new RegularTree(root);
            var algorithm = new RegularToDfaAlgorithm(regTree);

            this.dfa = new DfaSimulation(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]);
                }
            }
        }