Parse an enum: enum Color [id=101] { ... } Supports: - Inline type options: enum Color [id=101] { ... }
(self)
| 363 | ) |
| 364 | |
| 365 | def parse_enum(self) -> Enum: |
| 366 | """Parse an enum: enum Color [id=101] { ... } |
| 367 | |
| 368 | Supports: |
| 369 | - Inline type options: enum Color [id=101] { ... } |
| 370 | """ |
| 371 | start = self.current() |
| 372 | self.consume(TokenType.ENUM) |
| 373 | name = self.consume(TokenType.IDENT, "Expected enum name").value |
| 374 | |
| 375 | # Optional inline type options: [id=101, deprecated=true] |
| 376 | type_id = None |
| 377 | inline_options = {} |
| 378 | if self.check(TokenType.LBRACKET): |
| 379 | inline_options = self.parse_type_options(name, KNOWN_ENUM_OPTIONS) |
| 380 | if "id" in inline_options: |
| 381 | type_id = inline_options["id"] |
| 382 | |
| 383 | self.consume(TokenType.LBRACE, "Expected '{' after enum name") |
| 384 | |
| 385 | values = [] |
| 386 | body_options = {} |
| 387 | while not self.check(TokenType.RBRACE): |
| 388 | if self.check(TokenType.OPTION): |
| 389 | raise self.error("Option statements inside enum are not supported") |
| 390 | if self.check(TokenType.RESERVED): |
| 391 | self.parse_reserved() |
| 392 | else: |
| 393 | values.append(self.parse_enum_value()) |
| 394 | |
| 395 | self.consume(TokenType.RBRACE, "Expected '}' after enum values") |
| 396 | |
| 397 | # Merge inline options and body options (body options take precedence) |
| 398 | all_options = {**inline_options, **body_options} |
| 399 | |
| 400 | return Enum( |
| 401 | name=name, |
| 402 | type_id=type_id, |
| 403 | values=values, |
| 404 | options=all_options, |
| 405 | line=start.line, |
| 406 | column=start.column, |
| 407 | location=self.make_location(start), |
| 408 | ) |
| 409 | |
| 410 | def parse_reserved(self): |
| 411 | """Parse a reserved statement. |
no test coverage detected