private void print(IfStatement node) throws IOException { writer.append("if").ws().append('('); print(node.getCondition()); writer.append(')').ws(); print(node.getThenPart()); if (node.getElsePart() != null) { writer.ws().append("else "); print(node.getElsePart()); } }
private Node transformIf(IfStatement n) { decompiler.addToken(Token.IF); decompiler.addToken(Token.LP); Node cond = transform(n.getCondition()); decompiler.addToken(Token.RP); decompiler.addEOL(Token.LC); Node ifTrue = transform(n.getThenPart()); Node ifFalse = null; if (n.getElsePart() != null) { decompiler.addToken(Token.RC); decompiler.addToken(Token.ELSE); decompiler.addEOL(Token.LC); ifFalse = transform(n.getElsePart()); } decompiler.addEOL(Token.RC); return createIf(cond, ifTrue, ifFalse, n.getLineno()); }
private IfStatement ifStatement() throws IOException { if (currentToken != Token.IF) codeBug(); consumeToken(); int pos = ts.tokenBeg, lineno = ts.lineno, elsePos = -1; ConditionData data = condition(); AstNode ifTrue = statement(), ifFalse = null; if (matchToken(Token.ELSE)) { elsePos = ts.tokenBeg - pos; ifFalse = statement(); } int end = getNodeEnd(ifFalse != null ? ifFalse : ifTrue); IfStatement pn = new IfStatement(pos, end - pos); pn.setCondition(data.condition); pn.setParens(data.lp - pos, data.rp - pos); pn.setThenPart(ifTrue); pn.setElsePart(ifFalse); pn.setElsePosition(elsePos); pn.setLineno(lineno); return pn; }
/** * Extract variables from if/else node(s) */ private void processIfThenElse(Node child, CodeBlock block, Set<Completion> set, String entered, int offset) { IfStatement ifStatement = (IfStatement) child; if (canProcessNode(ifStatement)) { offset = ifStatement.getAbsolutePosition() + ifStatement.getLength(); addCodeBlock(ifStatement.getThenPart(), set, entered, block, offset); AstNode elseNode = ifStatement.getElsePart(); if (elseNode != null) { int start = elseNode.getAbsolutePosition(); CodeBlock childBlock = block.addChildCodeBlock(start); offset = start + elseNode.getLength(); iterateNode(elseNode, set, entered, childBlock, offset); childBlock.setEndOffset(offset); } } }
public boolean hasElse() { return node.getElsePart() != null; }
/** * Sets statement to execute if condition is true * @throws IllegalArgumentException if thenPart is {@code null} */ public void setThenPart(AstNode thenPart) { assertNotNull(thenPart); this.thenPart = thenPart; thenPart.setParent(this); }
@Override public String toSource(int depth) { String pad = makeIndent(depth); StringBuilder sb = new StringBuilder(32); sb.append(pad); sb.append("if ("); sb.append(condition.toSource(0)); sb.append(") "); if (thenPart.getType() != Token.BLOCK) { sb.append("\n").append(makeIndent(depth + 1)); } sb.append(thenPart.toSource(depth).trim()); if (elsePart != null) { if (thenPart.getType() != Token.BLOCK) { sb.append("\n").append(pad).append("else "); } else { sb.append(" else "); } if (elsePart.getType() != Token.BLOCK && elsePart.getType() != Token.IF) { sb.append("\n").append(makeIndent(depth + 1)); } sb.append(elsePart.toSource(depth).trim()); } sb.append("\n"); return sb.toString(); }
private Node transformIf(IfStatement n) { decompiler.addToken(Token.IF); decompiler.addToken(Token.LP); Node cond = transform(n.getCondition()); decompiler.addToken(Token.RP); decompiler.addEOL(Token.LC); Node ifTrue = transform(n.getThenPart()); Node ifFalse = null; if (n.getElsePart() != null) { decompiler.addToken(Token.RC); decompiler.addToken(Token.ELSE); decompiler.addEOL(Token.LC); ifFalse = transform(n.getElsePart()); } decompiler.addEOL(Token.RC); return createIf(cond, ifTrue, ifFalse, n.getLineno()); }
private IfStatement ifStatement() throws IOException { if (currentToken != Token.IF) codeBug(); consumeToken(); int pos = ts.tokenBeg, lineno = ts.lineno, elsePos = -1; ConditionData data = condition(); AstNode ifTrue = statement(), ifFalse = null; if (matchToken(Token.ELSE)) { elsePos = ts.tokenBeg - pos; ifFalse = statement(); } int end = getNodeEnd(ifFalse != null ? ifFalse : ifTrue); IfStatement pn = new IfStatement(pos, end - pos); pn.setCondition(data.condition); pn.setParens(data.lp - pos, data.rp - pos); pn.setThenPart(ifTrue); pn.setElsePart(ifFalse); pn.setElsePosition(elsePos); pn.setLineno(lineno); return pn; }
/** * Sets if condition. * @throws IllegalArgumentException if {@code condition} is {@code null}. */ public void setCondition(AstNode condition) { assertNotNull(condition); this.condition = condition; condition.setParent(this); }
@Override public String toSource(int depth) { String pad = makeIndent(depth); StringBuilder sb = new StringBuilder(32); sb.append(pad); sb.append("if ("); sb.append(condition.toSource(0)); sb.append(") "); if (thenPart.getType() != Token.BLOCK) { sb.append("\n").append(makeIndent(depth + 1)); } sb.append(thenPart.toSource(depth).trim()); if (elsePart != null) { if (thenPart.getType() != Token.BLOCK) { sb.append("\n").append(pad).append("else "); } else { sb.append(" else "); } if (elsePart.getType() != Token.BLOCK && elsePart.getType() != Token.IF) { sb.append("\n").append(makeIndent(depth + 1)); } sb.append(elsePart.toSource(depth).trim()); } sb.append("\n"); return sb.toString(); }
private Node transformIf(IfStatement n) { decompiler.addToken(Token.IF); decompiler.addToken(Token.LP); Node cond = transform(n.getCondition()); decompiler.addToken(Token.RP); decompiler.addEOL(Token.LC); Node ifTrue = transform(n.getThenPart()); Node ifFalse = null; if (n.getElsePart() != null) { decompiler.addToken(Token.RC); decompiler.addToken(Token.ELSE); decompiler.addEOL(Token.LC); ifFalse = transform(n.getElsePart()); } decompiler.addEOL(Token.RC); return createIf(cond, ifTrue, ifFalse, n.getLineno()); }
private IfStatement ifStatement() throws IOException { if (currentToken != Token.IF) codeBug(); consumeToken(); int pos = ts.tokenBeg, lineno = ts.lineno, elsePos = -1; ConditionData data = condition(); AstNode ifTrue = statement(), ifFalse = null; if (matchToken(Token.ELSE)) { elsePos = ts.tokenBeg - pos; ifFalse = statement(); } int end = getNodeEnd(ifFalse != null ? ifFalse : ifTrue); IfStatement pn = new IfStatement(pos, end - pos); pn.setCondition(data.condition); pn.setParens(data.lp - pos, data.rp - pos); pn.setThenPart(ifTrue); pn.setElsePart(ifFalse); pn.setElsePosition(elsePos); pn.setLineno(lineno); return pn; }
@Override public CAstNode visitIfStatement(IfStatement node, WalkContext arg) { if (node.getElsePart() != null) { return Ast.makeNode(CAstNode.IF_STMT, visit(node.getCondition(), arg), visit(node.getThenPart(), arg), visit(node.getElsePart(), arg)); } else { return Ast.makeNode(CAstNode.IF_STMT, visit(node.getCondition(), arg), visit(node.getThenPart(), arg)); } }
/** * Sets statement to execute if condition is true * @throws IllegalArgumentException if thenPart is {@code null} */ public void setThenPart(AstNode thenPart) { assertNotNull(thenPart); this.thenPart = thenPart; thenPart.setParent(this); }
@Override public String toSource(int depth) { String pad = makeIndent(depth); StringBuilder sb = new StringBuilder(32); sb.append(pad); sb.append("if ("); sb.append(condition.toSource(0)); sb.append(") "); if (thenPart.getType() != Token.BLOCK) { sb.append("\n").append(makeIndent(depth + 1)); } sb.append(thenPart.toSource(depth).trim()); if (elsePart != null) { if (thenPart.getType() != Token.BLOCK) { sb.append("\n").append(pad).append("else "); } else { sb.append(" else "); } if (elsePart.getType() != Token.BLOCK && elsePart.getType() != Token.IF) { sb.append("\n").append(makeIndent(depth + 1)); } sb.append(elsePart.toSource(depth).trim()); } sb.append("\n"); return sb.toString(); }
private Node transformIf(IfStatement n) { decompiler.addToken(Token.IF); decompiler.addToken(Token.LP); Node cond = transform(n.getCondition()); decompiler.addToken(Token.RP); decompiler.addEOL(Token.LC); Node ifTrue = transform(n.getThenPart()); Node ifFalse = null; if (n.getElsePart() != null) { decompiler.addToken(Token.RC); decompiler.addToken(Token.ELSE); decompiler.addEOL(Token.LC); ifFalse = transform(n.getElsePart()); } decompiler.addEOL(Token.RC); return createIf(cond, ifTrue, ifFalse, n.getLineno()); }
private IfStatement ifStatement() throws IOException { if (currentToken != Token.IF) codeBug(); consumeToken(); int pos = ts.tokenBeg, lineno = ts.lineno, elsePos = -1; ConditionData data = condition(); AstNode ifTrue = statement(), ifFalse = null; if (matchToken(Token.ELSE)) { elsePos = ts.tokenBeg - pos; ifFalse = statement(); } int end = getNodeEnd(ifFalse != null ? ifFalse : ifTrue); IfStatement pn = new IfStatement(pos, end - pos); pn.setCondition(data.condition); pn.setParens(data.lp - pos, data.rp - pos); pn.setThenPart(ifTrue); pn.setElsePart(ifFalse); pn.setElsePosition(elsePos); pn.setLineno(lineno); return pn; }
private void print(IfStatement node) throws IOException { writer.append("if").ws().append('('); print(node.getCondition()); writer.append(')').ws(); print(node.getThenPart()); if (node.getElsePart() != null) { writer.ws().append("else "); print(node.getElsePart()); } }
/** * Sets if condition. * @throws IllegalArgumentException if {@code condition} is {@code null}. */ public void setCondition(AstNode condition) { assertNotNull(condition); this.condition = condition; condition.setParent(this); }