Class: Luaof::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/luaof/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label, mapping) ⇒ Node

Returns a new instance of Node.



8
9
10
11
12
# File 'lib/luaof/node.rb', line 8

def initialize(label, mapping)
  @label = label
  @children = []
  @mapping = mapping
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



6
7
8
# File 'lib/luaof/node.rb', line 6

def children
  @children
end

#labelObject (readonly)

Returns the value of attribute label.



5
6
7
# File 'lib/luaof/node.rb', line 5

def label
  @label
end

Instance Method Details

#deconstruct_keys(_keys) ⇒ Object



115
116
117
# File 'lib/luaof/node.rb', line 115

def deconstruct_keys(_keys)
  { label: @label, children: @children }
end

#expand_indicator(directives) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/luaof/node.rb', line 31

def expand_indicator(directives)
  result = +''
  directives.each do |directive|
    directive => String
    result.concat(directive)
  end
  result
end

#parse_api_indicator!Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/luaof/node.rb', line 14

def parse_api_indicator!
  case self
  in label: { tag: :APIEntry }, children: [{ label: { tag: :apii }, children: indicator }, *_]
    pop, push, error, nope = expand_indicator(indicator).split(',')
    nope and raise indicator
    @label.update(indicator: { pop:, push:, error: })
    @children.shift
  in label: { tag: :apii }, children: indicator
    pop, push, error, nope = expand_indicator(indicator).split(',')
    nope and raise indicator
    @label.update(pop:, push:, error:)
    @children = []
  else
    @children.select { _1.is_a?(Node) }.each(&:parse_api_indicator!)
  end
end

#parse_section!Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/luaof/node.rb', line 40

def parse_section!
  case self
  in label: { tag: :section } => label,
     children: [Node[label: { tag: :title }, children: title], *_] => children
    children.shift
    @label = { **label, title: }
  else
    # nop
  end
  @children.select { _1.is_a?(Node) }.each(&:parse_section!)
end

#pretty_print(q) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/luaof/node.rb', line 119

def pretty_print(q)
  q.object_group(self) do
    q.breakable
    q.text 'label='
    q.pp(@label)
    q.text ','
    q.breakable
    q.text 'children='
    q.pp(@children)
  end
end

#register_mapping!Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/luaof/node.rb', line 52

def register_mapping!
  case self
  in label: { tag: :section, param: [String => id] }
    id = "sec:#{id}"
    @mapping[id] and raise "already registered: #{id}"
    @label[:id] = id
    @mapping[id] = self
  in label: { tag: :APIEntry, param: }
    text = signaturetext(param)
    text.match(/ (lua L? _ \w+) [)]? [ ]+ [(] /x) or
      text.match(/ (lua L? _ \w+) /x)
    id = ::Regexp.last_match(1) or raise "empty: #{text}"
    id = id.to_s
    @mapping[id] and raise "already registered: #{id}"
    @label[:id] = id
    @mapping[id] = self
  in label: { tag: :defid }, children: String => id
    @mapping[id] and raise "already registered: #{id}"
    @label[:id] = id
    @mapping[id] = self
  else
    # nop
  end
  @children.select { _1.is_a?(Node) }.each(&:register_mapping!)
end

#register_refs!Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/luaof/node.rb', line 78

def register_refs!
  case self
  in label: { tag: :see }, children: [String => id]
    label = @mapping[id] || @mapping["sec:#{id}"] or
      raise "not found: #{id}"
    @label[:param] = label
  in label: { tag: :See }, children: [String => id]
    label = @mapping[id] || @mapping["sec:#{id}"] or
      raise "not found: #{id}"
    @label[:param] = label
  else
    # nop
  end
  @children.select { _1.is_a?(Node) }.each(&:register_refs!)
end

#signaturetext(target) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/luaof/node.rb', line 94

def signaturetext(target)
  case target
  in Array
    target.map { |t| signaturetext(t) }.join
  in String
    target
  in :linebreak
    "\n"
  in keyword: :ldots
    '\\ldots{}'
  in Node[label: { tag: :rep }, children:]
    content = children.map { |c| signaturetext(c) }.join
    "\\emph{#{content}}"
  in Node[label: :leftbrace, children:]
    content = children.map { |c| signaturetext(c) }.join
    "{#{content}}"
  else
    raise target.inspect[..500]
  end
end