From 8286580ef27d1434c3c2c52e71f19b4a42ec19cf Mon Sep 17 00:00:00 2001 From: mryouse Date: Sat, 18 Jun 2022 01:47:06 +0000 Subject: make LexError a NebPanic, and fix lexing errors with :bools --- exceptions.py | 3 +++ lexer.py | 18 +++++++++++------- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/exceptions.py b/exceptions.py index 229ec88..8bbe000 100644 --- a/exceptions.py +++ b/exceptions.py @@ -8,3 +8,6 @@ class InterpretPanic(NebPanic): big_message += f" (got {arg})" super().__init__(big_message) +class LexError(NebPanic): + def __init__(self, message, line): + super().__init__(f"line {line}: {message}") diff --git a/lexer.py b/lexer.py index bce3f51..11b5750 100644 --- a/lexer.py +++ b/lexer.py @@ -1,10 +1,7 @@ from structs import TokenType, Token +from exceptions import LexError import sys -class LexError(BaseException): - - def __init__(self, message, line): - super().__init__(f"line {line}: {message}") types = { ":int": TokenType.INT_TYPE, @@ -146,12 +143,19 @@ def get_string(data, line): return Token(TokenType.STRING, str(string), str(string), line), counter + 1, offset def get_bool(data, line): - if len(data) >= 4 and data[:4] == "true": + counter = 0 + value = "" + while data[counter] not in SEPARATORS: + value += data[counter] + counter += 1 + if counter >= len(data): + break + if value == "true": return Token(TokenType.TRUE, "#true", True, line), 4 - elif len(data) >= 5 and data[:5] == "false": + elif value == "false": return Token(TokenType.FALSE, "#false", False, line), 5 else: - raise Exception("couldn't parse boolean") + raise LexError("couldn't parse boolean", line) def get_symbol(data, line): counter = 0 -- cgit v1.2.3