From: <ped...@us...> - 2006-12-09 16:30:08
|
Revision: 3012 http://svn.sourceforge.net/jython/?rev=3012&view=rev Author: pedronis Date: 2006-12-09 08:30:04 -0800 (Sat, 09 Dec 2006) Log Message: ----------- implement `csub`(<comment>) to do the substitutions in comments it was always meant for. Modified Paths: -------------- trunk/jython/src/templates/java_pretty.py trunk/jython/src/templates/java_templating.py Modified: trunk/jython/src/templates/java_pretty.py =================================================================== --- trunk/jython/src/templates/java_pretty.py 2006-12-09 05:56:35 UTC (rev 3011) +++ trunk/jython/src/templates/java_pretty.py 2006-12-09 16:30:04 UTC (rev 3012) @@ -82,9 +82,11 @@ self.nl() self.nl(soft) - def emit_comment(self,comment): # doesn't write final \n! + def emit_comment(self,comment, subst): # doesn't write final \n! col = comment.col comment = comment.value + if subst is not None: + comment = comment % subst lines = comment.split('\n') first = lines[0] self.write(first) @@ -98,27 +100,28 @@ self.nl() self.write(line[tostrip:]) - def emit_horizontal_comments(self,comments): # space + space separated comments... + def emit_horizontal_comments(self,comments, subst): + # space + space separated comments... for comment in comments: self.space() - self.emit_comment(comment) + self.emit_comment(comment, subst) - def emit_vertical_comments(self,comments): + def emit_vertical_comments(self,comments, subst): end = comments[0].end - self.emit_comment(comments[0]) + self.emit_comment(comments[0], subst) for comment in comments[1:]: self.vertical_space(comment.start - end) end = comment.end - self.emit_comment(comment) + self.emit_comment(comment, subst) - def emit_infront_comments(self,tok): + def emit_infront_comments(self,tok, subst): if not self.linehome(): self.nl() comments = tok.infront_comments - self.emit_vertical_comments(comments) + self.emit_vertical_comments(comments, subst) self.vertical_space(tok.lineno - comments[-1].end) - def emit_attached_comments(self,tok): + def emit_attached_comments(self,tok, subst): comments = tok.attached_comments start = comments[0].start horizontal = 1 # all on one line @@ -127,11 +130,11 @@ horizontal = 0 break if horizontal: - self.emit_horizontal_comments(comments) + self.emit_horizontal_comments(comments, subst) else: self.space() self.here() - self.emit_vertical_comments(comments) + self.emit_vertical_comments(comments, subst) self.dedent() if comments[-1].value.startswith("//"): # // needs newline delta = 1 @@ -143,16 +146,17 @@ else: self.vertical_space(delta,soft=1) - def emit_tok(self,tok,ctl='ika'): # ctl: i=>infront k=>token a=>attached + def emit_tok(self,tok,ctl='ika', subst=None): + # ctl: i=>infront k=>token a=>attached if 'i' in ctl and tok.infront_comments: - self.emit_infront_comments(tok) + self.emit_infront_comments(tok, subst) if 'k' in ctl: self.emit(tok.value) if 'a' in ctl and tok.attached_comments: - self.emit_attached_comments(tok) + self.emit_attached_comments(tok, subst) # ~~~ @@ -281,7 +285,7 @@ if tok is not None: self.printer.emit_tok(tok,'ia') - def emit_tok(self,tok,ctl='ika'): + def emit_tok(self,tok,ctl='ika', subst=None): if 'k' in ctl: seps = self._separators for i in range(len(seps)-1,-1,-1): @@ -290,7 +294,7 @@ seps[i] = None else: break - self.printer.emit_tok(tok,ctl) + self.printer.emit_tok(tok,ctl, subst) def default_visit(self,node,ctxt): if node is None: return Modified: trunk/jython/src/templates/java_templating.py =================================================================== --- trunk/jython/src/templates/java_templating.py 2006-12-09 05:56:35 UTC (rev 3011) +++ trunk/jython/src/templates/java_templating.py 2006-12-09 16:30:04 UTC (rev 3012) @@ -436,8 +436,9 @@ raise Exception,"csub expects no arguments" if not paren: raise Exception,"csub expects parenthesis" - visitor.emit_tok(paren[0],'a') - visitor.emit_tok(paren[1],'i') + bindings = visitor.bindings + visitor.emit_tok(paren[0],'a', subst=bindings) + visitor.emit_tok(paren[1],'i', subst=bindings) return None,0 csub = CSub() @@ -637,7 +638,7 @@ { `csub`( /* - ok + %(ok)s */); } } @@ -648,7 +649,7 @@ { `csub`( /* - ok + %(ok)s */); break; } @@ -658,14 +659,14 @@ with_comments.append(JavaTemplate(""" { { - invoke(a,`csub`(/* ok */),b); + invoke(a,`csub`(/* %(ok)s */),b); break; } } """)) for templ in with_comments: - print templ.texpand({'csub': csub}) + print templ.texpand({'csub': csub, 'ok': "OK"}) print (JavaTemplate("a(); b();")+JavaTemplate("c(); d();")).texpand({}) print (JavaTemplate("a,b")+JavaTemplate("c,d")).texpand({}) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |