/// <summary>
        /// Evaluates the <paramref name="inputText"/> given a <paramref name="contextFactory"/>.
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <typeparam name="TStream"></typeparam>
        /// <typeparam name="TParser"></typeparam>
        /// <typeparam name="TContext"></typeparam>
        /// <param name="inputText"></param>
        /// <param name="contextFactory"></param>
        /// <param name="errorListeners"></param>
        /// <returns></returns>
        public static TContext Evaluate <TSource, TStream, TParser, TContext>(this string inputText
                                                                              , AntlrEvaluateParserContextDelegate <TParser, TContext> contextFactory
                                                                              , params IParserErrorListener[] errorListeners)
            where TSource : class, ITokenSource
            where TStream : class, ITokenStream
            where TParser : Parser
            where TContext : class, IParseTree
        {
            contextFactory = contextFactory.VerifyNotNull();

            var inputStream = Construct <AntlrInputStream>((inputText ?? Empty).ToArgument()).VerifyNotNull();

            var inputSource = Construct <TSource>(inputStream.ToArgument <ICharStream>()).VerifyNotNull();

            var tokenStream = Construct <TStream>(inputSource.ToArgument()).VerifyNotNull();

            var parser = Construct <TParser>(tokenStream.ToArgument()).VerifyNotNull();

            // TODO: TBD: do we need to add tree listeners?
            foreach (var errorListener in errorListeners)
            {
                parser.AddErrorListener(errorListener);
            }

            return(contextFactory(parser).VerifyNotNull());
        }
예제 #2
0
        /// <summary>
        /// Returns the <typeparamref name="TListener"/> after Walking the
        /// <typeparamref name="TContext"/> during Parsing.
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <typeparam name="TStream"></typeparam>
        /// <typeparam name="TParser"></typeparam>
        /// <typeparam name="TContext"></typeparam>
        /// <typeparam name="TListener"></typeparam>
        /// <param name="inputText"></param>
        /// <param name="evaluateCallback"></param>
        /// <param name="errorListeners"></param>
        /// <returns></returns>
        public static TListener WalkEvaluatedContext <TSource, TStream, TParser, TContext, TListener>(this string inputText
                                                                                                      , AntlrEvaluateParserContextDelegate <TParser, TContext> evaluateCallback, params IParserErrorListener[] errorListeners)
            where TSource : class, ITokenSource
            where TStream : class, ITokenStream
            where TParser : Parser
            where TContext : class, IParseTree
            where TListener : class, IParseTreeListener
        {
            evaluateCallback = evaluateCallback.VerifyNotNull();

            var context = inputText.Evaluate <TSource, TStream, TParser, TContext>(evaluateCallback, errorListeners);

            var listener = Construct <TListener>().VerifyNotNull();

            Construct <ParseTreeWalker>().VerifyNotNull().Walk(listener, context);

            return(listener);
        }