Module

parsing.inline.match_registry

Match registry for emphasis delimiter tracking.

Decouples match state from token objects, enabling immutable tokens. This is a prerequisite for using NamedTuples as inline tokens.

Thread Safety:

MatchRegistry instances are single-use per _parse_inline() call. All state is instance-local; no shared mutable state.

Classes

DelimiterMatch 3
Record of a matched opener-closer pair.

Record of a matched opener-closer pair.

Attributes

Name Type Description
opener_idx int

Index of the opener token in the token list.

closer_idx int

Index of the closer token in the token list.

match_count int

Number of delimiters matched (1 for emphasis, 2 for strong).

MatchRegistry 10
External tracking for delimiter matches. Decouples match state from token objects, enabling immuta…

External tracking for delimiter matches.

Decouples match state from token objects, enabling immutable tokens. All delimiter matching state is tracked here instead of mutating tokens.

Usage:

registry = MatchRegistry()
registry.record_match(opener_idx=0, closer_idx=5, count=2)
if registry.is_active(3):
        ...

Complexity:

  • record_match(): O(1)
  • is_active(): O(1)
  • deactivate(): O(1)
  • remaining_count(): O(1)
  • get_matches_for_opener(): O(1) amortized

Attributes

Name Type Description
matches list[DelimiterMatch]
consumed dict[int, int]
deactivated set[int]
_opener_matches_cache dict[int, list[DelimiterMatch]]

Methods

record_match 3
Record a delimiter match.
def record_match(self, opener_idx: int, closer_idx: int, count: int) -> None
Parameters
Name Type Description
opener_idx

Index of the opening delimiter token.

closer_idx

Index of the closing delimiter token.

count

Number of delimiters matched (1 or 2).

is_active 1 bool
Check if delimiter at idx is still active.
def is_active(self, idx: int) -> bool
Parameters
Name Type Description
idx

Token index to check.

Returns
bool True if the delimiter is still active (not deactivated).
deactivate 1
Mark delimiter as inactive.
def deactivate(self, idx: int) -> None
Parameters
Name Type Description
idx

Token index to deactivate.

remaining_count 2 int
Get remaining delimiter count after matches.
def remaining_count(self, idx: int, original_count: int) -> int
Parameters
Name Type Description
idx

Token index to check.

original_count

Original delimiter count for this token.

Returns
int Number of remaining unused delimiters.
get_match_for_opener 1 DelimiterMatch | None
Get first match record where idx is the opener. For nested emphasis (***text**…
def get_match_for_opener(self, idx: int) -> DelimiterMatch | None

Get first match record where idx is the opener.

For nested emphasis (text), use get_matches_for_opener() instead.

Parameters
Name Type Description
idx

Token index to check.

Returns
DelimiterMatch | None First DelimiterMatch if this token is an opener, None otherwise.
get_matches_for_opener 1 list[DelimiterMatch]
Get all match records where idx is the opener. Used for nested emphasis (e.g.,…
def get_matches_for_opener(self, idx: int) -> list[DelimiterMatch]

Get all match records where idx is the opener.

Used for nested emphasis (e.g., text has 2 matches: strong and emphasis).

Parameters
Name Type Description
idx

Token index to check.

Returns
list[DelimiterMatch] List of DelimiterMatch records, empty if not an opener.