From 0a598c2ee3c93ab8e234dcc990ce7ffa2ebff9c1 Mon Sep 17 00:00:00 2001 From: Legotier Date: Fri, 3 Jun 2022 15:30:55 +0200 Subject: [PATCH] Make some variable names (true, false) illegal --- probably/pgcl/parser.py | 4 ++++ tests/{test_distributions.py => test_parser.py} | 10 ++++++++++ 2 files changed, 14 insertions(+) rename tests/{test_distributions.py => test_parser.py} (72%) 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