コード例 #1
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);
        }
コード例 #2
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;
        }
コード例 #3
0
ファイル: ExceptionConvertor.cs プロジェクト: ikvm/nesper
        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);
        }
コード例 #4
0
ファイル: ExceptionConvertor.cs プロジェクト: valmac/nesper
        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);
        }
コード例 #5
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);
        }