Exemplo n.º 1
0
 def createTryCatchFinally(self, tryBlock, catchBlocks, finallyBlock, lineno):
     hasFinally = finallyBlock is not None and ( (finallyBlock.getType() != Token.BLOCK) or finallyBlock.hasChildren())
     if (tryBlock.getType() == Token.BLOCK) and not tryBlock.hasChildren() and not hasFinally:
         return tryBlock
     hasCatch = catchBlocks.hasChildren()
     if not hasFinally and not hasCatch:
         return tryBlock
     handlerBlock = Node(Token.LOCAL_BLOCK)
     pn = Jump(Token.TRY, tryBlock, lineno)
     pn.putProp(Node.LOCAL_BLOCK_PROP, handlerBlock)
     if hasCatch:
         endCatch = Node.newTarget()
         pn.addChildToBack(self.makeJump(Token.GOTO, endCatch))
         catchTarget = Node.newTarget()
         pn.target = catchTarget
         pn.addChildToBack(catchTarget)
         catchScopeBlock = Node(Token.LOCAL_BLOCK)
         cb = catchBlocks.getFirstChild()
         hasDefault = False
         scopeIndex = 0
         while cb is not None:
             catchLineNo = cb.getLineno()
             name = cb.getFirstChild()
             cond = name.getNext()
             catchStatement = cond.getNext()
             cb.removeChild(name)
             cb.removeChild(cond)
             cb.removeChild(catchStatement)
             catchStatement.addChildToBack(Node(Token.LEAVEWITH))
             catchStatement.addChildToBack(self.makeJump(Token.GOTO, endCatch))
             condStmt = None#Node()
             if (cond.getType() == Token.EMPTY):
                 condStmt = catchStatement
                 hasDefault = True
             else:
                 condStmt = self.createIf(cond, catchStatement, None, catchLineNo)
             catchScope = Node(Token.CATCH_SCOPE, name, self.createUseLocal(handlerBlock))
             catchScope.putProp(Node.LOCAL_BLOCK_PROP, catchScopeBlock)
             catchScope.putIntProp(Node.CATCH_SCOPE_PROP, scopeIndex)
             catchScopeBlock.addChildToBack(catchScope)
             catchScopeBlock.addChildToBack(self.createWith(self.createUseLocal(catchScopeBlock), condStmt, catchLineNo))
             cb = cb.getNext()
             scopeIndex += 1
         pn.addChildToBack(catchScopeBlock)
         if not hasDefault:
             rethrow = Node(Token.RETHROW)
             rethrow.putProp(Node.LOCAL_BLOCK_PROP, handlerBlock)
             pn.addChildToBack(rethrow)
         pn.addChildToBack(endCatch)
     if hasFinally:
         finallyTarget = Node.newTarget()
         pn.setFinally(finallyTarget)
         pn.addChildToBack(self.makeJump(Token.JSR, finallyTarget))
         finallyEnd = Node.newTarget()
         pn.addChildToBack(self.makeJump(Token.GOTO, finallyEnd))
         pn.addChildToBack(finallyTarget)
         fBlock = Node(Token.FINALLY, finallyBlock)
         fBlock.putProp(Node.LOCAL_BLOCK_PROP, handlerBlock)
         pn.addChildToBack(fBlock)
         pn.addChildToBack(finallyEnd)
     handlerBlock.addChildToBack(pn)
     return handlerBlock