Module: CodeBreaker::Parsable::Node

Included in:
Assignments, DataTypes, Keywords, LanguageElements, Ranges, VariableTypes, Wrappers
Defined in:
lib/code_breaker/parsable/node.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



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

def method_missing(method, *args, &block)
  matches = method.to_s.match(/^parse_(.+)_node$/)
  node_type = matches ? matches.captures.first : []

  if node_type.empty?
    super
  else
    raise NotImplementedError, not_implemented_message(node_type)
  end
end

Instance Method Details

#not_implemented_message(node_type) ⇒ Object



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

def not_implemented_message(node_type)
  [
    %(Breaking the node type "#{node_type}" is not yet implemented.),
    'You can open an issue on this in the project’s Github repo under:',
    'https://github.com/daigaku-ruby/code_breaker/issues/new',
    ''
  ].join("\n")
end

#parse(node) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/code_breaker/parsable/node.rb', line 4

def parse(node)
  if node.is_a?(Symbol)
    node
  elsif node.nil?
    parse_nil_node(node)
  else
    send("parse_#{node.type}_node", node)
  end
end

#parse_as_hash(node) ⇒ Object



23
24
25
# File 'lib/code_breaker/parsable/node.rb', line 23

def parse_as_hash(node)
  { node.type => parse_children(node) }
end

#parse_as_last_child_hash(node) ⇒ Object



27
28
29
# File 'lib/code_breaker/parsable/node.rb', line 27

def parse_as_last_child_hash(node)
  { node.type => node.children.last }
end

#parse_as_node_type(node) ⇒ Object



31
32
33
# File 'lib/code_breaker/parsable/node.rb', line 31

def parse_as_node_type(node)
  node.type
end

#parse_children(node, compact: true) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/code_breaker/parsable/node.rb', line 14

def parse_children(node, compact: true)
  children = node.children
  children = children.compact if compact

  children.each_with_object([]) do |child, nodes|
    nodes << parse(child)
  end
end