Skip to content

Commit

Permalink
Handle more possible comment ambiguities
Browse files Browse the repository at this point in the history
  • Loading branch information
zhudotexe committed May 18, 2020
1 parent 7861af9 commit 9a246ad
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
4 changes: 3 additions & 1 deletion d20/dice.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

__all__ = ("CritType", "AdvType", "RollContext", "RollResult", "Roller")

POSSIBLE_COMMENT_AMBIGUITIES = {"k", "p", "rr", "ro", "ra", "e", "mi", "ma", "*", "d"}


class CritType(IntEnum):
"""
Expand Down Expand Up @@ -208,7 +210,7 @@ def _parse_with_comments(self, expr):
except lark.UnexpectedInput as ui:
# if the statement up to the unexpected token ends with an operator, remove that from the end
successful_fragment = expr[:ui.pos_in_stream]
for op in SetOperator.OPERATIONS:
for op in POSSIBLE_COMMENT_AMBIGUITIES:
if successful_fragment.endswith(op):
successful_fragment = successful_fragment[:-len(op)]
force_comment = expr[len(successful_fragment):]
Expand Down
1 change: 0 additions & 1 deletion d20/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,6 @@ def __repr__(self):
class SetOperator: # set_op, dice_op
"""Represents an operation on a set."""
__slots__ = ("op", "sels")
OPERATIONS = {"k", "p", "rr", "ro", "ra", "e", "mi", "ma"}

def __init__(self, op, sels):
"""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setuptools.setup(
name="d20",
version="1.0.2",
version="1.0.3",
author="Andrew Zhu",
author_email="andrew@zhu.codes",
description="A formal grammar-based dice parser and roller for D&D and other dice systems.",
Expand Down
16 changes: 16 additions & 0 deletions tests/test_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ def test_comments():
with pytest.raises(RollSyntaxError):
roll("1d20 foo bar", allow_comments=False)


def test_conflicting_comments():
# expressions with ambiguity
r = roll("1d20 keep something", allow_comments=True)
assert 1 <= r.total <= 20
Expand All @@ -20,6 +22,20 @@ def test_comments():
with pytest.raises(RollSyntaxError):
roll("1d20 keep something", allow_comments=False)

r = roll("1d20 damage", allow_comments=True)
assert 1 <= r.total <= 20
assert r.comment == "damage"

with pytest.raises(RollSyntaxError):
roll("1d20 damage", allow_comments=False)

r = roll("1d20 **bold**", allow_comments=True)
assert 1 <= r.total <= 20
assert r.comment == "**bold**"

with pytest.raises(RollSyntaxError):
roll("1d20 **bold**", allow_comments=False)


def test_advantage():
r = roll("1d20", advantage=AdvType.ADV)
Expand Down

0 comments on commit 9a246ad

Please sign in to comment.