parser.py¶
clew/guard/parser.py
Doxygen block parsing and the parse entry point.
_finalize_tag
None clew.guard.parser._finalize_tag(dict[str, list[str]] tags, str|None tag, list[str] value)
Store the current tag's value and reset state.
Source · 9 lines
def _finalize_tag(
tags: dict[str, list[str]],
tag: str | None,
value: list[str],
) -> None:
if tag is not None:
tags.setdefault(tag, []).append(_unmask_escaped_at(" ".join(value).strip()))
Activity · 2 steps
Flows
_mask_escaped_at
str clew.guard.parser._mask_escaped_at(str line)
Mask escaped at-signs so they are not detected as tags.
Source · 4 lines
def _mask_escaped_at(line: str) -> str:
return _ESCAPED_AT_RE.sub(_ESCAPED_AT_SENTINEL, line)
Activity · 1 steps
Flows
_split_inline_tags
list[str] clew.guard.parser._split_inline_tags(str line)
Split a line into segments at inline tag boundaries.
Source · 5 lines
def _split_inline_tags(line: str) -> list[str]:
parts = _INLINE_SPLIT_RE.split(line)
return [p.strip() for p in parts if p.strip()] if len(parts) > 1 else [line]
Activity · 2 steps
Flows
_unmask_escaped_at
str clew.guard.parser._unmask_escaped_at(str text)
Restore masked at-signs to a single literal at-sign.
Source · 4 lines
def _unmask_escaped_at(text: str) -> str:
return text.replace(_ESCAPED_AT_SENTINEL, "@")
Activity · 1 steps
Flows
apply_autobrief
None clew.guard.parser.apply_autobrief(str block_text, dict[str, list[str]] tags)
Supply @brief from a rustdoc block's summary paragraph when none is written.
Called by _check_file_block_tags, _find_preceding_doxygen, _find_python_docstring_block, render_doc, summarize
Calls _mask_escaped_at, _split_inline_tags, _unmask_escaped_at
Source · 22 lines
def apply_autobrief(block_text: str, tags: dict[str, list[str]]) -> None:
if "brief" in tags:
return
prefix_re = re.compile(r"^\s*(?:/\*\*|\*/|[/*]+!?)\s?")
summary: list[str] = []
for raw_line in block_text.splitlines():
line = _mask_escaped_at(prefix_re.sub("", raw_line).removesuffix("*/").strip())
if not line:
if summary:
break
continue
if _AUTOBRIEF_STOP_RE.match(line):
break
# A one-line block (`/** Adds one. @version 1 */`) ends its summary at the tag.
segments = _split_inline_tags(line)
summary.append(segments[0])
if len(segments) > 1:
break
if summary:
tags["brief"] = [_unmask_escaped_at(" ".join(summary))]
Activity · 18 steps
Requirements
Flows
parse_doxygen_tags
dict[str, list[str]] clew.guard.parser.parse_doxygen_tags(str block_text)
Parse all doxygen tag entries from comment text.
Called by _check_file_block_tags, _find_preceding_doxygen, _find_python_docstring_block, render_doc, summarize
Tested by 13 tests
test_backslash_escape_is_not_a_tag, test_double_at_escape_is_not_a_tag, test_duplicate_tags, test_empty_block, test_escape_at_start_of_line_is_not_a_tag, test_escape_in_brief_round_trips_single_at, test_escaped_and_real_tag_on_one_line, test_genuine_inline_multi_tag_line_still_parses, test_multiple_escapes_on_one_line, test_multiple_tags, test_no_sentinel_leaks_into_output, test_single_tag
Source · 29 lines
def parse_doxygen_tags(block_text: str) -> dict[str, list[str]]:
tags: dict[str, list[str]] = {}
current_tag: str | None = None
current_value: list[str] = []
# `!?` takes the bang of a Rust inner doc comment (`//!`), so `//! @file` parses.
prefix_re = re.compile(r"^\s*[/*#]+!?\s?")
suffix_re = re.compile(r"\s*\*/\s*$")
for raw_line in block_text.splitlines():
line = prefix_re.sub("", raw_line)
line = _mask_escaped_at(suffix_re.sub("", line).strip())
for segment in _split_inline_tags(line):
match = _TAG_RE.match(segment)
if match:
_finalize_tag(tags, current_tag, current_value)
current_tag = match.group(1)
current_value = [match.group(2) or ""]
elif not segment and current_tag is not None:
_finalize_tag(tags, current_tag, current_value)
current_tag = None
current_value = []
elif current_tag is not None and segment:
current_value.append(segment)
_finalize_tag(tags, current_tag, current_value)
return tags
# A markdown heading (`# Examples`) or a tag line ends rustdoc's summary paragraph.Activity · 22 steps
Requirements
Flows
parse_functions
list[Function] clew.guard.parser.parse_functions(str content, list[str] exclude_names, ParseSettings|None settings=None, *str lang_name)
Parse source code to find functions and their associated doxygen comments.
Called by _parse, _parse, parse_content_for_file, parse_lang
Calls parse_functions_ts
Source · 16 lines
def parse_functions(
content: str,
exclude_names: list[str],
settings: ParseSettings | None = None,
*,
lang_name: str,
) -> list[Function]:
from .ts_parser import parse_functions_ts
s = settings or ParseSettings()
return parse_functions_ts(
content,
lang_name,
exclude_names=exclude_names,
comment_start_pattern=s.comment_start,
)1 more lines, not shown