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')
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')
def genCode(self, indent, codeout, tagreg, tag): DTCompilerUtil.tagDebug(indent, codeout, tag) args=DTUtil.tagCall ( tag, ['expr', ('fmt', 'None'), ] ) #expr = args['expr'] args=DTCompilerUtil.pyifyArgs(tag, args) expr = args['expr'] #if len(expr)>1 and expr[0]==expr[-1]=='`': # expr=expr[1:-1] #elif len(expr)>1 and expr[0]==expr[-1]=='"': # expr=expr[1:-1] #elif len(expr)>1 and expr[0]==expr[-1]=="'": # expr=expr[1:-1] #else: # pass #expr=exrepr(expr) codeout.write(indent, '__t.val = %s' % expr) codeout.write(indent, 'if __t.val is None:') codeout.write(indent+4, '__t.val = ""') codeout.write(indent, 'else:') codeout.write(indent+4, '__t.val = str(__t.val)') if args['fmt'] and args['fmt'][1:-1] not in \ ('None', 'plain', 'plaintext'): codeout.write(indent, '__t.val=__h.VALFMTRGY.get(%s, lambda x: x)(__t.val)' % args['fmt']) codeout.write(indent, '__h.OUTPUT.write(__t.val)') codeout.write(indent, 'del __t.val')
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)
def genCode(self, indent, codeout, tagreg, tag): DTCompilerUtil.tagDebug(indent, codeout, tag) pargs=args=DTUtil.tagCall(tag, ['name', 'value']) args=DTCompilerUtil.pyifyArgs(tag, args) name = DTCompilerUtil.checkName(tag, 'name', args['name'], pargs['name']) codeout.write(indent, '%s = (%s)' % (name, args['value']))
def genCode(self, indent, codeout, tagreg, tag): DTCompilerUtil.tagDebug(indent, codeout, tag) pargs = args = DTUtil.tagCall(tag, ['name', 'value']) args = DTCompilerUtil.pyifyArgs(tag, args) name = DTCompilerUtil.checkName(tag, 'name', args['name'], pargs['name']) codeout.write(indent, '%s = (%s)' % (name, args['value']))
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)
def genCode(self, indent, codeout, tagreg, tag): DTCompilerUtil.tagDebug(indent, codeout, tag) oargs = args = DTUtil.tagCall(tag, ['module', ('items', 'None'), ('as', 'None')]) args = DTCompilerUtil.pyifyArgs(tag, args) module = DTCompilerUtil.checkName(tag, 'module', args['module'], oargs['module']) if oargs['as'] != None: asname = DTCompilerUtil.checkName(tag, 'as', args['as'], oargs['as']) asStr = " as %s" % asname else: asStr = '' if oargs['items'] != None: items = DTCompilerUtil.checkName('import', 'items', args['items'], oargs['items']) # allow items to be separated by commas or spaces if ',' in items: formattedItems=','.join(_splitter.split(items)) else: formattedItems=','.join(items.split()) codeout.write(indent, 'from %s import %s%s' % (module, formattedItems, asStr)) else: codeout.write(indent, 'import %s%s' % (module, asStr))
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)
def genCode(self, indent, codeout, tagreg, tag): DTCompilerUtil.tagDebug(indent, codeout, tag) oargs = args = DTUtil.tagCall( tag, ['module', ('items', 'None'), ('as', 'None')]) args = DTCompilerUtil.pyifyArgs(tag, args) module = DTCompilerUtil.checkName(tag, 'module', args['module'], oargs['module']) if oargs['as'] != None: asname = DTCompilerUtil.checkName(tag, 'as', args['as'], oargs['as']) asStr = " as %s" % asname else: asStr = '' if oargs['items'] != None: items = DTCompilerUtil.checkName('import', 'items', args['items'], oargs['items']) # allow items to be separated by commas or spaces if ',' in items: formattedItems = ','.join(_splitter.split(items)) else: formattedItems = ','.join(items.split()) codeout.write( indent, 'from %s import %s%s' % (module, formattedItems, asStr)) else: codeout.write(indent, 'import %s%s' % (module, asStr))
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)
def genCode(self, indent, codeout, tagreg, tag): DTCompilerUtil.tagDebug(indent, codeout, tag) args = DTUtil.tagCall(tag, [ 'expr', ('fmt', 'None'), ]) #expr = args['expr'] args = DTCompilerUtil.pyifyArgs(tag, args) expr = args['expr'] #if len(expr)>1 and expr[0]==expr[-1]=='`': # expr=expr[1:-1] #elif len(expr)>1 and expr[0]==expr[-1]=='"': # expr=expr[1:-1] #elif len(expr)>1 and expr[0]==expr[-1]=="'": # expr=expr[1:-1] #else: # pass #expr=exrepr(expr) codeout.write(indent, '__t.val = %s' % expr) codeout.write(indent, 'if __t.val is None:') codeout.write(indent + 4, '__t.val = ""') codeout.write(indent, 'else:') codeout.write(indent + 4, '__t.val = str(__t.val)') if args['fmt'] and args['fmt'][1:-1] not in \ ('None', 'plain', 'plaintext'): codeout.write( indent, '__t.val=__h.VALFMTRGY.get(%s, lambda x: x)(__t.val)' % args['fmt']) codeout.write(indent, '__h.OUTPUT.write(__t.val)') codeout.write(indent, 'del __t.val')
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)
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)
def genCode(self, indent, codeout, tagreg, tag): DTCompilerUtil.tagDebug(indent, codeout, tag) args = DTUtil.tagCall(tag, ['expr', ('valTrue', self._def_true), ('valFalse', self._def_false)],) args = DTCompilerUtil.pyifyArgs(tag, args) # Ok, write out the stuff codeout.write ( indent, 'if %s:' % args['expr'] ) codeout.write ( indent+4, '__h.OUTPUT.write ( %s )' % args['valTrue'] ) codeout.write ( indent, 'else:' ) codeout.write ( indent+4, '__h.OUTPUT.write ( %s )' % args['valFalse'] )
def genCode(self, indent, codeout, tagreg, tag): DTCompilerUtil.tagDebug(indent, codeout, tag) pargs=args=DTUtil.tagCall(tag, ['name', ('value', '""')]) args=DTCompilerUtil.pyifyArgs(tag, args) name = DTCompilerUtil.checkName(tag, 'name', args['name'], pargs['name']) codeout.write(indent, 'try:') codeout.write(indent+4, '%s' % name) codeout.write(indent, 'except (NameError, AttributeError):') codeout.write(indent+4, '%s = %s' % (name, args['value'])) codeout.write(indent, 'else:') codeout.write(indent+4, 'if %s is None:' % name) codeout.write(indent+8, '%s = %s' % (name, args['value']))
def genCode(self, indent, codeout, tagreg, tag): DTCompilerUtil.tagDebug(indent, codeout, tag) pargs = args = DTUtil.tagCall(tag, ['name', ('value', '""')]) args = DTCompilerUtil.pyifyArgs(tag, args) name = DTCompilerUtil.checkName(tag, 'name', args['name'], pargs['name']) codeout.write(indent, 'try:') codeout.write(indent + 4, '%s' % name) codeout.write(indent, 'except (NameError, AttributeError):') codeout.write(indent + 4, '%s = %s' % (name, args['value'])) codeout.write(indent, 'else:') codeout.write(indent + 4, 'if %s is None:' % name) codeout.write(indent + 8, '%s = %s' % (name, args['value']))
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)
def genCode(self, indent, codeout, tagreg, tag): DTCompilerUtil.tagDebug(indent, codeout, tag) args = DTUtil.tagCall( tag, [ 'expr', ('valTrue', self._def_true), ('valFalse', self._def_false) ], ) args = DTCompilerUtil.pyifyArgs(tag, args) # Ok, write out the stuff codeout.write(indent, 'if %s:' % args['expr']) codeout.write(indent + 4, '__h.OUTPUT.write ( %s )' % args['valTrue']) codeout.write(indent, 'else:') codeout.write(indent + 4, '__h.OUTPUT.write ( %s )' % args['valFalse'])
def genCode ( self, indent, codeout, tagreg, tag ): DTCompilerUtil.tagDebug ( indent, codeout, tag ) args=DTUtil.tagCall ( tag, [('exc', None)] ) args=DTCompilerUtil.pyifyArgs(tag, args) tvar=DTCompilerUtil.getTempName() # Just do a regular python raise - you should know to raise instances # # <:raise MyExcept('hello'):> works # <:raise MyExcept, 'hello':> doesn't # <:raise 'string exception':> works # <:raise:> by itself now re-raises exception properly if args['exc'] is None: codeout.write ( indent, 'import sys' ) codeout.write ( indent, 'raise' ) else: codeout.write ( indent, 'raise (%s)' % args['exc'] )
def genCode(self, indent, codeout, tagreg, tag): DTCompilerUtil.tagDebug(indent, codeout, tag) args = DTUtil.tagCall(tag, [('exc', None)]) args = DTCompilerUtil.pyifyArgs(tag, args) tvar = DTCompilerUtil.getTempName() # Just do a regular python raise - you should know to raise instances # # <:raise MyExcept('hello'):> works # <:raise MyExcept, 'hello':> doesn't # <:raise 'string exception':> works # <:raise:> by itself now re-raises exception properly if args['exc'] is None: codeout.write(indent, 'import sys') codeout.write(indent, 'raise') else: codeout.write(indent, 'raise (%s)' % args['exc'])
def genCode(self, indent, codeout, tagreg, tag): DTCompilerUtil.tagDebug(indent, codeout, tag) pargs = args = DTUtil.tagCall(tag, ['var'], 'plainArgs', 'kwArgs') kwargs = DTCompilerUtil.pyifyArgs(tag, args['kwArgs']) args = DTCompilerUtil.pyifyArgs(tag, args) varname = DTCompilerUtil.checkName(tag, 'var', args['var'], pargs['var']) # First, generate the plain args out = [] for _j in args['plainArgs']: out.append('"%s": %s' % (_j, _j)) codeout.write(indent, '%s = { %s }' % (varname, string.join(out, ', '))) # Now, add the keyword arguments codeout.write(indent, '%s.update(%s)' % (varname, str(args['kwArgs'])))
def genCode(self, indent, codeout, tagreg, tag): DTCompilerUtil.tagDebug(indent, codeout, tag) pargs = args = DTUtil.tagCall(tag, ['var'], 'plainArgs', 'kwArgs' ) kwargs = DTCompilerUtil.pyifyArgs(tag, args['kwArgs']) args = DTCompilerUtil.pyifyArgs(tag, args) varname = DTCompilerUtil.checkName(tag, 'var', args['var'], pargs['var']) # First, generate the plain args out = [] for _j in args['plainArgs']: out.append ( '"%s": %s' % ( _j, _j ) ) codeout.write ( indent, '%s = { %s }' % ( varname, string.join ( out, ', ' )) ) # Now, add the keyword arguments codeout.write ( indent, '%s.update(%s)' % ( varname, str(args['kwArgs']) ))
def genCode(self, indent, codeout, tagreg, tag): DTCompilerUtil.tagDebug(indent, codeout, tag) pargs = args = DTUtil.tagCall(tag, [('expr', 'None')]) args = DTCompilerUtil.pyifyArgs(tag, args) try: DTCompilerUtil.checkName(tag, 'expr', args['expr'], pargs['expr']) except: pass else: raise DTExcept.DTCompileError(tag, 'cannot call a string') stmt = args['expr'] + '\n' try: compile(stmt, stmt, 'exec') except SyntaxError: raise DTExcept.DTCompileError( tag, 'syntax error in statement "%s"' % stmt) codeout.writemultiline(indent, stmt)
def genCode(self, indent, codeout, tagreg, tag): DTCompilerUtil.tagDebug ( indent, codeout, tag ) pargs = args = DTUtil.tagCall(tag, [('expr', 'None')]) args = DTCompilerUtil.pyifyArgs(tag, args) try: DTCompilerUtil.checkName(tag, 'expr', args['expr'], pargs['expr']) except: pass else: raise DTExcept.DTCompileError( tag, 'cannot call a string') stmt = args['expr'] + '\n' try: compile(stmt, stmt, 'exec') except SyntaxError: raise DTExcept.DTCompileError ( tag, 'syntax error in statement "%s"' % stmt ) codeout.writemultiline(indent, stmt)
def genCode(self, indent, codeout, tagreg, tag): DTCompilerUtil.tagDebug(indent, codeout, tag) oargs = args = DTUtil.tagCall( tag, ['module', ('items', 'None'), ('as', 'None')]) args = DTCompilerUtil.pyifyArgs(tag, args) module = DTCompilerUtil.checkName(tag, 'module', args['module'], oargs['module']) if oargs['as'] != None: asname = DTCompilerUtil.checkName(tag, 'as', args['as'], oargs['as']) asStr = " as %s" % asname else: asStr = '' if oargs['items'] != None: items = DTCompilerUtil.checkName('import', 'items', args['items'], oargs['items']) codeout.write( indent, 'from %s import %s%s' % (module, string.join(string.split(items), ','), asStr)) else: codeout.write(indent, 'import %s%s' % (module, asStr))
def genCode(self, indent, codeout, tagreg, tag): DTCompilerUtil.tagDebug(indent, codeout, tag) args=DTUtil.tagCall(tag, []) codeout.write ( indent, 'break' )
def genCode(self, indent, codeout, tagreg, tag): DTCompilerUtil.tagDebug(indent, codeout, tag) args = DTUtil.tagCall(tag, []) # Generate a halt DTExcept.raiseHalt(codeout, indent)
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)
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)
def genCode(self, indent, codeout, tagreg, tag): DTCompilerUtil.tagDebug(indent, codeout, tag) args=DTUtil.tagCall(tag, []) # Generate a halt DTExcept.raiseHalt ( codeout, indent )
return self._taglist def __getitem__(self, item): return DTToken(self._taglist[item], self._text) def __getslice__(self, b, e): return LazyDTTokenList(self._taglist[b:e], self._text) def __len__(self): return len(self._taglist) def __repr__(self): tags = map(repr, self) return '[' + string.join(tags, ', ') + ']' if __name__ == "__main__": import sys import DTLexer import DTUtil tl = DTLexer.doTag(open(sys.argv[1]).read()) for i in tl: print '--------------------' print '%s:%s:%s' % (len(i), i.tagname(), i) for j in i: print '%s:%s:%s' % (len(j), j.tagname(), j) if len(j) > 1: for k in j: print '%s:%s:%s' % (len(k), k.tagname(), k) print DTUtil.tagattrToTupleDict(i)
def genCode(self, indent, codeout, tagreg, tag): DTCompilerUtil.tagDebug(indent, codeout, tag) args = DTUtil.tagCall(tag, []) codeout.write(indent, 'break')
def getTagList(self): return self._taglist def __getitem__(self, item): return DTToken(self._taglist[item], self._text) def __getslice__(self, b, e): return LazyDTTokenList(self._taglist[b:e], self._text) def __len__(self): return len(self._taglist) def __repr__(self): tags=map(repr, self) return '['+string.join(tags,', ')+']' if __name__=="__main__": import sys import DTLexer import DTUtil tl=DTLexer.doTag(open(sys.argv[1]).read()) for i in tl: print '--------------------' print '%s:%s:%s' % (len(i), i.tagname(), i) for j in i: print '%s:%s:%s' % (len(j), j.tagname(), j) if len(j) > 1: for k in j: print '%s:%s:%s' % (len(k), k.tagname(), k) print DTUtil.tagattrToTupleDict(i)