1 from plomrogue.errors import ArgError
2 from plomrogue.mapping import YX
7 def __init__(self, game=None):
10 def tokenize(self, msg):
11 """Parse msg string into tokens.
13 Separates by ' ' and '\n', but allows whitespace in tokens quoted by
14 '"', and allows escaping within quoted tokens by a prefixed backslash.
33 elif c in {' ', '\n'}:
43 def parse_yx_tuple(self, yx_string, range_=None):
44 """Parse yx_string as yx_tuple, return result.
46 The range_ argument may be 'nonneg' (non-negative, including
47 0) or 'pos' (positive, excluding 0) or 'all'.
51 def get_axis_position_from_argument(axis, token):
52 if token[:2] != axis + ':':
53 raise ArgError('invalid YX tuple formatting')
55 if n_string.strip() != n_string:
56 raise ArgError('invalid YX tuple formatting')
60 raise ArgError('non-int value for ' + axis + ' position')
63 if n < 1 and range == 'pos':
64 raise ArgError('Arg for ' + axis + ' position < 1.')
65 elif n < 0 and range_ == 'nonneg':
66 raise ArgError('Arg for ' + axis + ' position < 0.')
69 tokens = yx_string.split(',')
71 raise ArgError('Wrong number of yx-tuple arguments.')
72 y = get_axis_position_from_argument('Y', tokens[0])
73 x = get_axis_position_from_argument('X', tokens[1])
77 """Parse msg as call to function, return function with args tuple.
79 Respects function signature defined in function's .argtypes attribute.
81 tokens = self.tokenize(msg)
84 func = self.game.get_command(tokens[0])
86 if hasattr(func, 'argtypes'):
87 argtypes = func.argtypes
90 if len(argtypes) == 0:
92 raise ArgError('Command expects no argument(s).')
95 raise ArgError('Command expects argument(s).')
96 args_candidates = tokens[1:]
97 args = self.argsparse(argtypes, args_candidates)
100 def argsparse(self, signature, args_tokens):
101 tmpl_tokens = signature.split()
102 if len(tmpl_tokens) != len(args_tokens):
103 raise ArgError('Number of arguments (' + str(len(args_tokens)) +
104 ') not expected number (' + str(len(tmpl_tokens)) +
107 string_string = 'string'
108 for i in range(len(tmpl_tokens)):
109 tmpl = tmpl_tokens[i]
111 if tmpl == 'int:nonneg':
112 if not arg.isdigit():
113 raise ArgError('Argument must be non-negative integer.')
115 elif tmpl == 'int:pos':
116 if not arg.isdigit() or int(arg) < 1:
117 raise ArgError('Argument must be positive integer.')
123 raise ArgError('Argument must be single character.')
125 elif tmpl == 'yx_tuple:nonneg':
126 args += [self.parse_yx_tuple(arg, 'nonneg')]
127 elif tmpl == 'yx_tuple:pos':
128 args += [self.parse_yx_tuple(arg, 'pos')]
129 elif tmpl == 'yx_tuple':
130 args += [self.parse_yx_tuple(arg, 'all')]
131 elif tmpl == string_string:
133 elif tmpl[:len(string_string) + 1] == string_string + ':':
134 if not hasattr(self.game, 'get_string_options'):
135 raise ArgError('No string option directory.')
136 string_option_type = tmpl[len(string_string) + 1:]
137 options = self.game.get_string_options(string_option_type)
139 raise ArgError('Unknown string option type.')
140 if arg not in options:
141 msg = 'Argument #%s must be one of: %s' % (i + 1, options)
145 raise ArgError('Unknown argument type: %s' % tmpl)