class SyntaxTree::JSONVisitor

that can be easily serialized into JSON.
This visitor transforms the AST into a hash that contains only primitives

def comments(node)

def comments(node)
  target[:comments] = visit_all(node.comments)
end

def field(name, value)

def field(name, value)
  target[name] = value.is_a?(Node) ? visit(value) : value
end

def initialize

def initialize
  @target = nil
end

def list(name, values)

def list(name, values)
  target[name] = visit_all(values)
end

def node(node, type)

def node(node, type)
  previous = @target
  @target = { type: type, location: visit_location(node.location) }
  yield
  @target
ensure
  @target = previous
end

def pairs(name, values)

def pairs(name, values)
  target[name] = values.map { |(key, value)| [visit(key), visit(value)] }
end

def text(name, value)

def text(name, value)
  target[name] = value
end

def visit_location(location)

def visit_location(location)
  [
    location.start_line,
    location.start_char,
    location.end_line,
    location.end_char
  ]
end