Skip to content

Commit

Permalink
docs: add more detail
Browse files Browse the repository at this point in the history
  • Loading branch information
narenaryan committed May 5, 2024
1 parent ec4d00a commit 9bf332c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src/promptml/grammar.lark
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
?start: block+
# PromptML Grammar #

start: block+
block: prompt | var_block

var_block: "@vars" assignment* "@end"
Expand Down Expand Up @@ -34,9 +36,11 @@ metadata: "@metadata" prop* "@end"
prop: PROP_NAME ":" (NUMBER | STRING )
PROP_NAME: /[a-zA-Z_][a-zA-Z0-9_]*/

# Token Definitions #
STRING: /'[^']*'/ | /"[^"]*"/
text: /[^@]+/

# Ignored Tokens #
%import common.WS
%import common.NUMBER
%import common.STRING
Expand Down
31 changes: 24 additions & 7 deletions src/promptml/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,22 +117,32 @@ def var_block(self, items):
return var_map

def metadata(self, items):
""" Extract the metadata section content."""
"""
Extracts the metadata section content.
Args:
items (list): A list of items representing the metadata section content.
Returns:
dict: A dictionary containing the extracted metadata section content.
"""
metadata = {}

for item in items:
key = item.children[0].strip()
if key:
prop_type = item.children[1].type
value = item.children[1].strip()

if prop_type == "NUMBER":
try:
metadata[key] = int(item.children[1].strip())
value = int(value)
except ValueError:
metadata[key] = float(item.children[1].strip())
value = float(value)
elif prop_type == "STRING":
metadata[key] = item.children[1].strip().strip("\"").strip("\'")
else:
metadata[key] = item.children[1].strip()
value = value.strip("\"").strip("\'")

metadata[key] = value

return {"metadata": metadata}

Expand Down Expand Up @@ -197,7 +207,14 @@ class PromptParserFromFile(PromptParser):
"""
A subclass of PromptParser that reads DSL code from a file.
"""
def __init__(self, file_path):
def __init__(self, file_path: str):
"""
Initializes the PromptParserFromFile object by reading the DSL code from the specified file path
and passing it to the parent class constructor.
Args:
file_path (str): The path to the DSL code file.
"""
with open(file_path, 'r', encoding='utf-8') as f:
dsl_code = f.read()
super().__init__(dsl_code)

0 comments on commit 9bf332c

Please sign in to comment.