Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
klntsky committed Nov 15, 2024
1 parent 31d712f commit aebf2ab
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 92 deletions.
9 changes: 7 additions & 2 deletions python/src/parse_metaprompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ def __init__(self):
def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e):
raise Exception(f"Syntax error at line {line}:{column} - {msg}")

_pattern = r'\\([\[\\])'

_pattern = r"\\([\[\\])"


def _process_escaping(string):
return re.sub(_pattern, lambda match: match.group(1), string)


class MetaPromptASTBuilder(MetaPromptVisitor):
# TODO: consider joining text pieces
# see test_text_1 for an example.
Expand Down Expand Up @@ -137,7 +140,9 @@ def visitMeta_body(self, ctx: MetaPromptParser.Meta_bodyContext):
def visitText(self, ctx: MetaPromptParser.TextContext):
# text: CHAR+
# Collect all CHAR tokens
text = "".join([_process_escaping(child.getText()) for child in ctx.CHAR()])
text = "".join(
[_process_escaping(child.getText()) for child in ctx.CHAR()]
)
return {"type": "text", "text": text}


Expand Down
5 changes: 2 additions & 3 deletions python/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ def build(c):

@task
def check(c):
c.run("black --check *.py src/*.py")
c.run("black --check *.py src/*.py tests/*.py")


@task
def format(c):
c.run("black *.py")
c.run("black src/*.py")
c.run("black *.py src/*.py tests/*.py")


@task
Expand Down
18 changes: 4 additions & 14 deletions python/tests/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,20 @@
from metaprompt import metaprompt
from config import Config


@pytest.mark.asyncio
async def test_parameters():
prompt = "[:foo][:foo]"
await metaprompt(
prompt,
Config(
parameters = {
'foo': 'bar'
}
)
) == "barbar"
await metaprompt(prompt, Config(parameters={"foo": "bar"})) == "barbar"


@pytest.mark.asyncio
async def test_assign1():
prompt = "[:foo=bar][:foo][:foo]"
await metaprompt(
prompt
) == "barbar"
await metaprompt(prompt) == "barbar"


@pytest.mark.asyncio
async def test_assign2():
prompt = "[:foo=bar][:foo=[:foo][:foo]][:foo]"
await metaprompt(
prompt
) == "barbar"
await metaprompt(prompt) == "barbar"
14 changes: 8 additions & 6 deletions python/tests/test_loader.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
from loader import extract_variables
from parse_metaprompt import parse_metaprompt, extract_tokens


def test_extractor_1():
prompt="""
prompt = """
[:foo]
"""
assert extract_variables(parse_metaprompt(prompt)) == set(["foo"])


def test_extractor_2():
prompt="""
prompt = """
[:foo][:bar]
"""
assert extract_variables(parse_metaprompt(prompt)) == set(["foo","bar"])
assert extract_variables(parse_metaprompt(prompt)) == set(["foo", "bar"])


def test_extractor_assign_1():
prompt="""
prompt = """
[:foo=baz][:bar]
"""
assert extract_variables(parse_metaprompt(prompt)) == set(["bar"])


def test_extractor_assign_2():
prompt="""
prompt = """
[:foo=baz][:foo]
"""
assert extract_variables(parse_metaprompt(prompt)) == set()


def test_extractor_assign_3():
prompt="""
prompt = """
[:foo][:foo=baz] - first used, then assigned
"""
assert extract_variables(parse_metaprompt(prompt)) == set(["foo"])
130 changes: 63 additions & 67 deletions python/tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ def t(text):
def comment(exprs):
return {"type": "comment", "exprs": exprs}


def if_then(c, then_):
return if_then_else(c, then_, [])


def if_then_else(c, then_, else_):
return {
"type": "if_then_else",
Expand All @@ -21,17 +23,12 @@ def if_then_else(c, then_, else_):


def meta(exprs):
return {
'type': 'meta',
'exprs': exprs
}
return {"type": "meta", "exprs": exprs}


def use(module_name, parameters):
return {
'type': 'use',
'module_name': module_name,
'parameters': parameters
}
return {"type": "use", "module_name": module_name, "parameters": parameters}


def test_empty():
result = parse_metaprompt("")
Expand All @@ -42,38 +39,47 @@ def test_text_0():
result = parse_metaprompt("asd")
assert result["exprs"] == [{"text": "asd", "type": "text"}]


def test_text_1():
result = parse_metaprompt("$$")
assert result["exprs"] == [t("$"), t("$")]


def test_text_2():
result = parse_metaprompt("$=")
assert result["exprs"] == [t("$"), t("=")]


def test_escaping():
result = parse_metaprompt("\\")
assert result["exprs"] == [t("\\")]


def test_escaping_1():
result = parse_metaprompt("\\[")
assert result["exprs"] == [t("[")]


def test_escaping_2():
result = parse_metaprompt("\\[:foo]")
assert result["exprs"] == [t("["), t(":foo"), t("]")]


def test_escaping_3():
result = parse_metaprompt("\\[:foo")
assert result["exprs"] == [t("["), t(":foo")]


def test_escaping_4():
result = parse_metaprompt("\\\\[")
assert result["exprs"] == [t("\\"), t("[")]


def test_escaping_5():
result = parse_metaprompt("\\ \\")
assert result["exprs"] == [t("\\ \\")]


def test_comment():
result = parse_metaprompt("[# asd ]")
assert result["exprs"] == [comment([{"text": " asd ", "type": "text"}])]
Expand Down Expand Up @@ -140,7 +146,6 @@ def test_dummy_meta2():
assert result["exprs"] == [t("["), t("["), t("]"), t("]")]



def test_dummy_meta3():
result = parse_metaprompt("[a")
assert result["exprs"] == [t("["), t("a")]
Expand All @@ -153,104 +158,95 @@ def test_dummy_meta2():

def test_meta_dollar():
result = parse_metaprompt("[$ foo]")
assert result["exprs"] == [{'type': 'meta', 'exprs': [ {'type': 'text', 'text': " foo"}]}]
assert result["exprs"] == [
{"type": "meta", "exprs": [{"type": "text", "text": " foo"}]}
]


def test_meta_dollar2():
result = parse_metaprompt("[$ foo][$ foo]")
assert result["exprs"] == [
{'type': 'meta', 'exprs': [ {'type': 'text', 'text': " foo"}]},
{'type': 'meta', 'exprs': [ {'type': 'text', 'text': " foo"}]},
{"type": "meta", "exprs": [{"type": "text", "text": " foo"}]},
{"type": "meta", "exprs": [{"type": "text", "text": " foo"}]},
]


def test_meta_dollar2():
result = parse_metaprompt("[$ foo]]")
assert result["exprs"] == [
{'type': 'meta', 'exprs': [ {'type': 'text', 'text': " foo"}]},
t("]")
{"type": "meta", "exprs": [{"type": "text", "text": " foo"}]},
t("]"),
]


def test_assign():
result = parse_metaprompt("[:foo=bar]")
assert result["exprs"] == [{'type': 'assign', 'name': 'foo', 'exprs': [ {'type': 'text', 'text': "bar"}]}]
assert result["exprs"] == [
{
"type": "assign",
"name": "foo",
"exprs": [{"type": "text", "text": "bar"}],
}
]


def test_assign_trailing_bracket():
result = parse_metaprompt("[:foo=bar]]")
assert result["exprs"] == [
{'type': 'assign', 'name': 'foo',
'exprs': [ {'type': 'text', 'text': "bar"}]
},
t(']')
{
"type": "assign",
"name": "foo",
"exprs": [{"type": "text", "text": "bar"}],
},
t("]"),
]


def test_assign_trailing_bracket():
result = parse_metaprompt("[:foo=[$ hi ]]")
assert result["exprs"] == [
{
'type': 'assign', 'name': 'foo',
'exprs': [
meta(
[ t(" hi ") ]
)
]
}
{"type": "assign", "name": "foo", "exprs": [meta([t(" hi ")])]}
]


def test_use_1():
result = parse_metaprompt("[:use foo]")
assert result["exprs"] == [
{
'type': 'use',
'module_name': 'foo',
'parameters': {}
}
{"type": "use", "module_name": "foo", "parameters": {}}
]


def test_use_2():
result = parse_metaprompt("[:use foo :asd=hey :foo=bar]")
assert result["exprs"] == [
{
'type': 'use',
'module_name': 'foo',
'parameters': {
'asd': [ { 'type': 'text', 'text': 'hey ' } ],
'foo': [ { 'type': 'text', 'text': 'bar' } ]
}
"type": "use",
"module_name": "foo",
"parameters": {
"asd": [{"type": "text", "text": "hey "}],
"foo": [{"type": "text", "text": "bar"}],
},
}
]


def test_use_3():
result = parse_metaprompt("[:use\nfoo\n]")
assert result["exprs"] == [
{
'type': 'use',
'module_name': 'foo',
'parameters': {}
}
{"type": "use", "module_name": "foo", "parameters": {}}
]


def test_use_nested():
result = parse_metaprompt(
"[:use foo :asd=[:use bar] hiii :foo=bar]"
)
result = parse_metaprompt("[:use foo :asd=[:use bar] hiii :foo=bar]")
assert result["exprs"] == [
{
'type': 'use',
'module_name': 'foo',
'parameters': {
'asd': [
use('bar', {}),
{ 'type': 'text', 'text': ' hiii ' }
],
'foo': [ { 'type': 'text', 'text': 'bar' } ]
}
"type": "use",
"module_name": "foo",
"parameters": {
"asd": [use("bar", {}), {"type": "text", "text": " hiii "}],
"foo": [{"type": "text", "text": "bar"}],
},
}
]

Expand All @@ -261,17 +257,17 @@ def test_use_nested_2():
)
assert result["exprs"] == [
{
'type': 'use',
'module_name': 'foo',
'parameters': {
'asd': [
t('['),
t('hiiii '),
use('bar', {'qux': [t('asd')]}),
t(']'),
{ 'type': 'text', 'text': ' hiii ' },
"type": "use",
"module_name": "foo",
"parameters": {
"asd": [
t("["),
t("hiiii "),
use("bar", {"qux": [t("asd")]}),
t("]"),
{"type": "text", "text": " hiii "},
],
'foo': [ { 'type': 'text', 'text': 'bar' } ]
}
"foo": [{"type": "text", "text": "bar"}],
},
}
]

0 comments on commit aebf2ab

Please sign in to comment.