Module: TableSyntax::TableParser

Defined in:
lib/table_syntax/table_parser.rb

Class Method Summary collapse

Class Method Details

.eval_source_fragment(source_fragment) ⇒ Object



54
55
56
57
58
59
60
# 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) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# 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) ⇒ Object

Separates table-like block of code from AST



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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