module Rensei::Unparser::Ruby2_7_0
Private Instance Methods
NODE_ARYPTN(node, opt = {})
click to toggle source
array pattern format: [nd_pconst](, …, *[rest_arg], [post_args], …)
# File lib/rensei/unparser.rb, line 1465 def NODE_ARYPTN(node, opt = {}) node.children.then { |pconst, pre, rest, post| # e.g. in Array[a, b] pconst_ = unparse(pconst, opt) if pconst opt_flags = { expand_ARRAY: true, expand_HASH: true, pattern_match_OR: true, pattern_match_LVAR: true, pattern_match_BEGIN: true } pre_ = unparse(pre, opt.merge(opt_flags)) if rest == :NODE_SPECIAL_NO_NAME_REST rest_ = "*" elsif rest rest_ = "*#{unparse(rest, opt.merge(opt_flags))}" end post_ = unparse(post, opt.merge(opt_flags)) "#{pconst_}[#{[pre_, rest_, post_].compact.join(", ")}]" } end
NODE_CASE3(node, opt = {})
click to toggle source
case statement (pattern matching) format: case [nd_head]; [nd_body]; end example: case x; in 1; foo; in 2; bar; else baz; end
# File lib/rensei/unparser.rb, line 1415 def NODE_CASE3(node, opt = {}) node.children.then { |head, body, else_| <<~EOS.chomp case #{unparse(head, opt)} #{unparse(body, opt)}#{else_ ? "\nelse\n#{unparse(else_, opt)}" : ""} end EOS } end
NODE_HSHPTN(node, opt = {})
click to toggle source
hash pattern format: [nd_pconst](, …, **[nd_pkwrestarg])
# File lib/rensei/unparser.rb, line 1484 def NODE_HSHPTN(node, opt = {}) node.children.then { |pconst, pkwargs, pkwrestarg| opt_flags = { expand_ARRAY: true, expand_HASH: true, pattern_match_OR: true, pattern_match_LVAR: true, pattern_match_BEGIN: true } pkwargs_ = unparse(pkwargs, opt.merge(opt_flags)) if pkwrestarg == :NODE_SPECIAL_NO_REST_KEYWORD pkwargs_ += ", **nil" elsif pkwrestarg pkwargs_ += ", **#{unparse(pkwrestarg, opt.merge(opt_flags))}" end # Support `in A[a: 1, b: 2]` if pconst "#{unparse(pconst, opt)}[#{pkwargs_}]" else "{ #{pkwargs_} }" end } end
NODE_IN(node, opt = {})
click to toggle source
# File lib/rensei/unparser.rb, line 1436 def NODE_IN(node, opt = {}) node.children.then { |head, body, next_| in_ = opt.delete(:without_in) ? "" : "in " next__ = next_&.type == :IN || next_.nil? ? unparse(next_, opt) : "else\n #{unparse(next_, opt)}" case head.type when :IF <<~EOS.chomp #{in_}#{unparse(head.children[1], opt.merge(expand_ARRAY: true))} if #{unparse(head.children[0], opt)} #{unparse(body, opt)} #{next__} EOS when :UNLESS <<~EOS.chomp #{in_}#{unparse(head.children[1], opt.merge(expand_ARRAY: true))} unless #{unparse(head.children[0], opt)} #{unparse(body, opt)} #{next__} EOS else <<~EOS.chomp #{in_}#{unparse(head, opt.merge(expand_ARRAY: true))} #{unparse(body, opt)} #{next__} EOS end } end
NODE_LIST(node, opt = {})
click to toggle source
list constructor format: [ [nd_head], [nd_next].. ] (length: [nd_alen]) example: [1, 2, 3]
# File lib/rensei/unparser.rb, line 1428 def NODE_LIST(node, opt = {}) NODE_ARRAY(node, opt) end
NODE_ZLIST(node, opt = {})
click to toggle source
# File lib/rensei/unparser.rb, line 1432 def NODE_ZLIST(node, opt = {}) NODE_ZARRAY(node, opt) end