Class: Rouge::Token

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

Defined Under Namespace

Modules: Tokens

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

.parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

.shortnameObject (readonly)

Returns the value of attribute shortname.



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

def shortname
  @shortname
end

Class Method Details

.[](qualname) ⇒ Object



18
19
20
21
22
# File 'lib/rouge/token.rb', line 18

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

  Token.cache[qualname]
end

.cacheObject



10
11
12
# File 'lib/rouge/token.rb', line 10

def cache
  @cache ||= {}
end

.each_token(&b) ⇒ Object



61
62
63
64
65
# File 'lib/rouge/token.rb', line 61

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

.inspectObject



24
25
26
# File 'lib/rouge/token.rb', line 24

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

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



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

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)


28
29
30
# File 'lib/rouge/token.rb', line 28

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

.qualnameObject



36
37
38
# File 'lib/rouge/token.rb', line 36

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

.register!Object



40
41
42
43
# File 'lib/rouge/token.rb', line 40

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

.sub_tokensObject



14
15
16
# File 'lib/rouge/token.rb', line 14

def sub_tokens
  @sub_tokens ||= {}
end

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



56
57
58
59
# File 'lib/rouge/token.rb', line 56

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

.token_chainObject



32
33
34
# File 'lib/rouge/token.rb', line 32

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