func write()

in SwiftEvolve/Sources/SwiftEvolve/SyntaxDump.swift [27:70]


  func write<Target>(_ node: Syntax, to target: inout Target, indentation: Int)
    where Target : TextOutputStream
  {
    func write(_ str: String) {
      let lines = str.split(separator: "\n", omittingEmptySubsequences: false)
      let newSeparator = "\n" + String(repeating: " ", count: indentation)
      target.write(lines.joined(separator: newSeparator))
    }
    
    func writeLoc(_ loc: SourceLocation?) {
      guard let loc = loc else { return }
      
      let url = loc.file.map(URL.init(fileURLWithPath:))
      write(" ")
      write(url?.lastPathComponent ?? "<unknown>")
      write(":")
      write(loc.line.map(String.init) ?? "<unknown>")
      write(":")
      write(loc.column.map(String.init) ?? "<unknown>")
    }

    let startLoc = node.startLocation(converter: locationConverter)
    write("(")
    switch node.as(SyntaxEnum.self) {
    case .token(let node):
      switch node.tokenKind {
      case .identifier(let name):
        write("identifier")
        write(" \"\(name)\"")
      default:
        write("\(node.tokenKind)")
      }
      writeLoc(startLoc)
      
    default:
      write("\(type(of: node))")
      writeLoc(startLoc)
      for child in node.children {
        write("\n  ")
        self.write(child, to: &target, indentation: indentation + 2)
      }
    }
    write(")")
  }