Esempio n. 1
0
    def genCode(self, indent, codeout, tagreg, node, meta):
        iftag = node.children[0]
        DTCompilerUtil.tagDebug(indent, codeout, iftag)
        args = DTUtil.tagCall(iftag, ['expr'])
        args = DTCompilerUtil.pyifyArgs(iftag, args)

        codeout.write(indent, 'if (%s):' % args['expr'])
        for i in node.children[1:-1]:
            #if not an else or elif tag and on is true, render the child
            if (type(i) != InstanceType or i.__class__ != DTLexer.DTToken
                    or i.tagname not in ('else', 'elif')):
                DTCompilerUtil.genCodeChild(indent + 4, codeout, tagreg, i,
                                            meta)
            else:
                if i.tagname == 'elif':
                    #
                    # XXX has to move to the elif tag genCode()!
                    #
                    args = DTUtil.tagCall(i, ['expr'])
                    args = DTCompilerUtil.pyifyArgs(i, args)

                    codeout.write(indent, 'elif (%s):' % (args['expr']))
                    # XXX kind of sucks to do this after the elif statement
                    DTCompilerUtil.tagDebug(indent + 4, codeout, i)
                    codeout.write(indent + 4, 'pass')
                else:  #tagname is else
                    codeout.write(indent, 'else:')
                    codeout.write(indent + 4, 'pass')
Esempio n. 2
0
    def genCode(self, indent, codeout, tagreg, node, meta):
        tag = node.children[0]
        DTCompilerUtil.tagDebug(indent, codeout, tag)
        tagargs = ['name']
        if self._attend_to_comments:
            tagargs.append(('comments', None))
        pargs = args = DTUtil.tagCall(tag, tagargs)
        args = DTCompilerUtil.pyifyArgs(tag, args)
        if self._attend_to_comments and args['comments'] is not None:
            self._testCommentLevel(indent, codeout, args['comments'])
            stuff = self._pushCommentLevel(indent, codeout, args['comments'])

        name = DTCompilerUtil.checkName(tag, 'name', args['name'],
                                        pargs['name'])

        oldout = DTCompilerUtil.getTempName()
        codeout.write(indent, '%s = __h.OUTPUT' % oldout)
        codeout.write(indent, '__h.OUTPUT=__h.NEWOUTPUT()')
        for i in node.children[1:-1]:
            DTCompilerUtil.genCodeChild(indent, codeout, tagreg, i, meta)
        codeout.write(indent, '%s = __h.OUTPUT.getvalue()' % name)
        codeout.write(indent, '__h.OUTPUT = %s' % oldout)
        codeout.write(indent, 'del %s' % oldout)
        if self._attend_to_comments and args['comments'] is not None:
            self._popCommentLevel(indent, codeout, *stuff)
Esempio n. 3
0
    def genCode ( self, indent, codeout, tagreg, node, meta):
        tag = node.children[0]
        DTCompilerUtil.tagDebug(indent, codeout, tag)
        tagargs=['name']
        if self._attend_to_comments:
            tagargs.append(('comments', None))
        pargs = args = DTUtil.tagCall(tag, tagargs)
        args = DTCompilerUtil.pyifyArgs(tag, args)
        if self._attend_to_comments and args['comments'] is not None:
            self._testCommentLevel(indent, codeout, args['comments'])
            stuff=self._pushCommentLevel(indent, codeout, args['comments'])

        name = DTCompilerUtil.checkName(tag, 'name', args['name'],
                                        pargs['name'])
        
        oldout = DTCompilerUtil.getTempName()
        codeout.write(indent, '%s = __h.OUTPUT' % oldout)
        codeout.write(indent, '__h.OUTPUT=__h.NEWOUTPUT()')
        for i in node.children[1:-1]:
            DTCompilerUtil.genCodeChild(indent, codeout, tagreg, i, meta)
        codeout.write(indent, '%s = __h.OUTPUT.getvalue()' % name)
        codeout.write(indent, '__h.OUTPUT = %s' % oldout)
        codeout.write(indent, 'del %s' % oldout)
        if self._attend_to_comments and args['comments'] is not None:
            self._popCommentLevel(indent, codeout, *stuff)
Esempio n. 4
0
    def genCode(self, indent, codeout, tagreg, node, meta):
        tag = node.children[0]
        DTCompilerUtil.tagDebug(indent, codeout, tag)
        pargs = args = DTUtil.tagCall(tag,
                                      ['expr', ('name', '"sequence_item"')])
        args = DTCompilerUtil.pyifyArgs(tag, args)

        name = DTCompilerUtil.checkName(tag, 'name', args['name'],
                                        pargs['name'])
        exprval = DTCompilerUtil.getTempName()

        codeout.write(indent, '%s = %s' % (exprval, args['expr']))
        codeout.write(indent, 'if %s:' % exprval)
        codeout.write(indent + 4, 'for %s in %s:' % (name, exprval))
        #do main loop shit
        for i in node.children[1:-1]:
            if (type(i) == InstanceType and i.__class__ == DTLexer.DTToken
                    and i.tagname == 'else'):
                break
            else:
                DTCompilerUtil.genCodeChild(indent + 8, codeout, tagreg, i,
                                            meta)
        codeout.write(indent, 'else:')
        codeout.write(indent + 4, 'pass')
        #do else clause
        on = 0
        for i in node.children[1:-1]:
            if (type(i) == InstanceType and i.__class__ == DTLexer.DTToken
                    and i.tagname == 'else'):
                on = 1
            elif on:
                DTCompilerUtil.genCodeChild(indent + 4, codeout, tagreg, i,
                                            meta)
        codeout.write(indent, 'del %s' % exprval)
Esempio n. 5
0
    def genCode(self, indent, codeout, tagreg, node, meta):
        iftag = node.children[0]
        DTCompilerUtil.tagDebug(indent, codeout, iftag)
        args = DTUtil.tagCall(iftag, ['expr'])
        args = DTCompilerUtil.pyifyArgs(iftag, args)

        codeout.write ( indent, 'if (%s):' % args['expr'])
        for i in node.children[1:-1]:
            #if not an else or elif tag and on is true, render the child
            if (type(i) != InstanceType or i.__class__ != DTLexer.DTToken
                or i.tagname not in ('else', 'elif')):
                DTCompilerUtil.genCodeChild ( indent + 4, codeout, tagreg, i,
                                              meta )
            else:
                if i.tagname == 'elif':
                    #
                    # XXX has to move to the elif tag genCode()!
                    #
                    args=DTUtil.tagCall ( i, ['expr'] )
                    args=DTCompilerUtil.pyifyArgs(i, args)
                    
                    codeout.write(indent, 'elif (%s):' % (args['expr']))
                    # XXX kind of sucks to do this after the elif statement
                    DTCompilerUtil.tagDebug(indent + 4, codeout, i)
                    codeout.write(indent+4, 'pass' )
                else: #tagname is else
                    codeout.write(indent, 'else:')
                    codeout.write(indent+4, 'pass')
Esempio n. 6
0
    def genCode(self, indent, codeout, tagreg, node, meta):
        tag=node.children[0]
        DTCompilerUtil.tagDebug(indent, codeout, tag)
        pargs=args=DTUtil.tagCall(tag, ['expr', ('name','"sequence_item"')])
        args=DTCompilerUtil.pyifyArgs(tag, args)

        name = DTCompilerUtil.checkName(tag, 'name', args['name'],
                                        pargs['name'])
        exprval=DTCompilerUtil.getTempName()

        codeout.write(indent, '%s = %s' % (exprval, args['expr']))
        codeout.write(indent, 'if %s:' % exprval)
        codeout.write(indent+4, 'for %s in %s:' % (name, exprval))
        #do main loop shit
        for i in node.children[1:-1]:
            if (type(i) == InstanceType
                and i.__class__ == DTLexer.DTToken
                and i.tagname == 'else'):
                break
            else:
                DTCompilerUtil.genCodeChild(indent+8, codeout, tagreg, i, meta)
        codeout.write(indent, 'else:')
        codeout.write(indent+4, 'pass')
        #do else clause
        on=0
        for i in node.children[1:-1]:
            if (type(i) == InstanceType
                and i.__class__ == DTLexer.DTToken
                and i.tagname == 'else'):
                on=1
            elif on:
                DTCompilerUtil.genCodeChild(indent+4, codeout, tagreg, i, meta)
        codeout.write(indent, 'del %s' % exprval)
Esempio n. 7
0
    def genCode(self, indent, codeout, tagreg, node, meta):
        whiletag = node.children[0]
        DTCompilerUtil.tagDebug(indent, codeout, whiletag)
        args = DTUtil.tagCall(whiletag, ['expr'])
        args = DTCompilerUtil.pyifyArgs(whiletag, args)

        codeout.write(indent, 'while (%s):' % args['expr'])
        for i in node.children[1:-1]:
            # Just generate the code
            DTCompilerUtil.genCodeChild(indent + 4, codeout, tagreg, i, meta)
Esempio n. 8
0
    def genCode(self, indent, codeout, tagreg, node, meta):
        whiletag = node.children[0]
        DTCompilerUtil.tagDebug(indent, codeout, whiletag)
        args = DTUtil.tagCall(whiletag, ['expr'])
        args = DTCompilerUtil.pyifyArgs(whiletag, args)

        codeout.write ( indent, 'while (%s):' % args['expr'])
        for i in node.children[1:-1]:
            # Just generate the code
            DTCompilerUtil.genCodeChild ( indent + 4, codeout, tagreg, i, meta)
Esempio n. 9
0
    def genCode(self, indent, codeout, tagreg, node):
        tag = node.children[0]
        DTCompilerUtil.tagDebug(indent, codeout, tag)
        pargs = args = DTUtil.tagCall(tag, ['name'])
        args = DTCompilerUtil.pyifyArgs(tag, args)
        name = DTCompilerUtil.checkName(tag, 'name', args['name'],
                                        pargs['name'])

        oldout = DTCompilerUtil.getTempName()
        codeout.write(indent, '%s = __h.OUTPUT' % oldout)
        codeout.write(indent, '__h.OUTPUT=__h.NEWOUTPUT()')
        for i in node.children[1:-1]:
            DTCompilerUtil.genCodeChild(indent, codeout, tagreg, i)
        codeout.write(indent, '%s = __h.OUTPUT.getvalue()' % name)
        codeout.write(indent, '__h.OUTPUT = %s' % oldout)
        codeout.write(indent, 'del %s' % oldout)
Esempio n. 10
0
    def genCode(self, indent, codeout, tagreg, node, meta):
        tag = node.children[0]

        # Do the debug
        DTCompilerUtil.tagDebug(indent, codeout, tag)
        DTUtil.tagCall(tag, [])
        oldout = DTCompilerUtil.getTempName()

        #
        # First, determine if this is a 'try...finally' form
        #
        for i in node.children[1:-1]:
            if (type(i) == InstanceType and i.__class__ == DTLexer.DTToken
                    and i.tagname in ('finally', )):
                has_finally = 1
                break
        else:
            has_finally = 0

        if has_finally:
            # This is a try...finally block
            codeout.write(indent, 'try:')

            for i in node.children[1:-1]:
                if (type(i) == InstanceType and i.__class__ == DTLexer.DTToken
                        and i.tagname in ('else', 'except', 'finally')):

                    if i.tagname in ('else', 'except'):
                        # This is an error
                        raise DTExcept.DTCompileError(
                            i,
                            'tag %s inside of a try...finally block' % str(i))
                    else:  # i.tagname == 'finally':
                        codeout.write(indent, 'finally:')
                        codeout.write(indent + 4, '__d.FTAG=__d.CURRENT_TAG')
                        codeout.write(indent + 4,
                                      '__d.FLINENO=__d.CURRENT_LINENO')
                        DTCompilerUtil.tagDebug(indent + 4, codeout, i)
                else:
                    DTCompilerUtil.genCodeChild(indent + 4, codeout, tagreg, i,
                                                meta)
            codeout.write(indent + 4, '__d.CURRENT_TAG=__d.FTAG')
            codeout.write(indent + 4, '__d.CURRENT_LINENO=__d.FLINENO')
            codeout.write(indent + 4, 'del __d.FTAG, __d.FLINENO')
            #
            # Ok, we're done - return
            #
            return

        #
        # Store a reference to current output object. Basically, if an exception
        # occurs we kind of rollback to the point before the 'try' block
        #
        codeout.write(indent, '%s = __h.OUTPUT' % oldout)
        codeout.write(indent, '__h.OUTPUT=__h.NEWOUTPUT()')
        codeout.write(indent, 'try:')

        waselse = 0
        wasexcept = 0

        for i in node.children[1:-1]:
            if (type(i) == InstanceType and i.__class__ == DTLexer.DTToken
                    and i.tagname in ('else', 'except')):
                if i.tagname == 'except':
                    # Get the arguments for the except clause
                    args = DTUtil.tagCall(i, [('exc', 'None')])
                    args = DTCompilerUtil.pyifyArgs(tag, args)

                    if args['exc'] != None:
                        codeout.write(indent, 'except %s:' % args['exc'])
                    else:
                        codeout.write(indent, 'except:')

                    # Do the debugging
                    DTCompilerUtil.tagDebug(indent + 4, codeout, i)

                    # Restore the original output
                    codeout.write(indent + 4, '__h.OUTPUT = %s' % oldout)
                    codeout.write(indent + 4, 'del %s' % oldout)

                    wasexcept = 1
                else:  # i.tagname == 'else':
                    codeout.write(indent, 'else:')

                    DTCompilerUtil.tagDebug(indent + 4, codeout, i)

                    # Print and restore the original output
                    codeout.write(indent + 4,
                                  '%s.write(__h.OUTPUT.getvalue())' % oldout)
                    codeout.write(indent + 4, '__h.OUTPUT = %s' % oldout)
                    codeout.write(indent + 4, 'del %s' % oldout)

                    waselse = 1
            else:
                DTCompilerUtil.genCodeChild(indent + 4, codeout, tagreg, i,
                                            meta)

        if not wasexcept:
            raise DTExcept.DTCompileError(tag, '<:try:> without <:except:>')

        if not waselse:
            codeout.write(indent, 'else:')
            codeout.write(indent + 4,
                          '%s.write(__h.OUTPUT.getvalue())' % oldout)
            codeout.write(indent + 4, '__h.OUTPUT = %s' % oldout)
            codeout.write(indent + 4, 'del %s' % oldout)
Esempio n. 11
0
    def genCode(self, indent, codeout, tagreg, node, meta):
        tag=node.children[0]

        # Do the debug
        DTCompilerUtil.tagDebug(indent, codeout, tag)
        DTUtil.tagCall(tag,[])
        oldout=DTCompilerUtil.getTempName()

        #
        # First, determine if this is a 'try...finally' form
        #
        for i in node.children[1:-1]:
            if ( type(i) == InstanceType
                 and i.__class__ == DTLexer.DTToken 
                 and i.tagname in ( 'finally', )):
                has_finally = 1
                break
        else:
            has_finally = 0

        if has_finally:
            # This is a try...finally block
            codeout.write ( indent, 'try:')

            for i in node.children[1:-1]:
                if ( type(i) == InstanceType
                     and i.__class__ == DTLexer.DTToken
                     and i.tagname in ( 'else', 'except', 'finally' ) ):

                    if i.tagname in ( 'else', 'except' ):
                        # This is an error
                        raise DTExcept.DTCompileError ( i, 
                          'tag %s inside of a try...finally block' % str(i) )
                    else:            # i.tagname == 'finally':
                        codeout.write ( indent, 'finally:' )
                        codeout.write ( indent+4, '__d.FTAG=__d.CURRENT_TAG')
                        codeout.write ( indent+4,
                                        '__d.FLINENO=__d.CURRENT_LINENO')
                        DTCompilerUtil.tagDebug ( indent + 4, codeout, i)
                else:
                    DTCompilerUtil.genCodeChild(indent + 4, codeout, tagreg, i,
                                                meta)
            codeout.write( indent+4, '__d.CURRENT_TAG=__d.FTAG')
            codeout.write( indent+4, '__d.CURRENT_LINENO=__d.FLINENO')
            codeout.write( indent+4, 'del __d.FTAG, __d.FLINENO')
            #
            # Ok, we're done - return
            #
            return

        #
        # Store a reference to current output object. Basically, if an exception
        # occurs we kind of rollback to the point before the 'try' block
        #
        codeout.write ( indent, '%s = __h.OUTPUT' % oldout)
        codeout.write ( indent, '__h.OUTPUT=__h.NEWOUTPUT()')
        codeout.write ( indent, 'try:')

        waselse = 0
        wasexcept = 0

        for i in node.children[1:-1]:
            if ( type(i) == InstanceType
                 and i.__class__ == DTLexer.DTToken 
                 and i.tagname in ( 'else', 'except' )):
                if i.tagname == 'except':
                    # Get the arguments for the except clause
                    args = DTUtil.tagCall ( i, [('exc', 'None')] )
                    args = DTCompilerUtil.pyifyArgs(tag, args)

                    if args['exc'] != None:
                        codeout.write ( indent, 'except %s:' % args['exc'])
                    else:
                        codeout.write ( indent, 'except:' )

                    # Do the debugging
                    DTCompilerUtil.tagDebug ( indent + 4, codeout, i)
                        
                    # Restore the original output
                    codeout.write(indent + 4, '__h.OUTPUT = %s' % oldout)
                    codeout.write(indent + 4, 'del %s' % oldout)

                    wasexcept = 1
                else:      # i.tagname == 'else':
                    codeout.write ( indent, 'else:' )

                    DTCompilerUtil.tagDebug ( indent + 4, codeout, i)

                    # Print and restore the original output
                    codeout.write(indent+4, '%s.write(__h.OUTPUT.getvalue())' %
                                            oldout )
                    codeout.write(indent + 4, '__h.OUTPUT = %s' % oldout)
                    codeout.write(indent + 4, 'del %s' % oldout)

                    waselse = 1
            else:
                DTCompilerUtil.genCodeChild(indent + 4, codeout, tagreg, i,
                                            meta)

        if not wasexcept:
            raise DTExcept.DTCompileError ( tag, '<:try:> without <:except:>' )
            
        if not waselse:
            codeout.write(indent, 'else:')
            codeout.write(indent+4, '%s.write(__h.OUTPUT.getvalue())' % oldout)
            codeout.write(indent+4, '__h.OUTPUT = %s' % oldout)
            codeout.write(indent+4, 'del %s' % oldout)