2 from plomrogue.errors import ArgError
3 from plomrogue.mapping import YX
8 def __init__(self, game=None):
11 def tokenize(self, msg):
12 """Parse msg string into tokens.
14 Separates by ' ' and '\n', but allows whitespace in tokens quoted by
15 '"', and allows escaping within quoted tokens by a prefixed backslash.
34 elif c in {' ', '\n'}:
44 def parse_yx_tuple(self, yx_string, range_=None):
45 """Parse yx_string as yx_tuple, return result.
47 The range_ argument may be 'nonneg' (non-negative, including
48 0) or 'pos' (positive, excluding 0) or 'all'.
52 def get_axis_position_from_argument(axis, token):
53 if token[:2] != axis + ':':
54 raise ArgError('invalid YX tuple formatting')
56 if n_string.strip() != n_string:
57 raise ArgError('invalid YX tuple formatting')
61 raise ArgError('non-int value for ' + axis + ' position')
64 if n < 1 and range == 'pos':
65 raise ArgError('Arg for ' + axis + ' position < 1.')
66 elif n < 0 and range_ == 'nonneg':
67 raise ArgError('Arg for ' + axis + ' position < 0.')
70 tokens = yx_string.split(',')
72 raise ArgError('Wrong number of yx-tuple arguments.')
73 y = get_axis_position_from_argument('Y', tokens[0])
74 x = get_axis_position_from_argument('X', tokens[1])
78 """Parse msg as call to function, return function with args tuple.
80 Respects function signature defined in function's .argtypes attribute.
82 tokens = self.tokenize(msg)
85 func = self.game.get_command(tokens[0])
87 if hasattr(func, 'argtypes'):
88 argtypes = func.argtypes
91 if len(argtypes) == 0:
93 raise ArgError('Command expects no argument(s).')
96 raise ArgError('Command expects argument(s).')
97 args_candidates = tokens[1:]
98 args = self.argsparse(argtypes, args_candidates)
101 def argsparse(self, signature, args_tokens):
102 tmpl_tokens = signature.split()
103 if len(tmpl_tokens) != len(args_tokens):
104 raise ArgError('Number of arguments (' + str(len(args_tokens)) +
105 ') not expected number (' + str(len(tmpl_tokens))
108 string_string = 'string'
109 for i in range(len(tmpl_tokens)):
110 tmpl = tmpl_tokens[i]
112 if tmpl == 'int:nonneg':
113 if not arg.isdigit():
114 raise ArgError('Argument must be non-negative integer.')
116 elif tmpl == 'int:pos':
117 if not arg.isdigit() or int(arg) < 1:
118 raise ArgError('Argument must be positive integer.')
124 raise ArgError('Argument must be single character.')
126 elif tmpl == 'yx_tuple:nonneg':
127 args += [self.parse_yx_tuple(arg, 'nonneg')]
128 elif tmpl == 'yx_tuple:pos':
129 args += [self.parse_yx_tuple(arg, 'pos')]
130 elif tmpl == 'yx_tuple':
131 args += [self.parse_yx_tuple(arg, 'all')]
132 elif tmpl == string_string:
134 elif tmpl[:len(string_string) + 1] == string_string + ':':
135 if not hasattr(self.game, 'get_string_options'):
136 raise ArgError('No string option directory.')
137 string_option_type = tmpl[len(string_string) + 1:]
138 options = self.game.get_string_options(string_option_type)
140 raise ArgError('Unknown string option type.')
141 if arg not in options:
142 msg = 'Argument #%s must be one of: %s' % (i + 1, options)
146 raise ArgError('Unknown argument type: %s' % tmpl)