X-Git-Url: https://plomlompom.com/repos/foo.html?a=blobdiff_plain;f=new2%2Fplomrogue%2Fparser.py;h=5782d695fe1f026d4dc2b00f422cda389fae9988;hb=f7f11798a90187ab83d038f737368317a99e15b0;hp=f23a7aaacb58e8f89604f85ec1b525e0e0ac9d01;hpb=47d047c10bacf2463f48aec3e7f3cc3b92a78198;p=plomrogue2-experiments
diff --git a/new2/plomrogue/parser.py b/new2/plomrogue/parser.py
index f23a7aa..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.
@@ -79,6 +106,10 @@ class Parser:
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 + ':':