class Nasl::Node

Attributes

ctx[R]
tokens[R]

Public Class Methods

new(tree, *tokens) click to toggle source
# 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

Public Instance Methods

context(*args) click to toggle source
# File lib/nasl/parser/node.rb, line 50
def context(*args)
  if(!@ctx.nil?)
    @ctx.context(region, *args)
  end
end
region() click to toggle source
# 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) click to toggle source
# 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