class Kenma::NodeConverter

Constants

KENMA_MACRO_EMPTY_NODE

Attributes

bind[R]
scope_context[R]

Public Class Methods

new(context = {}) click to toggle source
# File lib/kenma/node_converter.rb, line 12
def initialize(context = {})
  @scope_context = context
end

Public Instance Methods

convert(node) click to toggle source
# File lib/kenma/node_converter.rb, line 16
def convert(node)
  _convert(node) { |node, parent|
    method_name = "NODE_#{node.type}"
    send_node(method_name, node, parent)
  }
end

Private Instance Methods

_convert(node, &block) click to toggle source
# File lib/kenma/node_converter.rb, line 39
def _convert(node, &block)
  return node unless node.node?

  if node.type == :SCOPE
    return scope_context_switch(scope_context) { |converter|
      children = [*node.children.take(node.children.size-1), converter.convert(node.children.last)]
                  .reject { |it| KENMA_MACRO_EMPTY_NODE == it }
      send_node(:NODE_SCOPE, [:SCOPE, children], node)
    }
  end

  children = node.children
  converted_children = children
    .map { |node| _convert(node) { |child| block.call(child, node) || KENMA_MACRO_EMPTY_NODE } }
    .reject { |it| KENMA_MACRO_EMPTY_NODE == it }

  if converted_children == children
    node
  else
    [node.type, converted_children]
  end.then { |node| block.call(node, nil) }
end
node_missing(node, parent) click to toggle source
# File lib/kenma/node_converter.rb, line 75
def node_missing(node, parent)
  node
end
scope_context_switch(context, &block) click to toggle source
# File lib/kenma/node_converter.rb, line 35
def scope_context_switch(context, &block)
  self.class.new(scope_context.merge(context)).then(&block)
end
send_node(method_name, node, parent) click to toggle source
# File lib/kenma/node_converter.rb, line 62
def send_node(method_name, node, parent)
  if respond_to?(method_name, true)
    result = send(method_name, node, parent)
    if result == node
      node_missing(node, parent)
    else
      result
    end
  else
    node_missing(node, parent)
  end
end