@Override public int getBeginColumn() { return token.getCharPositionInLine(); }
/** * Gets column number from ParseTree node. * @param tree * ParseTree node * @return column number */ private static int getColumn(ParseTree tree) { final int column; if (tree instanceof TerminalNode) { column = ((TerminalNode) tree).getSymbol().getCharPositionInLine(); } else { final ParserRuleContext rule = (ParserRuleContext) tree; column = rule.start.getCharPositionInLine(); } return column; }
@JsonProperty public int positionInLine() { return ctx.getStart().getCharPositionInLine(); }
public static NodeLocation getLocation(Token token) { requireNonNull(token, "token is null"); return new NodeLocation(token.getLine(), token.getCharPositionInLine()); }
private static Expression requireNonNull(Expression expression, Token token) { if (expression != null) { return expression; } else { final int line = token.getLine(); final int positionInLine = token.getCharPositionInLine(); final String msg = "Invalid expression (line: " + line + ", column: " + positionInLine + ")"; final SyntaxError syntaxError = new SyntaxError(token.getText(), line, positionInLine, msg, null); throw new ParseException(Collections.singleton(syntaxError)); } }
private static NodeLocation getLocation(final Token token) { requireNonNull(token, "token is null"); return new NodeLocation(token.getLine(), token.getCharPositionInLine()); } }
private static NodeLocation getLocation(final Token token) { requireNonNull(token, "token is null"); return new NodeLocation(token.getLine(), token.getCharPositionInLine()); }
@Override public int getEndColumn() { return token.getCharPositionInLine() + token.getStopIndex() - token.getStartIndex(); }
protected List<Comment> getCommentOnChannel(List<Token> refChannel) { List<Comment> comments = new ArrayList<>(); for (Token refTok : refChannel) { String text = refTok.getText(); // we strip the leading hash # character but we don't trim because we don't // know the "comment markup". Maybe its space sensitive, maybe its not. So // consumers can decide that if (text == null) { continue; } text = text.replaceFirst("^#", ""); comments.add(new Comment(text, new SourceLocation(refTok.getLine(), refTok.getCharPositionInLine()))); } return comments; }
protected SourceLocation getSourceLocation(Token token) { String sourceName = token.getTokenSource().getSourceName(); if (IntStream.UNKNOWN_SOURCE_NAME.equals(sourceName)) { // UNKNOWN_SOURCE_NAME is Antrl's way of indicating that no source name was given during parsing -- // which is the case when queries and other operations are parsed. We don't want this hardcoded // '<unknown>' sourceName to leak to clients when the response is serialized as JSON, so we null it. sourceName = null; } return new SourceLocation(token.getLine(), token.getCharPositionInLine() + 1, sourceName); }
@Override public void exitBackQuotedIdentifier(SqlBaseParser.BackQuotedIdentifierContext context) { Token token = context.BACKQUOTED_IDENTIFIER().getSymbol(); throw new ParsingException( "backquoted identifiers are not supported; use double quotes to quote identifiers", null, token.getLine(), token.getCharPositionInLine()); }
private static ParsingException parseError(String message, ParserRuleContext context) { return new ParsingException(message, null, context.getStart().getLine(), context.getStart().getCharPositionInLine()); } }
@Override public void exitDigitIdentifier(SqlBaseParser.DigitIdentifierContext context) { Token token = context.DIGIT_IDENTIFIER().getSymbol(); throw new ParsingException( "identifiers must not start with a digit; surround the identifier with double quotes", null, token.getLine(), token.getCharPositionInLine()); }
private boolean checkLeftSpaces(Token left, Token op, int numSpaces) { return op.getLine() == left.getLine() && op.getCharPositionInLine() - ListenerUtil.getLastCharPositionInLine(left) != numSpaces + 1; } }
private void printOnOffMismatchWarning(Token comment) { printer.warn(Messages.ON_OFF_MISMATCH, new Location(comment.getLine(), comment.getCharPositionInLine() + 1)); } }
InvalidSyntaxException mkMoreTokensException(Token token) { SourceLocation sourceLocation = new SourceLocation(token.getLine(), token.getCharPositionInLine()); String sourcePreview = mkPreview(token.getLine()); return new InvalidSyntaxException(sourceLocation, "There are more tokens in the query that have not been consumed", sourcePreview, token.getText(), null); }
private InvalidSyntaxException mkException(Parser recognizer, RecognitionException cause) { String sourcePreview = null; String offendingToken = null; SourceLocation sourceLocation = null; Token currentToken = recognizer.getCurrentToken(); if (currentToken != null) { int line = currentToken.getLine(); int column = currentToken.getCharPositionInLine(); offendingToken = currentToken.getText(); sourcePreview = mkPreview(line); sourceLocation = new SourceLocation(line, column, sourceName); } return new InvalidSyntaxException(sourceLocation, null, sourcePreview, offendingToken, cause); }
private static void check(final boolean condition, final String message, final ParserRuleContext context) { if (!condition) { throw new ParsingException( message, null, context.getStart().getLine(), context.getStart().getCharPositionInLine() ); } }
@Nullable default Object evaluate(EvaluationContext context) { try { return evaluateUnsafe(context); } catch (FunctionEvaluationException fee) { context.addEvaluationError(fee.getStartToken().getLine(), fee.getStartToken().getCharPositionInLine(), fee.getFunctionExpression().getFunction().descriptor(), getRootCause(fee)); } catch (Exception e) { context.addEvaluationError(getStartToken().getLine(), getStartToken().getCharPositionInLine(), null, getRootCause(e)); } return null; }
@Override public void exitUnquotedIdentifier(SqlBaseParser.UnquotedIdentifierContext context) { String identifier = context.IDENTIFIER().getText(); for (IdentifierSymbol identifierSymbol : EnumSet.complementOf(allowedIdentifierSymbols)) { char symbol = identifierSymbol.getSymbol(); if (identifier.indexOf(symbol) >= 0) { throw new ParsingException("identifiers must not contain '" + identifierSymbol.getSymbol() + "'", null, context.IDENTIFIER().getSymbol().getLine(), context.IDENTIFIER().getSymbol().getCharPositionInLine()); } } }