Skip to content

Commit

Permalink
Make some variable names (true, false) illegal
Browse files Browse the repository at this point in the history
  • Loading branch information
Legotier committed Jun 3, 2022
1 parent 579fdf6 commit 0a598c2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions probably/pgcl/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@
%import common.WS
"""

_illegal_variable_names = {"true", "false"}

_OPERATOR_TABLE = [[infixl("or", "||")], [infixl("and", "&")],
[infixl("leq", "<="),
infixl("le", "<"),
Expand Down Expand Up @@ -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))


Expand Down
10 changes: 10 additions & 0 deletions tests/test_distributions.py → tests/test_parser.py
Original file line number Diff line number Diff line change
@@ -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("""
Expand All @@ -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

0 comments on commit 0a598c2

Please sign in to comment.