@@ -57,7 +57,8 @@ def __init__(
5757 self ._if_depth = 0 # Track nesting depth of if blocks
5858 self ._in_elif_mode = False
5959 self ._just_closed_nested = False
60- self ._deferred_comments : list [str ] = []
60+
61+ self ._pre_section_if_start : int | None = None
6162
6263 @lru_cache (maxsize = 128 )
6364 def _cached_percent_parsing (self , pct_text : str ) -> tuple [str , str | None ]:
@@ -84,12 +85,6 @@ def _reset_condition_state(self) -> None:
8485 self ._in_group = False
8586 self ._group_terms .clear ()
8687
87- def _flush_deferred_comments (self ) -> None :
88- """Emit comments that were deferred while inside a rule's if-block."""
89- for comment in self ._deferred_comments :
90- self .output .append (comment )
91- self ._deferred_comments .clear ()
92-
9388 def _start_new_section (self , section_type : SectionType ) -> None :
9489 """Start a new section, handling continuation of existing sections."""
9590 with self .debug_context (f"start_section { section_type .value } " ):
@@ -100,21 +95,17 @@ def _start_new_section(self, section_type: SectionType) -> None:
10095 self .emit ("}" )
10196 self ._if_depth -= 1
10297 self ._reset_condition_state ()
103- self ._flush_deferred_comments ()
10498 if self .output and self .output [- 1 ] != "" :
10599 self .output .append ("" )
106100 return
107101
108- prev = bool (self .output )
109102 had_section = self ._section_opened
110103 self ._close_if_and_section ()
111104 self ._reset_condition_state ()
112105
113106 if had_section and self .output and self .output [- 1 ] != "" :
114107 self .output .append ("" )
115108
116- self ._flush_deferred_comments ()
117-
118109 self ._section_label = section_type
119110 self .emit (f"{ section_type .value } {{" )
120111 self ._section_opened = True
@@ -162,7 +153,6 @@ def visitProgram(self, ctx: u4wrhParser.ProgramContext) -> list[str]:
162153 for line in ctx .line ():
163154 self .visit (line )
164155 self ._close_if_and_section ()
165- self ._flush_deferred_comments ()
166156
167157 var_declarations = self .symbol_resolver .get_var_declarations ()
168158 if var_declarations :
@@ -178,9 +168,7 @@ def visitCommentLine(self, ctx: u4wrhParser.CommentLineContext) -> None:
178168 with self .debug_context ("visitCommentLine" ):
179169 comment_text = ctx .COMMENT ().getText ()
180170 self ._flush_pending_condition ()
181- if self ._if_depth > 0 :
182- self ._deferred_comments .append (comment_text )
183- elif self ._section_opened :
171+ if self ._if_depth > 0 or self ._section_opened :
184172 self .emit (comment_text )
185173 else :
186174 self .output .append (comment_text )
@@ -407,10 +395,27 @@ def _close_if_and_section(self) -> None:
407395 def _ensure_section_open (self , section_label : SectionType ) -> None :
408396 """Ensure a section is open for statements."""
409397 if not self ._section_opened :
398+ relocated_lines = None
399+ relocated_if_depth = 0
400+ if self ._if_depth > 0 and self ._pre_section_if_start is not None :
401+ relocated_lines = self .output [self ._pre_section_if_start :]
402+ relocated_if_depth = self ._if_depth
403+ self .output = self .output [:self ._pre_section_if_start ]
404+ self .stmt_indent -= self ._if_depth
405+ self ._if_depth = 0
406+ self ._pre_section_if_start = None
407+
410408 self .emit (f"{ section_label .value } {{" )
411409 self ._section_opened = True
412410 self .increase_indent ()
413411
412+ if relocated_lines :
413+ indent_prefix = " " * SystemDefaults .INDENT_SPACES
414+ for line in relocated_lines :
415+ self .output .append (indent_prefix + line if line .strip () else line )
416+ self ._if_depth = relocated_if_depth
417+ self .stmt_indent += relocated_if_depth
418+
414419 def _start_elif_mode (self ) -> None :
415420 """Handle elif line transitions."""
416421 # After endif, we need to close the parent if-statement
@@ -432,6 +437,9 @@ def _handle_else_transition(self) -> None:
432437
433438 def _start_if_block (self , condition_expr : str ) -> None :
434439 """Start a new if block."""
440+ if not self ._section_opened and self ._pre_section_if_start is None :
441+ self ._pre_section_if_start = len (self .output )
442+
435443 if self ._in_elif_mode :
436444 self .emit (f"}} elif { condition_expr } {{" )
437445 self ._in_elif_mode = False
0 commit comments