From 1ce2bccd3747f5c311f40389fa3895e29dfae21b Mon Sep 17 00:00:00 2001 From: Bryan Bishop Date: Sat, 31 Aug 2013 09:45:12 -0500 Subject: [PATCH 1/6] generic preprocessor-specific exception classes These are basic python Exception subclasses that can be used to throw more specific errors and exceptions from within the preprocessor. AssertionError is not a good idea. --- preprocessor.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/preprocessor.py b/preprocessor.py index b6bdfbe7d..69121747d 100644 --- a/preprocessor.py +++ b/preprocessor.py @@ -302,6 +302,16 @@ chars = { "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. From ecedde19931bd7d1eb71c59498dab98944639b85 Mon Sep 17 00:00:00 2001 From: Bryan Bishop Date: Sat, 31 Aug 2013 09:46:11 -0500 Subject: [PATCH 2/6] replace an assert in macro_translator Use a MacroException instead of an AssertionError. --- preprocessor.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/preprocessor.py b/preprocessor.py index 69121747d..a6a9efd29 100644 --- a/preprocessor.py +++ b/preprocessor.py @@ -449,7 +449,8 @@ def macro_translator(macro, token, line): Converts a line with a macro into a rgbasm-compatible line. """ - assert macro.macro_name == token, "macro/token mismatch" + if macro.macro_name != token: + raise MacroException("macro/token mismatch") original_line = line From 2c22d9220c48b2a36550b5a5a2bae1e0988da0d6 Mon Sep 17 00:00:00 2001 From: Bryan Bishop Date: Sat, 31 Aug 2013 09:50:17 -0500 Subject: [PATCH 3/6] fix "raise Exception" formatting in preprocessor --- preprocessor.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/preprocessor.py b/preprocessor.py index a6a9efd29..905e27533 100644 --- a/preprocessor.py +++ b/preprocessor.py @@ -478,7 +478,7 @@ def macro_translator(macro, token, line): # check if there are no params (redundant) if len(params) == 1 and params[0] == "": - raise Exception, "macro has no params?" + raise Exception("macro has no params?") # write out a comment showing the original line if show_original_lines: @@ -519,10 +519,14 @@ def macro_translator(macro, token, line): 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 Exception( + "dunno what to do with a macro param with a size > 3" + ) else: - raise Exception, "dunno what to do with this non db/dw macro param: " + \ - str(param_klass) + " in line: " + original_line + raise Exception( + "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"): @@ -577,9 +581,13 @@ def macro_translator(macro, token, line): 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 Exception( + "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: From 95f7270141cbdb32c7d178a8a89a822e66e3a113 Mon Sep 17 00:00:00 2001 From: Bryan Bishop Date: Sat, 31 Aug 2013 09:51:31 -0500 Subject: [PATCH 4/6] raise MacroException instead of Exception A more specific exception means that error handling can actually work in the future. --- preprocessor.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/preprocessor.py b/preprocessor.py index 905e27533..daed157d4 100644 --- a/preprocessor.py +++ b/preprocessor.py @@ -478,7 +478,7 @@ def macro_translator(macro, token, line): # 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: @@ -519,11 +519,11 @@ def macro_translator(macro, token, line): elif param_klass.size == 3: allowed_length += 2 # bank and label else: - raise Exception( + raise MacroException( "dunno what to do with a macro param with a size > 3" ) else: - raise Exception( + raise MacroException( "dunno what to do with this non db/dw macro param: {klass} in line {line}" .format(klass=param_klass, line=original_line) ) @@ -581,7 +581,7 @@ def macro_translator(macro, token, line): output += ("db " + param_klass.from_asm(param) + "\n") index += 1 else: - raise Exception( + raise MacroException( "dunno what to do with this macro param ({klass}) in line: {line}" .format( klass=param_klass, From 93514b18629fe42c0a2688dac57d90998ea5daaa Mon Sep 17 00:00:00 2001 From: Bryan Bishop Date: Sat, 31 Aug 2013 09:54:03 -0500 Subject: [PATCH 5/6] convert a macro_translator assert in preprocessor AssertionError -> PreprocessorException --- preprocessor.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/preprocessor.py b/preprocessor.py index daed157d4..c6ae137c4 100644 --- a/preprocessor.py +++ b/preprocessor.py @@ -534,9 +534,15 @@ def macro_translator(macro, token, line): 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 --- From ebb591a7ec624acce35d0378201c641724ac8d4c Mon Sep 17 00:00:00 2001 From: Bryan Bishop Date: Sat, 31 Aug 2013 09:56:24 -0500 Subject: [PATCH 6/6] make a MacroException more verbose in preprocessor --- preprocessor.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/preprocessor.py b/preprocessor.py index c6ae137c4..a31386801 100644 --- a/preprocessor.py +++ b/preprocessor.py @@ -520,7 +520,8 @@ def macro_translator(macro, token, line): allowed_length += 2 # bank and label else: raise MacroException( - "dunno what to do with a macro param with a size > 3" + "dunno what to do with a macro param with a size > 3 (size={size})" + .format(size=param_klass.size) ) else: raise MacroException(