Search in sources :

Example 16 with Location

use of org.elasticsearch.painless.Location in project elasticsearch by elastic.

the class EnhancedPainlessLexer method recover.

@Override
public void recover(final LexerNoViableAltException lnvae) {
    final CharStream charStream = lnvae.getInputStream();
    final int startIndex = lnvae.getStartIndex();
    final String text = charStream.getText(Interval.of(startIndex, charStream.index()));
    Location location = new Location(sourceName, _tokenStartCharIndex);
    String message = "unexpected character [" + getErrorDisplay(text) + "].";
    char firstChar = text.charAt(0);
    if ((firstChar == '\'' || firstChar == '"') && text.length() - 2 > 0 && text.charAt(text.length() - 2) == '\\') {
        /* Use a simple heuristic to guess if the unrecognized characters were trying to be a string but has a broken escape sequence.
             * If it was add an extra message about valid string escape sequences. */
        message += " The only valid escape sequences in strings starting with [" + firstChar + "] are [\\\\] and [\\" + firstChar + "].";
    }
    throw location.createError(new IllegalArgumentException(message, lnvae));
}
Also used : CharStream(org.antlr.v4.runtime.CharStream) Location(org.elasticsearch.painless.Location)

Example 17 with Location

use of org.elasticsearch.painless.Location in project elasticsearch by elastic.

the class ParserErrorStrategy method recover.

@Override
public void recover(final Parser recognizer, final RecognitionException re) {
    final Token token = re.getOffendingToken();
    String message;
    if (token == null) {
        message = "no parse token found.";
    } else if (re instanceof InputMismatchException) {
        message = "unexpected token [" + getTokenErrorDisplay(token) + "]" + " was expecting one of [" + re.getExpectedTokens().toString(recognizer.getVocabulary()) + "].";
    } else if (re instanceof NoViableAltException) {
        if (token.getType() == PainlessParser.EOF) {
            message = "unexpected end of script.";
        } else {
            message = "invalid sequence of tokens near [" + getTokenErrorDisplay(token) + "].";
        }
    } else {
        message = "unexpected token near [" + getTokenErrorDisplay(token) + "].";
    }
    Location location = new Location(sourceName, token == null ? -1 : token.getStartIndex());
    throw location.createError(new IllegalArgumentException(message, re));
}
Also used : NoViableAltException(org.antlr.v4.runtime.NoViableAltException) Token(org.antlr.v4.runtime.Token) InputMismatchException(org.antlr.v4.runtime.InputMismatchException) Location(org.elasticsearch.painless.Location)

Example 18 with Location

use of org.elasticsearch.painless.Location in project elasticsearch by elastic.

the class ParserErrorStrategy method recoverInline.

@Override
public Token recoverInline(final Parser recognizer) throws RecognitionException {
    final Token token = recognizer.getCurrentToken();
    final String message = "unexpected token [" + getTokenErrorDisplay(token) + "]" + " was expecting one of [" + recognizer.getExpectedTokens().toString(recognizer.getVocabulary()) + "].";
    Location location = new Location(sourceName, token.getStartIndex());
    throw location.createError(new IllegalArgumentException(message));
}
Also used : Token(org.antlr.v4.runtime.Token) Location(org.elasticsearch.painless.Location)

Aggregations

Location (org.elasticsearch.painless.Location)18 Struct (org.elasticsearch.painless.Definition.Struct)4 Token (org.antlr.v4.runtime.Token)2 Method (org.elasticsearch.painless.Definition.Method)2 MethodKey (org.elasticsearch.painless.Definition.MethodKey)2 Variable (org.elasticsearch.painless.Locals.Variable)2 Collections.emptyList (java.util.Collections.emptyList)1 Collections.singletonList (java.util.Collections.singletonList)1 List (java.util.List)1 Map (java.util.Map)1 CharStream (org.antlr.v4.runtime.CharStream)1 InputMismatchException (org.antlr.v4.runtime.InputMismatchException)1 NoViableAltException (org.antlr.v4.runtime.NoViableAltException)1 Cast (org.elasticsearch.painless.Definition.Cast)1 Field (org.elasticsearch.painless.Definition.Field)1 RuntimeClass (org.elasticsearch.painless.Definition.RuntimeClass)1 FeatureTest (org.elasticsearch.painless.FeatureTest)1 EFunctionRef (org.elasticsearch.painless.node.EFunctionRef)1 ENewArray (org.elasticsearch.painless.node.ENewArray)1 EString (org.elasticsearch.painless.node.EString)1