예제 #1
0
        private static string GetTokenText(
            EsperEPL2GrammarParser parser,
            int tokenIndex)
        {
            var expected = END_OF_INPUT_TEXT;
            var vocabulary = parser.Vocabulary;
            var vocabularyTokenText = vocabulary.GetLiteralName(tokenIndex) ?? vocabulary.GetSymbolicName(tokenIndex);
            if (vocabularyTokenText != null) {
                expected = vocabularyTokenText;
            }

            var lexerTokenParaphrases = EsperEPL2GrammarParser.GetLexerTokenParaphrases();
            if (lexerTokenParaphrases.Get(tokenIndex) != null)
            {
                expected = lexerTokenParaphrases.Get(tokenIndex);
            }

            var parserTokenParaphrases = EsperEPL2GrammarParser.GetParserTokenParaphrases();
            if (parserTokenParaphrases.Get(tokenIndex) != null)
            {
                expected = parserTokenParaphrases.Get(tokenIndex);
            }

            return expected;
        }
예제 #2
0
        private static ParseException GetMessage(
            bool module,
            bool uses,
            string resourceName,
            int type)
        {
            var message = "Keyword '";
            if (module) {
                message += "module";
            }
            else if (uses) {
                message += "uses";
            }
            else {
                message += "import";
            }

            message += "' must be followed by a name or package name (set of names separated by dots) for resource '" +
                       resourceName +
                       "'";

            if (type != -1) {
                string tokenName = EsperEPL2GrammarParser.GetLexerTokenParaphrases().Get(type);
                if (tokenName == null) {
                    tokenName = EsperEPL2GrammarParser.GetParserTokenParaphrases().Get(type);
                }

                if (tokenName != null) {
                    message += ", unexpected reserved keyword " + tokenName + " was encountered as part of the name";
                }
            }

            return new ParseException(message);
        }
예제 #3
0
        private static ScriptOrClassResult RewriteTokensScript(CommonTokenStream tokens)
        {
            IList<string> scripts = new List<string>();
            IList<string> classes = new List<string>();

            IList<UniformPair<int>> tokenIndexRanges = new List<UniformPair<int>>();
            int tokenIndex = 0;

            while (tokenIndex < tokens.Size) {
                if (tokens.Get(tokenIndex).Type == EsperEPL2GrammarParser.EXPRESSIONDECL) {
                    var tokenBefore = GetTokenBefore(tokenIndex, tokens);
                    var isCreateExpressionClause = tokenBefore != null && tokenBefore.Type == EsperEPL2GrammarParser.CREATE;
                    var nameAndNameStart = FindScriptName(tokenIndex + 1, tokens);

                    var startIndex = FindStartTokenScript(nameAndNameStart.Second, tokens, EsperEPL2GrammarParser.LBRACK);
                    if (startIndex != -1) {
                        var endIndex = FindEndTokenScript(
                            startIndex + 1,
                            tokens,
                            EsperEPL2GrammarParser.RBRACK,
                            EsperEPL2GrammarParser.GetAfterScriptTokens(),
                            !isCreateExpressionClause);
                        if (endIndex != -1) {
                            var writer = new StringWriter();
                            for (var j = startIndex + 1; j < endIndex; j++) {
                                writer.Write(tokens.Get(j).Text);
                            }

                            scripts.Add(writer.ToString());
                            tokenIndexRanges.Add(new UniformPair<int>(startIndex, endIndex));
                            tokenIndex = endIndex;
                        }
                    }
                }

                if (tokens.Get(tokenIndex).Type == EsperEPL2GrammarParser.CLASSDECL) {
                    int startIndex = FindTokenClass(tokenIndex, tokens);
                    if (startIndex != -1) {
                        int endIndex = FindTokenClass(startIndex + 1, tokens);
                        if (endIndex != -1) {

                            StringWriter writer = new StringWriter();
                            for (int j = startIndex + 1; j < endIndex; j++) {
                                writer.Write(tokens.Get(j).Text);
                            }

                            classes.Add(writer.ToString());
                            tokenIndexRanges.Add(new UniformPair<int>(startIndex, endIndex));
                            tokenIndex = endIndex;
                        }
                    }
                }

                tokenIndex++;
            }

            var rewrittenEPL = RewriteEPL(tokenIndexRanges, tokens);
            return new ScriptOrClassResult(rewrittenEPL, scripts, classes);
        }
예제 #4
0
 public static EsperEPL2GrammarParser NewParser(ITokenStream tokens)
 {
     var g = new EsperEPL2GrammarParser(tokens);
     g.RemoveErrorListeners();
     g.AddErrorListener(Antlr4ErrorListener<IToken>.INSTANCE);
     g.ErrorHandler = new Antlr4ErrorStrategy();
     return g;
 }
예제 #5
0
 /// <summary>
 /// Converts from a syntax error to a nice property exception.
 /// </summary>
 /// <param name="e">is the syntax error</param>
 /// <param name="expression">is the expression text</param>
 /// <param name="parser">the parser that parsed the expression</param>
 /// <param name="addPleaseCheck">indicates to add "please check" paraphrases</param>
 /// <returns>syntax exception</returns>
 public static PropertyAccessException ConvertProperty(
     RecognitionException e,
     string expression,
     bool addPleaseCheck,
     EsperEPL2GrammarParser parser)
 {
     var pair = Convert(e, expression, addPleaseCheck, parser);
     return new PropertyAccessException(pair.First, pair.Second);
 }
예제 #6
0
 /// <summary>
 /// Converts from a syntax error to a nice statement exception.
 /// </summary>
 /// <param name="e">is the syntax error</param>
 /// <param name="expression">is the expression text</param>
 /// <param name="parser">the parser that parsed the expression</param>
 /// <param name="addPleaseCheck">indicates to add "please check" paraphrases</param>
 /// <returns>syntax exception</returns>
 public static StatementSpecCompileSyntaxException ConvertStatement(
     RecognitionException e,
     string expression,
     bool addPleaseCheck,
     EsperEPL2GrammarParser parser)
 {
     var pair = Convert(e, expression, addPleaseCheck, parser);
     return new StatementSpecCompileSyntaxException(pair.First, e, pair.Second);
 }
예제 #7
0
        private static string GetTokenText(EsperEPL2GrammarParser parser, int tokenIndex)
        {
            var expected = END_OF_INPUT_TEXT;

            if ((tokenIndex >= 0) && (tokenIndex < parser.TokenNames.Length))
            {
                expected = parser.TokenNames[tokenIndex];
            }
            var lexerTokenParaphrases = EsperEPL2GrammarLexer.GetLexerTokenParaphrases();

            if (lexerTokenParaphrases.Get(tokenIndex) != null)
            {
                expected = lexerTokenParaphrases.Get(tokenIndex);
            }
            var parserTokenParaphrases = EsperEPL2GrammarParser.GetParserTokenParaphrases();

            if (parserTokenParaphrases.Get(tokenIndex) != null)
            {
                expected = parserTokenParaphrases.Get(tokenIndex);
            }
            return(expected);
        }
        private static StartEventPropertyRuleContext HandleRecognitionEx(
            RecognitionException e,
            CommonTokenStream tokens,
            string propertyName,
            EsperEPL2GrammarParser g)
        {
            // Check for keywords and escape each, parse again
            var escapedPropertyName = EscapeKeywords(tokens);

            var inputEscaped  = new CaseInsensitiveInputStream(escapedPropertyName);
            var lexEscaped    = ParseHelper.NewLexer(inputEscaped);
            var tokensEscaped = new CommonTokenStream(lexEscaped);
            var gEscaped      = ParseHelper.NewParser(tokensEscaped);

            try {
                return(gEscaped.startEventPropertyRule());
            }
            catch {
                // ignored
            }

            throw ExceptionConvertor.ConvertProperty(e, propertyName, true, g);
        }
예제 #9
0
        private static string GetTokenText(EsperEPL2GrammarParser parser, int tokenIndex)
        {
            var expected = END_OF_INPUT_TEXT;

            Vocabulary vocabulary = (Vocabulary)parser.Vocabulary;

            if ((tokenIndex >= 0) && (tokenIndex <= vocabulary.getMaxTokenType()))
            {
                expected = parser.Vocabulary.GetSymbolicName(tokenIndex);
            }
            var lexerTokenParaphrases = EsperEPL2GrammarLexer.GetLexerTokenParaphrases();

            if (lexerTokenParaphrases.Get(tokenIndex) != null)
            {
                expected = lexerTokenParaphrases.Get(tokenIndex);
            }
            var parserTokenParaphrases = EsperEPL2GrammarParser.GetParserTokenParaphrases();

            if (parserTokenParaphrases.Get(tokenIndex) != null)
            {
                expected = parserTokenParaphrases.Get(tokenIndex);
            }
            return(expected);
        }
예제 #10
0
 private static ITree DoEPLParseRule(EsperEPL2GrammarParser parser)
 {
     return(parser.startEPLExpressionRule());
 }
예제 #11
0
 private static ITree DoPatternParseRule(EsperEPL2GrammarParser parser)
 {
     return(parser.startPatternExpressionRule());
 }
예제 #12
0
 public ITree InvokeParseRule(EsperEPL2GrammarParser parser)
 {
     return(parser.startEPLExpressionRule());
 }
예제 #13
0
        private static ScriptResult RewriteTokensScript(CommonTokenStream tokens)
        {
            IList <String> scripts = new List <String>();

            IList <UniformPair <int?> > scriptTokenIndexRanges = new List <UniformPair <int?> >();

            for (var i = 0; i < tokens.Size; i++)
            {
                if (tokens.Get(i).Type == EsperEPL2GrammarParser.EXPRESSIONDECL)
                {
                    var tokenBefore = GetTokenBefore(i, tokens);
                    var isCreateExpressionClause = tokenBefore != null && tokenBefore.Type == EsperEPL2GrammarParser.CREATE;
                    var nameAndNameStart         = FindScriptName(i + 1, tokens);

                    var startIndex = FindStartTokenScript(nameAndNameStart.Second.Value, tokens, EsperEPL2GrammarParser.LBRACK);
                    if (startIndex != -1)
                    {
                        var endIndex = FindEndTokenScript(startIndex + 1, tokens, EsperEPL2GrammarParser.RBRACK, EsperEPL2GrammarParser.GetAfterScriptTokens(), !isCreateExpressionClause);
                        if (endIndex != -1)
                        {
                            var writer = new StringWriter();
                            for (var j = startIndex + 1; j < endIndex; j++)
                            {
                                writer.Write(tokens.Get(j).Text);
                            }
                            scripts.Add(writer.ToString());
                            scriptTokenIndexRanges.Add(new UniformPair <int?>(startIndex, endIndex));
                        }
                    }
                }
            }

            var rewrittenEPL = RewriteScripts(scriptTokenIndexRanges, tokens);

            return(new ScriptResult(rewrittenEPL, scripts));
        }
예제 #14
0
 public ITree InvokeParseRule(EsperEPL2GrammarParser parser) => ProcInvokeParseRule.Invoke(parser);
예제 #15
0
        private static EsperEPL2GrammarParser.StartEventPropertyRuleContext HandleRecognitionEx(RecognitionException e, CommonTokenStream tokens, string propertyName, EsperEPL2GrammarParser g)
        {
            // Check for keywords and escape each, parse again
            var escapedPropertyName = EscapeKeywords(tokens);

            ICharStream inputEscaped;

            try
            {
                inputEscaped = new NoCaseSensitiveStream(escapedPropertyName);
            }
            catch (IOException ex)
            {
                throw new PropertyAccessException("IOException parsing property name '" + propertyName + '\'', ex);
            }

            var lexEscaped    = ParseHelper.NewLexer(inputEscaped);
            var tokensEscaped = new CommonTokenStream(lexEscaped);
            var gEscaped      = ParseHelper.NewParser(tokensEscaped);

            try
            {
                return(gEscaped.startEventPropertyRule());
            }
            catch
            {
            }

            throw ExceptionConvertor.ConvertProperty(e, propertyName, true, g);
        }
예제 #16
0
        /// <summary>
        /// Converts from a syntax error to a nice exception.
        /// </summary>
        /// <param name="e">is the syntax error</param>
        /// <param name="expression">is the expression text</param>
        /// <param name="parser">the parser that parsed the expression</param>
        /// <param name="addPleaseCheck">indicates to add "please check" paraphrases</param>
        /// <returns>syntax exception</returns>
        public static UniformPair<string> Convert(
            RecognitionException e,
            string expression,
            bool addPleaseCheck,
            EsperEPL2GrammarParser parser)
        {
            if (string.IsNullOrWhiteSpace(expression))
            {
                var errorMessage = "Unexpected " + END_OF_INPUT_TEXT;
                return new UniformPair<string>(errorMessage, expression);
            }

            IToken t;
            IToken tBeforeBefore = null;
            IToken tBefore = null;
            IToken tAfter = null;

            var tIndex = e.OffendingToken != null ? e.OffendingToken.TokenIndex : int.MaxValue;
            if (tIndex < parser.TokenStream.Size)
            {
                t = parser.TokenStream.Get(tIndex);
                if ((tIndex + 1) < parser.TokenStream.Size)
                {
                    tAfter = parser.TokenStream.Get(tIndex + 1);
                }

                if (tIndex - 1 >= 0)
                {
                    tBefore = parser.TokenStream.Get(tIndex - 1);
                }

                if (tIndex - 2 >= 0)
                {
                    tBeforeBefore = parser.TokenStream.Get(tIndex - 2);
                }
            }
            else
            {
                if (parser.TokenStream.Size >= 1)
                {
                    tBeforeBefore = parser.TokenStream.Get(parser.TokenStream.Size - 1);
                }

                if (parser.TokenStream.Size >= 2)
                {
                    tBefore = parser.TokenStream.Get(parser.TokenStream.Size - 2);
                }

                t = parser.TokenStream.Get(parser.TokenStream.Size - 1);
            }

            IToken tEnd = null;
            if (parser.TokenStream.Size > 0)
            {
                tEnd = parser.TokenStream.Get(parser.TokenStream.Size - 1);
            }

            var positionInfo = GetPositionInfo(t);
            var token = t.Type == EsperEPL2GrammarParser.Eof ? "end-of-input" : "'" + t.Text + "'";

            var stack = parser.GetParaphrases();
            var check = "";
            var isSelect = stack.Count == 1 && (stack.Peek() == "select clause");
            if ((stack.Count > 0) && addPleaseCheck)
            {
                var delimiter = "";
                var checkList = new StringBuilder();
                checkList.Append(", please check the ");
                while (stack.Count != 0)
                {
                    checkList.Append(delimiter);
                    checkList.Append(stack.Pop());
                    delimiter = " within the ";
                }

                check = checkList.ToString();
            }

            // check if token is a reserved keyword
            var keywords = parser.GetKeywords();
            var reservedKeyword = false;
            if (keywords.Contains(token.ToLowerInvariant()))
            {
                token += " (a reserved keyword)";
                reservedKeyword = true;
            }
            else if (tAfter != null && keywords.Contains("'" + tAfter.Text.ToLowerInvariant() + "'"))
            {
                token += " ('" + tAfter.Text + "' is a reserved keyword)";
                reservedKeyword = true;
            }
            else
            {
                if ((tBefore != null) &&
                    (tAfter != null) &&
                    (keywords.Contains("'" + tBefore.Text.ToLowerInvariant() + "'")) &&
                    (keywords.Contains("'" + tAfter.Text.ToLowerInvariant() + "'")))
                {
                    token += " ('" + tBefore.Text + "' and '" + tAfter.Text + "' are a reserved keyword)";
                    reservedKeyword = true;
                }
                else if ((tBefore != null) &&
                         (keywords.Contains("'" + tBefore.Text.ToLowerInvariant() + "'")))
                {
                    token += " ('" + tBefore.Text + "' is a reserved keyword)";
                    reservedKeyword = true;
                }
                else if (tEnd != null && keywords.Contains("'" + tEnd.Text.ToLowerInvariant() + "'"))
                {
                    token += " ('" + tEnd.Text + "' is a reserved keyword)";
                    reservedKeyword = true;
                }
            }

            // special handling for the select-clause "as" keyword, which is required
            if (isSelect && !reservedKeyword)
            {
                check += GetSelectClauseAsText(tBeforeBefore, t);
            }

            var message = "Incorrect syntax near " + token + positionInfo + check;
            if (e is NoViableAltException || e is LexerNoViableAltException || CheckForInputMismatchWithNoExpected(e))
            {
                var nvaeToken = e.OffendingToken;
                var nvaeTokenType = nvaeToken != null ? nvaeToken.Type : EsperEPL2GrammarLexer.Eof;

                if (nvaeTokenType == EsperEPL2GrammarLexer.Eof)
                {
                    if (token.Equals(END_OF_INPUT_TEXT))
                    {
                        message = "Unexpected " + END_OF_INPUT_TEXT + positionInfo + check;
                    }
                    else
                    {
                        if (ParseHelper.HasControlCharacters(expression))
                        {
                            message = "Unrecognized control characters found in text" + positionInfo;
                        }
                        else
                        {
                            message = "Unexpected " + END_OF_INPUT_TEXT + " near " + token + positionInfo + check;
                        }
                    }
                }
                else {
                    var parserTokenParaphrases = EsperEPL2GrammarParser.GetParserTokenParaphrases();
                    if (parserTokenParaphrases.Get(nvaeTokenType) != null)
                    {
                        message = "Incorrect syntax near " + token + positionInfo + check;
                    }
                    else
                    {
                        // find next keyword in the next 3 tokens
                        var currentIndex = tIndex + 1;
                        while ((currentIndex > 0) &&
                               (currentIndex < parser.TokenStream.Size - 1) &&
                               (currentIndex < tIndex + 3))
                        {
                            var next = parser.TokenStream.Get(currentIndex);
                            currentIndex++;

                            var quotedToken = "'" + next.Text + "'";
                            if (parser.GetKeywords().Contains(quotedToken))
                            {
                                check += " near reserved keyword '" + next.Text + "'";
                                break;
                            }
                        }

                        message = "Incorrect syntax near " + token + positionInfo + check;
                    }
                }
            }
            else if (e is InputMismatchException)
            {
                var mismatched = (InputMismatchException) e;

                string expected;
                var expectedTokens = mismatched.GetExpectedTokens().ToList();
                if (expectedTokens.Count > 1)
                {
                    var writer = new StringWriter();
                    writer.Write("any of the following tokens {");
                    var delimiter = "";
                    for (var i = 0; i < expectedTokens.Count; i++)
                    {
                        writer.Write(delimiter);
                        if (i > 5)
                        {
                            writer.Write("...");
                            writer.Write(expectedTokens.Count - 5);
                            writer.Write(" more");
                            break;
                        }

                        delimiter = ", ";
                        writer.Write(GetTokenText(parser, expectedTokens[i]));
                    }

                    writer.Write("}");
                    expected = writer.ToString();
                }
                else
                {
                    expected = GetTokenText(parser, expectedTokens[0]);
                }

                var offendingTokenType = mismatched.OffendingToken.Type;
                var unexpected = GetTokenText(parser, offendingTokenType);

                var expecting = " expecting " + expected.Trim() + " but found " + unexpected.Trim();
                message = "Incorrect syntax near " + token + expecting + positionInfo + check;
            }

            return new UniformPair<string>(message, expression);
        }