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); } } }
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()); } }
/** * This method generates constraints for all relevant AstNodes. It delegates its work to various * processXXX() methods that handle AstNodes of type XXX. */ @Override public boolean visit(AstNode node) { if (node instanceof VariableInitializer){ processVariableInitializer(node); } else if (node instanceof ReturnStatement){ processReturnStatement((ReturnStatement)node); } else if (node instanceof ExpressionStatement){ processExpressionStatement((ExpressionStatement)node); } else if (node instanceof ForLoop){ processForLoop((ForLoop)node); } else if (node instanceof ForInLoop){ processForInLoop((ForInLoop)node); }else if (node instanceof WhileLoop){ processWhileLoop((WhileLoop)node); } else if (node instanceof DoLoop){ processDoLoop((DoLoop)node); } else if (node instanceof NewExpression){ processNewExpression((NewExpression)node); } else if (node instanceof FunctionCall){ processFunctionCall((FunctionCall)node); } else if (node instanceof ElementGet){ processElementGet((ElementGet)node); } else if (node instanceof FunctionNode){ processFunctionNode((FunctionNode)node); } else if (node instanceof IfStatement){ processIfStatement((IfStatement)node); } else if (node instanceof KeywordLiteral){ processKeywordLiteral((KeywordLiteral)node); } else if (node instanceof SwitchStatement){ processSwitchStatement((SwitchStatement)node); } else if (node instanceof SwitchCase){ processSwitchCase((SwitchCase)node); } else if ((node instanceof AstRoot) || //AstRoot: no constraints need to be generated (node instanceof BreakStatement) || //BreakStatement: no constraints need to be generated (node instanceof VariableDeclaration) || //VariableDeclaration: we generate constraints for its constituent VariableInitializer nodes (node instanceof Name) || //Name: generate constraints for complex expressions that refer to names (node instanceof NumberLiteral) || //NumberLiteral: generate constraints for complex expressions that refer to names (node instanceof StringLiteral) || //StringLiteral: generate constraints for complex expressions that refer to names (node instanceof Assignment) || // Assignment is a special case of InfixExpression (node instanceof ArrayLiteral) || (node instanceof UnaryExpression) || (node instanceof InfixExpression) || (node instanceof ConditionalExpression) || (node instanceof ParenthesizedExpression) || (node instanceof EmptyExpression) || (node instanceof ObjectLiteral) || (node instanceof EmptyStatement) || (node instanceof ContinueStatement) || (node instanceof Scope) || (node instanceof Block)){ // // occurs in programs with for loops -- nothing to be done here? /* nothing */ } else { error("unsupported node " + node.toSource().trim() + " of type: " + node.getClass().getName(), node); } return true; }
/** * generate constraints for an if-statement */ private void processIfStatement(IfStatement ifs) { AstNode condition = ifs.getCondition(); processExpression(condition); }