Skip to content

Commit df7db09

Browse files
authored
Merge pull request #18 from Bernardo-MG/antlr4
Antlr4
2 parents 138c04c + b623d36 commit df7db09

26 files changed

Lines changed: 1024 additions & 673 deletions

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ addons:
1818

1919
before_install:
2020
# Gets scripts
21-
- git clone -b v1.2.1 --single-branch https://github.com/Bernardo-MG/ci-shell-scripts.git ~/.scripts
21+
- git clone -b v1.2.2 --single-branch https://github.com/Bernardo-MG/ci-shell-scripts.git ~/.scripts
2222
# Sets scripts as executable
2323
- chmod -R +x ~/.scripts/*
2424
# Prepares CI environment

README.rst

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,34 +45,45 @@ The source files for the docs, a small `Sphinx`_ project, are kept in the 'docs
4545

4646
These can be built if needed:
4747

48-
``$ python setup.py build_docs``
48+
``python setup.py build_docs``
4949

5050
Prerequisites
5151
~~~~~~~~~~~~~
5252

5353
The project has been tested in the following versions of the interpreter:
5454

55-
- Python 3.4
56-
- Python 3.5
5755
- Python 3.6
56+
- Python 3.7
57+
- Python 3.8
5858

5959
All other dependencies are indicated on the requirements.txt file.
6060

6161
These can be installed with:
6262

63-
``$ pip install --upgrade -r requirements.txt``
63+
``pip install --upgrade -r requirements.txt``
64+
65+
Building the grammar
66+
~~~~~~~~~~~~~~~~~~~~
67+
68+
First of all install ANTLR `as told here <https://github.com/antlr/antlr4/blob/master/doc/getting-started.md/>`_.
69+
70+
Afterwards, follow `these indications <https://github.com/antlr/antlr4/blob/master/doc/python-target.md/>`_.
71+
72+
The command to generate the parser will be:
73+
74+
``antlr4 -Dlanguage=Python2 DiceNotation.g4 DiceNotationLexer.g4``
6475

6576
Installing
6677
~~~~~~~~~~
6778

6879
The project is offered as a `Pypi package`_, and using pip is the preferred way
6980
to install it. For this use the following command;
7081

71-
``$ pip3 install dice-notation``
82+
``pip install dice-notation``
7283

7384
If needed, manual installation is possible:
7485

75-
``$ python setup.py install``
86+
``python setup.py install``
7687

7788
Usage
7889
-----
@@ -89,24 +100,24 @@ And then use it to parse a dice notation expression::
89100
parser = DiceParser()
90101
dice = parser.parse('1d6+2')
91102

92-
The result can be accessed just by calling the 'roll' method as many times as
103+
The result can be accessed just by calling the 'value' method as many times as
93104
needed, which will generate a new random value each time it is called::
94105

95-
print(dice.roll())
96-
print(dice.roll())
106+
print(dice.value())
107+
print(dice.value())
97108

98109
Testing
99110
-------
100111

101112
The tests included with the project can be run with:
102113

103-
``$ python setup.py test``
114+
``python setup.py test``
104115

105116
This will delegate the execution to tox.
106117

107118
It is possible to run just one of the test profiles, in this case the py36 profile:
108119

109-
``$ python setup.py test -p "py36"``
120+
``python setup.py test -p "py36"``
110121

111122
Collaborate
112123
-----------

dice_notation/algebra.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from dice_notation.dice import Rollable
4+
5+
"""
6+
Algebraic classes.
7+
8+
These allow working with algebraic operations for dice notation.
9+
"""
10+
11+
__author__ = 'Benardo Martínez Garrido'
12+
__license__ = 'MIT'
13+
14+
15+
class BinaryOperation(Rollable):
16+
"""
17+
A binary operation. Matching an operator with two operands.
18+
"""
19+
20+
def __init__(self, left, right, operator, operation):
21+
super(BinaryOperation, self).__init__()
22+
self._left = left
23+
self._right = right
24+
self._operator = operator
25+
self._operation = operation
26+
27+
def __str__(self):
28+
return '%s%s%s' % (self._left, self._operator, self._right)
29+
30+
def __repr__(self):
31+
return '<class %s>(left=%r, right=%r, operator=%r)' % \
32+
(self.__class__.__name__, self._left, self._right, self._operator)
33+
34+
def roll(self):
35+
return self._operation(self._left.roll(), self._right.roll())
36+
37+
@property
38+
def left(self):
39+
"""
40+
The left operand.
41+
42+
:return: the left operand
43+
"""
44+
return self._left
45+
46+
@left.setter
47+
def left(self, left):
48+
self._left = left
49+
50+
@property
51+
def right(self):
52+
"""
53+
The right operand.
54+
55+
:return: the right operand
56+
"""
57+
return self._right
58+
59+
@right.setter
60+
def right(self, right):
61+
self._right = right
62+
63+
@property
64+
def operator(self):
65+
"""
66+
The operator.
67+
68+
:return: the operator
69+
"""
70+
return self._operator
71+
72+
@operator.setter
73+
def operator(self, operator):
74+
self._operator = operator
75+
76+
77+
class Number(Rollable):
78+
"""
79+
A numeric constant
80+
"""
81+
82+
def __init__(self, value):
83+
super(Number, self).__init__()
84+
self._value = value
85+
86+
def __str__(self):
87+
return '%s' % (self._value)
88+
89+
def __repr__(self):
90+
return '<class %s>(value=%r)' % \
91+
(self.__class__.__name__, self._value)
92+
93+
def roll(self):
94+
return self._value

dice_notation/dice.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ def __init__(self, quantity, sides):
6767
self._sides = sides
6868

6969
def __str__(self):
70-
return '%sd%s' % (self.quantity, self.sides)
70+
return '%sd%s' % (self._quantity, self._sides)
7171

7272
def __repr__(self):
7373
return '<class %s>(quantity=%r, sides=%r)' % \
74-
(self.__class__.__name__, self.quantity, self.sides)
74+
(self.__class__.__name__, self._quantity, self._sides)
7575

7676
@property
7777
def quantity(self):
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Generated from DiceNotationLexer.g4 by ANTLR 4.7.2
2+
# encoding: utf-8
3+
from __future__ import print_function
4+
from antlr4 import *
5+
from io import StringIO
6+
import sys
7+
8+
9+
10+
def serializedATN():
11+
with StringIO() as buf:
12+
buf.write(u"\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\2")
13+
buf.write(u"\t;\b\1\4\2\t\2\4\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t")
14+
buf.write(u"\7\4\b\t\b\4\t\t\t\4\n\t\n\4\13\t\13\4\f\t\f\3\2\3\2")
15+
buf.write(u"\3\3\6\3\35\n\3\r\3\16\3\36\3\4\3\4\5\4#\n\4\3\5\3\5")
16+
buf.write(u"\5\5\'\n\5\3\6\3\6\3\7\3\7\3\b\3\b\3\t\3\t\3\n\3\n\3")
17+
buf.write(u"\13\3\13\3\f\6\f\66\n\f\r\f\16\f\67\3\f\3\f\2\2\r\3\3")
18+
buf.write(u"\5\4\7\5\t\6\13\2\r\2\17\2\21\2\23\7\25\b\27\t\3\2\4")
19+
buf.write(u"\4\2FFff\4\2\13\f\17\17\2:\2\3\3\2\2\2\2\5\3\2\2\2\2")
20+
buf.write(u"\7\3\2\2\2\2\t\3\2\2\2\2\23\3\2\2\2\2\25\3\2\2\2\2\27")
21+
buf.write(u"\3\2\2\2\3\31\3\2\2\2\5\34\3\2\2\2\7\"\3\2\2\2\t&\3\2")
22+
buf.write(u"\2\2\13(\3\2\2\2\r*\3\2\2\2\17,\3\2\2\2\21.\3\2\2\2\23")
23+
buf.write(u"\60\3\2\2\2\25\62\3\2\2\2\27\65\3\2\2\2\31\32\t\2\2\2")
24+
buf.write(u"\32\4\3\2\2\2\33\35\4\62;\2\34\33\3\2\2\2\35\36\3\2\2")
25+
buf.write(u"\2\36\34\3\2\2\2\36\37\3\2\2\2\37\6\3\2\2\2 #\5\13\6")
26+
buf.write(u"\2!#\5\r\7\2\" \3\2\2\2\"!\3\2\2\2#\b\3\2\2\2$\'\5\17")
27+
buf.write(u"\b\2%\'\5\21\t\2&$\3\2\2\2&%\3\2\2\2\'\n\3\2\2\2()\7")
28+
buf.write(u"-\2\2)\f\3\2\2\2*+\7/\2\2+\16\3\2\2\2,-\7,\2\2-\20\3")
29+
buf.write(u"\2\2\2./\7\61\2\2/\22\3\2\2\2\60\61\7*\2\2\61\24\3\2")
30+
buf.write(u"\2\2\62\63\7+\2\2\63\26\3\2\2\2\64\66\t\3\2\2\65\64\3")
31+
buf.write(u"\2\2\2\66\67\3\2\2\2\67\65\3\2\2\2\678\3\2\2\289\3\2")
32+
buf.write(u"\2\29:\b\f\2\2:\30\3\2\2\2\7\2\36\"&\67\3\b\2\2")
33+
return buf.getvalue()
34+
35+
36+
class DiceNotationLexer(Lexer):
37+
38+
atn = ATNDeserializer().deserialize(serializedATN())
39+
40+
decisionsToDFA = [ DFA(ds, i) for i, ds in enumerate(atn.decisionToState) ]
41+
42+
DSEPARATOR = 1
43+
DIGIT = 2
44+
ADDOPERATOR = 3
45+
MULTOPERATOR = 4
46+
LPAREN = 5
47+
RPAREN = 6
48+
WS = 7
49+
50+
channelNames = [ u"DEFAULT_TOKEN_CHANNEL", u"HIDDEN" ]
51+
52+
modeNames = [ u"DEFAULT_MODE" ]
53+
54+
literalNames = [ u"<INVALID>",
55+
u"'('", u"')'" ]
56+
57+
symbolicNames = [ u"<INVALID>",
58+
u"DSEPARATOR", u"DIGIT", u"ADDOPERATOR", u"MULTOPERATOR", u"LPAREN",
59+
u"RPAREN", u"WS" ]
60+
61+
ruleNames = [ u"DSEPARATOR", u"DIGIT", u"ADDOPERATOR", u"MULTOPERATOR",
62+
u"ADD", u"SUB", u"MULT", u"DIV", u"LPAREN", u"RPAREN",
63+
u"WS" ]
64+
65+
grammarFileName = u"DiceNotationLexer.g4"
66+
67+
def __init__(self, input=None, output=sys.stdout):
68+
super(DiceNotationLexer, self).__init__(input, output=output)
69+
self.checkVersion("4.7.2")
70+
self._interp = LexerATNSimulator(self, self.atn, self.decisionsToDFA, PredictionContextCache())
71+
self._actions = None
72+
self._predicates = None
73+
74+

0 commit comments

Comments
 (0)