Sha256: c34c436d2ec600bfcb76bccc4d279749513bcdc51d4d35b681714815e2e14b98

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

# 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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
carbon-compiler-0.2.0 lib/carbon/compiler/parser/root/enum.rb