From 21977a3a8e47a5c5c8acdcf24acda275ed9b6d34 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Mon, 10 Apr 2023 17:04:11 +0800 Subject: [PATCH] Update compiler.h --- src/compiler.h | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/compiler.h b/src/compiler.h index c56a65ef..f717499b 100644 --- a/src/compiler.h +++ b/src/compiler.h @@ -28,6 +28,10 @@ class Compiler { const Token& prev() { return tokens.at(i-1); } const Token& curr() { return tokens.at(i); } const Token& next() { return tokens.at(i+1); } + const Token& err() { + if(i >= tokens.size()) return prev(); + return curr(); + } void advance(int delta=1) { i += delta; } CodeEmitContext* ctx() { return &contexts.top(); } @@ -921,9 +925,9 @@ __SUBSCR_END: return nullptr; } - void SyntaxError(Str msg){ lexer->throw_err("SyntaxError", msg, curr().line, curr().start); } - void SyntaxError(){ lexer->throw_err("SyntaxError", "invalid syntax", curr().line, curr().start); } - void IndentationError(Str msg){ lexer->throw_err("IndentationError", msg, curr().line, curr().start); } + void SyntaxError(Str msg){ lexer->throw_err("SyntaxError", msg, err().line, err().start); } + void SyntaxError(){ lexer->throw_err("SyntaxError", "invalid syntax", err().line, err().start); } + void IndentationError(Str msg){ lexer->throw_err("IndentationError", msg, err().line, err().start); } public: Compiler(VM* vm, const Str& source, const Str& filename, CompileMode mode){