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

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

Introduction

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

Prototype

int getCharPositionInLine();

Source Link

Document

The index of the first character of this token relative to the beginning of the line at which it occurs, 0..n-1

Usage

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 w  w.j  a  v a 2s  . c  om
        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   w  w  w  .  jav  a 2 s  .c  o  m
        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>/*ww  w.  j  a v a  2 s .  c o m*/
 * 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.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);
    }//from   ww  w  . j av  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  v  a 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);
}

From source file:org.osate.ba.texteditor.XtextAadlBaHighlighter.java

License:Open Source License

@Override
public void addToHighlighting(int annexOffset, Token token, String id) {
    int offset = ((CommonToken) token).getStartIndex();
    int length = token.getText().length();
    int column = token.getCharPositionInLine();

    _elementToHighlight.add(new AadlBaLocationReference(annexOffset, offset, length, column, id));
}

From source file:org.reaktivity.nukleus.maven.plugin.internal.AbstractMojo.java

License:Apache License

private AstSpecificationNode parseSpecification(String resourceName, URL resource) throws IOException {
    try (InputStream input = resource.openStream()) {
        ANTLRInputStream ais = new ANTLRInputStream(input);
        NukleusLexer lexer = new NukleusLexer(ais);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        NukleusParser parser = new NukleusParser(tokens);
        parser.setErrorHandler(new BailErrorStrategy());

        SpecificationContext ctx = parser.specification();
        return new AstParser().visitSpecification(ctx);
    } catch (ParseCancellationException ex) {
        Throwable cause = ex.getCause();
        if (cause instanceof RecognitionException) {
            RecognitionException re = (RecognitionException) cause;
            Token token = re.getOffendingToken();
            if (token != null) {
                String message = String.format("Parse failed in %s at %d:%d on \"%s\"", resourceName,
                        token.getLine(), token.getCharPositionInLine(), token.getText());
                getLog().error(message);
            }//  ww  w .j  a v  a  2s. c o  m
        }

        throw ex;
    }
}

From source file:org.smallpearl.compiler.Symbol.java

License:BSD License

public Symbol(Token t, int type) {
    this(t.getText(), t.getLine(), t.getCharPositionInLine(), type);
}

From source file:org.sourcepit.ltk.format.CommentParserDelegate.java

License:Apache License

@Override
public int getLen(RuleNode parent, Token token, List<Token> hiddenTokensToRight, TokenStream tokenStream) {
    int len = 0;/*  w  w w. ja v  a2  s  . c  om*/
    for (org.antlr.v4.runtime.Token hiddenToken : hiddenTokensToRight) {
        final String text = hiddenToken.getText();
        if (TokenUtils.isWs(text)) {
            len++;
        } else {
            if (hiddenToken.getCharPositionInLine() > token.getCharPositionInLine()) {
                len++;
            } else {
                break;
            }
        }
    }
    return len;
}