From 0dbc631c96431aeb795c41ed7184088931910acc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Thu, 12 Jan 2023 08:40:21 +0100 Subject: [PATCH] Parser - Disallow static initializers --- src/parser.rs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 15d2177..f3bee1f 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -226,13 +226,24 @@ impl Parser { self.consume(&TokenType::LeftBrace, "'{' expected")?; let mut members = Vec::new(); while !self.check(&TokenType::RightBrace) && !self.is_at_end() { - let is_static = self.expect(&[TokenType::Static]).is_some(); + let static_token = self.expect(&[TokenType::Static]); match self.parse_function(FunctionKind::Method)? { - StmtNode::FunDecl(d) => members.push(if is_static { - ClassMemberDecl::StaticMethod(d) - } else { - ClassMemberDecl::Method(d) - }), + StmtNode::FunDecl(d) => { + if let Some(tok) = static_token { + if d.name.lexeme == "init" { + return Err(SloxError::with_token( + ErrorKind::Parse, + &tok, + "initializer cannot be declared static".to_owned(), + )); + } + } + members.push(if static_token.is_some() { + ClassMemberDecl::StaticMethod(d) + } else { + ClassMemberDecl::Method(d) + }) + } _ => panic!("Function declaration expected"), } }