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
|
# frozen_string_literal: true
module Unparser
class Emitter
# Emitter for case nodes
class Case < self
handle :case
children :condition
define_group :whens, 1..-2
private
def dispatch
write('case')
emit_condition
emit_whens
emit_else
k_end
end
def emit_else
else_branch = children.last
return unless else_branch
write('else')
emit_body(else_branch)
end
def emit_whens
nl
whens.each(&method(:visit))
end
def emit_condition
return unless condition
ws
visit(condition)
end
end # Case
# Emitter for when nodes
class When < self
handle :when
define_group :captures, 0..-2
private
def dispatch
write('when ')
emit_captures
emit_optional_body(children.last)
end
def emit_captures
delimited(captures)
end
end # When
end # Emitter
end # Unparser
|