shithub: pokecrystal

Download patch

ref: e91fb8ac14da977a3d4ef76ea74c76c867247660
parent: 5118fb757879f7ccf177b7d84582d85c6dd5d14c
parent: ba67d3ba4675ed4f4a918264707788f9c4979c9b
author: Bryan Bishop <[email protected]>
date: Sat Aug 31 04:17:51 EDT 2013

Merge pull request #182 from kanzure/preprocessor-asserts

Use PreprocessorException instead of AssertionError in preprocessor

--- a/preprocessor.py
+++ b/preprocessor.py
@@ -296,6 +296,16 @@
 "9": 0xFF
 }
 
+class PreprocessorException(Exception):
+    """
+    There was a problem in the preprocessor.
+    """
+
+class MacroException(PreprocessorException):
+    """
+    There was a problem with a macro.
+    """
+
 def separate_comment(l):
     """
     Separates asm and comments on a single line.
@@ -435,7 +445,8 @@
     @param show_original_lines: show lines before preprocessing in stdout
     @param do_macro_sanity_check: helpful for debugging macros
     """
-    assert macro.macro_name == token, "macro/token mismatch"
+    if macro.macro_name != token:
+        raise MacroException("macro/token mismatch")
 
     original_line = line
 
@@ -463,7 +474,7 @@
 
         # check if there are no params (redundant)
         if len(params) == 1 and params[0] == "":
-            raise Exception, "macro has no params?"
+            raise MacroException("macro has no params?")
 
     # write out a comment showing the original line
     if show_original_lines:
@@ -504,10 +515,15 @@
                 elif param_klass.size == 3:
                     allowed_length += 2 # bank and label
                 else:
-                    raise Exception, "dunno what to do with a macro param with a size > 3"
+                    raise MacroException(
+                        "dunno what to do with a macro param with a size > 3 (size={size})"
+                        .format(size=param_klass.size)
+                    )
             else:
-                raise Exception, "dunno what to do with this non db/dw macro param: " + \
-                                 str(param_klass) + " in line: " + original_line
+                raise MacroException(
+                    "dunno what to do with this non db/dw macro param: {klass} in line {line}"
+                    .format(klass=param_klass, line=original_line)
+                )
 
         # sometimes the allowed length can vary
         if hasattr(macro, "allowed_lengths"):
@@ -515,9 +531,15 @@
         else:
             allowed_lengths = [allowed_length]
 
-        assert len(params) in allowed_lengths, \
-               "mismatched number of parameters on this line: " + \
-               original_line
+        if len(params) not in allowed_lengths:
+            raise PreprocessorException(
+                "mismatched number of parameters ({count}, instead of any of {allowed}) on this line: {line}"
+                .format(
+                    count=len(params),
+                    allowed=allowed_lengths,
+                    line=original_line,
+                )
+            )
 
     # --- end of ridiculously long sanity check ---
 
@@ -562,9 +584,13 @@
                 output += ("db " + param_klass.from_asm(param) + "\n")
                 index += 1
             else:
-                raise Exception, "dunno what to do with this macro " + \
-                "param (" + str(param_klass) + ") " + "on this line: " + \
-                original_line
+                raise MacroException(
+                    "dunno what to do with this macro param ({klass}) in line: {line}"
+                    .format(
+                        klass=param_klass,
+                        line=original_line,
+                    )
+                )
 
         # or just print out the byte
         else: