Class: Rouge::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/rouge/token.rb

Defined Under Namespace

Modules: Tokens

Class Method Summary collapse

Class Method Details

.[](qualname) ⇒ Object


16
17
18
19
20
# File 'lib/rouge/token.rb', line 16

def [](qualname)
  return qualname unless qualname.is_a? ::String

  Token.cache[qualname]
end

.cacheObject


8
9
10
# File 'lib/rouge/token.rb', line 8

def cache
  @cache ||= {}
end

.each_token(&b) ⇒ Object


59
60
61
62
63
# File 'lib/rouge/token.rb', line 59

def each_token(&b)
  Token.cache.each do |(_, t)|
    b.call(t)
  end
end

.inspectObject


22
23
24
# File 'lib/rouge/token.rb', line 22

def inspect
  "<Token #{qualname}>"
end

.make_token(name, shortname, &b) ⇒ Object


43
44
45
46
47
48
49
50
51
52
# File 'lib/rouge/token.rb', line 43

def make_token(name, shortname, &b)
  parent = self
  Class.new(parent) do
    @parent = parent
    @name = name
    @shortname = shortname
    register!
    class_eval(&b) if b
  end
end

.matches?(other) ⇒ Boolean

Returns:

  • (Boolean)

26
27
28
# File 'lib/rouge/token.rb', line 26

def matches?(other)
  other.token_chain.include? self
end

.nameObject


4
# File 'lib/rouge/token.rb', line 4

def name; @name; end

.parentObject


5
# File 'lib/rouge/token.rb', line 5

def parent; @parent; end

.qualnameObject


34
35
36
# File 'lib/rouge/token.rb', line 34

def qualname
  @qualname ||= token_chain.map(&:name).join('.')
end

.register!Object


38
39
40
41
# File 'lib/rouge/token.rb', line 38

def register!
  Token.cache[self.qualname] = self
  parent.sub_tokens[self.name] = self
end

.shortnameObject


6
# File 'lib/rouge/token.rb', line 6

def shortname; @shortname; end

.sub_tokensObject


12
13
14
# File 'lib/rouge/token.rb', line 12

def sub_tokens
  @sub_tokens ||= {}
end

.token(name, shortname, &b) ⇒ Object


54
55
56
57
# File 'lib/rouge/token.rb', line 54

def token(name, shortname, &b)
  tok = make_token(name, shortname, &b)
  const_set(name, tok)
end

.token_chainObject


30
31
32
# File 'lib/rouge/token.rb', line 30

def token_chain
  @token_chain ||= ancestors.take_while { |x| x != Token }.reverse
end