X-Git-Url: https://plomlompom.com/repos/berlin_corona.txt?a=blobdiff_plain;f=new2%2Fplomrogue%2Fparser.py;h=5782d695fe1f026d4dc2b00f422cda389fae9988;hb=d111426abddb8b84eb609cfa8916d036b05a5d52;hp=a56b5d1bb64bd8b07f2aecb101d88a4dc4ee91f3;hpb=33bfdaf647c6736d99aadc017ee935f3301d758a;p=plomrogue2-experiments diff --git a/new2/plomrogue/parser.py b/new2/plomrogue/parser.py index a56b5d1..5782d69 100644 --- a/new2/plomrogue/parser.py +++ b/new2/plomrogue/parser.py @@ -1,5 +1,6 @@ import unittest from plomrogue.errors import ArgError +from plomrogue.mapping import YX class Parser: @@ -40,6 +41,32 @@ 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. @@ -75,7 +102,15 @@ class Parser: 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:nonneg': + args += [self.parse_yx_tuple(arg, 'nonneg')] + 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'):