Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
da8ac9f
Changed docs command
Bernardo-MG Jan 12, 2018
37c8a7f
Merge remote-tracking branch 'origin/develop' into develop
Bernardo-MG Jan 12, 2018
a81964d
Merge remote-tracking branch 'remotes/origin/master' into develop
Bernardo-MG Jan 12, 2018
9482581
Updated info about building docs
Bernardo-MG Jan 12, 2018
3cacf68
Fixed Sphinx build path
Bernardo-MG Jan 12, 2018
edd6169
Updated scripts
Bernardo-MG Jan 12, 2018
3d83391
Fixed script name
Bernardo-MG Jan 12, 2018
9228ca9
Updated dependencies
Bernardo-MG Apr 20, 2019
863d9cc
Updated gitignore
Bernardo-MG Apr 20, 2019
3c5778a
Docs are only tested when deployed
Bernardo-MG Apr 20, 2019
ec94b29
Changed URL
Bernardo-MG Apr 20, 2019
baaf6eb
Disabled link check
Bernardo-MG Apr 20, 2019
323fef5
Updated dependencies
Bernardo-MG Mar 8, 2020
c23df9f
Added ANTLR grammar
Bernardo-MG Mar 8, 2020
c0a66d0
Added ANTLR grammar
Bernardo-MG Mar 8, 2020
82bfb73
Corrected grammar
Bernardo-MG Mar 8, 2020
62c7a02
Corrected grammar
Bernardo-MG Mar 8, 2020
5395857
Corrections to parser
Bernardo-MG Mar 8, 2020
422ac51
Registered listener
Bernardo-MG Mar 8, 2020
79d5b8b
First test at parsing dice
Bernardo-MG Mar 8, 2020
7f34320
Changed Python versions
Bernardo-MG Mar 8, 2020
138c04c
Using older dependency
Bernardo-MG Mar 8, 2020
abf1658
Updated dependencies
Bernardo-MG Mar 22, 2020
da2ba8c
Updated dependencies
Bernardo-MG Mar 22, 2020
cf2943d
Using older dependency
Bernardo-MG Mar 22, 2020
042be3f
Updated Travis config
Bernardo-MG Mar 22, 2020
c7eb546
Merge remote-tracking branch 'remotes/origin/develop' into antlr4
Bernardo-MG Mar 22, 2020
9be6473
Updated scripts
Bernardo-MG Mar 22, 2020
1b0ef15
Updated Python versions
Bernardo-MG Mar 22, 2020
652ffab
Corrected tox config
Bernardo-MG Mar 22, 2020
e82be1a
Added logs and the parser returns the parsed expression
Bernardo-MG Mar 22, 2020
99dc532
Numbers are parsed
Bernardo-MG Mar 22, 2020
ab604d4
Added binary operation parsing method
Bernardo-MG Mar 22, 2020
e613787
Binary operations are parsed
Bernardo-MG Mar 22, 2020
d5637d6
Numbers are parsed
Bernardo-MG Mar 22, 2020
24338c8
Operation results are parsed
Bernardo-MG Mar 22, 2020
d4fc02f
Corrected parsing
Bernardo-MG Mar 22, 2020
b623d36
Disabled tests
Bernardo-MG Mar 22, 2020
df7db09
Merge pull request #18 from Bernardo-MG/antlr4
Bernardo-MG Mar 22, 2020
4857999
Removed test runnable class
Bernardo-MG Mar 22, 2020
3699e12
Raised version
Bernardo-MG Mar 22, 2020
029f328
Corrected readme
Bernardo-MG Mar 22, 2020
5c30f88
Updated readme
Bernardo-MG Mar 22, 2020
4698f41
Corrected docs
Bernardo-MG Mar 22, 2020
0ad019a
Merge branch 'antlr4' into develop
Bernardo-MG Mar 22, 2020
2ea25ec
Corrected docs
Bernardo-MG Mar 22, 2020
99ca5fd
Corrected docs
Bernardo-MG Mar 22, 2020
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ __pycache__/
# Distribution / packaging
.Python
env/
venv/
build/
develop-eggs/
dist/
Expand Down
17 changes: 5 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,8 @@
# Using Python for the project
language: python
python:
- "3.4"
- "3.5"
# Python 3.6 is set to test and deploy the docs in the configuration matrix
#- "3.6"
# PyPy currently is not working with the required dependencies
#- "pypy"
#- "pypy3"
env:
- TEST_DOCS=true
- "3.7"
- "3.8"
matrix:
include:
# Tests and deploys docs, also runs coverage report
Expand All @@ -25,7 +18,7 @@ addons:

before_install:
# Gets scripts
- git clone -b v1.2.0 --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 All @@ -41,7 +34,7 @@ script:
- ~/.scripts/python/run_tests.sh $DO_TEST_DOCS docs
after_success:
# Documentation deployment
- ~/.scripts/sphinx/build-html.sh $DO_DEPLOY_DOCS docs
- cd ~/sphinx/build/html
- ~/.scripts/python/build_docs.sh $DO_DEPLOY_DOCS
- cd ./build/sphinx/html
- ~/.scripts/deploy/deploy-ssh.sh $DO_DEPLOY_DOCS_RELEASE $DEPLOY_DOCS_USERNAME $DEPLOY_DOCS_PASSWORD $DEPLOY_DOCS_HOST $DEPLOY_DOCS_PORT $DEPLOY_DOCS_PATH_RELEASE
- ~/.scripts/deploy/deploy-ssh.sh $DO_DEPLOY_DOCS_DEVELOP $DEPLOY_DOCS_USERNAME $DEPLOY_DOCS_PASSWORD $DEPLOY_DOCS_HOST $DEPLOY_DOCS_PORT $DEPLOY_DOCS_PATH_DEVELOP
35 changes: 24 additions & 11 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,36 +41,49 @@ documentation sites:
- The `latest docs`_ are always generated for the latest release, kept in the 'master' branch
- The `development docs`_ are generated from the latest code in the 'develop' branch

You can also create the documentation from the source files, kept in the 'docs'
folder, with the help of `Sphinx`_. For this use the makefile, or the make.bat
file, contained on that folder.
The source files for the docs, a small `Sphinx`_ project, are kept in the 'docs folder.

These can be built if needed:

``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 @@ -87,7 +100,7 @@ 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())
Expand All @@ -98,13 +111,13 @@ 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 "py38"``

Collaborate
-----------
Expand Down
2 changes: 1 addition & 1 deletion dice_notation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
:license: MIT, see LICENSE for more details.
"""

__version__ = '1.0.6'
__version__ = '1.1.0'
__license__ = 'MIT'
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