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.structr.core.graphql.GraphQLRequest.java

License:Open Source License

public static Document parse(final Parser parser, final String query) throws FrameworkException {

    try {/* w  ww.ja v a 2  s.  c o  m*/

        return parser.parseDocument(query);

    } catch (Throwable t) {

        String message = t.getMessage();
        if (message == null) {

            message = t.getClass().getName();

            if (t instanceof ParseCancellationException) {

                final Throwable cause = t.getCause();
                if (cause instanceof RecognitionException) {

                    final RecognitionException err = (RecognitionException) cause;
                    final Token offendingToken = err.getOffendingToken();

                    if (offendingToken != null) {

                        final int line = offendingToken.getLine();
                        final int column = offendingToken.getCharPositionInLine();
                        final String text = offendingToken.getText();

                        message = "Parse error at " + text + " in line " + line + ", column " + column;
                    }
                }

            }
        }

        final FrameworkException fex = new FrameworkException(422, message);
        final Map<String, String> data = new LinkedHashMap<>();

        // do not output an empty array of errors
        fex.setErrorBuffer(null);
        fex.setData(data);

        data.put("query", query);

        throw fex;
    }
}

From source file:org.wso2.ballerinalang.compiler.parser.BLangParserListener.java

License:Open Source License

private DiagnosticPos getCurrentPos(ParserRuleContext ctx) {
    int startLine = ctx.getStart().getLine();
    int startCol = ctx.getStart().getCharPositionInLine() + 1;

    int endLine = -1;
    int endCol = -1;
    Token stop = ctx.getStop();
    if (stop != null) {
        endLine = stop.getLine();/*w w w  .j  a  va  2s.  c o m*/
        endCol = stop.getCharPositionInLine() + 1;
    }

    return new DiagnosticPos(diagnosticSrc, startLine, endLine, startCol, endCol);
}

From source file:swiprolog.visitor.Visitor4Internal.java

License:Open Source License

/**
 * Create {@link SourceInfoObject} for given context.
 *
 * @param ctx/*ww w  .ja  v a 2  s  .  c o m*/
 *            the {@link DirectiveContext} from the parsed object
 * @return {@link SourceInfoObject}
 */
private SourceInfo getSourceInfo(ParserRuleContext ctx) {
    Token start = (ctx == null) ? null : ctx.getStart();
    Token stop = (ctx == null) ? null : ctx.getStop();
    if (stop == null) {
        // happens if we are at EOF...
        stop = start;
    }
    return (start == null) ? null
            : new SourceInfoObject(this.source.getSource(), start.getLine(), start.getCharPositionInLine(),
                    this.source.getStartIndex() + start.getStartIndex() + 1,
                    this.source.getStartIndex() + stop.getStopIndex() + 1);
}

From source file:swiprolog.visitor.Visitor4Internal.java

License:Open Source License

private SourceInfo getSourceInfo(TerminalNode leaf) {
    Token symbol = (leaf == null) ? null : leaf.getSymbol();
    return (symbol == null) ? null
            : new SourceInfoObject(this.source.getSource(), symbol.getLine(), symbol.getCharPositionInLine(),
                    this.source.getStartIndex() + symbol.getStartIndex() + 1,
                    this.source.getStartIndex() + symbol.getStopIndex() + 1);
}

From source file:tuprolog.visitor.Visitor4Internal.java

License:Open Source License

/**
 * Create {@link SourceInfoObject} for given context.
 *
 * @param ctx// ww  w. ja  v  a2  s .c  o  m
 *            the {@link DirectiveContext} from the parsed object
 * @return {@link SourceInfoObject}
 */
private SourceInfo getSourceInfo(ParserRuleContext ctx) {
    Token start = ctx.getStart();
    Token stop = ctx.getStop();
    if (stop == null) {
        // happens if we are at EOF...
        stop = start;
    }
    return new SourceInfoObject(this.source.getSource(), start.getLine(), start.getCharPositionInLine(),
            this.source.getStartIndex() + start.getStartIndex() + 1,
            this.source.getStartIndex() + stop.getStopIndex() + 1);
}

From source file:tuprolog.visitor.Visitor4Internal.java

License:Open Source License

private SourceInfo getSourceInfo(TerminalNode leaf) {
    Token symbol = leaf.getSymbol();
    return new SourceInfoObject(this.source.getSource(), symbol.getLine(), symbol.getCharPositionInLine(),
            this.source.getStartIndex() + symbol.getStartIndex() + 1,
            this.source.getStartIndex() + symbol.getStopIndex() + 1);
}

From source file:wich.errors.WichErrorHandler.java

License:Open Source License

protected String getErrorMessage(Token token, ErrorType type, String[] args) {
    ST template = new ST(type.getMessageTemplate());
    for (int i = 0; i < args.length; ++i) {
        template.add("arg" + String.valueOf(i + 1), args[i]);
    }//  www  .  j a va  2  s.co m
    String location = "";
    if (token != null) {
        location = "line " + token.getLine() + ":" + token.getCharPositionInLine() + " ";
    }
    return location + template.render();
}

From source file:x10dt.ui.parser.ParseController.java

License:Open Source License

private IToken getIToken(final Token token) {
    IToken ret = new IToken() {

        public int getAdjunctIndex() {
            // TODO Auto-generated method stub
            return 0;
        }/*from   ww  w  .  j a  v a 2s.c  o m*/

        public int getColumn() {
            return token.getCharPositionInLine();
        }

        public int getEndColumn() {
            return token.getCharPositionInLine() + token.getStopIndex() - token.getStartIndex();
        }

        public int getEndLine() {
            return token.getLine();
        }

        public int getEndOffset() {
            return token.getStopIndex();
        }

        public IToken[] getFollowingAdjuncts() {
            // TODO Auto-generated method stub
            return null;
        }

        public ILexStream getILexStream() {
            // TODO Auto-generated method stub
            return null;
        }

        public IPrsStream getIPrsStream() {
            // TODO Auto-generated method stub
            return null;
        }

        public int getKind() {
            return token.getType();
        }

        public ILexStream getLexStream() {
            // TODO Auto-generated method stub
            return null;
        }

        public int getLine() {
            return token.getLine();
        }

        public IToken[] getPrecedingAdjuncts() {
            // TODO Auto-generated method stub
            return null;
        }

        public IPrsStream getPrsStream() {
            // TODO Auto-generated method stub
            return null;
        }

        public int getStartOffset() {
            return token.getStartIndex();
        }

        public int getTokenIndex() {
            return token.getTokenIndex();
        }

        public String getValue(char[] arg0) {
            return token.getText();
        }

        public void setAdjunctIndex(int arg0) {
            // TODO Auto-generated method stub

        }

        public void setEndOffset(int arg0) {
        }

        public void setKind(int arg0) {
            // TODO Auto-generated method stub

        }

        public void setStartOffset(int arg0) {
            // TODO Auto-generated method stub

        }

        public void setTokenIndex(int arg0) {
            // TODO Auto-generated method stub

        }

    };
    return ret;
}