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.

clew/guard/parser.py:119

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
lines 124–124◇ if tag is not Nonelines 125–125tags.setdefault(tag, []).append(_unmask_escaped_at(" ".join(value).strip()))

Flows

_mask_escaped_at

str clew.guard.parser._mask_escaped_at(str line)

Mask escaped at-signs so they are not detected as tags.

clew/guard/parser.py:104

Source · 4 lines
def _mask_escaped_at(line: str) -> str:
    return _ESCAPED_AT_RE.sub(_ESCAPED_AT_SENTINEL, line)

Activity · 1 steps
lines 105–105return _ESCAPED_AT_RE.sub(_ESCAPED_AT_SENTINEL, line)

Flows

_split_inline_tags

list[str] clew.guard.parser._split_inline_tags(str line)

Split a line into segments at inline tag boundaries.

clew/guard/parser.py:204

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
lines 205–205parts = _INLINE_SPLIT_RE.split(line)lines 206–206return [p.strip() for p in parts if p.strip()] if len(parts) > 1 else [line]

Flows

_unmask_escaped_at

str clew.guard.parser._unmask_escaped_at(str text)

Restore masked at-signs to a single literal at-sign.

clew/guard/parser.py:112

Source · 4 lines
def _unmask_escaped_at(text: str) -> str:
    return text.replace(_ESCAPED_AT_SENTINEL, "@")

Activity · 1 steps
lines 113–113return text.replace(_ESCAPED_AT_SENTINEL, "@")

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.

clew/guard/parser.py:176

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
lines 177–177◇ if "brief" in tagslines 178–178returnlines 179–179prefix_re = re.compile(r"^\s*(?:/\*\*|\*/|[/*]+!?)\s?")lines 180–180summary: list[str] = []lines 181–181◇ for raw_line in block_text.splitlines()lines 182–182line = _mask_escaped_at(prefix_re.sub("", raw_line).removesuffix("*/").strip())lines 183–183◇ if not linelines 184–184◇ if summarylines 185–185breaklines 186–186continuelines 187–187◇ if _AUTOBRIEF_STOP_RE.match(line)lines 188–188breaklines 190–190segments = _split_inline_tags(line)lines 191–191summary.append(segments[0])lines 192–192◇ if len(segments) > 1lines 193–193breaklines 194–194◇ if summarylines 195–195tags["brief"] = [_unmask_escaped_at(" ".join(summary))]

Requirements

REQ-DDB-GUARD-024

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.

clew/guard/parser.py:135

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
lines 136–136tags: dict[str, list[str]] = {}lines 137–137current_tag: str | None = Nonelines 138–138current_value: list[str] = []lines 140–140prefix_re = re.compile(r"^\s*[/*#]+!?\s?")lines 141–141suffix_re = re.compile(r"\s*\*/\s*$")lines 143–143◇ for raw_line in block_text.splitlines()lines 144–144line = prefix_re.sub("", raw_line)lines 145–145line = _mask_escaped_at(suffix_re.sub("", line).strip())lines 146–146◇ for segment in _split_inline_tags(line)lines 147–147match = _TAG_RE.match(segment)lines 148–148◇ if matchlines 149–149_finalize_tag(tags, current_tag, current_value)lines 150–150current_tag = match.group(1)lines 151–151current_value = [match.group(2) or ""]lines 152–152◇ elif not segment and current_tag is not Nonelines 153–153_finalize_tag(tags, current_tag, current_value)lines 154–154current_tag = Nonelines 155–155current_value = []lines 156–156◇ elif current_tag is not None and segmentlines 157–157current_value.append(segment)lines 159–159_finalize_tag(tags, current_tag, current_value)lines 160–160return tags

Requirements

REQ-DDB-GUARD-019

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.

clew/guard/parser.py:213

Show on the map →

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

Activity · 3 steps
lines 220–220from .ts_parser import parse_functions_tslines 222–222s = settings or ParseSettings()lines 223–228return parse_functions_ts( content, lang_name, exclude_names=exclude_names, comment_start_pattern=s.comment_start, )

Requirements

REQ-DDB-GUARD-018

Flows