scriptast.py¶
clew/scriptast.py
What the shell and CMake front ends share: a tree walk, node text, and the `#` doc block.
comment_block_above
str clew.scriptast.comment_block_above(int line_index, list[str] lines)
The # comment block immediately above a line, with its markers stripped.
Source · 22 lines
def comment_block_above(line_index: int, lines: list[str]) -> str:
"""THE SHARED RULE. Immediately above, with no blank line between — a detached comment
documents nothing, which is what every other front end here already enforces for its
own comment form.
A shebang is not documentation: it addresses the kernel, not a reader, so a block that
reaches one stops there. That matters for the common shape of a script whose first
line is `#!/bin/bash` followed by a title comment: the title documents the FILE, and
attributing it to whatever function happens to be defined first would be wrong.
@brief The preceding comment block, markers stripped.
@return The block text.
"""
collected: list[str] = []
index = line_index - 1
while index >= 0:
stripped = lines[index].strip()
if not stripped.startswith("#") or stripped.startswith("#!"):
break
collected.append(stripped.lstrip("#").strip())
index -= 1
return "\n".join(reversed(collected)).strip()1 more lines, not shown