Skip to content

Commit

Permalink
Merge pull request elastic#18298 from jdconrad/line
Browse files Browse the repository at this point in the history
Make Line Number Available in Painless
  • Loading branch information
jdconrad committed May 12, 2016
2 parents 0830bd4 + 9b2b474 commit ec4825d
Show file tree
Hide file tree
Showing 48 changed files with 170 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ private SourceContext buildAntlrTree(final String source) {
return parser.source();
}

private int line(final ParserRuleContext ctx) {
return ctx.getStart().getLine();
}

private String location(final ParserRuleContext ctx) {
return "[ " + ctx.getStart().getLine() + " : " + ctx.getStart().getCharPositionInLine() + " ]";
}
Expand All @@ -158,7 +162,7 @@ public ANode visitSource(final SourceContext ctx) {
statements.add((AStatement)visit(statement));
}

return new SSource(location(ctx), statements);
return new SSource(line(ctx), location(ctx), statements);
}

@Override
Expand All @@ -167,7 +171,7 @@ public ANode visitIf(final IfContext ctx) {
final AStatement ifblock = (AStatement)visit(ctx.block(0));
final AStatement elseblock = ctx.block(1) == null ? null : (AStatement)visit(ctx.block(1));

return new SIfElse(location(ctx), condition, ifblock, elseblock);
return new SIfElse(line(ctx), location(ctx), condition, ifblock, elseblock);
}

@Override
Expand All @@ -177,7 +181,7 @@ public ANode visitWhile(final WhileContext ctx) {

reserved.usesLoop();

return new SWhile(location(ctx), condition, block);
return new SWhile(line(ctx), location(ctx), condition, block);
}

@Override
Expand All @@ -187,7 +191,7 @@ public ANode visitDo(final DoContext ctx) {

reserved.usesLoop();

return new SDo(location(ctx), block, condition);
return new SDo(line(ctx), location(ctx), block, condition);
}

@Override
Expand All @@ -199,7 +203,7 @@ public ANode visitFor(final ForContext ctx) {

reserved.usesLoop();

return new SFor(location(ctx), intializer, condition, afterthought, block);
return new SFor(line(ctx), location(ctx), intializer, condition, afterthought, block);
}

@Override
Expand All @@ -209,19 +213,19 @@ public ANode visitDecl(final DeclContext ctx) {

@Override
public ANode visitContinue(final ContinueContext ctx) {
return new SContinue(location(ctx));
return new SContinue(line(ctx), location(ctx));
}

@Override
public ANode visitBreak(final BreakContext ctx) {
return new SBreak(location(ctx));
return new SBreak(line(ctx), location(ctx));
}

@Override
public ANode visitReturn(final ReturnContext ctx) {
final AExpression expression = (AExpression)visit(ctx.expression());

return new SReturn(location(ctx), expression);
return new SReturn(line(ctx), location(ctx), expression);
}

@Override
Expand All @@ -233,21 +237,21 @@ public ANode visitTry(final TryContext ctx) {
traps.add((STrap)visit(trap));
}

return new STry(location(ctx), block, traps);
return new STry(line(ctx), location(ctx), block, traps);
}

@Override
public ANode visitThrow(final ThrowContext ctx) {
final AExpression expression = (AExpression)visit(ctx.expression());

return new SThrow(location(ctx), expression);
return new SThrow(line(ctx), location(ctx), expression);
}

@Override
public ANode visitExpr(final ExprContext ctx) {
final AExpression expression = (AExpression)visit(ctx.expression());

return new SExpression(location(ctx), expression);
return new SExpression(line(ctx), location(ctx), expression);
}

@Override
Expand All @@ -258,15 +262,15 @@ public ANode visitMultiple(final MultipleContext ctx) {
statements.add((AStatement)visit(statement));
}

return new SBlock(location(ctx), statements);
return new SBlock(line(ctx), location(ctx), statements);
}

@Override
public ANode visitSingle(final SingleContext ctx) {
final List<AStatement> statements = new ArrayList<>();
statements.add((AStatement)visit(ctx.statement()));

return new SBlock(location(ctx), statements);
return new SBlock(line(ctx), location(ctx), statements);
}

@Override
Expand Down Expand Up @@ -303,10 +307,10 @@ public ANode visitDeclaration(final DeclarationContext ctx) {
for (final DeclvarContext declvar : ctx.declvar()) {
final String name = declvar.identifier().getText();
final AExpression expression = declvar.expression() == null ? null : (AExpression)visit(declvar.expression());
declarations.add(new SDeclaration(location(ctx), type, name, expression));
declarations.add(new SDeclaration(line(ctx), location(ctx), type, name, expression));
}

return new SDeclBlock(location(ctx), declarations);
return new SDeclBlock(line(ctx), location(ctx), declarations);
}

@Override
Expand All @@ -325,7 +329,7 @@ public ANode visitTrap(final TrapContext ctx) {
final String name = ctx.identifier(1).getText();
final AStatement block = ctx.block() == null ? null : (AStatement)visit(ctx.block());

return new STrap(location(ctx), type, name, block);
return new STrap(line(ctx), location(ctx), type, name, block);
}

@Override
Expand All @@ -348,31 +352,31 @@ public ANode visitNumeric(final NumericContext ctx) {
final boolean negate = ctx.parent instanceof UnaryContext && ((UnaryContext)ctx.parent).SUB() != null;

if (ctx.DECIMAL() != null) {
return new EDecimal(location(ctx), (negate ? "-" : "") + ctx.DECIMAL().getText());
return new EDecimal(line(ctx), location(ctx), (negate ? "-" : "") + ctx.DECIMAL().getText());
} else if (ctx.HEX() != null) {
return new ENumeric(location(ctx), (negate ? "-" : "") + ctx.HEX().getText().substring(2), 16);
return new ENumeric(line(ctx), location(ctx), (negate ? "-" : "") + ctx.HEX().getText().substring(2), 16);
} else if (ctx.INTEGER() != null) {
return new ENumeric(location(ctx), (negate ? "-" : "") + ctx.INTEGER().getText(), 10);
return new ENumeric(line(ctx), location(ctx), (negate ? "-" : "") + ctx.INTEGER().getText(), 10);
} else if (ctx.OCTAL() != null) {
return new ENumeric(location(ctx), (negate ? "-" : "") + ctx.OCTAL().getText().substring(1), 8);
return new ENumeric(line(ctx), location(ctx), (negate ? "-" : "") + ctx.OCTAL().getText().substring(1), 8);
} else {
throw new IllegalStateException("Error " + location(ctx) + ": Unexpected state.");
}
}

@Override
public ANode visitTrue(final TrueContext ctx) {
return new EBoolean(location(ctx), true);
return new EBoolean(line(ctx), location(ctx), true);
}

@Override
public ANode visitFalse(FalseContext ctx) {
return new EBoolean(location(ctx), false);
return new EBoolean(line(ctx), location(ctx), false);
}

@Override
public ANode visitNull(final NullContext ctx) {
return new ENull(location(ctx));
return new ENull(line(ctx), location(ctx));
}

@Override
Expand All @@ -390,7 +394,7 @@ public ANode visitPostinc(final PostincContext ctx) {
throw new IllegalStateException("Error " + location(ctx) + ": Unexpected state.");
}

return new EChain(location(ctx), links, false, true, operation, null);
return new EChain(line(ctx), location(ctx), links, false, true, operation, null);
}

@Override
Expand All @@ -408,7 +412,7 @@ public ANode visitPreinc(final PreincContext ctx) {
throw new IllegalStateException("Error " + location(ctx) + ": Unexpected state.");
}

return new EChain(location(ctx), links, true, false, operation, null);
return new EChain(line(ctx), location(ctx), links, true, false, operation, null);
}

@Override
Expand All @@ -417,7 +421,7 @@ public ANode visitRead(final ReadContext ctx) {

visitChain(ctx.chain(), links);

return new EChain(location(ctx), links, false, false, null, null);
return new EChain(line(ctx), location(ctx), links, false, false, null, null);
}

@Override
Expand All @@ -439,13 +443,13 @@ public ANode visitUnary(final UnaryContext ctx) {
throw new IllegalStateException("Error " + location(ctx) + ": Unexpected state.");
}

return new EUnary(location(ctx), operation, (AExpression)visit(ctx.expression()));
return new EUnary(line(ctx), location(ctx), operation, (AExpression)visit(ctx.expression()));
}
}

@Override
public ANode visitCast(final CastContext ctx) {
return new EExplicit(location(ctx), ctx.decltype().getText(), (AExpression)visit(ctx.expression()));
return new EExplicit(line(ctx), location(ctx), ctx.decltype().getText(), (AExpression)visit(ctx.expression()));
}

@Override
Expand Down Expand Up @@ -480,7 +484,7 @@ public ANode visitBinary(final BinaryContext ctx) {
throw new IllegalStateException("Error " + location(ctx) + ": Unexpected state.");
}

return new EBinary(location(ctx), operation, left, right);
return new EBinary(line(ctx), location(ctx), operation, left, right);
}

@Override
Expand Down Expand Up @@ -509,7 +513,7 @@ public ANode visitComp(PainlessParser.CompContext ctx) {
throw new IllegalStateException("Error " + location(ctx) + ": Unexpected state.");
}

return new EComp(location(ctx), operation, left, right);
return new EComp(line(ctx), location(ctx), operation, left, right);
}

@Override
Expand All @@ -526,7 +530,7 @@ public ANode visitBool(PainlessParser.BoolContext ctx) {
throw new IllegalStateException("Error " + location(ctx) + ": Unexpected state.");
}

return new EBool(location(ctx), operation, left, right);
return new EBool(line(ctx), location(ctx), operation, left, right);
}


Expand All @@ -536,7 +540,7 @@ public ANode visitConditional(final ConditionalContext ctx) {
final AExpression left = (AExpression)visit(ctx.expression(1));
final AExpression right = (AExpression)visit(ctx.expression(2));

return new EConditional(location(ctx), condition, left, right);
return new EConditional(line(ctx), location(ctx), condition, left, right);
}

@Override
Expand Down Expand Up @@ -572,7 +576,7 @@ public ANode visitAssignment(final AssignmentContext ctx) {
operation = null;
}

return new EChain(location(ctx), links, false, false, operation, (AExpression)visit(ctx.expression()));
return new EChain(line(ctx), location(ctx), links, false, false, operation, (AExpression)visit(ctx.expression()));
}

private void visitChain(final ChainContext ctx, final List<ALink> links) {
Expand Down Expand Up @@ -638,7 +642,7 @@ private void visitLinkcast(final LinkcastContext ctx, final List<ALink> links) {
throw new IllegalStateException("Error " + location(ctx) + ": Unexpected state.");
}

links.add(new LCast(location(ctx), ctx.decltype().getText()));
links.add(new LCast(line(ctx), location(ctx), ctx.decltype().getText()));
}

@Override
Expand All @@ -647,7 +651,7 @@ public ANode visitLinkcast(final LinkcastContext ctx) {
}

private void visitLinkbrace(final LinkbraceContext ctx, final List<ALink> links) {
links.add(new LBrace(location(ctx), (AExpression)visit(ctx.expression())));
links.add(new LBrace(line(ctx), location(ctx), (AExpression)visit(ctx.expression())));

if (ctx.linkbrace() != null) {
visitLinkbrace(ctx.linkbrace(), links);
Expand Down Expand Up @@ -681,7 +685,7 @@ private void visitLinkcall(final LinkcallContext ctx, final List<ALink> links) {
arguments.add((AExpression)visit(expression));
}

links.add(new LCall(location(ctx), ctx.EXTID().getText(), arguments));
links.add(new LCall(line(ctx), location(ctx), ctx.EXTID().getText(), arguments));

if (ctx.linkbrace() != null) {
visitLinkbrace(ctx.linkbrace(), links);
Expand All @@ -700,7 +704,7 @@ private void visitLinkvar(final LinkvarContext ctx, final List<ALink> links) {

reserved.markReserved(name);

links.add(new LVariable(location(ctx), name));
links.add(new LVariable(line(ctx), location(ctx), name));

if (ctx.linkbrace() != null) {
visitLinkbrace(ctx.linkbrace(), links);
Expand All @@ -725,7 +729,7 @@ private void visitLinkfield(final LinkfieldContext ctx, final List<ALink> links)
throw new IllegalStateException("Error " + location(ctx) + ": Unexpected state.");
}

links.add(new LField(location(ctx), value));
links.add(new LField(line(ctx), location(ctx), value));

if (ctx.linkbrace() != null) {
visitLinkbrace(ctx.linkbrace(), links);
Expand All @@ -747,13 +751,13 @@ private void visitLinknew(final LinknewContext ctx, final List<ALink> links) {
arguments.add((AExpression)visit(expression));
}

links.add(new LNewObj(location(ctx), ctx.identifier().getText(), arguments));
links.add(new LNewObj(line(ctx), location(ctx), ctx.identifier().getText(), arguments));
} else if (ctx.expression().size() > 0) {
for (final ExpressionContext expression : ctx.expression()) {
arguments.add((AExpression)visit(expression));
}

links.add(new LNewArray(location(ctx), ctx.identifier().getText(), arguments));
links.add(new LNewArray(line(ctx), location(ctx), ctx.identifier().getText(), arguments));
} else {
throw new IllegalStateException("Error " + location(ctx) + ": Unexpected state.");
}
Expand All @@ -769,7 +773,7 @@ public ANode visitLinknew(final LinknewContext ctx) {
}

private void visitLinkstring(final LinkstringContext ctx, final List<ALink> links) {
links.add(new LString(location(ctx), ctx.STRING().getText().substring(1, ctx.STRING().getText().length() - 1)));
links.add(new LString(line(ctx), location(ctx), ctx.STRING().getText().substring(1, ctx.STRING().getText().length() - 1)));

if (ctx.linkbrace() != null) {
visitLinkbrace(ctx.linkbrace(), links);
Expand Down
Loading

0 comments on commit ec4825d

Please sign in to comment.