コード例 #1
0
        private RuleDependencyProcessor.RuleRelations ExtractRuleRelations(ITypeMirror recognizer
                                                                           )
        {
            string serializedATN = GetSerializedATN(recognizer);

            if (serializedATN == null)
            {
                return(null);
            }
            ATN atn = ATNSimulator.Deserialize(serializedATN.ToCharArray());

            RuleDependencyProcessor.RuleRelations relations = new RuleDependencyProcessor.RuleRelations
                                                                  (atn.ruleToStartState.Length);
            foreach (ATNState state in atn.states)
            {
                if (!state.epsilonOnlyTransitions)
                {
                    continue;
                }
                foreach (Transition transition in state.GetTransitions())
                {
                    if (transition.TransitionType != TransitionType.Rule)
                    {
                        continue;
                    }
                    RuleTransition ruleTransition = (RuleTransition)transition;
                    relations.AddRuleInvocation(state.ruleIndex, ruleTransition.target.ruleIndex);
                }
            }
            return(relations);
        }
コード例 #2
0
        protected ParserFactory getParserFactory(string lexerName, string parserName, string listenerName, string entryPoint)
        {
            Assembly loader        = Assembly.LoadFile(Path.Combine(tmpdir, "Parser.dll"));
            Type     lexerClass    = loader.GetType(lexerName);
            Type     parserClass   = loader.GetType(parserName);
            Type     listenerClass = loader.GetType(listenerName);

            ConstructorInfo lexerCtor  = lexerClass.GetConstructor(new Type[] { typeof(ICharStream) });
            ConstructorInfo parserCtor = parserClass.GetConstructor(new Type[] { typeof(ITokenStream) });

            // construct initial instances of the lexer and parser to deserialize their ATNs
            ITokenSource tokenSource = (ITokenSource)lexerCtor.Invoke(new object[] { new AntlrInputStream("") });

            parserCtor.Invoke(new object[] { new CommonTokenStream(tokenSource) });

            if (!REUSE_LEXER_DFA)
            {
                FieldInfo lexerSerializedATNField = lexerClass.GetField("_serializedATN");
                string    lexerSerializedATN      = (string)lexerSerializedATNField.GetValue(null);
                for (int i = 0; i < NUMBER_OF_THREADS; i++)
                {
                    sharedLexerATNs[i] = ATNSimulator.Deserialize(lexerSerializedATN.ToCharArray());
                }
            }

            if (RUN_PARSER && !REUSE_PARSER_DFA)
            {
                FieldInfo parserSerializedATNField = parserClass.GetField("_serializedATN");
                string    parserSerializedATN      = (string)parserSerializedATNField.GetValue(null);
                for (int i = 0; i < NUMBER_OF_THREADS; i++)
                {
                    sharedParserATNs[i] = ATNSimulator.Deserialize(parserSerializedATN.ToCharArray());
                }
            }

            return(new ParserFactory_1(listenerClass, parserClass, lexerCtor, parserCtor, entryPoint));
        }