diff options
Diffstat (limited to 'projects/hackc/classes.py')
-rw-r--r-- | projects/hackc/classes.py | 62 |
1 files changed, 20 insertions, 42 deletions
diff --git a/projects/hackc/classes.py b/projects/hackc/classes.py index bd83cc4..f1358f6 100644 --- a/projects/hackc/classes.py +++ b/projects/hackc/classes.py @@ -30,21 +30,16 @@ class Class: if tokens_total < 4: return None if tokens[0] != "class": - raise JackSyntaxError( - f"Expected `class`, got `{tokens[0]}` instead", tokens[0] - ) + raise UnexpectedToken("class", tokens[0]) name = tokens[1] if name.type != "identifier": raise JackSyntaxError(f"You cannot name a class `{name}`", name) if tokens[2] != LEFT_BRACE: - raise JackSyntaxError( - f"Expected `{LEFT_BRACE}`, got `{tokens[2]}` instead", tokens[2] - ) + raise UnexpectedToken(LEFT_BRACE, tokens[2]) t = 3 - variables = [] while t < tokens_total: variable, dt = Variable.from_tokens(tokens[t:], context="class") @@ -115,7 +110,7 @@ class Variable: type = tokens[1] if type not in VAR_TYPES and type.type != "identifier": - raise JackSyntaxError(f"Expected datatype, got `{tokens[1]}` instead", type) + raise UnexpectedToken("datatype", type) t = 2 names = [] # names of variables @@ -129,26 +124,20 @@ class Variable: names.append(token) expecting_identifier = False else: - raise JackSyntaxError(f"Expected `,`, got `{token}` instead", token) + raise UnexpectedToken(",", token) elif token == ",": if not expecting_identifier: expecting_identifier = True else: - raise JackSyntaxError( - f"Expected variable name, got `,` instead", token - ) + raise UnexpectedToken("variable name", token) elif token == ";": if expecting_identifier: - raise JackSyntaxError( - f"Expected variable name, got `;` instead", token - ) + raise UnexpectedToken("variable name", token) found_semicolon = True break else: expected = "variable name" if expecting_identifier else "`,` or `;`" - raise JackSyntaxError( - f"Expected {expected}, got `{token}` instead", token - ) + raise UnexpectedToken(expected, token) if not found_semicolon: # TODO: print caret at end of token @@ -186,16 +175,14 @@ class ParamList: params = [] for type, name, delim in zip(tokens[0::3], tokens[1::3], tokens[2::3]): if type not in VAR_TYPES and type.type != "identifier": - raise JackSyntaxError(f"Expected datatype, got `{type}` instead", type) + raise UnexpectedToken("datatype", type) if not name: # TODO: print caret at end of type raise JackSyntaxError("Expected variable name", type) if name.type != "identifier": - raise JackSyntaxError( - f"Expected variable name, got `{name}` instead", name - ) + raise UnexpectedToken("variable name", name) if not delim: - raise JackSyntaxError(f"Expected `,` or `{RIGHT_PAREN}`", name) + raise UnexpectedToken(f"`,` or `{RIGHT_PAREN}`", name) if delim == ",": t += 3 params.append((type, name)) @@ -205,9 +192,7 @@ class ParamList: params.append((type, name)) break else: - raise JackSyntaxError( - f"Expected `,` or `{RIGHT_PAREN}`, got `{delim}` instead", delim - ) + raise UnexpectedToken(f"`,` or `{RIGHT_PAREN}`", delim) return (ParamList(params), t) @@ -251,20 +236,14 @@ class Subroutine: return_type = tokens[1] if return_type not in RETURN_TYPES and return_type.type != "identifier": - raise JackSyntaxError( - f"Expected datatype, got `{return_type}` instead", return_type - ) + raise UnexpectedToken("datatype", return_type) name = tokens[2] if name.type != "identifier": - raise JackSyntaxError( - f"Expected {category} name, got `{name}` instead", name - ) + raise UnexpectedToken(f"{category} name", name) if tokens[3] != LEFT_PAREN: - raise JackSyntaxError( - f"Expected `{LEFT_PAREN}`, got `{tokens[3]}` instead", tokens[3] - ) + raise UnexpectedToken(LEFT_PAREN, tokens[3]) t = 4 params, dt = ParamList.from_tokens(tokens[t:]) @@ -276,9 +255,7 @@ class Subroutine: # TODO: catch IndexError body_open = tokens[t] if body_open != LEFT_BRACE: - raise JackSyntaxError( - f"Expected `{LEFT_BRACE}`, got `{body_open}` instead", body_open - ) + raise UnexpectedToken(LEFT_BRACE, body_open) t += 1 variables = [] @@ -299,12 +276,13 @@ class Subroutine: body_close = tokens[t] if body_close != RIGHT_BRACE: - raise JackSyntaxError( - f"Expected `{RIGHT_BRACE}`, got `{body_close}` instead", body_close - ) + raise UnexpectedToken(RIGHT_BRACE, body_close) t += 1 - return (Subroutine(category, return_type, name, params, variables, statements), t) + return ( + Subroutine(category, return_type, name, params, variables, statements), + t, + ) def print_verbose(self): print(f"Define {self.category} {self.type} {self.name}") |