Sha256: 2b7932d59c51d93ed81cf1855427db7d0265e4e14dad94fde7f9bf9802fe86e9

Contents?: true

Size: 1.6 KB

Versions: 7

Compression:

Stored size: 1.6 KB

Contents

# encoding: utf-8
require 'rouge/metadata'

class Rouge::Symbol
  include Rouge::Metadata

  attr_reader :ns, :name, :ns_s, :name_s, :name_parts, :new_sym

  # The symbols for t/f/n are the Ruby objects themselves.
  LOOKUP = {
    :true => true,
    :false => false,
    :nil => nil,
  }

  KNOWNS = {
    :/ => [nil, :/],
    :"./" => [nil, :"./"],
    :"rouge.core//" => [:"rouge.core", :/],
    :"rouge.core/./" => [:"rouge.core", :"./"]
  }

  CACHE = {}

  def initialize(sym)
    if r = KNOWNS[sym]
      @ns = r[0]
      @name = r[1]
    else
      str = sym.to_s
      solidus = str.rindex('/')
      if solidus
        @ns = str[0...solidus].intern
        @name = str[solidus + 1..-1].intern
      else
        @ns = nil
        @name = sym
      end
    end

    @ns_s = @ns.to_s unless @ns.nil?
    @name_s = @name.to_s

    # split(sep, 0) means a trailing '.' won't become an empty component.  (0
    # is default)  Contrast with split(sep, -1).
    @name_parts =
      @name_s.length > 1 ? @name_s.split('.', 0).map(&:intern) : [@name]

    @new_sym = (@name_s[-1] == ?. and @name_s.length > 1)
  end

  def self.[](inner)
    return LOOKUP[inner] if LOOKUP.include? inner
    c = CACHE[inner]
    return c.dup if c
    # Note: don't cache symbols themselves, they may have metadata.
    c = new inner
    CACHE[inner] = c.dup.freeze
    c
  end

  def to_sym
    :"#{to_s}"
  end

  def inspect
    "Rouge::Symbol[#{to_sym.inspect}]"
  end

  def to_s
    "#{@ns ? "#@ns/" : ""}#@name"
  end

  def ==(right)
    right.is_a?(Rouge::Symbol) and right.ns == @ns and right.name == @name
  end
end

# vim: set sw=2 et cc=80:

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rouge-lang-0.0.16 lib/rouge/symbol.rb
rouge-lang-0.0.15 lib/rouge/symbol.rb
rouge-lang-0.0.14 lib/rouge/symbol.rb
rouge-lang-0.0.13 lib/rouge/symbol.rb
rouge-lang-0.0.12 lib/rouge/symbol.rb
rouge-lang-0.0.11 lib/rouge/symbol.rb
rouge-lang-0.0.10 lib/rouge/symbol.rb