File: hash_pattern.rb

package info (click to toggle)
ruby-unparser 0.6.13-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 936 kB
  • sloc: ruby: 7,691; sh: 6; makefile: 4
file content (67 lines) | stat: -rw-r--r-- 1,233 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# frozen_string_literal: true

module Unparser
  class Emitter
    # Emitter for hash patterns
    class HashPattern < self

      handle :hash_pattern

      def emit_const_pattern
        parentheses do
          emit_hash_body
        end
      end

    private

      def dispatch
        parentheses('{', '}') do
          emit_hash_body
        end
      end

      def emit_hash_body
        delimited(children, &method(:emit_member))
      end

      def emit_member(node)
        case node.type
        when :pair
          emit_pair(node)
        when :match_var
          emit_match_var(node)
        when :match_rest
          writer_with(MatchRest, node).emit_hash_pattern
        else
          visit(node)
        end
      end

      def emit_match_var(node)
        write_symbol_body(node.children.first)
        write(':')
      end

      def emit_pair(node)
        key, value = node.children

        if n_sym?(key)
          write_symbol_body(key.children.first)
        else
          visit(s(:dstr, *key))
        end

        write(':')

        ws

        visit(value)
      end

      def write_symbol_body(symbol)
        write(symbol.inspect[1..])
      end
    end # Pin
  end # Emitter
end # Unparser