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
# File 'lib/nasl/parser/node.rb', line 31

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

  # Create the attributes array which is used for converting the parse tree
  # to XML.
  @attributes = []

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

  # Extract the context object from the first token.
  @ctx = @tokens.first.ctx
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



46
47
48
# File 'lib/nasl/parser/node.rb', line 46

def context(*args)
  @ctx.context(region, *args)
end

#regionObject



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

def region
  @tokens.first.region.begin..@tokens.last.region.end
end

#to_xml(xml) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/nasl/parser/node.rb', line 54

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

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

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

      # Skip over unused attributes.
      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 { |node| node.to_xml(xml) }
      elsif obj.is_a? Node
        obj.to_xml(xml)
      else
        xml.tag!(name, obj.to_s)
      end
    end
  end
end