Parse a message: message Dog [id=102] { ... } Supports: - Inline type options: message Dog [id=102] { ... } - Nested messages and enums: message Outer { message Inner { ... } enum Status { ... } Inner inner = 1;
(self)
| 466 | ) |
| 467 | |
| 468 | def parse_message(self) -> Message: |
| 469 | """Parse a message: message Dog [id=102] { ... } |
| 470 | |
| 471 | Supports: |
| 472 | - Inline type options: message Dog [id=102] { ... } |
| 473 | - Nested messages and enums: |
| 474 | message Outer { |
| 475 | message Inner { ... } |
| 476 | enum Status { ... } |
| 477 | Inner inner = 1; |
| 478 | } |
| 479 | """ |
| 480 | start = self.current() |
| 481 | self.consume(TokenType.MESSAGE) |
| 482 | name = self.consume(TokenType.IDENT, "Expected message name").value |
| 483 | |
| 484 | # Optional inline type options: [id=102, deprecated=true] |
| 485 | type_id = None |
| 486 | inline_options = {} |
| 487 | if self.check(TokenType.LBRACKET): |
| 488 | inline_options = self.parse_type_options(name, KNOWN_MESSAGE_OPTIONS) |
| 489 | if "id" in inline_options: |
| 490 | type_id = inline_options["id"] |
| 491 | |
| 492 | self.consume(TokenType.LBRACE, "Expected '{' after message name") |
| 493 | |
| 494 | fields = [] |
| 495 | nested_messages = [] |
| 496 | nested_enums = [] |
| 497 | nested_unions = [] |
| 498 | body_options = {} |
| 499 | |
| 500 | while not self.check(TokenType.RBRACE): |
| 501 | if self.check(TokenType.RESERVED): |
| 502 | self.parse_reserved() |
| 503 | elif self.check(TokenType.OPTION): |
| 504 | raise self.error("Option statements inside message are not supported") |
| 505 | elif self.check(TokenType.MESSAGE): |
| 506 | nested_messages.append(self.parse_message()) |
| 507 | elif self.check(TokenType.ENUM): |
| 508 | nested_enums.append(self.parse_enum()) |
| 509 | elif self.check(TokenType.UNION): |
| 510 | nested_unions.append(self.parse_union()) |
| 511 | else: |
| 512 | fields.append(self.parse_field()) |
| 513 | |
| 514 | self.consume(TokenType.RBRACE, "Expected '}' after message fields") |
| 515 | |
| 516 | # Merge inline options and body options (body options take precedence) |
| 517 | all_options = {**inline_options, **body_options} |
| 518 | |
| 519 | return Message( |
| 520 | name=name, |
| 521 | type_id=type_id, |
| 522 | fields=fields, |
| 523 | nested_messages=nested_messages, |
| 524 | nested_enums=nested_enums, |
| 525 | nested_unions=nested_unions, |
no test coverage detected