Skip to content

Commit

Permalink
Merge pull request #1 from avrae/better-comments
Browse files Browse the repository at this point in the history
be explicit about where whitespace is allowed
  • Loading branch information
zhudotexe committed Jul 9, 2020
2 parents 9a246ad + dced763 commit bff2889
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion d20/dice.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

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

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


class CritType(IntEnum):
Expand Down
2 changes: 1 addition & 1 deletion d20/diceast.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def __init__(self, value, *annotations):
"""
super().__init__()
self.value = value
self.annotations = [str(a) for a in annotations]
self.annotations = [str(a).strip() for a in annotations]

@property
def children(self):
Expand Down
21 changes: 11 additions & 10 deletions d20/grammar.lark
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@ commented_expr: num COMMENT?
// ^ starting node for commented rolls

// comments are given -1 priority - only match comment if no other possibilities
COMMENT.-1: /.+/
COMMENT.-1: _WS? /.+/

// math and operators, PMDAS
?num: comparison

?comparison: (comparison COMP_OPERATOR)? a_num
?comparison: (comparison COMP_OPERATOR _WS?)? a_num _WS?
COMP_OPERATOR: "==" | ">=" | "<=" | "!=" | "<" | ">"

?a_num: (a_num A_OP)? m_num
?a_num: (a_num A_OP _WS?)? m_num _WS?
A_OP: "+" | "-"

?m_num: (m_num M_OP)? u_num
?m_num: (m_num M_OP _WS?)? u_num _WS?
M_OP: "*" | "//" | "/" | "%"

?u_num: numexpr | U_OP u_num
?u_num: numexpr | U_OP _WS? u_num
U_OP: "+" | "-"

// numbers
?numexpr: (dice | set | literal) ANNOTATION*
?numexpr: (dice | set | literal) _WS? ANNOTATION*

ANNOTATION: /\[.*?\]/
ANNOTATION: /\[.*?\]/ _WS?

literal: INTEGER | DECIMAL

Expand All @@ -35,7 +35,7 @@ literal: INTEGER | DECIMAL
set_op: SET_OPERATOR selector
SET_OPERATOR: "k" | "p"

setexpr: "(" (num ("," num)* comma?)? ")"
setexpr: "(" _WS? (num (_WS? "," _WS? num)* _WS? comma? _WS?)? _WS? ")"
comma: ","

// dice
Expand All @@ -50,8 +50,9 @@ selector: [SELTYPE] INTEGER

SELTYPE: "l" | "h" | "<" | ">"

// whitespace
_WS: /[ \t\f\r\n]/+

// useful constants
%import common.WS_INLINE
%import common.INT -> INTEGER
%import common.DECIMAL
%ignore WS_INLINE
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
cachetools>=3.1.0
lark-parser~=0.8.0
lark-parser~=0.9.0
7 changes: 7 additions & 0 deletions tests/test_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ def test_conflicting_comments():
with pytest.raises(RollSyntaxError):
roll("1d20 **bold**", allow_comments=False)

r = roll("1d20 please save me from this parsing weirdness", allow_comments=True)
assert 1 <= r.total <= 20
assert r.comment == "please save me from this parsing weirdness"

with pytest.raises(RollSyntaxError):
roll("1d20 please save me from this parsing weirdness", allow_comments=False)


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

0 comments on commit bff2889

Please sign in to comment.