match

Macro input pattern matching utilities.

class macro_polo.match.DelimitedMacroMatcher

A delimited macro match pattern.

__init__(delimiter, matcher)
Parameters:
Return type:

None

delimiter: Delimiter

The delimiter to match.

match(tokens)

Attempt to match against a token sequence.

Parameters:

tokens (Sequence[Token])

Return type:

MacroMatch | None

matcher: MacroMatcher

The inner matcher.

class macro_polo.match.MacroMatch

Result of a successful macro match.

__init__(size, captures)
Parameters:
  • size (int)

  • captures (MacroMatchCaptures)

Return type:

None

captures: MacroMatchCaptures

Captured tokens.

size: int

Number of tokens matched.

exception macro_polo.match.MacroMatchError

Exception raised for macro matching errors.

class macro_polo.match.MacroMatcher

A macro match pattern.

full_match(tokens)

Attempt to match against an entire token sequence.

Parameters:

tokens (Sequence[Token])

Return type:

MacroMatch | None

match(tokens)

Attempt to match against a token sequence.

Parameters:

tokens (Sequence[Token])

Return type:

MacroMatch | None

class macro_polo.match.MacroMatcherEmptyCapture

An empty macro capture.

Preserves nesting depth information, primarily to enable better transcription error messages.

__init__(depth)
Parameters:

depth (int)

Return type:

None

depth: int

Repeater nesting depth.

class macro_polo.match.MacroMatcherNegativeLookahead

A negative lookahead macro match.

Matches zero tokens only if MacroMatcher would fail to match, and fails to match otherwise.

match(tokens)

Attempt to match against a token sequence.

Parameters:

tokens (Sequence[Token])

Return type:

MacroMatch | None

class macro_polo.match.MacroMatcherRepeater

A repeated sub-matcher.

__init__(matcher, mode, sep=None)
Parameters:
Return type:

None

property base_captures: Mapping[str, MacroMatcherEmptyCapture]

Get a set of empty captures for this matcher.

This is used to provide empty capture lists for matchers that match zero times, allowing transcribers to handle empty captures properly.

match(tokens)

Attempt to match against a token sequence.

Parameters:

tokens (Sequence[Token])

Return type:

MacroMatch | None

matcher: MacroMatcher

The matcher to repeat.

mode: MacroMatcherRepeaterMode

The repitition mode.

sep: Token | None

An optional separator token.

class macro_polo.match.MacroMatcherRepeaterMode

Matcher repeat mode.

ONE_OR_MORE = '+'

Match ≥1 times.

ZERO_OR_MORE = '*'

Match ≥0 times.

ZERO_OR_ONE = '?'

Match ≤1 times.

class macro_polo.match.MacroMatcherUnion

A union of macro matchers.

The first sub-matcher to match is used.

static __new__(cls, *args)

Create a new MacroMatcherUnion.

match(tokens)

Attempt to match against a token sequence.

Parameters:

tokens (Sequence[Token])

Return type:

MacroMatch | None

class macro_polo.match.MacroMatcherVar

A capture-variable in a macro matcher.

__init__(name, type)
Parameters:
Return type:

None

match(tokens)

Attempt to match against a token sequence.

Parameters:

tokens (Sequence[Token])

Return type:

MacroMatch | None

name: str

The name to bind captured tokens to.

type: MacroMatcherVarType

The type of token(s) to match.

class macro_polo.match.MacroMatcherVarType

Capture-variable type.

NAME = 'name'

Any token.NAME token.

NULL = 'null'

Always matches, capturing an empty TokenTree

NUMBER = 'number'

Any token.NUMBER token.

OP = 'op'

Any non-delimeter token.OP token.

STRING = 'string'

Any token.STRING token.

TOKEN = 'token'

Any non-delimiter token.

TOKEN_TREE = 'tt'

Any non-delimiter token or a delimited sequence of tokens.