storing full sourcemap in token, refactored error reporting

This commit is contained in:
Ilya Puchka
2017-12-27 02:31:47 +01:00
parent cb124319ec
commit ac2fd56e8e
15 changed files with 149 additions and 188 deletions

View File

@@ -40,18 +40,34 @@ extension String {
}
}
public struct SourceMap: Equatable {
public let filename: String?
public let line: RangeLine
init(filename: String? = nil, line: RangeLine = ("", 0, 0)) {
self.filename = filename
self.line = line
}
static let unknown = SourceMap()
public static func ==(lhs: SourceMap, rhs: SourceMap) -> Bool {
return lhs.filename == rhs.filename && lhs.line == rhs.line
}
}
public enum Token : Equatable {
/// A token representing a piece of text.
case text(value: String, at: Range<String.Index>)
case text(value: String, at: SourceMap)
/// A token representing a variable.
case variable(value: String, at: Range<String.Index>)
case variable(value: String, at: SourceMap)
/// A token representing a comment.
case comment(value: String, at: Range<String.Index>)
case comment(value: String, at: SourceMap)
/// A token representing a template block.
case block(value: String, at: Range<String.Index>)
case block(value: String, at: SourceMap)
/// Returns the underlying value as an array seperated by spaces
public func components() -> [String] {
@@ -74,29 +90,29 @@ public enum Token : Equatable {
}
}
public var range: Range<String.Index> {
public var sourceMap: SourceMap {
switch self {
case .block(_, let range),
.variable(_, let range),
.text(_, let range),
.comment(_, let range):
return range
case .block(_, let sourceMap),
.variable(_, let sourceMap),
.text(_, let sourceMap),
.comment(_, let sourceMap):
return sourceMap
}
}
}
public func == (lhs: Token, rhs: Token) -> Bool {
switch (lhs, rhs) {
case (.text(let lhsValue), .text(let rhsValue)):
return lhsValue == rhsValue
case (.variable(let lhsValue), .variable(let rhsValue)):
return lhsValue == rhsValue
case (.block(let lhsValue), .block(let rhsValue)):
return lhsValue == rhsValue
case (.comment(let lhsValue), .comment(let rhsValue)):
return lhsValue == rhsValue
case let (.text(lhsValue, lhsAt), .text(rhsValue, rhsAt)):
return lhsValue == rhsValue && lhsAt == rhsAt
case let (.variable(lhsValue, lhsAt), .variable(rhsValue, rhsAt)):
return lhsValue == rhsValue && lhsAt == rhsAt
case let (.block(lhsValue, lhsAt), .block(rhsValue, rhsAt)):
return lhsValue == rhsValue && lhsAt == rhsAt
case let (.comment(lhsValue, lhsAt), .comment(rhsValue, rhsAt)):
return lhsValue == rhsValue && lhsAt == rhsAt
default:
return false
}