MCPcopy Index your code
hub / github.com/aiscriptdev/aiscript / enum_declaration

Method enum_declaration

aiscript-vm/src/parser/mod.rs:346–437  ·  view source on GitHub ↗
(&mut self, visibility: Visibility)

Source from the content-addressed store, hash-verified

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 {

Callers 1

declarationMethod · 0.80

Calls 15

EnumClass · 0.85
consume_eitherMethod · 0.80
pushMethod · 0.80
checkMethod · 0.80
check_nextMethod · 0.80
error_at_currentMethod · 0.80
is_at_endMethod · 0.80
method_declarationMethod · 0.80
advanceMethod · 0.80
check_variantMethod · 0.80
match_tokenMethod · 0.80

Tested by

no test coverage detected