Skip to content

Commit e82be1a

Browse files
committed
Added logs and the parser returns the parsed expression
1 parent 652ffab commit e82be1a

2 files changed

Lines changed: 35 additions & 16 deletions

File tree

dice_notation/parser/DiceNotationListener.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,59 +12,71 @@ def __init__(self):
1212
self._logger = logging.getLogger("DiceNotationListener")
1313
self._nodes = []
1414

15+
def getExpression(self):
16+
expression = None
17+
18+
self._logger.debug("Checking stack %s for parsed expression", self._nodes)
19+
if(self._nodes):
20+
expression = self._nodes.pop()
21+
self._logger.debug("Got expression %s from stack", expression)
22+
else:
23+
self._logger.debug("No expression found")
24+
25+
return expression
26+
1527
# Enter a parse tree produced by DiceNotationParser#notation.
1628
def enterNotation(self, ctx):
17-
self._logger.warning("Entering notation %s", ctx.getText())
29+
self._logger.debug("Entering notation %s", ctx.getText())
1830
pass
1931

2032
# Exit a parse tree produced by DiceNotationParser#notation.
2133
def exitNotation(self, ctx):
22-
self._logger.warning("Exiting notation %s", ctx.getText())
34+
self._logger.debug("Exiting notation %s", ctx.getText())
2335
pass
2436

2537

2638
# Enter a parse tree produced by DiceNotationParser#addOp.
2739
def enterAddOp(self, ctx):
28-
self._logger.warning("Entering add %s", ctx.getText())
40+
self._logger.debug("Entering add %s", ctx.getText())
2941
pass
3042

3143
# Exit a parse tree produced by DiceNotationParser#addOp.
3244
def exitAddOp(self, ctx):
33-
self._logger.warning("Exiting add %s", ctx.getText())
45+
self._logger.debug("Exiting add %s", ctx.getText())
3446
pass
3547

3648

3749
# Enter a parse tree produced by DiceNotationParser#multOp.
3850
def enterMultOp(self, ctx):
39-
self._logger.warning("Entering multiplication %s", ctx.getText())
51+
self._logger.debug("Entering multiplication %s", ctx.getText())
4052
pass
4153

4254
# Exit a parse tree produced by DiceNotationParser#multOp.
4355
def exitMultOp(self, ctx):
44-
self._logger.warning("Exiting multiplication %s", ctx.getText())
56+
self._logger.debug("Exiting multiplication %s", ctx.getText())
4557
pass
4658

4759

4860
# Enter a parse tree produced by DiceNotationParser#operand.
4961
def enterOperand(self, ctx):
50-
self._logger.warning("Entering operand %s", ctx.getText())
62+
self._logger.debug("Entering operand %s", ctx.getText())
5163
pass
5264

5365
# Exit a parse tree produced by DiceNotationParser#operand.
5466
def exitOperand(self, ctx):
55-
self._logger.warning("Exiting operand %s", ctx.getText())
67+
self._logger.debug("Exiting operand %s", ctx.getText())
5668
pass
5769

5870

5971
# Enter a parse tree produced by DiceNotationParser#dice.
6072
def enterDice(self, ctx):
61-
self._logger.warning("Entering dice %s", ctx.getText())
73+
self._logger.debug("Entering dice %s", ctx.getText())
6274
pass
6375

6476
# Exit a parse tree produced by DiceNotationParser#dice.
6577
def exitDice(self, ctx):
66-
self._logger.warning("Exiting dice %s", ctx.getText())
67-
self._logger.warning("Quantity %s, sides %s", ctx.DIGIT()[0], ctx.DIGIT()[1])
78+
self._logger.debug("Exiting dice %s", ctx.getText())
79+
self._logger.debug("Quantity %s, sides %s", ctx.DIGIT()[0], ctx.DIGIT()[1])
6880
digits = iter(ctx.DIGIT())
6981
if(len(ctx.DIGIT()) > 1):
7082
# Contains the quantity of dice
@@ -76,17 +88,20 @@ def exitDice(self, ctx):
7688

7789
sides = next(digits)
7890

79-
self._nodes.append(Dice(quantity, sides))
91+
dice = Dice(quantity, sides)
92+
self._nodes.append(dice)
93+
self._logger.debug("Added dice %s to stack", dice)
94+
self._logger.debug("Currently the stack contains %s", self._nodes)
8095

8196

8297
# Enter a parse tree produced by DiceNotationParser#number.
8398
def enterNumber(self, ctx):
84-
self._logger.warning("Entering number %s", ctx.getText())
99+
self._logger.debug("Entering number %s", ctx.getText())
85100
pass
86101

87102
# Exit a parse tree produced by DiceNotationParser#number.
88103
def exitNumber(self, ctx):
89-
self._logger.warning("Exiting number %s", ctx.getText())
104+
self._logger.debug("Exiting number %s", ctx.getText())
90105
pass
91106

92107

dice_notation/parser/dice.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ def parse(self, input):
2121
print(tree.toStringTree(recog=parser))
2222

2323
walker = ParseTreeWalker()
24-
walker.walk(DiceNotationListener(), tree)
2524

26-
return tree
25+
listener = DiceNotationListener()
26+
walker.walk(listener, tree)
27+
28+
expression = listener.getExpression()
29+
self._logger.debug("Parsed expression %s", expression)
30+
return expression

0 commit comments

Comments
 (0)