Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ addons:

before_install:
# Gets scripts
- git clone -b v1.2.1 --single-branch https://github.com/Bernardo-MG/ci-shell-scripts.git ~/.scripts
- git clone -b v1.2.2 --single-branch https://github.com/Bernardo-MG/ci-shell-scripts.git ~/.scripts
# Sets scripts as executable
- chmod -R +x ~/.scripts/*
# Prepares CI environment
Expand Down
33 changes: 22 additions & 11 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,34 +45,45 @@ The source files for the docs, a small `Sphinx`_ project, are kept in the 'docs

These can be built if needed:

``$ python setup.py build_docs``
``python setup.py build_docs``

Prerequisites
~~~~~~~~~~~~~

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

- Python 3.4
- Python 3.5
- Python 3.6
- Python 3.7
- Python 3.8

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

These can be installed with:

``$ pip install --upgrade -r requirements.txt``
``pip install --upgrade -r requirements.txt``

Building the grammar
~~~~~~~~~~~~~~~~~~~~

First of all install ANTLR `as told here <https://github.com/antlr/antlr4/blob/master/doc/getting-started.md/>`_.

Afterwards, follow `these indications <https://github.com/antlr/antlr4/blob/master/doc/python-target.md/>`_.

The command to generate the parser will be:

``antlr4 -Dlanguage=Python2 DiceNotation.g4 DiceNotationLexer.g4``

Installing
~~~~~~~~~~

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

``$ pip3 install dice-notation``
``pip install dice-notation``

If needed, manual installation is possible:

``$ python setup.py install``
``python setup.py install``

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

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

print(dice.roll())
print(dice.roll())
print(dice.value())
print(dice.value())

Testing
-------

The tests included with the project can be run with:

``$ python setup.py test``
``python setup.py test``

This will delegate the execution to tox.

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

``$ python setup.py test -p "py36"``
``python setup.py test -p "py36"``

Collaborate
-----------
Expand Down
94 changes: 94 additions & 0 deletions dice_notation/algebra.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# -*- coding: utf-8 -*-

from dice_notation.dice import Rollable

"""
Algebraic classes.

These allow working with algebraic operations for dice notation.
"""

__author__ = 'Benardo Martínez Garrido'
__license__ = 'MIT'


class BinaryOperation(Rollable):
"""
A binary operation. Matching an operator with two operands.
"""

def __init__(self, left, right, operator, operation):
super(BinaryOperation, self).__init__()
self._left = left
self._right = right
self._operator = operator
self._operation = operation

def __str__(self):
return '%s%s%s' % (self._left, self._operator, self._right)

def __repr__(self):
return '<class %s>(left=%r, right=%r, operator=%r)' % \
(self.__class__.__name__, self._left, self._right, self._operator)

def roll(self):
return self._operation(self._left.roll(), self._right.roll())

@property
def left(self):
"""
The left operand.

:return: the left operand
"""
return self._left

@left.setter
def left(self, left):
self._left = left

@property
def right(self):
"""
The right operand.

:return: the right operand
"""
return self._right

@right.setter
def right(self, right):
self._right = right

@property
def operator(self):
"""
The operator.

:return: the operator
"""
return self._operator

@operator.setter
def operator(self, operator):
self._operator = operator


class Number(Rollable):
"""
A numeric constant
"""

def __init__(self, value):
super(Number, self).__init__()
self._value = value

def __str__(self):
return '%s' % (self._value)

def __repr__(self):
return '<class %s>(value=%r)' % \
(self.__class__.__name__, self._value)

def roll(self):
return self._value
4 changes: 2 additions & 2 deletions dice_notation/dice.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ def __init__(self, quantity, sides):
self._sides = sides

def __str__(self):
return '%sd%s' % (self.quantity, self.sides)
return '%sd%s' % (self._quantity, self._sides)

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

@property
def quantity(self):
Expand Down
74 changes: 74 additions & 0 deletions dice_notation/parser/DiceNotationLexer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Generated from DiceNotationLexer.g4 by ANTLR 4.7.2
# encoding: utf-8
from __future__ import print_function
from antlr4 import *
from io import StringIO
import sys



def serializedATN():
with StringIO() as buf:
buf.write(u"\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\2")
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")
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")
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")
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")
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")
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")
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")
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")
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")
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")
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")
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")
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")
buf.write(u"\2!#\5\r\7\2\" \3\2\2\2\"!\3\2\2\2#\b\3\2\2\2$\'\5\17")
buf.write(u"\b\2%\'\5\21\t\2&$\3\2\2\2&%\3\2\2\2\'\n\3\2\2\2()\7")
buf.write(u"-\2\2)\f\3\2\2\2*+\7/\2\2+\16\3\2\2\2,-\7,\2\2-\20\3")
buf.write(u"\2\2\2./\7\61\2\2/\22\3\2\2\2\60\61\7*\2\2\61\24\3\2")
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")
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")
buf.write(u"\2\29:\b\f\2\2:\30\3\2\2\2\7\2\36\"&\67\3\b\2\2")
return buf.getvalue()


class DiceNotationLexer(Lexer):

atn = ATNDeserializer().deserialize(serializedATN())

decisionsToDFA = [ DFA(ds, i) for i, ds in enumerate(atn.decisionToState) ]

DSEPARATOR = 1
DIGIT = 2
ADDOPERATOR = 3
MULTOPERATOR = 4
LPAREN = 5
RPAREN = 6
WS = 7

channelNames = [ u"DEFAULT_TOKEN_CHANNEL", u"HIDDEN" ]

modeNames = [ u"DEFAULT_MODE" ]

literalNames = [ u"<INVALID>",
u"'('", u"')'" ]

symbolicNames = [ u"<INVALID>",
u"DSEPARATOR", u"DIGIT", u"ADDOPERATOR", u"MULTOPERATOR", u"LPAREN",
u"RPAREN", u"WS" ]

ruleNames = [ u"DSEPARATOR", u"DIGIT", u"ADDOPERATOR", u"MULTOPERATOR",
u"ADD", u"SUB", u"MULT", u"DIV", u"LPAREN", u"RPAREN",
u"WS" ]

grammarFileName = u"DiceNotationLexer.g4"

def __init__(self, input=None, output=sys.stdout):
super(DiceNotationLexer, self).__init__(input, output=output)
self.checkVersion("4.7.2")
self._interp = LexerATNSimulator(self, self.atn, self.decisionsToDFA, PredictionContextCache())
self._actions = None
self._predicates = None


Loading