X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=plomrogue%2Fparser.py;h=d1307a97701d3ef5c5eee6ecc2c16cfcb8d94282;hb=d9c9b5b7d5cac2469ac075010c4d729e1adf0cc4;hp=5782d695fe1f026d4dc2b00f422cda389fae9988;hpb=540aec0e9bf55d0452cffda4b34e1995d3724abf;p=plomrogue2 diff --git a/plomrogue/parser.py b/plomrogue/parser.py index 5782d69..d1307a9 100644 --- a/plomrogue/parser.py +++ b/plomrogue/parser.py @@ -45,16 +45,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.') @@ -106,10 +113,22 @@ class Parser: if not arg.isdigit(): raise ArgError('Argument must be non-negative integer.') args += [int(arg)] + elif tmpl == 'int:pos': + if not arg.isdigit() or int(arg) < 1: + raise ArgError('Argument must be positive integer.') + args += [int(arg)] + elif tmpl == 'char': + try: + ord(arg) + except TypeError: + raise ArgError('Argument must be single character.') + args += [arg] elif tmpl == 'yx_tuple:nonneg': 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 + ':':