Class: KiCad::AST::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/kicad/ast.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values, children) ⇒ Node

Returns a new instance of Node.



6
7
8
9
# File 'lib/kicad/ast.rb', line 6

def initialize values, children
  @values = values
  @children = children
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



4
5
6
# File 'lib/kicad/ast.rb', line 4

def children
  @children
end

#valuesObject (readonly)

Returns the value of attribute values.



4
5
6
# File 'lib/kicad/ast.rb', line 4

def values
  @values
end

Class Method Details

.child_types(*cts) ⇒ Object

Define methods for each child type this class allows



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/kicad/ast.rb', line 47

def self.child_types *cts
  # puts "#{self.name} allows child types #{cts.inspect}"
  cts.each do |c|
    if Array === c
      define_method(:"all_#{c[0].to_s}") do
        # puts "Looking for all [#{class_names*', '}] in #{@children.map{|q| q.class.name}.inspect}"
        children_of_type(c)
      end
      # REVISIT: Allow deleting and adding instances to the array
      # new_child = KiCad.parse('(some new node)').value
      # @children.append(new_child)
    else
      class_name = 'KiCad::AST::'+to_class_name(c)
      define_method(:"#{c}") do
        # puts "Looking for first #{class_name} in #{@children.map{|q| q.class.name}.inspect}"
        a = children_of_type(c)
        puts "Choosing first #{self.class.name}.#{c} of #{a.size}" if a.size > 1
        a.first
      end
      # Allow deleting this instance
      define_method(:"unset_#{c.to_s}") do
        child = send(:"#{c}")
        @children = @children - [child] if child
        child ? true : nil
      end
    end
  end
end

.to_class_name(sym) ⇒ Object



76
77
78
# File 'lib/kicad/ast.rb', line 76

def self.to_class_name sym
  sym.to_s.gsub(/\A[a-z]|_[a-z]/) {|from| from[-1].upcase }
end

.to_symbol(class_name) ⇒ Object



80
81
82
# File 'lib/kicad/ast.rb', line 80

def self.to_symbol class_name
  class_name.to_s.gsub(/[A-Z]/) {|from| '_'+from[-1].downcase }.sub(/\A_/,'')
end

.value_types(vts) ⇒ Object

Define setter and getter methods for each value type this class allows



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/kicad/ast.rb', line 20

def self.value_types vts
  i = 1   # @values[0] is always the class symbol
  vts.each do |k, v|
    # puts "#{self.name} attribute #{k.to_s} => #{v.inspect}"
    # puts "attr_accessor #{self.name}.#{k} (values #{v.inspect}) is stored in @values[#{i}]"
    begin
      o = i # Avoid capturing i after the loop ends
      define_method(:"#{k}") do
        # puts "accessing #{self.class.name}.#{k} as @values[#{o}]"
        @values[o]
      end
      define_method(:"#{k}=") do |v|
        # puts "setting #{self.class.name}.#{k} as @values[#{o}]"
        # REVISIT: Check valid data type matching v
        @values[o] = v
      end
    end
    i = i+1
  end
end

Instance Method Details

#children_of_type(*cts) ⇒ Object

cts is an array of AST class symbols or strings



41
42
43
44
# File 'lib/kicad/ast.rb', line 41

def children_of_type *cts  # cts is an array of AST class symbols or strings
  class_names = cts.flatten.map{|k| 'KiCad::AST::'+self.class.to_class_name(k)}
  @children.filter{|h| class_names.include?(h.class.name) }
end

#emit(depth = 0) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/kicad/ast.rb', line 11

def emit depth = 0
  "\t"*depth +
  '(' +
  @values.map{|v| String === v ? v.inspect : v.to_s }*' ' +
  (@children.size == 0 ? '' : "\n" + @children.map{|c| c.emit(depth+1) }*''+"\t"*depth) +
  ")\n"
end