diff --git a/probably/pgcl/parser.py b/probably/pgcl/parser.py index 7f05a0e..59fdd86 100644 --- a/probably/pgcl/parser.py +++ b/probably/pgcl/parser.py @@ -99,6 +99,8 @@ %import common.WS """ +_illegal_variable_names = {"true", "false"} + _OPERATOR_TABLE = [[infixl("or", "||")], [infixl("and", "&")], [infixl("leq", "<="), infixl("le", "<"), @@ -183,6 +185,8 @@ def _child_str(t: Tree, index: int) -> str: def _parse_var(t: Tree) -> Var: assert t.data == 'var' assert t.children[0].type == 'CNAME' # type: ignore + if t.children[0] in _illegal_variable_names: + raise SyntaxError(f"Illegal variable name: {t.children[0]}") return str(_child_str(t, 0)) diff --git a/tests/test_distributions.py b/tests/test_parser.py similarity index 72% rename from tests/test_distributions.py rename to tests/test_parser.py index 2e2e8ce..b954ec0 100644 --- a/tests/test_distributions.py +++ b/tests/test_parser.py @@ -1,5 +1,6 @@ from probably.pgcl.parser import parse_pgcl from probably.pgcl.ast import * +from pytest import raises def test_all_distributions(): program = parse_pgcl(""" @@ -23,3 +24,12 @@ def test_all_distributions(): LogDistExpr(NatLitExpr(1)), BernoulliExpr(NatLitExpr(1)), BinomialExpr(NatLitExpr(1), NatLitExpr(2))] + +def test_illegal_variable_names(): + illegal_names = {"true", "false"} + for name in illegal_names: + with raises(SyntaxError) as e: + parse_pgcl(f""" + nat {name} + """) + assert "Illegal variable name: " in e.value.msg