home · contact · privacy
Register game commands and tasks outside of game module.
[plomrogue2-experiments] / parser.py
index bc2e70bbfaecf82a3a74c728ed2d790236eb3004..03b688f24e8eccf18cd1c024e5804ef96e1087d5 100644 (file)
--- a/parser.py
+++ b/parser.py
@@ -1,5 +1,4 @@
 import unittest
-from functools import partial
 
 
 class ArgError(Exception):
@@ -45,38 +44,42 @@ class Parser:
         return tokens
 
     def parse(self, msg):
-        """Parse msg as call to self.game method, return method with arguments.
+        """Parse msg as call to function, return function with args tuple.
 
-        Respects method signatures defined in methods' .argtypes attributes.
+        Respects function signature defined in function's .argtypes attribute.
         """
         tokens = self.tokenize(msg)
         if len(tokens) == 0:
-            return None
-        method_candidate = 'cmd_' + tokens[0]
-        if not hasattr(self.game, method_candidate):
-            return None
-        method = getattr(self.game, method_candidate)
+            return None, ()
+        func, argtypes = self.game.get_command_signature(tokens[0])
+        if func is None:
+            return None, ()
+        if len(argtypes) == 0:
+            if len(tokens) > 1:
+                raise ArgError('Command expects no argument(s).')
+            return func, ()
         if len(tokens) == 1:
-            if not hasattr(method, 'argtypes'):
-                return method
-            else:
-                raise ArgError('Command expects argument(s).')
+            raise ArgError('Command expects argument(s).')
         args_candidates = tokens[1:]
-        if not hasattr(method, 'argtypes'):
-            raise ArgError('Command expects no argument(s).')
-        args, kwargs = self.argsparse(method.argtypes, args_candidates)
-        return partial(method, *args, **kwargs)
+        args = self.argsparse(argtypes, args_candidates)
+        return func, args
+
+    def parse_yx_tuple(self, yx_string, range_):
+        """Parse yx_string as yx_tuple:nonneg argtype, return result.
 
-    def parse_yx_tuple(self, yx_string):
-        """Parse yx_string as yx_tuple:nonneg argtype, 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():
                 raise ArgError('Non-int arg for ' + axis + ' position.')
             n = int(token[2:])
-            if n < 1:
+            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(',')
@@ -87,11 +90,12 @@ class Parser:
         return (y, x)
 
     def argsparse(self, signature, args_tokens):
-        """Parse into / return args_tokens as args/kwargs defined by signature.
+        """Parse into / return args_tokens as args defined by signature.
 
         Expects signature to be a ' '-delimited sequence of any of the strings
-        'int:nonneg', 'yx_tuple:nonneg', 'string', 'seq:int:nonneg', defining
-        the respective argument types.
+        'int:nonneg', 'yx_tuple:nonneg', 'yx_tuple:pos', 'string',
+        'seq:int:nonneg', 'string:' + an option type string accepted by
+        self.game.get_string_options, defining the respective argument types.
         """
         tmpl_tokens = signature.split()
         if len(tmpl_tokens) != len(args_tokens):
@@ -99,6 +103,7 @@ class Parser:
                            ') not expected number (' + str(len(tmpl_tokens))
                            + ').')
         args = []
+        string_string = 'string'
         for i in range(len(tmpl_tokens)):
             tmpl = tmpl_tokens[i]
             arg = args_tokens[i]
@@ -107,9 +112,9 @@ class Parser:
                     raise ArgError('Argument must be non-negative integer.')
                 args += [int(arg)]
             elif tmpl == 'yx_tuple:nonneg':
-                args += [self.parse_yx_tuple(arg)]
-            elif tmpl == 'string':
-                args += [arg]
+                args += [self.parse_yx_tuple(arg, 'nonneg')]
+            elif tmpl == 'yx_tuple:pos':
+                args += [self.parse_yx_tuple(arg, 'pos')]
             elif tmpl == 'seq:int:nonneg':
                 sub_tokens = arg.split(',')
                 if len(sub_tokens) < 1:
@@ -121,9 +126,22 @@ class Parser:
                                        'non-negative integers.')
                     seq += [int(tok)]
                 args += [seq]
+            elif tmpl == string_string:
+                args += [arg]
+            elif tmpl[:len(string_string) + 1] == string_string + ':':
+                if not hasattr(self.game, 'get_string_options'):
+                    raise ArgError('No string option directory.')
+                string_option_type = tmpl[len(string_string) + 1:]
+                options = self.game.get_string_options(string_option_type)
+                if options is None:
+                    raise ArgError('Unknown string option type.')
+                if arg not in options:
+                    msg = 'Argument #%s must be one of: %s' % (i + 1, options)
+                    raise ArgError(msg)
+                args += [arg]
             else:
                 raise ArgError('Unknown argument type.')
-        return args, {}
+        return args
 
 
 class TestParser(unittest.TestCase):
@@ -165,12 +183,14 @@ class TestParser(unittest.TestCase):
         self.assertEqual(p.argsparse('int:nonneg', ('0',)),
                          ([0], {}))
         assertErr('yx_tuple:nonneg', ['x'])
-        assertErr('yx_tuple:nonneg', ['Y:0,X:1'])
-        assertErr('yx_tuple:nonneg', ['Y:1,X:0'])
+        assertErr('yx_tuple:nonneg', ['Y:0,X:-1'])
+        assertErr('yx_tuple:nonneg', ['Y:-1,X:0'])
         assertErr('yx_tuple:nonneg', ['Y:1.1,X:1'])
         assertErr('yx_tuple:nonneg', ['Y:1,X:1.1'])
         self.assertEqual(p.argsparse('yx_tuple:nonneg', ('Y:1,X:2',)),
                          ([(1, 2)], {}))
+        assertErr('yx_tuple:pos', ['Y:0,X:1'])
+        assertErr('yx_tuple:pos', ['Y:1,X:0'])
         assertErr('seq:int:nonneg', [''])
         assertErr('seq:int:nonneg', [','])
         assertErr('seq:int:nonneg', ['a'])