Module: CodeBreaker::Parsable::Keywords

Includes:
Node
Included in:
CodeBreaker::Parser
Defined in:
lib/code_breaker/parsable/keywords.rb

Instance Method Summary collapse

Methods included from Node

#method_missing, #not_implemented_message, #parse, #parse_as_hash, #parse_as_last_child_hash, #parse_as_node_type, #parse_children

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class CodeBreaker::Parsable::Node

Instance Method Details

#parse_case_node(node) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/code_breaker/parsable/keywords.rb', line 77

def parse_case_node(node)
  case_part  = parse(node.children.first)
  when_parts = node.children[1...-1].map { |child| parse(child) }
  else_part  = parse(node.children.last)

  statement = { case: when_parts.unshift(case_part) }
  statement[:case] << { else: else_part } if else_part
  statement
end

#parse_for_node(node) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/code_breaker/parsable/keywords.rb', line 28

def parse_for_node(node)
  variable  = node.children[0]
  range     = node.children[1]
  body      = node.children[2]

  { node.type => parse(variable), in: parse(range), do: parse(body) }
end

#parse_if_node(node) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/code_breaker/parsable/keywords.rb', line 36

def parse_if_node(node)
  condition = node.children[0]
  if_body   = node.children[1]
  else_body = node.children[2]

  clause = { node.type => parse(condition), then: parse(if_body) }
  clause[:else] = parse(else_body) if else_body

  clause
end

#parse_kwbegin_node(node) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/code_breaker/parsable/keywords.rb', line 62

def parse_kwbegin_node(node)
  rescue_given = node.children.first.type == :rescue

  if rescue_given
    rescue_part = parse(node.children.first)[:rescue]

    {
      begin: rescue_part.first,
      rescue: rescue_part.last[:resbody].first
    }
  else
    { begin: parse(node.children.last) }
  end
end

#parse_loop_node(node) ⇒ Object Also known as: parse_while_node, parse_until_node



18
19
20
21
22
23
# File 'lib/code_breaker/parsable/keywords.rb', line 18

def parse_loop_node(node)
  condition = node.children[0]
  body      = node.children[1]

  { node.type => parse(condition), do: parse(body) }
end

#parse_module_node(node) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/code_breaker/parsable/keywords.rb', line 47

def parse_module_node(node)
  name  = parse(node.children[0])
  body  = node.children[1].nil? ? nil : parse(node.children[1])
  value = body ? [name, body] : [name]

  { node.type => value }
end

#parse_return_node(node) ⇒ Object



55
56
57
58
59
60
# File 'lib/code_breaker/parsable/keywords.rb', line 55

def parse_return_node(node)
  children = parse_children(node)
  values   = children.length == 1 ? children[0] : children

  { node.type => values }
end

#parse_when_node(node) ⇒ Object



87
88
89
90
91
92
# File 'lib/code_breaker/parsable/keywords.rb', line 87

def parse_when_node(node)
  when_part = parse(node.children[0])
  then_part = parse(node.children[1])

  { when: when_part, then: then_part }
end