-
Notifications
You must be signed in to change notification settings - Fork 0
/
commandParser.py
36 lines (30 loc) · 1.27 KB
/
commandParser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import re
from dice import Dice
# !roll
roll_command_regex = re.compile(ur'(?P<roll_command>^[!]\w+)', re.IGNORECASE)
class Parser:
def __init__(self):
self.roll_command_regex = roll_command_regex
@staticmethod
def parse_for_command(text):
if re.search(roll_command_regex, text):
return Parser.parse_roll_command(text)
else:
return None
@staticmethod
def parse_roll_command(text):
# print 'Parsing roll command "{0}" ... '.format(text)
regex = re.compile(ur'(?P<roll_command>^[!]\w+)\s*'
ur'(?P<number_of_dice>\d+)[d](?P<dice_sides>\d+)\s*'
ur'(?P<modifier>(?P<modifier_operator>[-+*/])\s*(?P<modifier_value>\d+))*', re.IGNORECASE)
match = re.search(regex, text)
if match:
number_of_dice = match.group('number_of_dice')
dice_sides = match.group('dice_sides')
modifier_operator = match.group('modifier_operator')
modifier_value = match.group('modifier_value')
# print 'Match: {0}'.format(match.group())
return Dice.roll(number_of_dice, dice_sides, modifier_operator, modifier_value)
else:
print 'Invalid roll command syntax'
return None