Example usage for org.antlr.v4.runtime Token getLine

List of usage examples for org.antlr.v4.runtime Token getLine

Introduction

In this page you can find the example usage for org.antlr.v4.runtime Token getLine.

Prototype

int getLine();

Source Link

Document

The line number on which the 1st character of this token was matched, line=1..n

Usage

From source file:org.elasticsearch.xpack.sql.parser.AbstractBuilder.java

License:Open Source License

static Location source(Token token) {
    Check.notNull(token, "token is null");
    return new Location(token.getLine(), token.getCharPositionInLine());
}

From source file:org.flightgear.clgen.listener.ErrorListener.java

License:Open Source License

@Override
public void semanticError(final ParseTreeListener listener, final Token token, final String msg) {
    System.err.format("error at line %d: %s\n", token.getLine(), msg);
    System.err.println(errorContext(token.getLine(), token.getCharPositionInLine()));
}

From source file:org.flightgear.clgen.listener.ErrorListener.java

License:Open Source License

@Override
public void semanticWarning(final ParseTreeListener listener, final Token token, final String msg) {
    System.err.format("warning at line %d: %s\n", token.getLine(), msg);
    System.err.println(errorContext(token.getLine(), token.getCharPositionInLine()));
}

From source file:org.kaazing.k3po.lang.internal.parser.ScriptParserImpl.java

License:Open Source License

private ScriptParseException createScriptParseException(RobotParser parser, RecognitionException re) {

    if (re instanceof NoViableAltException) {
        return createScriptParseException(parser, (NoViableAltException) re);
    } else {//from   w ww.ja  va  2  s. c o  m
        Token token = re.getOffendingToken();
        String desc = format("line %d:%d: ", token.getLine(), token.getCharPositionInLine());

        String tokenText = token.getText();
        String msg = null;

        if (tokenText == null) {
            msg = "error: end of input";

        } else {
            desc = format("%s'%s'", desc, tokenText);

            @SuppressWarnings("unused")
            String unexpectedTokenName = token.getType() != -1 ? parser.getTokenNames()[token.getType()]
                    : parser.getTokenNames()[0];

            msg = format("error: unexpected keyword '%s'", tokenText);
        }

        return new ScriptParseException(msg, re);
    }
}

From source file:org.kaazing.robot.lang.parser.ScriptParserImpl.java

License:Open Source License

private ScriptParseException createScriptParseException(RobotParser parser, RecognitionException re) {

    if (re instanceof InputMismatchException) {
        return createScriptParseException(parser, (InputMismatchException) re);

    } else if (re instanceof NoViableAltException) {
        return createScriptParseException(parser, (NoViableAltException) re);

    } else {//from www . j a  va 2 s  .  c  om
        Token token = re.getOffendingToken();
        String desc = String.format("line %d:%d: ", token.getLine(), token.getCharPositionInLine());

        String tokenText = token.getText();
        String msg = null;

        if (tokenText == null) {
            msg = "error: end of input";

        } else {
            desc = String.format("%s'%s'", desc, tokenText);

            @SuppressWarnings("unused")
            String unexpectedTokenName = token.getType() != -1 ? parser.getTokenNames()[token.getType()]
                    : parser.getTokenNames()[0];

            msg = String.format("error: unexpected keyword '%s'", tokenText);
        }

        return new ScriptParseException(msg, re);
    }
}

From source file:org.lockss.tdb.AntlrUtil.java

License:Open Source License

/**
 * <p>/*from   www.  j a  v  a 2 s.  c om*/
 * Throws a {@link SyntaxError} based on the offending {@link Token}.
 * </p>
 * 
 * @param token
 *          An offending token.
 * @param format
 *          A format string.
 * @param args
 *          Arguments for the format string.
 * @throws SyntaxError
 *           <b>always</b> thrown by this method.
 * @since 1.67
 * @see #errorMessage(String, int, int, String, Object...)
 */
public static void syntaxError(Token token, String format, Object... args) throws SyntaxError {
    String errMsg = errorMessage(token.getInputStream().getSourceName(), token.getLine(),
            token.getCharPositionInLine(), format, args);
    throw new SyntaxError(errMsg);
}

From source file:org.opencypher.tools.g4processors.BNFListener.java

License:Apache License

private String findHiddenTextBefore(ParserRuleContext ctx, boolean forHeader) {
    Token startCtx = ctx.getStart();
    int i = startCtx.getTokenIndex();
    List<Token> normalTextChannel = tokens.getHiddenTokensToLeft(i, BNFLexer.HIDDEN);
    if (normalTextChannel != null) {
        // find where the blank lines are
        // when called for a rule, is the quasi-comment part of the content of the previous rule or
        // the description of this one. Immaterial for grammar header

        List<Token> lineTokens = normalTextChannel.stream().collect(Collectors.toList());

        int precedingBlankLines = startCtx.getLine() - lineTokens.get(lineTokens.size() - 1).getLine() - 1;
        if (precedingBlankLines > 0) {
            if (forHeader) {
                // this will preserve the linefeeds
                return lineTokens.stream().map(tk -> tk.getText().replaceFirst("// ?", ""))
                        .collect(Collectors.joining("\n"));
            } // it wasn't a description (just a stray comment ?)
        } else {//from  w  ww  . j a va 2 s .c om
            if (forHeader) {
                // no blank line, so this is a description to the first 
                return "";
            }
            // description - go back and find any gap showing a last blank line
            int lastGoodLine = startCtx.getLine() - 1;
            int currentIndex = lineTokens.size() - 1;
            while (currentIndex >= 0 && lineTokens.get(currentIndex).getLine() == lastGoodLine) {
                currentIndex--;
                lastGoodLine--;
            }
            List<String> content = new ArrayList<>();
            for (int j = currentIndex + 1; j < lineTokens.size(); j++) {
                content.add(lineTokens.get(j).getText().replaceFirst("// ?", ""));
            }
            return content.stream().collect(Collectors.joining("\n"));
        }
    }
    return "";
}

From source file:org.opencypher.tools.g4processors.BNFListener.java

License:Apache License

private String findHiddenTextAfter(ParserRuleContext ctx) {
    Token endCtx = ctx.getStop();
    int i = endCtx.getTokenIndex();
    List<Token> normalTextChannel = tokens.getHiddenTokensToRight(i, BNFLexer.HIDDEN);
    if (normalTextChannel != null) {
        // the quasi-comment (description) may be the end of a rule or start of the next. separation is on
        // a blank line
        int nextLine = endCtx.getLine() + 1;
        List<String> content = new ArrayList<>();
        for (Token lineToken : normalTextChannel) {
            if (lineToken.getLine() == nextLine) {
                content.add(lineToken.getText().replaceFirst("// ?", ""));
                nextLine++;/*from   ww  w. j  av a 2 s.  c o m*/
            } else {
                break;
            }
        }
        return content.stream().collect(Collectors.joining("\n"));
    }
    return "";
}

From source file:org.openehr.adl.parser.tree.AdlTreeParserException.java

License:Open Source License

private static String createMessage(@Nullable Token location, String message, Object... params) {
    if (location != null) {
        return location.getLine() + ":" + (location.getCharPositionInLine() + 1) + " "
                + String.format(message, params);
    }// w  ww  .  j  a  v  a  2  s.com
    return String.format(message, params);
}

From source file:org.osate.ba.parser.AadlBaParserVisitor.java

License:Open Source License

/**
 * Sets obj's location reference based on full token informations.
 *
 * @param obj the AObject to be set//from w ww.  ja va 2s .  co  m
 * @param src the token 
 */
protected void setLocationReference(AObject obj, Token token) {
    int offset = ((CommonToken) token).getStartIndex();
    int length = token.getText().length();
    int column = token.getCharPositionInLine() + 1; // Zero index based.
    int line = token.getLine();

    AadlBaLocationReference location = new AadlBaLocationReference(_annexOffset, _filename, line, offset,
            length, column, behaviorElementId);

    obj.setLocationReference(location);
}