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.apache.sysml.parser.dml.DmlPreprocessor.java

License:Apache License

protected void notifyErrorListeners(String message, Token op) {
    errorListener.validationError(op.getLine(), op.getCharPositionInLine(), message);
}

From source file:org.apache.sysml.parser.python.PydmlSyntacticValidator.java

License:Apache License

private ConstIdentifier getConstIdFromString(String varValue, Token start) {
    // Both varName and varValue are correct
    int linePosition = start.getLine();
    int charPosition = start.getCharPositionInLine();
    try {/*from  www .j  a v a  2s .  co m*/
        long val = Long.parseLong(varValue);
        return new IntIdentifier(val, helper.getCurrentFileName(), linePosition, charPosition, linePosition,
                charPosition);
    } catch (Exception e) {
        try {
            double val = Double.parseDouble(varValue);
            return new DoubleIdentifier(val, helper.getCurrentFileName(), linePosition, charPosition,
                    linePosition, charPosition);
        } catch (Exception e1) {
            try {
                if (varValue.compareTo("True") == 0 || varValue.compareTo("False") == 0) {
                    boolean val = false;
                    if (varValue.compareTo("True") == 0) {
                        val = true;
                    }
                    return new BooleanIdentifier(val, helper.getCurrentFileName(), linePosition, charPosition,
                            linePosition, charPosition);
                } else {
                    String val = "";
                    String text = varValue;
                    if ((text.startsWith("\"") && text.endsWith("\""))
                            || (text.startsWith("\'") && text.endsWith("\'"))) {
                        if (text.length() > 2) {
                            val = text.substring(1, text.length() - 1);
                        }
                    } else {
                        // the commandline parameters can be passed without any quotes
                        val = text;
                    }
                    return new StringIdentifier(val, helper.getCurrentFileName(), linePosition, charPosition,
                            linePosition, charPosition);
                }
            } catch (Exception e3) {
                helper.notifyErrorListeners("unable to cast the commandline parameter into int/float/bool/str",
                        start);
                return null;
            }
        }
    }
}

From source file:org.apache.sysml.parser.python.PydmlSyntacticValidatorHelper.java

License:Apache License

public void raiseWarning(String message, Token op) {
    this._errorListener.validationWarning(op.getLine(), op.getCharPositionInLine(), message);
}

From source file:org.ballerinalang.composer.service.workspace.rest.datamodel.BallerinaComposerErrorStrategy.java

License:Open Source License

@Override
public void reportInputMismatch(Parser parser, InputMismatchException e) {
    Token missingSymbol = getMissingSymbol(parser);
    int line = missingSymbol.getLine();
    int position = missingSymbol.getCharPositionInLine();
    String mismatchedToken = getTokenErrorDisplay(e.getOffendingToken());
    String expectedToken = e.getExpectedTokens().toString(parser.getVocabulary());
    String msg = getSourceLocation(parser, line, position) + "mismatched input " + mismatchedToken
            + ". Expecting one of " + expectedToken;
    // FixMe: This need to be handled by grammar itself
    if (!EOF.equals(mismatchedToken)) {
        errorTokens.add(createError(line, position, msg));
    }/*from ww w.ja v a2 s.com*/
}

From source file:org.ballerinalang.composer.service.workspace.rest.datamodel.BallerinaComposerErrorStrategy.java

License:Open Source License

@Override
public void reportMissingToken(Parser parser) {
    Token token = parser.getCurrentToken();
    IntervalSet expecting = getExpectedTokens(parser);
    Token missingSymbol = getMissingSymbol(parser);
    int line = missingSymbol.getLine();
    int position = getMissingSymbol(parser).getCharPositionInLine();
    String missingToken = expecting.toString(parser.getVocabulary());
    String msg = getSourceLocation(parser, line, position) + "missing " + missingToken + " before "
            + getTokenErrorDisplay(token);
    errorTokens.add(createError(line, position, msg));
}

From source file:org.ballerinalang.composer.service.workspace.suggetions.CapturePossibleTokenStrategy.java

License:Open Source License

/**
 * Checks whether cursor is within the whitespace region between current token to last token
 * @param token Token to be evaluated// w  w w  .  ja  v  a 2  s . c  om
 * @param parser Parser Instance
 * @return true|false
 */
protected boolean isCursorBetweenGivenTokenAndLastNonHiddenToken(Token token, Parser parser) {
    boolean isCursorBetween = false;
    if (cursorPosition.equals(getSourcePosition(token))) {
        isCursorBetween = true;
    } else {
        Token lastNonHiddenToken = null;
        for (int tokenIdx = token.getTokenIndex() - 1; tokenIdx >= 0; tokenIdx--) {
            Token lastToken = parser.getTokenStream().get(tokenIdx);
            if (lastToken.getChannel() != Token.HIDDEN_CHANNEL) {
                lastNonHiddenToken = lastToken;
                break;
            }
        }
        if (lastNonHiddenToken != null) {
            if (cursorPosition.getLine() >= lastNonHiddenToken.getLine()
                    && cursorPosition.getLine() <= token.getLine()) {
                if (cursorPosition.getLine() == lastNonHiddenToken.getLine()) {
                    isCursorBetween = cursorPosition
                            .getCharacter() >= (lastNonHiddenToken.getCharPositionInLine()
                                    + lastNonHiddenToken.getText().length());
                } else if (cursorPosition.getLine() == token.getLine()) {
                    isCursorBetween = cursorPosition.getCharacter() <= token.getCharPositionInLine();
                } else {
                    isCursorBetween = true;
                }
            }
        }

    }
    return isCursorBetween;
}

From source file:org.ballerinalang.composer.service.workspace.suggetions.CapturePossibleTokenStrategy.java

License:Open Source License

protected Position getSourcePosition(Token token) {
    Position position = new Position();
    position.setLine(token.getLine());
    position.setCharacter(token.getCharPositionInLine());
    return position;
}

From source file:org.ballerinalang.langserver.common.utils.CommonUtil.java

License:Open Source License

/**
 * Get the current token index from the token stream.
 *
 * @param context LSServiceOperationContext
 * @return {@link Integer}      token index
 *//*from w  w w .  j a va  2  s  .c  om*/
public static int getCurrentTokenFromTokenStream(LSContext context) {
    TokenStream tokenStream = context.get(CompletionKeys.TOKEN_STREAM_KEY);
    Position position = context.get(DocumentServiceKeys.POSITION_KEY).getPosition();
    Token lastToken = null;
    int line = position.getLine();
    int col = position.getCharacter();
    int tokenLine;
    int tokenCol;
    int index = 0;

    if (tokenStream == null) {
        return -1;
    }

    while (true) {
        Token token = tokenStream.get(index);
        tokenLine = token.getLine() - 1;
        tokenCol = token.getCharPositionInLine();
        if (tokenLine > line || (tokenLine == line && tokenCol >= col)) {
            break;
        }
        index++;
        lastToken = token;
    }

    return lastToken == null ? -1 : lastToken.getTokenIndex();
}

From source file:org.ballerinalang.langserver.completions.BallerinaCustomErrorStrategy.java

License:Open Source License

/**
 * Checks whether cursor is within the whitespace region between current token to last token.
 * @param token Token to be evaluated/*from  ww  w  .j  ava 2 s. c o  m*/
 * @param parser Parser Instance
 * @return true|false
 */
private boolean isCursorBetweenGivenTokenAndLastNonHiddenToken(Token token, Parser parser) {
    this.setContextException(parser);
    boolean isCursorBetween = false;
    int line = this.context.get(DocumentServiceKeys.POSITION_KEY).getPosition().getLine();
    int character = this.context.get(DocumentServiceKeys.POSITION_KEY).getPosition().getCharacter();

    Token lastNonHiddenToken = null;
    for (int tokenIdx = token.getTokenIndex() - 1; tokenIdx >= 0; tokenIdx--) {
        Token lastToken = parser.getTokenStream().get(tokenIdx);
        if (lastToken.getChannel() != Token.HIDDEN_CHANNEL) {
            lastNonHiddenToken = lastToken;
            break;
        }
    }
    if (lastNonHiddenToken != null) {

        // Convert the token lines and char positions to zero based indexing
        int lastNonHiddenTokenLine = lastNonHiddenToken.getLine() - 1;
        int lastNonHiddenTokenChar = lastNonHiddenToken.getCharPositionInLine();
        int tokenLine = token.getLine() - 1;
        int tokenChar = token.getCharPositionInLine();

        if (line >= lastNonHiddenTokenLine && line <= tokenLine) {
            if (line == lastNonHiddenTokenLine) {
                isCursorBetween = character >= (lastNonHiddenTokenChar + lastNonHiddenToken.getText().length());
            } else {
                isCursorBetween = line != tokenLine || character <= tokenChar;
            }
        }
    }
    return isCursorBetween;
}

From source file:org.ballerinalang.langserver.completions.CompletionCustomErrorStrategy.java

License:Open Source License

private void deleteTokensUpToCursor(Parser recognizer, boolean isInLastTermination,
        boolean isInFirstTokenOfCursorLine) {
    Position cursorPosition = this.context.get(DocumentServiceKeys.POSITION_KEY).getPosition();
    int cursorLine = cursorPosition.getLine() + 1;
    int cursorCol = cursorPosition.getCharacter() + 1;

    int index = 1;
    Token beforeCursorToken = recognizer.getInputStream().LT(index);
    int type = beforeCursorToken.getType();
    int tLine = beforeCursorToken.getLine();
    int tCol = beforeCursorToken.getCharPositionInLine();

    boolean noTerminationToken = ((isInFirstTokenOfCursorLine && lastTerminationToken == null)
            || (firstTokenOfCursorLine != null
                    && lastTerminationToken.getTokenIndex() > firstTokenOfCursorLine.getTokenIndex()));
    int needToRemoveTokenCount = noTerminationToken ? 0 : -1;
    while (type != BallerinaParser.EOF && ((tLine < cursorLine) || (tLine == cursorLine && tCol < cursorCol))) {
        index++;/*from w  w w . ja  v a  2s .com*/
        needToRemoveTokenCount++;
        beforeCursorToken = recognizer.getInputStream().LT(index);
        type = beforeCursorToken.getType();
        tLine = beforeCursorToken.getLine();
        tCol = beforeCursorToken.getCharPositionInLine();
    }

    if (isInLastTermination) {
        removeTokenCount = needToRemoveTokenCount;
        removeStartToken = lastTerminationToken;
    } else if (noTerminationToken) {
        removeTokenCount = needToRemoveTokenCount;
        removeStartToken = recognizer.getInputStream().LT(1);
        removePendingTokensAfterThisToken(recognizer, removeStartToken, TokenRemovalStrategy.SYNC);
    }
}