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.ballerinalang.langserver.sourceprune.SourcePruner.java

License:Open Source License

private static TokenPosition locateCursorAtToken(Token token, int cLine, int cCol) {
    int tokenLine = token.getLine() - 1;
    int tokenStartCol = token.getCharPositionInLine();
    int tokenEndCol = tokenStartCol
            + ((token.getText().equals("\r\n") || token.getText().equals("\n")) ? 0 : token.getText().length());

    /*/*from   w  ww.  j ava2 s. co m*/
    Token which is considered as the token at cursor is the token immediate before the cursor,
     where its end column is cursor column 
     */
    if (tokenLine == cLine && tokenStartCol < cCol && tokenEndCol >= cCol
            && token.getType() != BallerinaParser.NEW_LINE) {
        return TokenPosition.ON;
    } else if (cLine > tokenLine || (tokenLine == cLine && cCol > tokenEndCol)) {
        return TokenPosition.RIGHT;
    } else {
        return TokenPosition.LEFT;
    }
}

From source file:org.ballerinalang.langserver.util.references.ReferenceFindTokenErrorStrategy.java

License:Open Source License

@Override
public void reportMatch(Parser recognizer) {
    super.reportMatch(recognizer);

    if (!terminateCheck) {
        Token currentToken = recognizer.getCurrentToken();
        // -1 added since the ANTLR line position is not zero based
        int tokenLine = currentToken.getLine() - 1;
        int tokenStartCol = currentToken.getCharPositionInLine();
        int tokenStopCol = tokenStartCol + currentToken.getText().length();

        if (this.line == tokenLine && this.col >= tokenStartCol && this.col <= tokenStopCol) {
            this.lsContext.put(NodeContextKeys.NODE_NAME_KEY, currentToken.getText());
            this.terminateCheck = true;
        }//  ww w .  jav a 2  s.c  o m
    }
}

From source file:org.ballerinalang.util.parser.BallerinaParserErrorStrategy.java

License:Open Source License

@Override
public void recover(Parser parser, RecognitionException e) {
    Token missingSymbol = parser.getCurrentToken();
    int line = missingSymbol.getLine();
    int position = missingSymbol.getCharPositionInLine();
    String mismatchedToken = getTokenErrorDisplay(missingSymbol);
    String msg = getSourceLocation(parser, line, position) + "invalid token " + mismatchedToken + ". "
            + e.getMessage();// ww  w  .  jav  a  2  s.c o  m
    setContextException(parser);
    throw new ParseCancellationException(msg);
}

From source file:org.beetl.core.AntlrProgramBuilder.java

License:BSD License

public GrammarToken getBTToken(Token t) {
    org.beetl.core.statement.GrammarToken token = new org.beetl.core.statement.GrammarToken(t.getText(),
            t.getLine(), t.getCharPositionInLine());
    return token;
}

From source file:org.beetl.core.parser.BeetlAntlrErrorStrategy.java

License:BSD License

protected GrammarToken getGrammarToken(Token token) {
    return GrammarToken.createToken(token.getText(), token.getLine());
}

From source file:org.eclipse.titan.common.parsers.cfg.CfgLocation.java

License:Open Source License

/**
 * Constructor for ANTLR v4 tokens/*  w w w.  ja  v a2s  .c  o m*/
 * @param aFile the parsed file
 * @param aStartToken the 1st token, its line and start position will be used for the location
 *                  NOTE: start position is the column index of the tokens 1st character.
 *                        Column index starts with 0.
 * @param aEndToken the last token, its end position will be used for the location.
 *                  NOTE: end position is the column index after the token's last character.
 */
public CfgLocation(final IFile aFile, final Token aStartToken, final Token aEndToken) {
    setLocation(aFile, aStartToken.getLine(), aStartToken.getStartIndex(), aEndToken.getStopIndex() + 1);
}

From source file:org.eclipse.titan.common.parsers.IntervalDetector.java

License:Open Source License

/**
 * Creates and pushes a new interval onto the stack of intervals. This new interval becomes the actual one.
 * <p>/*from   w w w .  jav a 2  s  . c o m*/
 * The ending offset of this interval is not yet set. @see #popInterval(int)
 *
 * @param aToken the first token of the interval
 * @param aType the type of the interval
 */
public final void pushInterval(final Token aToken, final interval_type aType) {
    pushInterval(aToken.getCharPositionInLine(), aToken.getLine(), aType);
}

From source file:org.eclipse.titan.common.parsers.IntervalDetector.java

License:Open Source License

/**
 * Pops the actual interval off of the stack, making its parent the actual interval. The ending offset of the popped off interval is set here.
 * <p>//  w w w  .j  a  v  a2s  .  co m
 * If the actual interval is the root interval, than it is not popped off the stack. This situation can only happen in case of a syntactically
 * invalid file.
 *
 * @param aToken the last token of the interval
 */
public final void popInterval(final Token aToken) {
    popInterval(aToken.getCharPositionInLine(), aToken.getLine());
}

From source file:org.eclipse.titan.common.parsers.ParserLogger.java

License:Open Source License

/**
 * Token info in string format for logging purpose
 * @param aToken token//  w  w w  .j a v  a2 s.  c o m
 * @param aTokenNameResolver resolver to get token name
 * @return &lt;token name&gt;: '&lt;token text&gt;', @&lt;token index&gt;, &lt;line&gt;:&lt;column&gt;[, channel=&lt;channel&gt;]
 *         <br>where
 *         <br>&lt;token index&gt; starts  from 0,
 *         <br>&lt;line&gt; starts from 1,
 *         <br>&lt;column&gt; starts from 0,
 *         <br>channel info is provided if &lt;channel&gt; > 0 (hidden channel)
 */
private static String getTokenInfo(final Token aToken, final TokenNameResolver aTokenNameResolver) {
    final StringBuilder sb = new StringBuilder();
    final int tokenType = aToken.getType();
    final String tokenName = getTokenName(tokenType, aTokenNameResolver);
    sb.append(tokenName);
    sb.append(": ");

    sb.append("'");
    sb.append(getEscapedTokenText(aToken));
    sb.append("'");

    sb.append(", @" + aToken.getTokenIndex());
    sb.append(", " + aToken.getLine() + ":" + aToken.getCharPositionInLine());
    if (aToken.getChannel() > 0) {
        sb.append(", channel=");
        sb.append(aToken.getChannel());
    }
    return sb.toString();
}

From source file:org.eclipse.titan.designer.AST.ASN1.Block.java

License:Open Source License

public Block(final Token token) {
    if (token instanceof TokenWithIndexAndSubTokens) {
        tokenList = ((TokenWithIndexAndSubTokens) token).getSubTokens();
        final IFile sourceFile = ((TokenWithIndexAndSubTokens) token).getSourceFile();
        setLocation(new Location(sourceFile, token.getLine(), token.getStartIndex(), token.getStopIndex()));
    } else {//ww  w  .  ja v  a  2s.  c om
        setLocation(NULL_Location.INSTANCE);
        tokenList = ((TokenWithIndexAndSubTokens) token).getSubTokens();
    }
}