@@ -41,11 +41,21 @@ def insertComments(self, tmpl, tree, index, memo):
4141 prefix = self .config .last ('commentPrefix' , '# ' )
4242 cache , parser , comTypes = memo .comments , tree .parser , tokens .commentTypes
4343 comNew = lambda t :t .type in comTypes and t .index not in cache
44+
4445 for tok in ifilter (comNew , parser .input .tokens [memo .last :index ]):
4546 cache .add (tok .index )
46- if tmpl .isExpression and tok .line == parser .input .tokens [index ].line :
47- tmpl .tail += prefix if not tmpl .tail .startswith (prefix ) else ''
48- tmpl .tail += '' .join (self .stripComment (tok .text ))
47+
48+ # loop over parents until we find the top expression
49+ base = tmpl
50+ while base :
51+ if base and base .parent and base .parent .isExpression :
52+ base = base .parent
53+ else :
54+ break
55+
56+ if hasattr (base , 'tail' ) and tok .line == parser .input .tokens [index ].line :
57+ base .tail += prefix if not base .tail .startswith (prefix ) else ''
58+ base .tail += '' .join (self .stripComment (tok .text ))
4959 else :
5060 for line in self .stripComment (tok .text ):
5161 self .factory .comment (left = prefix , right = line , parent = self )
@@ -250,12 +260,14 @@ def acceptFunctionMethodDecl(self, node, memo):
250260 ident = node .firstChildOfType (tokens .IDENT )
251261 type = node .firstChildOfType (tokens .TYPE ).children [0 ].text
252262 mods = node .firstChildOfType (tokens .MODIFIER_LIST )
263+ self .variables .append (ident .text )
253264 return self .factory .method (name = ident .text , type = type , parent = self )
254265
255266 def acceptVoidMethodDecl (self , node , memo ):
256267 """ Accept and process a void method declaration. """
257268 ident = node .firstChildOfType (tokens .IDENT )
258269 mods = node .firstChildOfType (tokens .MODIFIER_LIST )
270+ self .variables .append (ident .text )
259271 return self .factory .method (name = ident .text , type = 'void' , parent = self )
260272
261273
@@ -352,9 +364,17 @@ def acceptAssert(self, node, memo):
352364
353365 def acceptBreak (self , node , memo ):
354366 """ Accept and process a break statement. """
355- if node .parent and node .parent .type in (tokens .CASE , tokens .DEFAULT ):
356- return
357- breakStat = self .factory .statement ('break' , parent = self )
367+ # possible parents of a break statement: switch, while, do, for
368+ # we want to skip inserting a break statement if we're inside a switch.
369+ insert , ok_types = True , [tokens .WHILE , tokens .DO , tokens .FOR ]
370+ for parent in node .parents ():
371+ if parent .type == tokens .SWITCH :
372+ insert = False
373+ break
374+ if parent .type in ok_types :
375+ break
376+ if insert :
377+ breakStat = self .factory .statement ('break' , parent = self )
358378
359379 def acceptCatch (self , node , memo ):
360380 """ Accept and process a catch statement. """
@@ -536,7 +556,10 @@ def acceptWhile(self, node, memo):
536556 parNode , blkNode = node .children
537557 whileStat = self .factory .statement ('while' , fs = FS .lsrc , parent = self )
538558 whileStat .expr .walk (parNode , memo )
539- whileStat .walk (blkNode , memo )
559+ if not blkNode .children :
560+ self .factory .expr (left = 'pass' , parent = whileStat )
561+ else :
562+ whileStat .walk (blkNode , memo )
540563
541564
542565class Method (VarAcceptor , ModifiersAcceptor , MethodContent ):
@@ -720,6 +743,7 @@ def acceptMethodCall(self, node, memo):
720743 arg .left .walk (child , memo )
721744 arg .right = arg = expr (parent = self )
722745
746+
723747 def acceptThisConstructorCall (self , node , memo ):
724748 """ Accept and process a 'this(...)' constructor call. """
725749 self .acceptMethodCall (node , memo )
0 commit comments