Sha256: afe91380c06ff92167dfc59fd695990342192456edb5b7dd2f526a8f2f4952b4

Contents?: true

Size: 1.7 KB

Versions: 1

Compression:

Stored size: 1.7 KB

Contents

# encoding: utf-8
# frozen_string_literal: true

module Carbon
  module Compiler
    class Scanner
      # A token from the scanner.
      class Token
        # The type of the token.  For any explicit matches (e.g. `"module"`),
        # this is the exact match (e.g. `:module`).  For any regular matches
        # (e.g. an identifier), this is in all caps (e.g. `:IDENTIFIER`).
        #
        # @return [Symbol]
        attr_reader :type
        # The lexeme that this token is associated.  This is what the token
        # matched directly from the source.
        #
        # @return [String]
        attr_reader :value
        # The exact location the token was taken from.  This only spans one
        # line.
        #
        # @return [Location]
        attr_reader :location

        # Initialize the token.  Freezes it after initializing.
        #
        # @param type [Symbol] The type.  See {#type}.
        # @param value [String] The value.  See {#value}.
        # @param location [Location] The location.  See {#location}.
        def initialize(type, value, location)
          @type = type
          @value = value
          @location = location
          @children = []
          to_a
          freeze
        end

        # Creates an array representation of this class.  This is cached and
        # frozen, since nothing about this class changes.
        #
        # @return [(Class, Symbol, String, Location)]
        def to_a
          @array ||= [self.class, @type, @value, @location].freeze
        end

        # Creates a numeric representation of this class for hashing.
        #
        # @see #to_a
        # @return [Numeric]
        def hash
          to_a.hash
        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/scanner/token.rb