Skip to content

Commit 79d5b8b

Browse files
committed
First test at parsing dice
1 parent 422ac51 commit 79d5b8b

3 files changed

Lines changed: 47 additions & 2 deletions

File tree

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,92 @@
11
# Generated from DiceNotation.g4 by ANTLR 4.7.2
22
from antlr4 import *
33

4+
import logging
5+
from dice_notation.dice import Dice
6+
47
# This class defines a complete listener for a parse tree produced by DiceNotationParser.
58
class DiceNotationListener(ParseTreeListener):
69

10+
def __init__(self):
11+
super(DiceNotationListener, self).__init__()
12+
self._logger = logging.getLogger("DiceNotationListener")
13+
self._nodes = []
14+
715
# Enter a parse tree produced by DiceNotationParser#notation.
816
def enterNotation(self, ctx):
17+
self._logger.warning("Entering notation %s", ctx.getText())
918
pass
1019

1120
# Exit a parse tree produced by DiceNotationParser#notation.
1221
def exitNotation(self, ctx):
22+
self._logger.warning("Exiting notation %s", ctx.getText())
1323
pass
1424

1525

1626
# Enter a parse tree produced by DiceNotationParser#addOp.
1727
def enterAddOp(self, ctx):
28+
self._logger.warning("Entering add %s", ctx.getText())
1829
pass
1930

2031
# Exit a parse tree produced by DiceNotationParser#addOp.
2132
def exitAddOp(self, ctx):
33+
self._logger.warning("Exiting add %s", ctx.getText())
2234
pass
2335

2436

2537
# Enter a parse tree produced by DiceNotationParser#multOp.
2638
def enterMultOp(self, ctx):
39+
self._logger.warning("Entering multiplication %s", ctx.getText())
2740
pass
2841

2942
# Exit a parse tree produced by DiceNotationParser#multOp.
3043
def exitMultOp(self, ctx):
44+
self._logger.warning("Exiting multiplication %s", ctx.getText())
3145
pass
3246

3347

3448
# Enter a parse tree produced by DiceNotationParser#operand.
3549
def enterOperand(self, ctx):
50+
self._logger.warning("Entering operand %s", ctx.getText())
3651
pass
3752

3853
# Exit a parse tree produced by DiceNotationParser#operand.
3954
def exitOperand(self, ctx):
55+
self._logger.warning("Exiting operand %s", ctx.getText())
4056
pass
4157

4258

4359
# Enter a parse tree produced by DiceNotationParser#dice.
4460
def enterDice(self, ctx):
61+
self._logger.warning("Entering dice %s", ctx.getText())
4562
pass
4663

4764
# Exit a parse tree produced by DiceNotationParser#dice.
4865
def exitDice(self, ctx):
49-
pass
66+
self._logger.warning("Exiting dice %s", ctx.getText())
67+
self._logger.warning("Quantity %s, sides %s", ctx.DIGIT()[0], ctx.DIGIT()[1])
68+
digits = iter(ctx.DIGIT())
69+
if(len(ctx.DIGIT()) > 1):
70+
# Contains the quantity of dice
71+
quantity = next(digits)
72+
else:
73+
# No quantity of dice defined
74+
# Defaults to 1
75+
quantity = 1
76+
77+
sides = next(digits)
78+
79+
self._nodes.append(Dice(quantity, sides))
5080

5181

5282
# Enter a parse tree produced by DiceNotationParser#number.
5383
def enterNumber(self, ctx):
84+
self._logger.warning("Entering number %s", ctx.getText())
5485
pass
5586

5687
# Exit a parse tree produced by DiceNotationParser#number.
5788
def exitNumber(self, ctx):
89+
self._logger.warning("Exiting number %s", ctx.getText())
5890
pass
5991

6092

dice_notation/parser/dice.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import io
21
import logging
32
from antlr4 import InputStream, CommonTokenStream, ParseTreeWalker
43

test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from io import StringIO
2+
import sys
3+
from antlr4 import *
4+
5+
from dice_notation.parser.dice import DiceParser
6+
7+
8+
def main(argv):
9+
parser = DiceParser()
10+
tree = parser.parse("4d56")
11+
12+
13+
if __name__ == '__main__':
14+
main(sys.argv)

0 commit comments

Comments
 (0)