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.



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

def name
  @name
end

.parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

.shortnameObject (readonly)

Returns the value of attribute shortname.



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

def shortname
  @shortname
end

Class Method Details

.[](qualname) ⇒ Object



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

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

  Token.cache[qualname]
end

.cacheObject



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

def cache
  @cache ||= {}
end

.each_token(&b) ⇒ Object



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

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

.inspectObject



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

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

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



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

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)


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

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

.qualnameObject



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

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

.register!Object



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

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

.sub_tokensObject



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

def sub_tokens
  @sub_tokens ||= {}
end

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



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

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

.token_chainObject



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

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