def visit(node)
case node
when Psych::Nodes::Scalar, Psych::Nodes::Alias
node.leading_comments.push(*read_comments(node.start_line, node.start_column))
@last = [node.end_line, node.end_column]
when Psych::Nodes::Sequence, Psych::Nodes::Mapping
has_delim = /[\[{]/.match?(char_at(node.start_line, node.start_column))
has_bullet = node.is_a?(Psych::Nodes::Sequence) && !has_delim
node.leading_comments.push(*read_comments(node.start_line, node.start_column)) if has_delim
node.children.each do |subnode|
@bullet_owner = subnode if has_bullet
visit(subnode)
end
if has_delim
target = node.children[-1] || node
target.trailing_comments.push(*read_comments(node.end_line, node.end_column))
end
when Psych::Nodes::Document
if !node.implicit
node.leading_comments.push(*read_comments(node.start_line, node.start_column))
end
visit(node.root)
if !node.implicit_end
node.root.trailing_comments.push(*read_comments(node.end_line, node.end_column))
end
when Psych::Nodes::Stream
node.children.each do |subnode|
visit(subnode)
end
target = node.children[-1] || node
target.trailing_comments.push(*read_comments(node.end_line, node.end_column))
else
raise TypeError
end
end