Skip to main content

An embedded parser and LLM AI prompt compiler for the Metaphor language

Project description

m6rclib API Documentation

m6rclib is a Python package that provides a parser and prompt compiler for the Metaphor language, a structured text format for defining roles, contexts, and actions.

Installation

pip install m6rclib

Basic Usage

from m6rclib import MetaphorParser

# Create a parser instance
parser = MetaphorParser()

# Parse a Metaphor string
input_text = """
Role:
    You are a helpful assistant.
Context:
    The user needs help with Python.
Action:
    Provide clear Python examples.
"""

# Parse the input with search paths for Include: directives
syntax_tree = parser.parse(input_text, "example.m6r", ["/path/to/includes"])

Core Classes

MetaphorParser

The main class for parsing Metaphor documents.

Methods

  • parse(input_text: str, filename: str, search_paths: List[str]) -> MetaphorASTNode

    • Parses a string containing Metaphor content
    • Args:
      • input_text: The Metaphor content to parse
      • filename: Name to use for error reporting
      • search_paths: List of paths to search for included files
    • Returns: The root of the abstract syntax tree
    • Raises:
      • MetaphorParserError: If there are syntax errors
      • FileNotFoundError: If a required file cannot be found
  • parse_file(filename: str, search_paths: List[str]) -> MetaphorASTNode

    • Parses a Metaphor file
    • Args:
      • filename: Path to the file to parse
      • search_paths: List of paths to search for included files
    • Returns: The root of the abstract syntax tree
    • Raises:
      • MetaphorParserError: If there are syntax errors
      • FileNotFoundError: If the file cannot be found

MetaphorASTNode

Represents a node in the abstract syntax tree (AST).

Properties

  • node_type: MetaphorASTNodeType

    • The type of the node
    • Read-only
  • value: str

    • The raw text value of the node
    • Read-only
  • parent: Optional[MetaphorASTNode]

    • The parent node, if any
    • Read-only
  • children: List[MetaphorASTNode]

    • The node's child nodes (returns a shallow copy)
    • Read-only

Methods

  • attach_child(child: MetaphorASTNode) -> None

    • Adds a child node to this node
    • Args:
      • child: The node to attach as a child
  • detach_child(child: MetaphorASTNode) -> None

    • Removes a child node from this node
    • Args:
      • child: The node to detach
    • Raises:
      • ValueError: If the node is not a child of this node
  • get_children_of_type(self, node_type: MetaphorASTNodeType) -> List['MetaphorASTNode']

    • Returns a list of all immediate children that match the specified node type.
    • Args:
      • node_type (MetaphorASTNodeType): The type of nodes to filter for
    • Returns:List of child nodes matching the specified type

MetaphorASTNodeType

An enumeration of possible AST node types.

Values

  • ROOT (0): Root node of the AST
  • TEXT (1): Text content node
  • ROLE (2): Role definition node
  • CONTEXT (3): Context definition node
  • ACTION (4): Action definition node

Formatting Functions

Helper functions for formatting nodes and errors.

  • format_ast(node: MetaphorASTNode) -> str

    • Format an AST node and its children as a string
    • Args:
      • node: The root node to format
    • Returns: Formatted string representation of the AST
  • format_errors(errors: List[MetaphorParserSyntaxError]) -> str

    • Format a list of syntax errors as a string
    • Args:
      • errors: List of syntax errors to format
    • Returns: Formatted error string with each error on separate lines

Exceptions

MetaphorParserError

Main exception wrapper for parser errors.

  • Attributes:
    • errors: List[MetaphorParserSyntaxError]: List of syntax errors encountered

MetaphorParserSyntaxError

Detailed syntax error information.

  • Attributes:
    • message: str: Error description
    • filename: str: File where error occurred
    • line: int: Line number of error
    • column: int: Column number of error
    • input_text: str: Input line containing error

File Format

Metaphor files use the following format:

Role:
    Description of the role
    Additional role details

Context:
    Description of the context
    Context details
    Context: Subsection
        More detailed context information

Action:
    Description of the action
    Step-by-step actions to take

Special Directives

  • Include: filename

    • Includes another Metaphor file
    • File is searched for in the provided search paths
  • Embed: pattern

    • Embeds the contents of matching files
    • Supports glob patterns
    • Use **/ for recursive matching

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

m6rclib-0.4.1.tar.gz (80.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

m6rclib-0.4.1-py3-none-any.whl (23.4 kB view details)

Uploaded Python 3

File details

Details for the file m6rclib-0.4.1.tar.gz.

File metadata

  • Download URL: m6rclib-0.4.1.tar.gz
  • Upload date:
  • Size: 80.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.15

File hashes

Hashes for m6rclib-0.4.1.tar.gz
Algorithm Hash digest
SHA256 6b1bde3fcee954f4b382cbfc7455a8c3ac830f9952b74f59a54a9afd2f6450e0
MD5 b739cb140370ca130935a0fef44d20e1
BLAKE2b-256 d586f1d949c3cf52a9a74c45a6ca5f0565e1eb6c5845a444e08c2120cd9b4160

See more details on using hashes here.

File details

Details for the file m6rclib-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: m6rclib-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 23.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.15

File hashes

Hashes for m6rclib-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5b58d16f1e764a58ff23b0f2f16dfd55a462a048eb1ebf2489c96551baf443a6
MD5 5795b88f73314835ab3d723a91f11254
BLAKE2b-256 0b799f59920dedfdd68c50e22faea8f7dcf468d1339abb9e9162f8ce0e90dfd2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page