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.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);
            }/*from w  w w. j  a  v a  2 s  .  co 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.structr.core.graphql.GraphQLRequest.java

License:Open Source License

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

    try {/*w  ww . j a  v  a 2s.  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();
        endCol = stop.getCharPositionInLine() + 1;
    }// w w  w .ja va  2s  .c om

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

From source file:sonar.AntlrMumpsTokenizer.java

License:Open Source License

@Override
public void tokenize(SourceCode source, Tokens pmdTokens) throws IOException {
    LOG.info("Tokenizing file: " + source.getFileName());
    String fileName = source.getFileName();
    ANTLRInputStream input = new ANTLRFileStream(fileName);
    MLexer lexer = new MLexer(input);
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    final int eof = -1;
    Token antlrToken = tokenStream.LT(1);

    while (antlrToken.getType() != eof) {
        tokenStream.consume();//from ww w  .  j a  v a 2s.  c om
        TokenEntry pmdToken = new TokenEntry(MLexer.tokenNames[antlrToken.getType()], fileName,
                antlrToken.getLine());
        pmdTokens.add(pmdToken);
        antlrToken = tokenStream.LT(1);
    }

    pmdTokens.add(TokenEntry.getEOF());
}

From source file:swiprolog.visitor.Visitor4Internal.java

License:Open Source License

/**
 * Create {@link SourceInfoObject} for given context.
 *
 * @param ctx/*w w  w. j  av 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//from   w w w  .j  a v a2  s .co  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]);
    }/* ww  w  . j a  v a 2s  .  c  o m*/
    String location = "";
    if (token != null) {
        location = "line " + token.getLine() + ":" + token.getCharPositionInLine() + " ";
    }
    return location + template.render();
}