X-Git-Url: https://plomlompom.com/repos/foo.html?a=blobdiff_plain;f=plomrogue%2Fparser.py;h=a043bde4615f80474aaab11b974ddb2a87a48b0b;hb=6cb3a857a09ae974bf0f510dfa94fb19fba2ce31;hp=a1b56b571b650431d297badf3f0e4573da731359;hpb=06c3b773099e2264831d5f4b4650eadb9290b296;p=plomrogue2 diff --git a/plomrogue/parser.py b/plomrogue/parser.py index a1b56b5..a043bde 100644 --- a/plomrogue/parser.py +++ b/plomrogue/parser.py @@ -1,4 +1,3 @@ -import unittest from plomrogue.errors import ArgError from plomrogue.mapping import YX @@ -45,16 +44,23 @@ class Parser: """Parse yx_string as yx_tuple, return result. The range_ argument may be 'nonneg' (non-negative, including - 0) or 'pos' (positive, excluding 0). + 0) or 'pos' (positive, excluding 0) or 'all'. """ 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': + if token[:2] != axis + ':': + raise ArgError('invalid YX tuple formatting') + n_string = token[2:] + if n_string.strip() != n_string: + raise ArgError('invalid YX tuple formatting') + try: + n = int(n_string) + except ValueError: + raise ArgError('non-int value for ' + axis + ' position') + if range_ == 'all': + return n + 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.') @@ -95,8 +101,8 @@ class Parser: tmpl_tokens = signature.split() if len(tmpl_tokens) != len(args_tokens): raise ArgError('Number of arguments (' + str(len(args_tokens)) + - ') not expected number (' + str(len(tmpl_tokens)) - + ').') + ') not expected number (' + str(len(tmpl_tokens)) + + ').') args = [] string_string = 'string' for i in range(len(tmpl_tokens)): @@ -120,6 +126,8 @@ class Parser: args += [self.parse_yx_tuple(arg, 'nonneg')] elif tmpl == 'yx_tuple:pos': args += [self.parse_yx_tuple(arg, 'pos')] + elif tmpl == 'yx_tuple': + args += [self.parse_yx_tuple(arg, 'all')] elif tmpl == string_string: args += [arg] elif tmpl[:len(string_string) + 1] == string_string + ':':