# encoding: utf-8 # frozen_string_literal: true module Carbon module Compiler class Parser module Root # Parses an enum. module Enum protected def parse_enum start = expect :enum expect :do elements = [] elements << parse_enum_element until peek?(:end) stop = expect :end Node::Definition::Enum.new(elements, components: [start, stop]) end def parse_enum_element name = expect :MNAME value = parse_enum_element_value if peek?(:"(") || peek?(:"=") expect :";" Node::Definition::Enum::Element.new([name, value]) end def parse_enum_element_value case peek.type when :"(" then parse_unit_type when :"=" then parse_enum_element_value_expr end end def parse_enum_element_value_expr expect :"=" parse_expression end end end end end end