home · contact · privacy
Refactor parser code.
[plomrogue2] / plomrogue / parser.py
index 140637b468aea0982462a9e3a697690ee24c676d..51f5cc20390ac71c50b5b1baae7f53fcd7c12bdd 100644 (file)
@@ -6,6 +6,7 @@ class Parser:
 
     def __init__(self, game=None):
         self.game = game
 
     def __init__(self, game=None):
         self.game = game
+        self.string_options = {}
 
     def tokenize(self, msg):
         """Parse msg string into tokens.
 
     def tokenize(self, msg):
         """Parse msg string into tokens.
@@ -73,18 +74,19 @@ class Parser:
         x = get_axis_position_from_argument('X', tokens[1])
         return YX(y, x)
 
         x = get_axis_position_from_argument('X', tokens[1])
         return YX(y, x)
 
-    def parse(self, msg):
+    def parse(self, msg, replace_newline=True):
         """Parse msg as call to function, return function with args tuple.
 
         Respects function signature defined in function's .argtypes attribute.
 
         """Parse msg as call to function, return function with args tuple.
 
         Respects function signature defined in function's .argtypes attribute.
 
-        Throws out messages with any but a small list of acceptable characters.
+        Refuses messages with any but a small list of acceptable characters.
 
         """
         import string
 
         """
         import string
-        msg = msg.replace('\n', ' ')  # Inserted by some tablet keyboards.
+        if replace_newline:
+            msg = msg.replace('\n', ' ')  # Inserted by some tablet keyboards.
         legal_chars = string.digits + string.ascii_letters +\
         legal_chars = string.digits + string.ascii_letters +\
-            string.punctuation + ' ' + 'ÄäÖöÜüߧ' + 'éèáàô' + '–'
+            string.punctuation + ' ' + 'ÄäÖöÜüߧ' + 'éèáàô' + '–'
         for c in msg:
             if not c in legal_chars:
                 raise ArgError('Command/message contains illegal character(s), '
         for c in msg:
             if not c in legal_chars:
                 raise ArgError('Command/message contains illegal character(s), '
@@ -151,12 +153,10 @@ class Parser:
             elif tmpl == string_string:
                 args += [arg]
             elif tmpl[:len(string_string) + 1] == string_string + ':':
             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:]
                 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 not string_option_type in self.string_options.keys():
+                    raise ArgError('Unknown string option type: %s' % string_option_type)
+                options = self.string_options[string_option_type]
                 if arg not in options:
                     msg = 'Argument #%s must be one of: %s' % (i + 1, options)
                     raise ArgError(msg)
                 if arg not in options:
                     msg = 'Argument #%s must be one of: %s' % (i + 1, options)
                     raise ArgError(msg)