Sha256: 2b482d6b7bd56ce67bb5ecc45cec1e005c9db890171690ff95d0cb412aadb385

Contents?: true

Size: 1.6 KB

Versions: 9

Compression:

Stored size: 1.6 KB

Contents

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

class Rouge::Symbol
  include Rouge::Metadata

  # 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
    :"#{@ns ? "#@ns/" : ""}#@name"
  end

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

  def to_s; inspect; end

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

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

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

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rouge-lang-0.0.9 lib/rouge/symbol.rb
rouge-lang-0.0.8 lib/rouge/symbol.rb
rouge-lang-0.0.7 lib/rouge/symbol.rb
rouge-lang-0.0.6 lib/rouge/symbol.rb
rouge-lang-0.0.5 lib/rouge/symbol.rb
rouge-lang-0.0.4 lib/rouge/symbol.rb
rouge-lang-0.0.3 lib/rouge/symbol.rb
rouge-lang-0.0.2 lib/rouge/symbol.rb
rouge-lang-0.0.1 lib/rouge/symbol.rb