macro_polo

Rust-style macros for Python.

class macro_polo.Delimiter

Represents a delimiter which must be kept balanced.

__init__(open_type, open_string, close_type, close_string)
Parameters:
  • open_type (int)

  • open_string (str | None)

  • close_type (int)

  • close_string (str | None)

Return type:

None

static from_token(token)

Attempt to create a Delimiter from a Token.

Parameters:

token (Token)

Return type:

Delimiter | None

matches_close(token)

Check if the given token matches the delimiter’s close token.

Parameters:

token (Token)

Return type:

bool

matches_open(token)

Check if the given token matches the delimiter’s open token.

Parameters:

token (Token)

Return type:

bool

exception macro_polo.MacroError

Base class for macro-processing errors.

class macro_polo.Token

Minimal representation of a token.

static __new__(_cls, type, string)

Create new instance of Token(type, string)

Parameters:
string: str

Alias for field number 1

type: int

Alias for field number 0

class macro_polo.TokenTree

A delimited sequence of tokens.

exception macro_polo.TranscriptionError

Exception raised for macro transcription errors.

macro_polo.lex(source)

Create a simplified token stream from source code.

Some simplifications are applied to make matching easier:
  • Semantically innert tokens, such as NL and COMMENT, are stripped.

  • NEWLINE-INDENT and NEWLINE-DEDENT pairs are reduced to INDENT and DEDENT, respectively. (This is reversed by :func:desimplify.)

  • INDENT and NEWLINE tokens’ strings are normalized.

  • The trailing NEWLINE and ENDMARKER are stripped.

Parameters:

source (str)

Return type:

Iterator[Token]

macro_polo.stringify(tokens)

Construct source code from a token stream.

Parameters:

tokens (Iterable[Token])

Return type:

str