(&mut self, visibility: Visibility)
| 344 | } |
| 345 | |
| 346 | fn enum_declaration(&mut self, visibility: Visibility) -> Option<Stmt<'gc>> { |
| 347 | self.consume_either(TokenType::Identifier, TokenType::Error, "Expect enum name."); |
| 348 | let name = self.previous; |
| 349 | self.scopes.push(name.lexeme.to_owned()); |
| 350 | if self.check(TokenType::OpenParen) && self.check_next(TokenType::Identifier) { |
| 351 | self.error_at_current("Enum doesn't support inherit."); |
| 352 | } |
| 353 | |
| 354 | let class_compiler = ClassCompiler { |
| 355 | has_superclass: false, |
| 356 | is_enum: true, |
| 357 | enclosing: self.class_compiler.take(), |
| 358 | current_method_type: self.fn_type, |
| 359 | }; |
| 360 | self.class_compiler = Some(Box::new(class_compiler)); |
| 361 | self.consume(TokenType::OpenBrace, "Expect '{' before enum body."); |
| 362 | |
| 363 | let mut variants = Vec::new(); |
| 364 | let mut methods = Vec::new(); |
| 365 | let mut checker = EnumVariantChecker::new(name.lexeme); |
| 366 | |
| 367 | while !self.check(TokenType::CloseBrace) && !self.is_at_end() { |
| 368 | if self.current.is_function_def_keyword() { |
| 369 | // Parse method |
| 370 | methods.push(self.method_declaration()?); |
| 371 | continue; |
| 372 | } |
| 373 | |
| 374 | // Parse variant |
| 375 | if !self.check(TokenType::Identifier) { |
| 376 | self.error_at_current("Expect variant name."); |
| 377 | } |
| 378 | // Consume the variant identifier |
| 379 | self.advance(); |
| 380 | let variant_name = self.previous; |
| 381 | if let Err(err) = checker.check_variant(variant_name) { |
| 382 | self.error_at(variant_name, &err); |
| 383 | } |
| 384 | |
| 385 | let value = if self.match_token(TokenType::Equal) { |
| 386 | // Check for valid literal tokens |
| 387 | if !self.current.is_literal_token() { |
| 388 | self.error_at_current( |
| 389 | "Enum variant value must be a literal (number, string, or boolean)", |
| 390 | ); |
| 391 | return None; |
| 392 | } |
| 393 | |
| 394 | if let Some(Expr::Literal { value: literal, .. }) = self.expression() { |
| 395 | if let Err(msg) = checker.check_value(variant_name, &literal) { |
| 396 | self.error_at(variant_name, &msg); |
| 397 | return None; |
| 398 | } |
| 399 | Some(literal) |
| 400 | } else { |
| 401 | None |
| 402 | } |
| 403 | } else { |
no test coverage detected