Remove custom Result type and throw errors

This commit is contained in:
Kyle Fuller
2015-09-25 12:53:45 -07:00
parent 25f5583542
commit 9c335caeb6
17 changed files with 211 additions and 460 deletions

View File

@@ -1,83 +1,52 @@
import Foundation
struct NodeError : Error {
let token:Token
let message:String
public struct TemplateSyntaxError : ErrorType, Equatable, CustomStringConvertible {
public let description:String
init(token:Token, message:String) {
self.token = token
self.message = message
}
var description:String {
return "\(token.components().first!): \(message)"
public init(_ description:String) {
self.description = description
}
}
public protocol Node {
/// Return the node rendered as a string, or returns a failure
func render(context:Context) -> Result
public func ==(lhs:TemplateSyntaxError, rhs:TemplateSyntaxError) -> Bool {
return lhs.description == rhs.description
}
extension Array {
func map<U>(block:((Element) -> (U?, Error?))) -> ([U]?, Error?) {
var results = [U]()
for item in self {
let (result, error) = block(item)
if let error = error {
return (nil, error)
} else if (result != nil) {
// let result = result exposing a bug in the Swift compier :(
results.append(result!)
}
}
return (results, nil)
}
public protocol NodeType {
/// Render the node in the given context
func render(context:Context) throws -> String
}
public func renderNodes(nodes:[Node], context:Context) -> Result {
var result = ""
for item in nodes {
switch item.render(context) {
case .Success(let string):
result += string
case .Error(let error):
return .Error(error)
}
}
return .Success(result)
/// Render the collection of nodes in the given context
public func renderNodes(nodes:[NodeType], _ context:Context) throws -> String {
return try nodes.map { try $0.render(context) }.joinWithSeparator("")
}
public class SimpleNode : Node {
let handler:(Context) -> (Result)
public class SimpleNode : NodeType {
let handler:Context throws -> String
public init(handler:((Context) -> (Result))) {
public init(handler:Context throws -> String) {
self.handler = handler
}
public func render(context:Context) -> Result {
return handler(context)
public func render(context: Context) throws -> String {
return try handler(context)
}
}
public class TextNode : Node {
public class TextNode : NodeType {
public let text:String
public init(text:String) {
self.text = text
}
public func render(context:Context) -> Result {
return .Success(self.text)
public func render(context:Context) throws -> String {
return self.text
}
}
public class VariableNode : Node {
public class VariableNode : NodeType {
public let variable:Variable
public init(variable:Variable) {
@@ -88,23 +57,23 @@ public class VariableNode : Node {
self.variable = Variable(variable)
}
public func render(context:Context) -> Result {
public func render(context:Context) throws -> String {
let result:AnyObject? = variable.resolve(context)
if let result = result as? String {
return .Success(result)
return result
} else if let result = result as? NSObject {
return .Success(result.description)
return result.description
}
return .Success("")
return ""
}
}
public class NowNode : Node {
public class NowNode : NodeType {
public let format:Variable
public class func parse(parser:TokenParser, token:Token) -> TokenParser.Result {
public class func parse(parser:TokenParser, token:Token) -> NodeType {
var format:Variable?
let components = token.components()
@@ -112,7 +81,7 @@ public class NowNode : Node {
format = Variable(components[1])
}
return .Success(node:NowNode(format:format))
return NowNode(format:format)
}
public init(format:Variable?) {
@@ -123,7 +92,7 @@ public class NowNode : Node {
}
}
public func render(context: Context) -> Result {
public func render(context: Context) throws -> String {
let date = NSDate()
let format: AnyObject? = self.format.resolve(context)
var formatter:NSDateFormatter?
@@ -134,156 +103,115 @@ public class NowNode : Node {
formatter = NSDateFormatter()
formatter!.dateFormat = format
} else {
return .Success("")
return ""
}
return .Success(formatter!.stringFromDate(date))
return formatter!.stringFromDate(date)
}
}
public class ForNode : Node {
public class ForNode : NodeType {
let variable:Variable
let loopVariable:String
let nodes:[Node]
let nodes:[NodeType]
public class func parse(parser:TokenParser, token:Token) -> TokenParser.Result {
public class func parse(parser:TokenParser, token:Token) throws -> NodeType {
let components = token.components()
if components.count == 4 && components[2] == "in" {
let loopVariable = components[1]
let variable = components[3]
var forNodes:[Node]!
var emptyNodes = [Node]()
var emptyNodes = [NodeType]()
switch parser.parse(until(["endfor", "empty"])) {
case .Success(let nodes):
forNodes = nodes
case .Error(let error):
return .Error(error: error)
}
let forNodes = try parser.parse(until(["endfor", "empty"]))
if let token = parser.nextToken() {
if token.contents == "empty" {
switch parser.parse(until(["endfor"])) {
case .Success(let nodes):
emptyNodes = nodes
case .Error(let error):
return .Error(error: error)
}
emptyNodes = try parser.parse(until(["endfor"]))
parser.nextToken()
}
} else {
return .Error(error: NodeError(token: token, message: "`endfor` was not found."))
throw TemplateSyntaxError("`endfor` was not found.")
}
return .Success(node:ForNode(variable: variable, loopVariable: loopVariable, nodes: forNodes, emptyNodes:emptyNodes))
return ForNode(variable: variable, loopVariable: loopVariable, nodes: forNodes, emptyNodes:emptyNodes)
}
return .Error(error: NodeError(token: token, message: "Invalid syntax. Expected `for x in y`."))
throw TemplateSyntaxError("'for' statements should use the following 'for x in y' `\(token.contents)`.")
}
public init(variable:String, loopVariable:String, nodes:[Node], emptyNodes:[Node]) {
public init(variable:String, loopVariable:String, nodes:[NodeType], emptyNodes:[NodeType]) {
self.variable = Variable(variable)
self.loopVariable = loopVariable
self.nodes = nodes
}
public func render(context: Context) -> Result {
let values = variable.resolve(context) as? [AnyObject]
var output = ""
if let values = values {
for item in values {
public func render(context: Context) throws -> String {
if let values = variable.resolve(context) as? [AnyObject] {
return try values.map { item in
context.push()
context[loopVariable] = item
let result = renderNodes(nodes, context: context)
let result = try renderNodes(nodes, context)
context.pop()
switch result {
case .Success(let string):
output += string
case .Error(let error):
return .Error(error)
}
}
return result
}.joinWithSeparator("")
}
return .Success(output)
return ""
}
}
public class IfNode : Node {
public class IfNode : NodeType {
public let variable:Variable
public let trueNodes:[Node]
public let falseNodes:[Node]
public let trueNodes:[NodeType]
public let falseNodes:[NodeType]
public class func parse(parser:TokenParser, token:Token) -> TokenParser.Result {
public class func parse(parser:TokenParser, token:Token) throws -> NodeType {
let variable = token.components()[1]
var trueNodes = [Node]()
var falseNodes = [Node]()
var trueNodes = [NodeType]()
var falseNodes = [NodeType]()
switch parser.parse(until(["endif", "else"])) {
case .Success(let nodes):
trueNodes = nodes
case .Error(let error):
return .Error(error: error)
}
trueNodes = try parser.parse(until(["endif", "else"]))
if let token = parser.nextToken() {
if token.contents == "else" {
switch parser.parse(until(["endif"])) {
case .Success(let nodes):
falseNodes = nodes
case .Error(let error):
return .Error(error: error)
}
falseNodes = try parser.parse(until(["endif"]))
parser.nextToken()
}
} else {
return .Error(error:NodeError(token: token, message: "`endif` was not found."))
throw TemplateSyntaxError("`endif` was not found.")
}
return .Success(node:IfNode(variable: variable, trueNodes: trueNodes, falseNodes: falseNodes))
return IfNode(variable: variable, trueNodes: trueNodes, falseNodes: falseNodes)
}
public class func parse_ifnot(parser:TokenParser, token:Token) -> TokenParser.Result {
public class func parse_ifnot(parser:TokenParser, token:Token) throws -> NodeType {
let variable = token.components()[1]
var trueNodes = [Node]()
var falseNodes = [Node]()
var trueNodes = [NodeType]()
var falseNodes = [NodeType]()
switch parser.parse(until(["endif", "else"])) {
case .Success(let nodes):
falseNodes = nodes
case .Error(let error):
return .Error(error: error)
}
falseNodes = try parser.parse(until(["endif", "else"]))
if let token = parser.nextToken() {
if token.contents == "else" {
switch parser.parse(until(["endif"])) {
case .Success(let nodes):
trueNodes = nodes
case .Error(let error):
return .Error(error: error)
}
trueNodes = try parser.parse(until(["endif"]))
parser.nextToken()
}
} else {
return .Error(error:NodeError(token: token, message: "`endif` was not found."))
throw TemplateSyntaxError("`endif` was not found.")
}
return .Success(node:IfNode(variable: variable, trueNodes: trueNodes, falseNodes: falseNodes))
return IfNode(variable: variable, trueNodes: trueNodes, falseNodes: falseNodes)
}
public init(variable:String, trueNodes:[Node], falseNodes:[Node]) {
public init(variable:String, trueNodes:[NodeType], falseNodes:[NodeType]) {
self.variable = Variable(variable)
self.trueNodes = trueNodes
self.falseNodes = falseNodes
}
public func render(context: Context) -> Result {
public func render(context: Context) throws -> String {
let result: AnyObject? = variable.resolve(context)
var truthy = false
@@ -296,7 +224,12 @@ public class IfNode : Node {
}
context.push()
let output = renderNodes(truthy ? trueNodes : falseNodes, context: context)
let output:String
if truthy {
output = try renderNodes(trueNodes, context)
} else {
output = try renderNodes(falseNodes, context)
}
context.pop()
return output

View File

@@ -1,5 +1,3 @@
import Foundation
public func until(tags:[String])(parser:TokenParser, token:Token) -> Bool {
if let name = token.components().first {
for tag in tags {
@@ -14,18 +12,7 @@ public func until(tags:[String])(parser:TokenParser, token:Token) -> Bool {
/// A class for parsing an array of tokens and converts them into a collection of Node's
public class TokenParser {
public typealias TagParser = (TokenParser, Token) -> Result
public typealias NodeList = [Node]
public enum Result {
case Success(node: Node)
case Error(error: Stencil.Error)
}
public enum Results {
case Success(nodes: NodeList)
case Error(error: Stencil.Error)
}
public typealias TagParser = (TokenParser, Token) throws -> NodeType
private var tokens:[Token]
private var tags = [String:TagParser]()
@@ -47,19 +34,19 @@ public class TokenParser {
}
/// Registers a simple template tag with a name and a handler
public func registerSimpleTag(name:String, handler:((Context) -> (Stencil.Result))) {
registerTag(name, parser: { (parser, token) -> TokenParser.Result in
return .Success(node:SimpleNode(handler: handler))
public func registerSimpleTag(name:String, handler:(Context throws -> String)) {
registerTag(name, parser: { parser, token in
return SimpleNode(handler: handler)
})
}
/// Parse the given tokens into nodes
public func parse() -> Results {
return parse(nil)
public func parse() throws -> [NodeType] {
return try parse(nil)
}
public func parse(parse_until:((parser:TokenParser, token:Token) -> (Bool))?) -> TokenParser.Results {
var nodes = NodeList()
public func parse(parse_until:((parser:TokenParser, token:Token) -> (Bool))?) throws -> [NodeType] {
var nodes = [NodeType]()
while tokens.count > 0 {
let token = nextToken()!
@@ -75,26 +62,19 @@ public class TokenParser {
if let parse_until = parse_until {
if parse_until(parser: self, token: token) {
prependToken(token)
return .Success(nodes:nodes)
return nodes
}
}
if let tag = tag {
if let parser = self.tags[tag] {
switch parser(self, token) {
case .Success(let node):
nodes.append(node)
case .Error(let error):
return .Error(error:error)
}
}
if let tag = tag, let parser = self.tags[tag] {
nodes.append(try parser(self, token))
}
case .Comment:
continue
}
}
return .Success(nodes:nodes)
return nodes
}
public func nextToken() -> Token? {

View File

@@ -1,25 +0,0 @@
import Foundation
public protocol Error : CustomStringConvertible {
}
public func ==(lhs:Error, rhs:Error) -> Bool {
return lhs.description == rhs.description
}
public enum Result : Equatable {
case Success(String)
case Error(Stencil.Error)
}
public func ==(lhs:Result, rhs:Result) -> Bool {
switch (lhs, rhs) {
case (.Success(let lhsValue), .Success(let rhsValue)):
return lhsValue == rhsValue
case (.Error(let lhsValue), .Error(let rhsValue)):
return lhsValue == rhsValue
default:
return false
}
}

View File

@@ -12,15 +12,15 @@ public class Template {
/// Create a template with the given name inside the given bundle
public convenience init(named:String, inBundle bundle:NSBundle?) throws {
var url:NSURL?
let url:NSURL
if let bundle = bundle {
url = bundle.URLForResource(named, withExtension: nil)
url = bundle.URLForResource(named, withExtension: nil)!
} else {
url = NSBundle.mainBundle().URLForResource(named, withExtension: nil)
url = NSBundle.mainBundle().URLForResource(named, withExtension: nil)!
}
try self.init(URL:url!)
try self.init(URL:url)
}
/// Create a template with a file found at the given URL
@@ -40,20 +40,9 @@ public class Template {
parser = TokenParser(tokens: tokens)
}
/// Render the given template in a context
public func render(context:Context) -> Result {
switch parser.parse() {
case .Success(let nodes):
return renderNodes(nodes, context: context)
case .Error(let error):
return .Error(error)
}
}
/// Render the given template without a context
public func render() -> Result {
let context = Context()
return render(context)
/// Render the given template
public func render(context:Context? = nil) throws -> String {
let nodes = try parser.parse()
return try renderNodes(nodes, context ?? Context())
}
}

View File

@@ -1,11 +1,3 @@
//
// TemplateLoader.swift
// Stencil
//
// Created by Kyle Fuller on 28/12/2014.
// Copyright (c) 2014 Cocode. All rights reserved.
//
import Foundation
import PathKit

View File

@@ -1,44 +1,37 @@
import Foundation
import PathKit
extension String : Error {
public var description:String {
return self
}
}
public class IncludeNode : Node {
public class IncludeNode : NodeType {
public let templateName:String
public class func parse(parser:TokenParser, token:Token) -> TokenParser.Result {
public class func parse(parser:TokenParser, token:Token) throws -> NodeType {
let bits = token.contents.componentsSeparatedByString("\"")
if bits.count != 3 {
return .Error(error:NodeError(token: token, message: "Tag takes one argument, the template file to be included"))
throw TemplateSyntaxError("'include' tag takes one argument, the template file to be included")
}
return .Success(node:IncludeNode(templateName: bits[1]))
return IncludeNode(templateName: bits[1])
}
public init(templateName:String) {
self.templateName = templateName
}
public func render(context: Context) -> Result {
public func render(context: Context) throws -> String {
if let loader = context["loader"] as? TemplateLoader {
if let template = loader.loadTemplate(templateName) {
return template.render(context)
return try template.render(context)
}
let paths:String = loader.paths.map { path in
return path.description
}.joinWithSeparator(", ")
let error = "Template '\(templateName)' not found in \(paths)"
return .Error(error)
}.joinWithSeparator(", ")
throw TemplateSyntaxError("'\(templateName)' template not found in \(paths)")
}
let error = "Template loader not in context"
return .Error(error)
throw TemplateSyntaxError("Template loader not in context")
}
}

View File

@@ -24,36 +24,32 @@ func any<Element>(elements:[Element], closure:(Element -> Bool)) -> Element? {
return nil
}
class ExtendsNode : Node {
class ExtendsNode : NodeType {
let templateName:String
let blocks:[String:BlockNode]
class func parse(parser:TokenParser, token:Token) -> TokenParser.Result {
class func parse(parser:TokenParser, token:Token) throws -> NodeType {
let bits = token.contents.componentsSeparatedByString("\"")
if bits.count != 3 {
return .Error(error:NodeError(token: token, message: "Tag takes one argument, the template file to be extended"))
throw TemplateSyntaxError("'extends' takes one argument, the template file to be extended")
}
switch parser.parse() {
case .Success(let nodes):
if (any(nodes) { ($0 as? ExtendsNode) != nil }) != nil {
return .Error(error:"'extends' cannot appear more than once in the same template")
}
let blockNodes = nodes.filter { node in node is BlockNode }
let nodes = blockNodes.reduce([String:BlockNode](), combine: { (accumulator, node:Node) -> [String:BlockNode] in
let node = (node as! BlockNode)
var dict = accumulator
dict[node.name] = node
return dict
})
return .Success(node:ExtendsNode(templateName: bits[1], blocks: nodes))
case .Error(let error):
return .Error(error:error)
let parsedNodes = try parser.parse()
if (any(parsedNodes) { ($0 as? ExtendsNode) != nil }) != nil {
throw TemplateSyntaxError("'extends' cannot appear more than once in the same template")
}
let blockNodes = parsedNodes.filter { node in node is BlockNode }
let nodes = blockNodes.reduce([String:BlockNode](), combine: { (accumulator, node:NodeType) -> [String:BlockNode] in
let node = (node as! BlockNode)
var dict = accumulator
dict[node.name] = node
return dict
})
return ExtendsNode(templateName: bits[1], blocks: nodes)
}
init(templateName:String, blocks:[String:BlockNode]) {
@@ -61,12 +57,12 @@ class ExtendsNode : Node {
self.blocks = blocks
}
func render(context: Context) -> Result {
func render(context: Context) throws -> String {
if let loader = context["loader"] as? TemplateLoader {
if let template = loader.loadTemplate(templateName) {
let blockContext = BlockContext(blocks: blocks)
context.push([BlockContext.contextKey: blockContext])
let result = template.render(context)
let result = try template.render(context)
context.pop()
return result
}
@@ -74,51 +70,39 @@ class ExtendsNode : Node {
let paths:String = loader.paths.map { path in
return path.description
}.joinWithSeparator(", ")
let error = "Template '\(templateName)' not found in \(paths)"
return .Error(error)
throw TemplateSyntaxError("'\(templateName)' template not found in \(paths)")
}
let error = "Template loader not in context"
return .Error(error)
throw TemplateSyntaxError("Template loader not in context")
}
}
class BlockNode : Node {
class BlockNode : NodeType {
let name:String
let nodes:[Node]
let nodes:[NodeType]
class func parse(parser:TokenParser, token:Token) -> TokenParser.Result {
class func parse(parser:TokenParser, token:Token) throws -> NodeType {
let bits = token.components()
if bits.count != 2 {
return .Error(error:NodeError(token: token, message: "Tag takes one argument, the template file to be included"))
throw TemplateSyntaxError("'block' tag takes one argument, the template file to be included")
}
let blockName = bits[1]
var nodes = [Node]()
switch parser.parse(until(["endblock"])) {
case .Success(let blockNodes):
nodes = blockNodes
case .Error(let error):
return .Error(error: error)
}
return .Success(node:BlockNode(name:blockName, nodes:nodes))
let nodes = try parser.parse(until(["endblock"]))
return BlockNode(name:blockName, nodes:nodes)
}
init(name:String, nodes:[Node]) {
init(name:String, nodes:[NodeType]) {
self.name = name
self.nodes = nodes
}
func render(context: Context) -> Result {
if let blockContext = context[BlockContext.contextKey] as? BlockContext {
if let node = blockContext.pop(name) {
return node.render(context)
}
func render(context: Context) throws -> String {
if let blockContext = context[BlockContext.contextKey] as? BlockContext, node = blockContext.pop(name) {
return try node.render(context)
}
return renderNodes(nodes, context: context)
return try renderNodes(nodes, context)
}
}

View File

@@ -1,6 +1,3 @@
import Foundation
public enum Token : Equatable {
/// A token representing a piece of text.
case Text(value:String)