@Override public int getEndLine() { return token.getLine(); }
@Override public int getBeginLine() { return token.getLine(); }
/** * Gets line number from ParseTree node. * @param tree * ParseTree node * @return line number */ private static int getLine(ParseTree tree) { final int line; if (tree instanceof TerminalNode) { line = ((TerminalNode) tree).getSymbol().getLine() - 1; } else { final ParserRuleContext rule = (ParserRuleContext) tree; line = rule.start.getLine() - 1; } return line; }
@JsonProperty public int line() { return ctx.getStart().getLine(); }
/** * Error message */ public void error(ParserRuleContext ctx, String message) { if (ctx != null) { System.err.println("Ln:" + ctx.getStart().getLine() + " " + message); } else { System.err.println(message); } }
public static NodeLocation getLocation(Token token) { requireNonNull(token, "token is null"); return new NodeLocation(token.getLine(), token.getCharPositionInLine()); }
/** * Trace information */ public void trace(ParserRuleContext ctx, String message) { if (!trace) { return; } if (ctx != null) { System.out.println("Ln:" + ctx.getStart().getLine() + " " + message); } else { System.out.println(message); } }
/** * Informational messages */ public void info(ParserRuleContext ctx, String message) { if (!info) { return; } if (ctx != null) { System.err.println("Ln:" + ctx.getStart().getLine() + " " + message); } else { System.err.println(message); } }
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)); } }
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 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()); }
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()); }
@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 static ParsingException parseError(String message, ParserRuleContext context) { return new ParsingException(message, null, context.getStart().getLine(), context.getStart().getCharPositionInLine()); } }
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()); } } }