Example #1
0
        internal IExecutableQuery Compile(
            TextReader inputQuery,
            IQueryErrorListener queryErrorListener,
            QueryCompilationParameters parameters)
        {
            // create all necessary components to parse a query
            var input = new AntlrInputStream(inputQuery);
            var lexer = new QueryLexer(input);

            lexer.AddErrorListener(new LexerErrorListener(queryErrorListener));

            var compiler = new QueryCompilationListener(
                _queryFactory,
                this,
                queryErrorListener,
                _runtime,
                parameters);

            var tokenStream   = new CommonTokenStream(lexer);
            var errorListener = new ParserErrorListener(queryErrorListener);
            var parser        = new QueryParser(tokenStream);

            parser.AddErrorListener(errorListener);

            // parse and compile the query
            if (!parameters.IsRecursiveCall)
            {
                queryErrorListener.BeforeCompilation();
            }

            IQuery result;

            try
            {
                var tree = parser.entry();
                ParseTreeWalker.Default.Walk(compiler, tree);
                result = compiler.Finish();

                if (result == null)
                {
                    return(null);
                }

                result = result.WithText(input.ToString());
            }
            catch (ParseCanceledException)
            {
                // an error has already been reported
                return(null);
            }
            finally
            {
                if (!parameters.IsRecursiveCall)
                {
                    queryErrorListener.AfterCompilation();
                }
            }

            return(result);
        }
Example #2
0
        public static QueryParser.ProgramContext Parse(string query)
        {
            var lexer = new QueryLexer(new AntlrInputStream(query));

            lexer.RemoveErrorListeners();
            lexer.AddErrorListener(new ThrowingErrorListener <int>());

            var parser = new QueryParser(new CommonTokenStream(lexer));

            parser.RemoveErrorListeners();
            parser.AddErrorListener(new ThrowingErrorListener <IToken>());

            return(parser.program());
        }