module TableSyntax::TableParser

Public Instance Methods

eval_source_fragment(source_fragment) click to toggle source
# File lib/table_syntax/table_parser.rb, line 54
def eval_source_fragment(source_fragment)
  instance = Object.new  # for evaluate let methods.
  if defined?(self.superclass::LetDefinitions)
    instance.extend self.superclass::LetDefinitions
  end
  instance.instance_eval(source_fragment)
end
extract_value(node, buf) click to toggle source
# File lib/table_syntax/table_parser.rb, line 40
def extract_value(node, buf)
  receiver, method, arg = node.children

  if method == :|
    buf << eval_source_fragment(Unparser.unparse(arg))
  end

  if receiver.is_a?(AST::Node) && receiver.type == :send && receiver.children[1] == :|
    extract_value(receiver, buf)
  else
    buf << eval_source_fragment(Unparser.unparse(receiver))
  end
end
parse(block) click to toggle source

Separates table-like block of code from AST

# File lib/table_syntax/table_parser.rb, line 16
def parse(block)
  ast = block.to_ast
  # produces sth. like:
  # (block
  #   (send ..)
  #   (args)
  #   (begin ..)
  # )
  inner_ast = ast.children[2] # pick up (begin..)
  if inner_ast.type == :send
    lines = [inner_ast]
  else
    lines = inner_ast.children
  end

  lines.map do |node|
    if node.type == :send
      buf = []
      extract_value(node, buf)
      buf.reverse
    end
  end
end