From df0fba34bd764fe7bbb58ee878d1212c3ff4338a Mon Sep 17 00:00:00 2001 From: Shunsuke Shibayama Date: Mon, 28 Nov 2022 00:21:44 +0900 Subject: [PATCH] Fix #205 --- compiler/erg_parser/ast.rs | 4 ++++ compiler/erg_parser/parse.rs | 26 ++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/compiler/erg_parser/ast.rs b/compiler/erg_parser/ast.rs index 9ce69515..2097ba8d 100644 --- a/compiler/erg_parser/ast.rs +++ b/compiler/erg_parser/ast.rs @@ -3450,6 +3450,10 @@ impl Expr { matches!(self, Expr::Accessor(acc) if acc.is_const()) } + pub const fn is_definition(&self) -> bool { + matches!(self, Expr::Def(_) | Expr::ClassDef(_) | Expr::Methods(_)) + } + pub fn need_to_be_closed(&self) -> bool { matches!( self, diff --git a/compiler/erg_parser/parse.rs b/compiler/erg_parser/parse.rs index 978e2d33..76554143 100644 --- a/compiler/erg_parser/parse.rs +++ b/compiler/erg_parser/parse.rs @@ -298,8 +298,15 @@ impl Parser { .try_reduce_chunk(true, false) .map_err(|_| self.stack_dec())?; block.push(chunk); - self.level -= 1; - return Ok(block); + if block.last().unwrap().is_definition() { + let err = ParseError::simple_syntax_error(0, block.last().unwrap().loc()); + self.level -= 1; + self.errs.push(err); + return Err(()); + } else { + self.level -= 1; + return Ok(block); + } } if !self.cur_is(Newline) { let err = self.skip_and_throw_syntax_err("try_reduce_block"); @@ -362,6 +369,21 @@ impl Parser { self.level -= 1; self.errs.push(err); Err(()) + } else if block.last().unwrap().is_definition() { + let err = ParseError::syntax_error( + line!() as usize, + block.last().unwrap().loc(), + switch_lang!( + "japanese" => "ブロックの終端で変数を定義することは出来ません", + "simplified_chinese" => "无法在块的末尾定义变量", + "traditional_chinese" => "無法在塊的末尾定義變量", + "english" => "cannot define a variable at the end of a block", + ), + None, + ); + self.level -= 1; + self.errs.push(err); + Err(()) } else { self.level -= 1; Ok(block)