MCPcopy Create free account
hub / github.com/apache/fory / parse_enum

Method parse_enum

compiler/fory_compiler/frontend/fdl/parser.py:365–408  ·  view source on GitHub ↗

Parse an enum: enum Color [id=101] { ... } Supports: - Inline type options: enum Color [id=101] { ... }

(self)

Source from the content-addressed store, hash-verified

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.

Callers 2

parseMethod · 0.95
parse_messageMethod · 0.95

Calls 10

currentMethod · 0.95
consumeMethod · 0.95
checkMethod · 0.95
parse_type_optionsMethod · 0.95
errorMethod · 0.95
parse_reservedMethod · 0.95
parse_enum_valueMethod · 0.95
make_locationMethod · 0.95
EnumClass · 0.90
appendMethod · 0.45

Tested by

no test coverage detected