44
55module DFHackLuaDefinitions
66 class << self
7+ # Attempts to return the relevant Ruby class for the provided XML node.
78 def node_to_type ( node , path )
89 case node [ 'ld:meta' ]
910 when 'enum-type'
10- EnumType . new ( node )
11+ EnumType . new ( node : )
1112 when 'bitfield-type'
12- BitfieldType . new ( node )
13+ BitfieldType . new ( node : )
1314 when 'class-type' , 'struct-type'
14- StructType . new ( node )
15+ StructType . new ( node : )
1516 when 'static-array'
16- StaticArray . new ( node , path )
17+ StaticArray . new ( node : , path : )
1718 when 'container'
18- Vector . new ( node , path )
19+ Vector . new ( node : , path : )
1920 when 'compound'
2021 case node [ 'ld:subtype' ]
2122 when 'bitfield'
22- BitfieldType . new ( node , path )
23+ BitfieldType . new ( node : , path : )
2324 when 'enum'
24- EnumType . new ( node , path )
25+ EnumType . new ( node : , path : )
2526 else
2627 raise "Unknown compound subtype: #{ node . inspect } " if node [ 'ld:subtype' ]
2728
28- StructType . new ( node , path )
29+ StructType . new ( node : , path : )
2930 end
3031 else
3132 raise "Unknown top-level node: #{ node . inspect } " if node [ 'ld:level' ] == '0'
3233
33- Field . new ( node , path )
34+ Field . new ( node : , path : )
3435 end
3536 end
3637 end
3738
3839 # Abstract node, named type or object reference.
3940 class Type
40- attr_reader :node
41+ attr_reader :node , :type
4142
42- def initialize ( node , path = [ ] )
43+ def initialize ( node : , path : [ ] )
4344 @node = node
4445 @children = node . children
4546
@@ -54,35 +55,33 @@ def initialize(node, path = [])
5455 end
5556
5657 @class_name = @path . join ( '.' )
58+ @type = @class_name
59+ end
60+
61+ def render?
62+ true
5763 end
5864 end
5965
66+ # <enum-type> or <enum> in df-structures.
6067 class EnumType < Type
61- def initialize ( node , path = [ ] )
62- super ( node , path )
68+ def initialize ( node : , path : [ ] )
69+ super ( node : , path : )
6370
6471 @attributes = node . xpath ( 'enum-attr' )
6572 @items = items
6673 end
6774
68- def type
69- @class_name
70- end
71-
7275 def items
7376 index = 0
7477 @node . xpath ( 'enum-item' ) . map do |item |
7578 index = item [ 'value' ] . to_i if item [ 'value' ]
76- enum = EnumItem . new ( item , index , @attributes )
79+ enum = EnumItem . new ( node : item , index : , attrs : @attributes )
7780 index += 1
7881 enum
7982 end
8083 end
8184
82- def render?
83- true
84- end
85-
8685 def to_field
8786 LuaLS . field ( @name , @class_name , @comment )
8887 end
@@ -139,12 +138,12 @@ def render
139138 end
140139
141140 class EnumItem
142- def initialize ( field , index , attrs )
143- @field = field
141+ def initialize ( node : , index : , attrs : )
142+ @field = node
144143
145- @name = field [ 'name' ]
146- @value = field [ 'value' ] || index
147- @comment = field [ 'comment' ]
144+ @name = node [ 'name' ]
145+ @value = node [ 'value' ] || index
146+ @comment = node [ 'comment' ]
148147
149148 @enum_attrs = attrs
150149 end
@@ -209,22 +208,19 @@ def to_field
209208 end
210209
211210 class BitfieldType < Type
212- def initialize ( node , path = [ ] )
213- super ( node , path )
211+ def initialize ( node : , path : [ ] )
212+ super ( node : , path : )
214213
215- @items = items
214+ @count = 0
215+ @items = flag_bits
216216 end
217217
218- def type
219- @class_name
220- end
221-
222- def items
223- index = 0
218+ # TODO: Check if this index is accurate.
219+ def flag_bits
224220 @node . xpath ( 'ld:field' ) . map do |item |
225- flag = FlagBit . new ( item , index )
226- index += item [ 'count' ] &.to_i &.abs || 1
227- flag
221+ FlagBit . new ( node : item , index : @count ) . tap do
222+ @count += item [ 'count' ] &.to_i &.abs || 1
223+ end
228224 end
229225 end
230226
@@ -246,10 +242,6 @@ def to_type
246242 annotation . join
247243 end
248244
249- def render?
250- true
251- end
252-
253245 def render
254246 annotation = ''
255247 annotation << to_type
@@ -262,12 +254,12 @@ def render
262254 end
263255
264256 class FlagBit
265- def initialize ( field , index )
266- @field = field
257+ def initialize ( node : , index : )
258+ @field = node
267259
268- @name = field [ 'name' ]
260+ @name = node [ 'name' ]
269261 @value = index
270- @comment = field [ 'comment' ]
262+ @comment = node [ 'comment' ]
271263 end
272264
273265 def render?
@@ -317,18 +309,14 @@ class StructType < Type
317309 'compound' => 'struct-type'
318310 } . freeze
319311
320- def initialize ( node , path = [ ] )
321- super ( node , path )
312+ def initialize ( node : , path : [ ] )
313+ super ( node : , path : )
322314
323315 @class = node [ 'inherits-from' ]
324316 @fields = fields
325317 @methods = methods
326318 end
327319
328- def type
329- @class_name
330- end
331-
332320 def fields
333321 @node . children . map do |child |
334322 DFHackLuaDefinitions . node_to_type ( child , @path )
@@ -337,7 +325,7 @@ def fields
337325
338326 def methods
339327 @node . xpath ( 'virtual-methods' ) . map do |node |
340- VirtualMethods . new ( node , @path )
328+ VirtualMethods . new ( node : , path : @path )
341329 end
342330 end
343331
@@ -402,10 +390,6 @@ def to_type
402390 annotation
403391 end
404392
405- def render?
406- true
407- end
408-
409393 def render
410394 annotation = to_object
411395 annotation << to_type
@@ -419,14 +403,14 @@ def render
419403 end
420404
421405 class GlobalType
422- def initialize ( nodes )
406+ def initialize ( nodes : )
423407 @nodes = nodes
424408 @fields = fields
425409 end
426410
427411 def fields
428412 @nodes . map do |node |
429- GlobalObject . new ( node , %w[ global ] )
413+ GlobalObject . new ( node : , path : %w[ global ] )
430414 end
431415 end
432416
@@ -445,8 +429,8 @@ def render
445429 end
446430
447431 class GlobalObject < Type
448- def initialize ( node , path = [ ] )
449- super ( node , path )
432+ def initialize ( node : , path : [ ] )
433+ super ( node : , path : )
450434
451435 @child = child
452436 @type = @child . type
@@ -460,10 +444,6 @@ def to_field
460444 LuaLS . field ( @name , @type , @comment )
461445 end
462446
463- def render?
464- true
465- end
466-
467447 def render
468448 annotation = [ ]
469449 annotation << @child . render if @child . render?
@@ -476,8 +456,8 @@ def render
476456 class Container < Type
477457 attr_accessor :type
478458
479- def initialize ( node , path = [ ] )
480- super ( node , path )
459+ def initialize ( node : , path : [ ] )
460+ super ( node : , path : )
481461
482462 @name = node [ 'name' ]
483463 @path = @path . slice ( 0 ..-1 )
@@ -509,10 +489,6 @@ def to_field
509489 LuaLS . field ( @name , @type , @comment )
510490 end
511491
512- def render?
513- true
514- end
515-
516492 def render
517493 annotation = [ ]
518494 annotation << @child . render if @child &.render?
@@ -523,8 +499,8 @@ def render
523499 class StaticArray < Container
524500 attr_accessor :type
525501
526- def initialize ( node , path = [ ] )
527- super ( node , path )
502+ def initialize ( node : , path : [ ] )
503+ super ( node : , path : )
528504 end
529505
530506 def render?
@@ -537,8 +513,8 @@ def render?
537513 class Vector < Container
538514 attr_accessor :type
539515
540- def initialize ( node , path = [ ] )
541- super ( node , path )
516+ def initialize ( node : , path : [ ] )
517+ super ( node : , path : )
542518
543519 @type = @child &.type || @type
544520 @class_name = class_name
@@ -595,22 +571,18 @@ def render
595571 end
596572
597573 class VirtualMethods < Type
598- def initialize ( node , path = [ ] )
599- super ( node , path )
574+ def initialize ( node : , path : [ ] )
575+ super ( node : , path : )
600576
601577 @methods = methods
602578 end
603579
604580 def methods
605581 @node . xpath ( 'vmethod' ) . map do |node |
606- VMethod . new ( node , @path )
582+ VMethod . new ( node : , path : @path )
607583 end
608584 end
609585
610- def render?
611- true
612- end
613-
614586 def render
615587 annotation = [ ]
616588 @methods . each do |method |
@@ -621,8 +593,8 @@ def render
621593 end
622594
623595 class VMethod < Type
624- def initialize ( node , path = [ ] )
625- super ( node , path )
596+ def initialize ( node : , path : [ ] )
597+ super ( node : , path : )
626598
627599 @name = node [ 'name' ]
628600 # The name of the function is appended, we need to remove it.
@@ -631,7 +603,7 @@ def initialize(node, path = [])
631603 end
632604
633605 def return_type
634- return Field . new ( @node . at_xpath ( 'ret-type' ) ) . type if @node . at_xpath ( 'ret-type' )
606+ return Field . new ( node : @node . at_xpath ( 'ret-type' ) ) . type if @node . at_xpath ( 'ret-type' )
635607
636608 node [ 'ret-type' ]
637609 end
@@ -653,14 +625,14 @@ def render
653625 class Field < Type
654626 attr_accessor :type
655627
656- def initialize ( field , path = [ ] )
657- super ( field , path )
628+ def initialize ( node : , path : [ ] )
629+ super ( node : , path : )
658630
659- @field = field
631+ @field = node
660632 # TODO: Temporary until we add anon indexes.
661- @name = field [ 'name' ] || 'anon_'
662- @type = field [ 'type-name' ] || 'any'
663- @ref_target = field [ 'ref-target' ]
633+ @name = node [ 'name' ] || 'anon_'
634+ @type = node [ 'type-name' ] || 'any'
635+ @ref_target = node [ 'ref-target' ]
664636 @comment = comment
665637
666638 @type = 'DFPointer<integer>' if @type == 'any' && node [ 'ld:meta' ] == 'pointer'
0 commit comments