replaced Token with Lexeme protocol on TemplateSyntaxError

This commit is contained in:
Ilya Puchka
2017-10-07 20:54:42 +02:00
parent 6300dbc7bf
commit 69cd8e4d3b
6 changed files with 52 additions and 69 deletions

View File

@@ -1,32 +1,5 @@
import Foundation
public struct TemplateSyntaxError : Error, Equatable, CustomStringConvertible {
public let description:String
public var token: Token?
public init(_ description:String) {
self.description = description
}
public func contextAwareError(templateName: String?, templateContent: String) -> TemplateSyntaxError? {
guard let token = token, token.range != .unknown else { return nil }
let templateName = templateName.map({ "\($0):" }) ?? ""
let (line, lineNumber, offset) = templateContent.lineAndPosition(at: token.range)
let tokenContent = templateContent.substring(with: token.range)
let highlight = "\(String(Array(repeating: " ", count: offset)))^\(String(Array(repeating: "~", count: max(tokenContent.count - 1, 0))))"
let description = "\(templateName)\(lineNumber):\(offset): error: " + self.description + "\n\(line)\n\(highlight)\n"
return TemplateSyntaxError(description)
}
}
public func ==(lhs:TemplateSyntaxError, rhs:TemplateSyntaxError) -> Bool {
return lhs.description == rhs.description
}
public protocol NodeType {
/// Render the node in the given context
func render(_ context:Context) throws -> String