Return a list of :class:`ProtoComment` objects parsed from the given `prefix`.
(prefix: str, *, is_endmarker: bool, mode: Mode)
| 88 | |
| 89 | @lru_cache(maxsize=4096) |
| 90 | def list_comments(prefix: str, *, is_endmarker: bool, mode: Mode) -> list[ProtoComment]: |
| 91 | """Return a list of :class:`ProtoComment` objects parsed from the given `prefix`.""" |
| 92 | result: list[ProtoComment] = [] |
| 93 | if not prefix or "#" not in prefix: |
| 94 | return result |
| 95 | |
| 96 | consumed = 0 |
| 97 | nlines = 0 |
| 98 | ignored_lines = 0 |
| 99 | form_feed = False |
| 100 | for index, full_line in enumerate(re.split("\r?\n|\r", prefix)): |
| 101 | consumed += len(full_line) + 1 # adding the length of the split '\n' |
| 102 | match = re.match(r"^(\s*)(\S.*|)$", full_line) |
| 103 | assert match |
| 104 | whitespace, line = match.groups() |
| 105 | if not line: |
| 106 | nlines += 1 |
| 107 | if "\f" in full_line: |
| 108 | form_feed = True |
| 109 | if not line.startswith("#"): |
| 110 | # Escaped newlines outside of a comment are not really newlines at |
| 111 | # all. We treat a single-line comment following an escaped newline |
| 112 | # as a simple trailing comment. |
| 113 | if line.endswith("\\"): |
| 114 | ignored_lines += 1 |
| 115 | continue |
| 116 | |
| 117 | if index == ignored_lines and not is_endmarker: |
| 118 | comment_type = token.COMMENT # simple trailing comment |
| 119 | else: |
| 120 | comment_type = STANDALONE_COMMENT |
| 121 | comment = make_comment(line, mode=mode) |
| 122 | result.append( |
| 123 | ProtoComment( |
| 124 | type=comment_type, |
| 125 | value=comment, |
| 126 | newlines=nlines, |
| 127 | consumed=consumed, |
| 128 | form_feed=form_feed, |
| 129 | leading_whitespace=whitespace, |
| 130 | ) |
| 131 | ) |
| 132 | form_feed = False |
| 133 | nlines = 0 |
| 134 | return result |
| 135 | |
| 136 | |
| 137 | def normalize_trailing_prefix(leaf: LN, total_consumed: int) -> None: |
no test coverage detected