home · contact · privacy
Add MAP command to explicitely set map size.
[plomrogue2-experiments] / new2 / plomrogue / parser.py
index f23a7aaacb58e8f89604f85ec1b525e0e0ac9d01..b350ee57298d10740c8d78f047689633c7733fd3 100644 (file)
@@ -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.
 
@@ -79,6 +106,8 @@ class Parser:
                 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 + ':':