Class: Nasl::Node

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tree, *tokens) ⇒ Node

Returns a new instance of Node.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/nasl/parser/node.rb', line 31

def initialize(tree, *tokens)
  # Register new node in the tree.
  tree.register(self)

  # Create the arrays which are used for converting the parse tree to XML.
  @attributes = []
  @children = []

  # Store all of the tokens that made up this node.
  @tokens = tokens

  # handle empty token set

  # Extract the context object from the first token.
  if(!@tokens.nil? and @tokens.length>0 and !@tokens.first.nil?)
    @ctx = @tokens.first.ctx
  end
end

Instance Attribute Details

#ctxObject (readonly)

Returns the value of attribute ctx.



29
30
31
# File 'lib/nasl/parser/node.rb', line 29

def ctx
  @ctx
end

#tokensObject (readonly)

Returns the value of attribute tokens.



29
30
31
# File 'lib/nasl/parser/node.rb', line 29

def tokens
  @tokens
end

Instance Method Details

#context(*args) ⇒ Object



50
51
52
53
54
# File 'lib/nasl/parser/node.rb', line 50

def context(*args)
  if(!@ctx.nil?)
    @ctx.context(region, *args)
  end
end

#regionObject



56
57
58
59
60
61
# File 'lib/nasl/parser/node.rb', line 56

def region
  if(@tokens.flatten.first.nil?)
    return []
  end
  @tokens.flatten.first.region.begin..@tokens.flatten.last.region.end
end

#to_xml(xml) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/nasl/parser/node.rb', line 63

def to_xml(xml)
  # Mangle the class name into something more appropriate for XML.
  name = self.class.name.split('::').last
  name = name.gsub(/(.)([A-Z])/, '\1_\2').downcase

  # Create a hash from the attribute array.
  attr = Hash[@attributes.map{ |el| [el, self.send(el)] }]

  # If there are no attributes, make a modified opening tag.
  return xml.tag!(name, attr) if @children.empty?

  # Create the tag representing this node.
  xml.tag!(name, attr) do
    @children.each do |name|
      # Retrieve the object that the symbol indicates.
      obj = self.send(name)

      # Skip over empty children.
      next if obj.nil?

      # Handle objects that are arrays holding nodes, or basic types that
      # aren't nodes.
      if obj.is_a? ::Array
        obj.each { |el| el.to_xml(xml) }
      elsif obj.is_a? Node
        obj.to_xml(xml)
      else
        xml.tag!(name, obj.to_s)
      end
    end
  end
end