|
1 | 1 | # Generated from DiceNotation.g4 by ANTLR 4.7.2 |
2 | 2 | from antlr4 import * |
3 | 3 |
|
| 4 | +import logging |
| 5 | +from dice_notation.dice import Dice |
| 6 | + |
4 | 7 | # This class defines a complete listener for a parse tree produced by DiceNotationParser. |
5 | 8 | class DiceNotationListener(ParseTreeListener): |
6 | 9 |
|
| 10 | + def __init__(self): |
| 11 | + super(DiceNotationListener, self).__init__() |
| 12 | + self._logger = logging.getLogger("DiceNotationListener") |
| 13 | + self._nodes = [] |
| 14 | + |
7 | 15 | # Enter a parse tree produced by DiceNotationParser#notation. |
8 | 16 | def enterNotation(self, ctx): |
| 17 | + self._logger.warning("Entering notation %s", ctx.getText()) |
9 | 18 | pass |
10 | 19 |
|
11 | 20 | # Exit a parse tree produced by DiceNotationParser#notation. |
12 | 21 | def exitNotation(self, ctx): |
| 22 | + self._logger.warning("Exiting notation %s", ctx.getText()) |
13 | 23 | pass |
14 | 24 |
|
15 | 25 |
|
16 | 26 | # Enter a parse tree produced by DiceNotationParser#addOp. |
17 | 27 | def enterAddOp(self, ctx): |
| 28 | + self._logger.warning("Entering add %s", ctx.getText()) |
18 | 29 | pass |
19 | 30 |
|
20 | 31 | # Exit a parse tree produced by DiceNotationParser#addOp. |
21 | 32 | def exitAddOp(self, ctx): |
| 33 | + self._logger.warning("Exiting add %s", ctx.getText()) |
22 | 34 | pass |
23 | 35 |
|
24 | 36 |
|
25 | 37 | # Enter a parse tree produced by DiceNotationParser#multOp. |
26 | 38 | def enterMultOp(self, ctx): |
| 39 | + self._logger.warning("Entering multiplication %s", ctx.getText()) |
27 | 40 | pass |
28 | 41 |
|
29 | 42 | # Exit a parse tree produced by DiceNotationParser#multOp. |
30 | 43 | def exitMultOp(self, ctx): |
| 44 | + self._logger.warning("Exiting multiplication %s", ctx.getText()) |
31 | 45 | pass |
32 | 46 |
|
33 | 47 |
|
34 | 48 | # Enter a parse tree produced by DiceNotationParser#operand. |
35 | 49 | def enterOperand(self, ctx): |
| 50 | + self._logger.warning("Entering operand %s", ctx.getText()) |
36 | 51 | pass |
37 | 52 |
|
38 | 53 | # Exit a parse tree produced by DiceNotationParser#operand. |
39 | 54 | def exitOperand(self, ctx): |
| 55 | + self._logger.warning("Exiting operand %s", ctx.getText()) |
40 | 56 | pass |
41 | 57 |
|
42 | 58 |
|
43 | 59 | # Enter a parse tree produced by DiceNotationParser#dice. |
44 | 60 | def enterDice(self, ctx): |
| 61 | + self._logger.warning("Entering dice %s", ctx.getText()) |
45 | 62 | pass |
46 | 63 |
|
47 | 64 | # Exit a parse tree produced by DiceNotationParser#dice. |
48 | 65 | 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)) |
50 | 80 |
|
51 | 81 |
|
52 | 82 | # Enter a parse tree produced by DiceNotationParser#number. |
53 | 83 | def enterNumber(self, ctx): |
| 84 | + self._logger.warning("Entering number %s", ctx.getText()) |
54 | 85 | pass |
55 | 86 |
|
56 | 87 | # Exit a parse tree produced by DiceNotationParser#number. |
57 | 88 | def exitNumber(self, ctx): |
| 89 | + self._logger.warning("Exiting number %s", ctx.getText()) |
58 | 90 | pass |
59 | 91 |
|
60 | 92 |
|
0 commit comments