Esempio n. 1
0
        public static Pair <ITree, CommonTokenStream> Parse(ParseRuleSelector parseRuleSelector, String text)
        {
            NoCaseSensitiveStream input;

            try
            {
                input = new NoCaseSensitiveStream(text);
            }
            catch (IOException ex)
            {
                throw new IOException("IOException parsing text '" + text + '\'', ex);
            }

            var lex    = ParseHelper.NewLexer(input);
            var tokens = new CommonTokenStream(lex);
            var g      = ParseHelper.NewParser(tokens);

            var ctx = parseRuleSelector.Invoke(g);

            return(new Pair <ITree, CommonTokenStream>(ctx, tokens));
        }
Esempio n. 2
0
        /// <summary>
        /// Parse expression using the rule the ParseRuleSelector instance supplies.
        /// </summary>
        /// <param name="expression">text to parse</param>
        /// <param name="eplStatementErrorMsg">text for error</param>
        /// <param name="addPleaseCheck">true to include depth paraphrase</param>
        /// <param name="parseRuleSelector">parse rule to select</param>
        /// <param name="rewriteScript">if set to <c>true</c> [rewrite script].</param>
        /// <returns>
        /// AST - syntax tree
        /// </returns>
        /// <exception cref="EPException">IOException parsing expression ' + expression + '\''</exception>
        /// <throws>EPException when the AST could not be parsed</throws>
        public static ParseResult Parse(String expression, String eplStatementErrorMsg, bool addPleaseCheck, ParseRuleSelector parseRuleSelector, bool rewriteScript)
        {
            if (Log.IsDebugEnabled)
            {
                Log.Debug(".parse Parsing expr=" + expression);
            }

            ICharStream input;

            try
            {
                input = new NoCaseSensitiveStream(expression);
            }
            catch (IOException ex)
            {
                throw new EPException("IOException parsing expression '" + expression + '\'', ex);
            }

            var lex = NewLexer(input);

            var tokens = new CommonTokenStream(lex);
            var parser = ParseHelper.NewParser(tokens);

            ITree tree;

            try
            {
                tree = parseRuleSelector.Invoke(parser);
            }
            catch (RecognitionException ex)
            {
                tokens.Fill();
                if (rewriteScript && IsContainsScriptExpression(tokens))
                {
                    return(HandleScriptRewrite(tokens, eplStatementErrorMsg, addPleaseCheck, parseRuleSelector));
                }
                Log.Debug("Error parsing statement [" + expression + "]", ex);
                throw ExceptionConvertor.ConvertStatement(ex, eplStatementErrorMsg, addPleaseCheck, parser);
            }
            catch (Exception e)
            {
                try
                {
                    tokens.Fill();
                }
                catch (Exception ex)
                {
                    Log.Debug("Token-fill produced exception: " + ex.Message, ex);
                }

                if (Log.IsDebugEnabled)
                {
                    Log.Debug("Error parsing statement [" + eplStatementErrorMsg + "]", e);
                }
                if (e.InnerException is RecognitionException)
                {
                    if (rewriteScript && IsContainsScriptExpression(tokens))
                    {
                        return(HandleScriptRewrite(tokens, eplStatementErrorMsg, addPleaseCheck, parseRuleSelector));
                    }
                    throw ExceptionConvertor.ConvertStatement((RecognitionException)e.InnerException, eplStatementErrorMsg, addPleaseCheck, parser);
                }
                else
                {
                    throw;
                }
            }

            // if we are re-writing scripts and contain a script, then rewrite
            if (rewriteScript && IsContainsScriptExpression(tokens))
            {
                return(HandleScriptRewrite(tokens, eplStatementErrorMsg, addPleaseCheck, parseRuleSelector));
            }

            if (Log.IsDebugEnabled)
            {
                Log.Debug(".parse Dumping AST...");
                ASTUtil.DumpAST(tree);
            }

            var expressionWithoutAnnotation = expression;

            if (tree is EsperEPL2GrammarParser.StartEPLExpressionRuleContext)
            {
                var epl = (EsperEPL2GrammarParser.StartEPLExpressionRuleContext)tree;
                expressionWithoutAnnotation = GetNoAnnotation(expression, epl.annotationEnum(), tokens);
            }
            else if (tree is EsperEPL2GrammarParser.StartPatternExpressionRuleContext)
            {
                var pattern = (EsperEPL2GrammarParser.StartPatternExpressionRuleContext)tree;
                expressionWithoutAnnotation = GetNoAnnotation(expression, pattern.annotationEnum(), tokens);
            }

            return(new ParseResult(tree, expressionWithoutAnnotation, tokens, Collections.GetEmptyList <String>()));
        }