tokens

Utilities for working with tokens.

class macro_polo.tokens.Delimiter(open_type, open_string, close_type, close_string)[source]

Represents a delimiter which must be kept balanced.

Parameters:
  • open_type (int)

  • open_string (str | None)

  • close_type (int)

  • close_string (str | None)

close_string: str | None

String representation of the closing token.

If None, only close_type is checked.

close_type: int

Type code of the closing token.

static from_token(token)[source]

Attempt to create a Delimiter from a Token.

Parameters:

token (Token)

Return type:

Delimiter | None

matches_close(token)[source]

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

Parameters:

token (Token)

Return type:

bool

matches_open(token)[source]

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

Parameters:

token (Token)

Return type:

bool

open_string: str | None

String representation of the opening token.

If None, only open_type is checked.

open_type: int

Type code of the opening token.

class macro_polo.tokens.Token(type, string)[source]

Minimal representation of a token.

Parameters:
string: str

The string representation of the token.

type: int

The token’s type code.

class macro_polo.tokens.TokenTree(*args)[source]

A delimited sequence of tokens.

Parameters:

args (Token)

Return type:

Self

macro_polo.tokens.desimplify(tokens, *, indent='    ')[source]

Revert simplifications made by lex() and fix indentation.

Only reverts simplifications that change semantics.

Parameters:
Return type:

Iterator[Token]

macro_polo.tokens.lex(source)[source]

Create a simplified token stream from source code.

Some simplifications are applied to make matching easier:

Parameters:

source (str)

Return type:

Iterator[Token]

macro_polo.tokens.stringify(tokens)[source]

Construct source code from a token stream.

Parameters:

tokens (Iterable[Token])

Return type:

str