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 parsefilename
: Name to use for error reportingsearch_paths
: List of paths to search for included files
- Returns: The root of the abstract syntax tree
- Raises:
MetaphorParserError
: If there are syntax errorsFileNotFoundError
: 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 parsesearch_paths
: List of paths to search for included files
- Returns: The root of the abstract syntax tree
- Raises:
MetaphorParserError
: If there are syntax errorsFileNotFoundError
: 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
MetaphorASTNodeType
An enumeration of possible AST node types.
Values
ROOT (0)
: Root node of the ASTTEXT (1)
: Text content nodeROLE (2)
: Role definition nodeCONTEXT (3)
: Context definition nodeACTION (4)
: Action definition node
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 descriptionfilename: str
: File where error occurredline: int
: Line number of errorcolumn: int
: Column number of errorinput_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
Built Distribution
File details
Details for the file m6rclib-0.1.1.tar.gz
.
File metadata
- Download URL: m6rclib-0.1.1.tar.gz
- Upload date:
- Size: 67.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.15
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e1d375113750c2785dd4f86fd4e6213dc1601782a17d1fd23e2301b0b95d2799 |
|
MD5 | 0d6292090726fd6bcd7b6709a0e2d26d |
|
BLAKE2b-256 | 6ae1c03ee1dbaa014abceb678328db9412fbd96c7d650973a05f1909340380c6 |
File details
Details for the file m6rclib-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: m6rclib-0.1.1-py3-none-any.whl
- Upload date:
- Size: 20.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.15
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fc33029ee1383630178ab4e2a414fc0ff2347576fe51dc74ae57ae091d99f11c |
|
MD5 | 6b285057aa4c8918c784aa976531f3e1 |
|
BLAKE2b-256 | fbd801fe388b1566cdb17781a661234c246764b76715f71980f26b7f95b88e3b |