Search in sources :

Example 1 with LexerNoViableAltException

use of org.antlr.v4.runtime.LexerNoViableAltException in project antlr4 by tunnelvisionlabs.

the class XPath method split.

// TODO: check for invalid token/rule names, bad syntax
public XPathElement[] split(String path) {
    XPathLexer lexer = new XPathLexer(CharStreams.fromString(path)) {

        @Override
        public void recover(LexerNoViableAltException e) {
            throw e;
        }
    };
    lexer.removeErrorListeners();
    lexer.addErrorListener(new XPathLexerErrorListener());
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    try {
        tokenStream.fill();
    } catch (LexerNoViableAltException e) {
        int pos = lexer.getCharPositionInLine();
        String msg = "Invalid tokens or characters at index " + pos + " in path '" + path + "'";
        throw new IllegalArgumentException(msg, e);
    }
    List<Token> tokens = tokenStream.getTokens();
    // System.out.println("path="+path+"=>"+tokens);
    List<XPathElement> elements = new ArrayList<XPathElement>();
    int n = tokens.size();
    int i = 0;
    loop: while (i < n) {
        Token el = tokens.get(i);
        Token next = null;
        switch(el.getType()) {
            case XPathLexer.ROOT:
            case XPathLexer.ANYWHERE:
                boolean anywhere = el.getType() == XPathLexer.ANYWHERE;
                i++;
                next = tokens.get(i);
                boolean invert = next.getType() == XPathLexer.BANG;
                if (invert) {
                    i++;
                    next = tokens.get(i);
                }
                XPathElement pathElement = getXPathElement(next, anywhere);
                pathElement.invert = invert;
                elements.add(pathElement);
                i++;
                break;
            case XPathLexer.TOKEN_REF:
            case XPathLexer.RULE_REF:
            case XPathLexer.WILDCARD:
                elements.add(getXPathElement(el, false));
                i++;
                break;
            case Token.EOF:
                break loop;
            default:
                throw new IllegalArgumentException("Unknowth path element " + el);
        }
    }
    return elements.toArray(new XPathElement[0]);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) LexerNoViableAltException(org.antlr.v4.runtime.LexerNoViableAltException) ArrayList(java.util.ArrayList) Token(org.antlr.v4.runtime.Token)

Example 2 with LexerNoViableAltException

use of org.antlr.v4.runtime.LexerNoViableAltException in project antlr4 by antlr.

the class XPath method split.

// TODO: check for invalid token/rule names, bad syntax
public XPathElement[] split(String path) {
    ANTLRInputStream in;
    try {
        in = new ANTLRInputStream(new StringReader(path));
    } catch (IOException ioe) {
        throw new IllegalArgumentException("Could not read path: " + path, ioe);
    }
    XPathLexer lexer = new XPathLexer(in) {

        @Override
        public void recover(LexerNoViableAltException e) {
            throw e;
        }
    };
    lexer.removeErrorListeners();
    lexer.addErrorListener(new XPathLexerErrorListener());
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    try {
        tokenStream.fill();
    } catch (LexerNoViableAltException e) {
        int pos = lexer.getCharPositionInLine();
        String msg = "Invalid tokens or characters at index " + pos + " in path '" + path + "'";
        throw new IllegalArgumentException(msg, e);
    }
    List<Token> tokens = tokenStream.getTokens();
    // System.out.println("path="+path+"=>"+tokens);
    List<XPathElement> elements = new ArrayList<XPathElement>();
    int n = tokens.size();
    int i = 0;
    loop: while (i < n) {
        Token el = tokens.get(i);
        Token next = null;
        switch(el.getType()) {
            case XPathLexer.ROOT:
            case XPathLexer.ANYWHERE:
                boolean anywhere = el.getType() == XPathLexer.ANYWHERE;
                i++;
                next = tokens.get(i);
                boolean invert = next.getType() == XPathLexer.BANG;
                if (invert) {
                    i++;
                    next = tokens.get(i);
                }
                XPathElement pathElement = getXPathElement(next, anywhere);
                pathElement.invert = invert;
                elements.add(pathElement);
                i++;
                break;
            case XPathLexer.TOKEN_REF:
            case XPathLexer.RULE_REF:
            case XPathLexer.WILDCARD:
                elements.add(getXPathElement(el, false));
                i++;
                break;
            case Token.EOF:
                break loop;
            default:
                throw new IllegalArgumentException("Unknowth path element " + el);
        }
    }
    return elements.toArray(new XPathElement[0]);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ArrayList(java.util.ArrayList) Token(org.antlr.v4.runtime.Token) IOException(java.io.IOException) LexerNoViableAltException(org.antlr.v4.runtime.LexerNoViableAltException) StringReader(java.io.StringReader) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Example 3 with LexerNoViableAltException

use of org.antlr.v4.runtime.LexerNoViableAltException in project antlr4 by antlr.

the class XPathLexer method nextToken.

@Override
public Token nextToken() {
    _tokenStartCharIndex = _input.index();
    CommonToken t = null;
    while (t == null) {
        switch(_input.LA(1)) {
            case '/':
                consume();
                if (_input.LA(1) == '/') {
                    consume();
                    t = new CommonToken(ANYWHERE, "//");
                } else {
                    t = new CommonToken(ROOT, "/");
                }
                break;
            case '*':
                consume();
                t = new CommonToken(WILDCARD, "*");
                break;
            case '!':
                consume();
                t = new CommonToken(BANG, "!");
                break;
            case '\'':
                String s = matchString();
                t = new CommonToken(STRING, s);
                break;
            case CharStream.EOF:
                return new CommonToken(EOF, "<EOF>");
            default:
                if (isNameStartChar(_input.LA(1))) {
                    String id = matchID();
                    if (Character.isUpperCase(id.charAt(0)))
                        t = new CommonToken(TOKEN_REF, id);
                    else
                        t = new CommonToken(RULE_REF, id);
                } else {
                    throw new LexerNoViableAltException(this, _input, _tokenStartCharIndex, null);
                }
                break;
        }
    }
    t.setStartIndex(_tokenStartCharIndex);
    t.setCharPositionInLine(_tokenStartCharIndex);
    t.setLine(line);
    return t;
}
Also used : LexerNoViableAltException(org.antlr.v4.runtime.LexerNoViableAltException) CommonToken(org.antlr.v4.runtime.CommonToken)

Example 4 with LexerNoViableAltException

use of org.antlr.v4.runtime.LexerNoViableAltException 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 5 with LexerNoViableAltException

use of org.antlr.v4.runtime.LexerNoViableAltException in project L42 by ElvisResearchGroup.

the class Parser method getParser.

public static antlrGenerated.L42Parser getParser(String s) {
    ANTLRInputStream in = new ANTLRInputStream(s);
    antlrGenerated.L42Lexer l = new antlrGenerated.L42Lexer(in);
    /*{
      @Override public void recover(LexerNoViableAltException e) {
        throw new IllegalArgumentException(e); // Bail out
      }
    @Override public void recover(RecognitionException re) {
        throw new IllegalArgumentException(re); // Bail out
      }};*/
    CommonTokenStream t = new CommonTokenStream(l);
    antlrGenerated.L42Parser p = new antlrGenerated.L42Parser(t);
    //    p.setErrorHandler(new BailErrorStrategy());
    return p;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) L42Lexer(antlrGenerated.L42Lexer) L42Lexer(antlrGenerated.L42Lexer) L42Parser(antlrGenerated.L42Parser) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) L42Parser(antlrGenerated.L42Parser)

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