class Mustermann::AST::Node

@!visibility private

Attributes

payload[RW]

@!visibility private

start[RW]

@!visibility private

stop[RW]

@!visibility private

Public Class Methods

[](name) click to toggle source

@!visibility private @param [Symbol] name of the node @return [Class] factory for the node

# File lib/mustermann/ast/node.rb, line 12
def self.[](name)
  @names       ||= {}
  @names[name] ||= begin
    const_name = constant_name(name)
    Object.const_get(const_name) if Object.const_defined?(const_name)
  end
end
constant_name(name) click to toggle source

@!visibility private @param [Symbol] name of the node @return [String] qualified name of factory for the node

# File lib/mustermann/ast/node.rb, line 29
def self.constant_name(name)
  return self.name if name.to_sym == :node
  name = name.to_s.split(?_).map(&:capitalize).join
  "#{self.name}::#{name}"
end
new(payload = nil, **options) click to toggle source

@!visibility private

# File lib/mustermann/ast/node.rb, line 43
def initialize(payload = nil, **options)
  options.each { |key, value| public_send("#{key}=", value) }
  self.payload = payload
end
parse(*args, &block) click to toggle source

Helper for creating a new instance and calling parse on it. @return [Mustermann::AST::Node] @!visibility private

# File lib/mustermann/ast/node.rb, line 38
def self.parse(*args, &block)
  new(*args).tap { |n| n.parse(&block) }
end
type() click to toggle source

Turns a class name into a node identifier. @!visibility private

# File lib/mustermann/ast/node.rb, line 22
def self.type
  name[/[^:]+$/].split(/(?<=.)(?=[A-Z])/).map(&:downcase).join(?_).to_sym
end

Public Instance Methods

each_leaf() { |self| ... } click to toggle source

Loop through all nodes that don't have child nodes. @!visibility private

# File lib/mustermann/ast/node.rb, line 65
def each_leaf(&block)
  return enum_for(__method__) unless block_given?
  called = false
  Array(payload).each do |entry|
    next unless entry.respond_to? :each_leaf
    entry.each_leaf(&block)
    called = true
  end
  yield(self) unless called
end
is_a?(type) click to toggle source

@!visibility private

Calls superclass method
# File lib/mustermann/ast/node.rb, line 49
def is_a?(type)
  type = Node[type] if type.is_a? Symbol
  super(type)
end
length() click to toggle source

@return [Integer] length of the substring @!visibility private

# File lib/mustermann/ast/node.rb, line 78
def length
  stop - start if start and stop
end
min_size() click to toggle source

@return [Integer] minimum size for a node @!visibility private

# File lib/mustermann/ast/node.rb, line 84
def min_size
  0
end
parse() click to toggle source

Double dispatch helper for reading from the buffer into the payload. @!visibility private

# File lib/mustermann/ast/node.rb, line 56
def parse
  self.payload ||= []
  while element = yield
    payload << element
  end
end
type() click to toggle source

Turns a class name into a node identifier. @!visibility private

# File lib/mustermann/ast/node.rb, line 90
def type
  self.class.type
end