Slide 36
Slide 36 text
36
/// A location in a source file.
public struct SourceLocation: Hashable, CustomStringConvertible, Comparable {
public static func < (lhs: SourceLocation, rhs: SourceLocation) -> Bool {
if lhs.line < rhs.line {
return true
} else if lhs.line == rhs.line {
return lhs.column < rhs.column
} else {
return false
}
}
/// The line number of the location.
public var line: Int
/// The number of bytes in UTF-8 encoding from the start of the line to the character at this source location.
public var column: Int
/// The source file for which this location applies, if it came from an accessible location.
public var source: URL?
/// Create a source location with line, column, and optional source to which the location applies.
///
/// - parameter line: The line number of the location, starting with 1.
/// - parameter column: The column of the location, starting with 1.
/// - parameter source: The URL in which the location resides, or `nil` if there is not a specific
/// file or resource that needs to be identified.
public init(line: Int, column: Int, source: URL?) {
self.line = line
self.column = column
self.source = source
}
public var description: String {
let path = source.map {
$0.path.isEmpty
? ""
: "\($0.path):"
} ?? ""
return "\(path)\(line):\(column)"
}
}
SourceLocationͷ࣮