|
37 | 37 |
|
38 | 38 | from unicorn import Uc, UcError |
39 | 39 |
|
| 40 | +# Markers for /* */ comment preservation |
| 41 | +_BLOCK_COMMENT_MARKER = "__SLOTHY_BLOCK__" |
| 42 | +_NEWLINE_MARKER = "__SLOTHY_NL__" |
| 43 | + |
40 | 44 |
|
41 | 45 | class SourceLine: |
42 | 46 | """Representation of a single line of source code""" |
@@ -232,7 +236,21 @@ def to_string(self, indentation=True, comments=True, tags=True): |
232 | 236 | additional = [] |
233 | 237 |
|
234 | 238 | if comments is True: |
235 | | - additional += list(map(lambda s: f"// {s}", double_comments)) |
| 239 | + # Convert comments with markers back to /* */ format |
| 240 | + def format_comment(s): |
| 241 | + if _NEWLINE_MARKER in s: |
| 242 | + # Restore as multi-line /* */ comment |
| 243 | + lines = s.split(_NEWLINE_MARKER) |
| 244 | + lines = [line.strip() for line in lines] |
| 245 | + return "/* " + "\n ".join(lines) + " */" |
| 246 | + elif s.startswith(_BLOCK_COMMENT_MARKER): |
| 247 | + # Restore as single-line /* */ comment |
| 248 | + return f"/* {s[len(_BLOCK_COMMENT_MARKER):]} */" |
| 249 | + else: |
| 250 | + # Regular // comment - keep as is |
| 251 | + return f"// {s}" |
| 252 | + |
| 253 | + additional += list(map(format_comment, double_comments)) |
236 | 254 | additional += list(map(lambda s: f"///{s}", triple_comments)) |
237 | 255 |
|
238 | 256 | if tags is True: |
@@ -346,6 +364,21 @@ def read_multiline(s, reduce=True): |
346 | 364 | if isinstance(s, str): |
347 | 365 | # Retain newline termination |
348 | 366 | terminated_by_newline = len(s) > 0 and s[-1] == "\n" |
| 367 | + |
| 368 | + # Convert /* */ comments to // style with markers for restoration |
| 369 | + def replace_comment(match): |
| 370 | + content = match.group(1) |
| 371 | + if "\n" in content: |
| 372 | + # Multi-line /* */ comment - mark newlines |
| 373 | + content = content.replace("\n", f" {_NEWLINE_MARKER} ") |
| 374 | + content = " ".join(content.split()) |
| 375 | + return f"// {content}" if content else "" |
| 376 | + else: |
| 377 | + # Single-line /* */ comment - mark with prefix |
| 378 | + content = content.strip() |
| 379 | + return f"// {_BLOCK_COMMENT_MARKER}{content}" if content else "" |
| 380 | + |
| 381 | + s = re.sub(r"/\*(.*?)\*/", replace_comment, s, flags=re.DOTALL) |
349 | 382 | s = s.splitlines() |
350 | 383 | if terminated_by_newline: |
351 | 384 | s.append("") |
|
0 commit comments