refactor: move lexer try-catch to runner
This commit is contained in:
@@ -22,7 +22,13 @@ class Runner:
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def run(source: str, path: Optional[Path] = None):
|
def run(source: str, path: Optional[Path] = None):
|
||||||
lexer: Lexer = Lexer(source, path)
|
lexer: Lexer = Lexer(source, path)
|
||||||
tokens: list[Token] = lexer.process()
|
tokens: list[Token]
|
||||||
|
try:
|
||||||
|
tokens = lexer.process()
|
||||||
|
except:
|
||||||
|
print("Partially parsed tokens:")
|
||||||
|
print(lexer.tokens)
|
||||||
|
return
|
||||||
print(list(filter(lambda t: t.type not in Parser.IGNORE, tokens)))
|
print(list(filter(lambda t: t.type not in Parser.IGNORE, tokens)))
|
||||||
|
|
||||||
if Pebble.had_error:
|
if Pebble.had_error:
|
||||||
|
|||||||
@@ -21,13 +21,8 @@ class Lexer:
|
|||||||
raise SyntaxError(f"[ERROR] Error at {self.start_pos}: {msg}")
|
raise SyntaxError(f"[ERROR] Error at {self.start_pos}: {msg}")
|
||||||
|
|
||||||
def process(self) -> list[Token]:
|
def process(self) -> list[Token]:
|
||||||
try:
|
self.scan_tokens()
|
||||||
self.scan_tokens()
|
self.tokens.append(Token(TokenType.EOF, "", None, self.get_position()))
|
||||||
self.tokens.append(Token(TokenType.EOF, "", None, self.get_position()))
|
|
||||||
except Exception as e:
|
|
||||||
print("Partially parsed tokens:")
|
|
||||||
print(self.tokens)
|
|
||||||
raise e
|
|
||||||
return self.tokens
|
return self.tokens
|
||||||
|
|
||||||
def is_at_end(self) -> bool:
|
def is_at_end(self) -> bool:
|
||||||
|
|||||||
Reference in New Issue
Block a user