Skip to content

Commit

Permalink
Refactor grammar test into a function.
Browse files Browse the repository at this point in the history
  • Loading branch information
lemmy committed Feb 21, 2024
1 parent 709edc6 commit 9c7ca20
Showing 1 changed file with 39 additions and 40 deletions.
79 changes: 39 additions & 40 deletions controllers/pyctrl/samples/apalache.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,43 +81,42 @@

async def test_grammar():
await aici.FixedTokens("Start")

aici.test(test_grammar())


## define a list of test inputs
inputs = [
"Int",
"Int",
"Str",
"(Int)",
"Bool",
"Int -> Int",
"Set(Int)",
"Seq(Int)",
"Set(Int) -> Set(Int)",
"Set(<<Int, Int>> -> Int)",
"<<Int,Int>>",
"(Int,Int) => Int",
"(Int,Bool) => Int",
"((Int,Bool) => Int) => Bool",
"$aalias",
]

## loop over the inputs and test the grammar
for input in inputs:
print(f"Testing input: {input}")
tokens = aici.tokenize(input)
# print(tokens)
constr = aici.CfgConstraint(apalache)
outp = []
for t in tokens:
if not constr.token_allowed(t):
## Abort/terminate if token is not allowed.
print(f"Token {t} not allowed")
print("OK: " + repr(aici.detokenize(outp).decode("utf-8", "ignore")))
print("fail: " + repr(aici.detokenize([t]).decode("utf-8", "ignore")))
sys.exit(1)
outp.append(t)
constr.append_token(t)
# print(f"EOS allowed: {constr.eos_allowed()}")
## define a list of test inputs
inputs = [
"Int",
"Int",
"Str",
"(Int)",
"Bool",
"Int -> Int",
"Set(Int)",
"Seq(Int)",
"Set(Int) -> Set(Int)",
"Set(<<Int, Int>> -> Int)",
"<<Int,Int>>",
"(Int,Int) => Int",
"(Int,Bool) => Int",
"((Int,Bool) => Int) => Bool"
# "$aalias",
# "Variant(va)"
]

## loop over the inputs and test the grammar
for input in inputs:
print(f"Testing input: {input}")
tokens = aici.tokenize(input)
# print(tokens)
constr = aici.CfgConstraint(apalache)
outp = []
for t in tokens:
if not constr.token_allowed(t):
## Abort/terminate if token is not allowed.
print(f"Token {t} not allowed")
print("OK: " + repr(aici.detokenize(outp).decode("utf-8", "ignore")))
print("fail: " + repr(aici.detokenize([t]).decode("utf-8", "ignore")))
break
outp.append(t)
constr.append_token(t)
print(f"EOS allowed: {constr.eos_allowed()}")

aici.test(test_grammar())

0 comments on commit 9c7ca20

Please sign in to comment.