@@ -331,8 +331,19 @@ def split_header_to_chunks(path: Path) -> List[Chunk]:
331
331
# Make sure no line starts with @, which is used as a marker.
332
332
assert not re .search (r'^\s*@' , code , flags = re .MULTILINE )
333
333
334
- # Remove block comments.
335
- code = re .sub (r'/\*.*?\*/' , lambda x : re .sub (r'[^\n]' , '' , x .group (0 )), code , flags = re .DOTALL )
334
+ # Remove block comments at the top of the file.
335
+ code = re .sub (r'^/\*.*?\*/' , lambda x : re .sub (r'[^\n]' , '' , x .group (0 )), code , flags = re .DOTALL )
336
+
337
+ # Turn block comments into single-line comments with markers for easier parsing.
338
+ assert '//@' not in code # Used as a marker.
339
+ code = re .sub (
340
+ r'/\*.*?\*/ *$' ,
341
+ lambda x : '\n ' .join ([f'//@{ line } ' for line in x .group (0 ).split ('\n ' )]),
342
+ code ,
343
+ flags = re .DOTALL | re .MULTILINE ,
344
+ )
345
+ assert '/*' not in re .sub (r'//.*' , '' , code )
346
+ assert '*/' not in re .sub (r'//.*' , '' , code )
336
347
337
348
# Remove extern "C" declarations.
338
349
code = code .replace ('\n #ifdef __cplusplus\n extern "C" {\n #endif\n ' , '\n \n \n \n ' )
@@ -431,7 +442,18 @@ def split_header_to_chunks(path: Path) -> List[Chunk]:
431
442
if len (idents ) == 0 :
432
443
continue
433
444
434
- result .append (Chunk (path .name , line_number , idents , before .copy (), intro , body , after .copy ()))
445
+ def remove_markers (code : str ):
446
+ return re .sub (r'//@' , '' , code )
447
+
448
+ result .append (Chunk (
449
+ path .name ,
450
+ line_number ,
451
+ idents ,
452
+ [(remove_markers (x ), remove_markers (y )) for x , y in before ],
453
+ remove_markers (intro ),
454
+ remove_markers (body ),
455
+ [remove_markers (x ) for x in after ],
456
+ ))
435
457
436
458
assert len (before ) == 0 , before
437
459
assert len (after ) == 0 , after
0 commit comments