List of usage examples for org.antlr.v4.runtime Token getCharPositionInLine
int getCharPositionInLine();
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 {// ww w . java2s . 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.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/*from w ww. j a v a 2 s. com*/ * @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());/* w w w.j a va 2 s . c om*/ 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.ja va 2 s . c o m*/ 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 w w w . j av a 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 www. j a va 2 s . co m*/ 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); } }
From source file:org.ballerinalang.langserver.completions.CompletionCustomErrorStrategy.java
License:Open Source License
private Token getFirstTokenOfCursorLine(Parser recognizer) { TokenStream tokenStream = recognizer.getInputStream(); Token firstCursorLineToken = null;/*from ww w.ja va 2s . c o m*/ int cursorLine = this.context.get(DocumentServiceKeys.POSITION_KEY).getPosition().getLine() + 1; int cursorCol = this.context.get(DocumentServiceKeys.POSITION_KEY).getPosition().getCharacter() + 1; int index = 1; Token beforeCursorToken = tokenStream.LT(index); int type = beforeCursorToken.getType(); int tLine = beforeCursorToken.getLine(); int tCol = beforeCursorToken.getCharPositionInLine(); if (cursorLine < tLine || (cursorLine == tLine && cursorCol <= tCol)) { return null; } firstCursorLineToken = (tLine == cursorLine) ? beforeCursorToken : null; while (type != BallerinaParser.EOF && (tLine <= cursorLine)) { int tokenIndex = beforeCursorToken.getTokenIndex(); if (tLine == cursorLine && (firstCursorLineToken == null || firstCursorLineToken.getTokenIndex() > tokenIndex)) { firstCursorLineToken = beforeCursorToken; } index++; beforeCursorToken = tokenStream.LT(index); type = beforeCursorToken.getType(); tLine = beforeCursorToken.getLine(); } return firstCursorLineToken; }