import unittest
from plomrogue.errors import ArgError
+from plomrogue.mapping import YX
class Parser:
tokens += [token]
return tokens
+ def parse_yx_tuple(self, yx_string, range_=None):
+ """Parse yx_string as yx_tuple, return result.
+
+ The range_ argument may be 'nonneg' (non-negative, including
+ 0) or 'pos' (positive, excluding 0).
+
+ """
+
+ def get_axis_position_from_argument(axis, token):
+ if len(token) < 3 or token[:2] != axis + ':' or \
+ not (token[2:].isdigit() or token[2] == '-'):
+ raise ArgError('Non-int arg for ' + axis + ' position.')
+ n = int(token[2:])
+ if n < 1 and range_ == 'pos':
+ raise ArgError('Arg for ' + axis + ' position < 1.')
+ elif n < 0 and range_ == 'nonneg':
+ raise ArgError('Arg for ' + axis + ' position < 0.')
+ return n
+
+ tokens = yx_string.split(',')
+ if len(tokens) != 2:
+ raise ArgError('Wrong number of yx-tuple arguments.')
+ y = get_axis_position_from_argument('Y', tokens[0])
+ x = get_axis_position_from_argument('X', tokens[1])
+ return YX(y, x)
+
def parse(self, msg):
"""Parse msg as call to function, return function with args tuple.
for i in range(len(tmpl_tokens)):
tmpl = tmpl_tokens[i]
arg = args_tokens[i]
- if tmpl == string_string:
+ if tmpl == 'int:nonneg':
+ if not arg.isdigit():
+ raise ArgError('Argument must be non-negative integer.')
+ args += [int(arg)]
+ elif tmpl == 'yx_tuple:pos':
+ args += [self.parse_yx_tuple(arg, 'pos')]
+ elif tmpl == string_string:
args += [arg]
elif tmpl[:len(string_string) + 1] == string_string + ':':
if not hasattr(self.game, 'get_string_options'):