macros

Submodules

High-level macro utilities.

class macro_polo.macros.types.Macro(*args, **kwargs)[source]

Transforms a token sequence.

class macro_polo.macros.types.ParameterizedMacro(*args, **kwargs)[source]

Macro that takes additional parameters.

class macro_polo.macros.types.PartialMatchMacro(*args, **kwargs)[source]

Transforms the beginning of a token sequence.

class macro_polo.macros.super.LoopingMacro(*args)[source]

A super-macro that repeatedely applies its inner macros until none match.

Parameters:

args (Macro)

Return type:

Self

class macro_polo.macros.super.MultiMacro(*args)[source]

A super-macro that applies each of its inner macros in sequence.

Parameters:

args (Macro)

Return type:

Self

class macro_polo.macros.super.ScanningMacro(*args)[source]

A super-macro that scans input and applies its inner macros as they match.

This macro will only perform a single pass on the input. It can be combined with LoopingMacro to recursively expand macros.

Parameters:

args (PartialMatchMacro)

Return type:

Self

class macro_polo.macros.importer.ImporterMacro(function_macros=<factory>, module_macros=<factory>, decorator_macros=<factory>)[source]

Imports macros from other modules.

This macro expects its parameters to be in one of two forms: 1. module_name 2. macro_name1, macro_name2, ... from module_name

In the first case all macros from the target module will be imported.

Imported macros are added to function_macros, module_macros, and decorator_macros as appropriate.

Parameters:
decorator_macros: dict[str, ParameterizedMacro]

Imported decorator macros will be added to this dict.

It may be shared with other macros, such as a DecoratorMacroInvokerMacro.

function_macros: dict[str, Macro]

Imported function macros will be added to this dict.

It may be shared with other macros, such as a FunctionMacroInvokerMacro.

module_macros: dict[str, ParameterizedMacro]

Imported module macros will be added to this dict.

It may be shared with other macros, such as a ModuleMacroInvokerMacro.

class macro_polo.macros.function.FunctionMacroInvokerMacro(macros=<factory>)[source]

A macro that processes function-like macro invocations.

The syntax for invoking a function-style macro is:

macro_name!(input tokens)

or

macro_name![input tokens]

or

macro_name!{input tokens}

or

macro_name!:
    input
    tokens

Important

Due to the way Python’s tokenizer works, indentation and newlines are only preserved by the last (block) style.

When invoked, the registered macro is called with a single argument, the token sequence passed as input.

Macros are defined by macros (which can be updated after this class is instantiated).

Parameters:

macros (Mapping[str, Macro])

macros: Mapping[str, Macro]

A mapping of names to function macros.

When a function macro is invoked, its name is looked up here.

This mapping may be shared with other macros, such as a ImporterMacro.

class macro_polo.macros.macro_rules.MacroRule(matcher, transcriber)[source]

A macro matcher/macro transcriber pair.

Parameters:
matcher: MacroMatcher

The rule’s matcher

transcriber: MacroTranscriber

The rule’s transcriber

class macro_polo.macros.macro_rules.MacroRules(*args)[source]

A sequence of MacroRules.

Parameters:

args (MacroRule)

Return type:

Self

class macro_polo.macros.macro_rules.MacroRulesParserMacro(macros=<factory>)[source]

A macro that parses macro_rules macro definitions.

Parsed macros are added to macros.

Parameters:

macros (dict[str, Macro])

macros: dict[str, Macro]

Parsed macro_rules macros will be added to this dict.

It may be shared with other macros, such as a FunctionMacroInvokerMacro.

class macro_polo.macros.module.ModuleMacroError(name, parameters, msg)[source]

Errors during invocation of module-level macros.

Parameters:
class macro_polo.macros.module.ModuleMacroInvokerMacro(macros=<factory>)[source]

A macro that processes module-level macro invocations.

The syntax for invoking a module-level macro is ![name(parameters)] or ![name] (equivalent to ![name()]).

Module-level macro invocations must come before all other code (with the exception of a docstring), and must each appear on their own line.

When invoked, the registered macro is called with two arguments:

  1. parameters (as a token sequence)

  2. the remainder of the module starting from the line immediately following the

    invocation (as a token sequence).

Macros are defined by macros (which can be updated after this class is instantiated).

Parameters:

macros (Mapping[str, ParameterizedMacro])

macros: Mapping[str, ParameterizedMacro]

A mapping of names to module macros.

When a module macro is invoked, its name is looked up here.

This mapping may be shared with other macros, such as a ImporterMacro.

class macro_polo.macros.decorator.DecoratorMacroError(name, parameters, msg)[source]

Error during invocation of decorator-style macros.

Parameters:
class macro_polo.macros.decorator.DecoratorMacroInvokerMacro(macros=<factory>)[source]

A macro that processes decorator-style macro invocations.

The syntax for invoking a decorator-style macro is @![name(parameters)] or @![name] (equivalent to @![name()]).

Decorator-style macro invocations must immediately precede a “block”, defined as either a single newline-terminated line, or a line followed by an indented block.

When invoked, the registered macro is called with two arguments:

  1. parameters (as a token sequence)

  2. the block immediately following the invocation (as a token sequence).

When multiple decorator-style macros are stacked, they are invoked from bottom to top.

Macros are defined by macros (which can be updated after this class is instantiated).

Parameters:

macros (Mapping[str, ParameterizedMacro])

macros: Mapping[str, ParameterizedMacro]

A mapping of names to decorator macros.

When a decorator macro is invoked, its name is looked up here.

This mapping may be shared with other macros, such as a ImporterMacro.