Search in sources :

Example 6 with LexerNoViableAltException

use of org.antlr.v4.runtime.LexerNoViableAltException in project lucene-solr by apache.

the class JavascriptErrorHandlingLexer method recover.

/**
   * Ensures the ANTLR lexer will throw an exception after the first error
   * @param lnvae the lexer exception
   */
@Override
public void recover(LexerNoViableAltException lnvae) {
    CharStream charStream = lnvae.getInputStream();
    int startIndex = lnvae.getStartIndex();
    String text = charStream.getText(Interval.of(startIndex, charStream.index()));
    ParseException parseException = new ParseException("unexpected character '" + getErrorDisplay(text) + "'" + " on line (" + _tokenStartLine + ") position (" + _tokenStartCharPositionInLine + ")", _tokenStartCharIndex);
    parseException.initCause(lnvae);
    throw new RuntimeException(parseException);
}
Also used : ParseException(java.text.ParseException) CharStream(org.antlr.v4.runtime.CharStream)

Example 7 with LexerNoViableAltException

use of org.antlr.v4.runtime.LexerNoViableAltException in project groovy by apache.

the class SmartDocumentFilter method parseDocument.

private void parseDocument() throws BadLocationException {
    GroovyLangLexer lexer;
    try {
        lexer = createLexer(styledDocument.getText(0, styledDocument.getLength()));
    } catch (IOException e) {
        e.printStackTrace();
        this.latest = false;
        return;
    }
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    try {
        tokenStream.fill();
    } catch (LexerNoViableAltException | GroovySyntaxError e) {
        // ignore
        this.latest = false;
        return;
    } catch (Exception e) {
        e.printStackTrace();
        this.latest = false;
        return;
    }
    List<Token> tokenList = tokenStream.getTokens();
    List<Token> tokenListToRender;
    try {
        tokenListToRender = findTokensToRender(tokenList);
    } finally {
        this.setRenderRange(null);
    }
    for (Token token : tokenListToRender) {
        int tokenType = token.getType();
        if (EOF == tokenType) {
            continue;
        }
        int tokenStartIndex = token.getStartIndex();
        int tokenStopIndex = token.getStopIndex();
        int tokenLength = tokenStopIndex - tokenStartIndex + 1;
        styledDocument.setCharacterAttributes(tokenStartIndex, tokenLength, findStyleByTokenType(tokenType), true);
        if (GStringBegin == tokenType || GStringPart == tokenType) {
            styledDocument.setCharacterAttributes(tokenStartIndex + tokenLength - 1, 1, defaultStyle, true);
        }
    }
    this.latestTokenList = tokenList;
    this.latest = true;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) GroovyLangLexer(org.apache.groovy.parser.antlr4.GroovyLangLexer) LexerNoViableAltException(org.antlr.v4.runtime.LexerNoViableAltException) Token(org.antlr.v4.runtime.Token) IOException(java.io.IOException) GroovySyntaxError(org.apache.groovy.parser.antlr4.GroovySyntaxError) BadLocationException(javax.swing.text.BadLocationException) LexerNoViableAltException(org.antlr.v4.runtime.LexerNoViableAltException) IOException(java.io.IOException)

Aggregations

CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)4 LexerNoViableAltException (org.antlr.v4.runtime.LexerNoViableAltException)4 Token (org.antlr.v4.runtime.Token)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)2 CharStream (org.antlr.v4.runtime.CharStream)2 L42Lexer (antlrGenerated.L42Lexer)1 L42Parser (antlrGenerated.L42Parser)1 StringReader (java.io.StringReader)1 ParseException (java.text.ParseException)1 BadLocationException (javax.swing.text.BadLocationException)1 CommonToken (org.antlr.v4.runtime.CommonToken)1 GroovyLangLexer (org.apache.groovy.parser.antlr4.GroovyLangLexer)1 GroovySyntaxError (org.apache.groovy.parser.antlr4.GroovySyntaxError)1 Location (org.elasticsearch.painless.Location)1