Switch to Swift 2.0

This commit is contained in:
Kyle Fuller
2015-09-08 18:42:54 -07:00
parent c1a485c429
commit dcf2611ac2
21 changed files with 162 additions and 171 deletions

View File

@@ -15,7 +15,7 @@ public class Context : Equatable {
public subscript(key: String) -> AnyObject? {
/// Retrieves a variable's value, starting at the current context and going upwards
get {
for dictionary in reverse(dictionaries) {
for dictionary in Array(dictionaries.reverse()) {
if let value:AnyObject = dictionary[key] {
return value
}

View File

@@ -7,7 +7,7 @@
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>org.cocode.$(PRODUCT_NAME:rfc1034identifier)</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>

View File

@@ -2,7 +2,7 @@ import Foundation
public struct Lexer {
public let templateString:String
let regex = NSRegularExpression(pattern: "(\\{\\{.*?\\}\\}|\\{%.*?%\\}|\\{#.*?#\\})", options: nil, error: nil)!
let regex = try! NSRegularExpression(pattern: "(\\{\\{.*?\\}\\}|\\{%.*?%\\}|\\{#.*?#\\})", options: [])
public init(templateString:String) {
self.templateString = templateString
@@ -31,23 +31,25 @@ public struct Lexer {
var tokens = [Token]()
let range = NSMakeRange(0, count(templateString))
let range = NSMakeRange(0, templateString.characters.count)
var lastIndex = 0
let nsTemplateString = templateString as NSString
let options = NSMatchingOptions(0)
let options = NSMatchingOptions(rawValue: 0)
regex.enumerateMatchesInString(templateString, options: options, range: range) { (result, flags, b) in
if result.range.location != lastIndex {
let previousMatch = nsTemplateString.substringWithRange(NSMakeRange(lastIndex, result.range.location - lastIndex))
tokens.append(self.createToken(previousMatch))
if let result = result {
if result.range.location != lastIndex {
let previousMatch = nsTemplateString.substringWithRange(NSMakeRange(lastIndex, result.range.location - lastIndex))
tokens.append(self.createToken(previousMatch))
}
let match = nsTemplateString.substringWithRange(result.range)
tokens.append(self.createToken(match))
lastIndex = result.range.location + result.range.length
}
let match = nsTemplateString.substringWithRange(result.range)
tokens.append(self.createToken(match))
lastIndex = result.range.location + result.range.length
}
if lastIndex < count(templateString) {
if lastIndex < templateString.characters.count {
let substring = (templateString as NSString).substringFromIndex(lastIndex)
tokens.append(Token.Text(value: substring))
}

View File

@@ -149,7 +149,7 @@ public class ForNode : Node {
public class func parse(parser:TokenParser, token:Token) -> TokenParser.Result {
let components = token.components()
if count(components) == 4 && components[2] == "in" {
if components.count == 4 && components[2] == "in" {
let loopVariable = components[1]
let variable = components[3]
@@ -198,7 +198,7 @@ public class ForNode : Node {
for item in values {
context.push()
context[loopVariable] = item
let result = renderNodes(nodes, context)
let result = renderNodes(nodes, context: context)
context.pop()
switch result {
@@ -291,12 +291,12 @@ public class IfNode : Node {
if result.count > 0 {
truthy = true
}
} else if let result: AnyObject = result {
} else if result != nil {
truthy = true
}
context.push()
let output = renderNodes(truthy ? trueNodes : falseNodes, context)
let output = renderNodes(truthy ? trueNodes : falseNodes, context: context)
context.pop()
return output

View File

@@ -69,7 +69,7 @@ public class TokenParser {
nodes.append(TextNode(text: text))
case .Variable(let variable):
nodes.append(VariableNode(variable: variable))
case .Block(let value):
case .Block:
let tag = token.components().first
if let parse_until = parse_until {
@@ -89,7 +89,7 @@ public class TokenParser {
}
}
}
case .Comment(let value):
case .Comment:
continue
}
}

View File

@@ -1,6 +1,6 @@
import Foundation
public protocol Error : Printable {
public protocol Error : CustomStringConvertible {
}

View File

@@ -6,12 +6,12 @@ public class Template {
public let parser:TokenParser
/// Create a template with the given name inside the main bundle
public convenience init?(named:String) {
self.init(named:named, inBundle:nil)
public convenience init?(named:String) throws {
try self.init(named:named, inBundle:nil)
}
/// Create a template with the given name inside the given bundle
public convenience init?(named:String, inBundle bundle:NSBundle?) {
public convenience init?(named:String, inBundle bundle:NSBundle?) throws {
var url:NSURL?
if let bundle = bundle {
@@ -20,31 +20,18 @@ public class Template {
url = NSBundle.mainBundle().URLForResource(named, withExtension: nil)
}
self.init(URL:url!)
try self.init(URL:url!)
}
/// Create a template with a file found at the given URL
public convenience init?(URL:NSURL) {
var error:NSError?
let maybeTemplateString = NSString(contentsOfURL: URL, encoding: NSUTF8StringEncoding, error: &error)
if let templateString = maybeTemplateString {
self.init(templateString:templateString as String)
} else {
self.init(templateString:"")
return nil
}
public convenience init(URL:NSURL) throws {
let templateString = try NSString(contentsOfURL: URL, encoding: NSUTF8StringEncoding)
self.init(templateString: templateString as String)
}
/// Create a template with a file found at the given path
public convenience init?(path:Path) {
var error:NSError?
if let string:String = path.read() {
self.init(templateString:string)
} else {
self.init(templateString:"")
return nil
}
public convenience init?(path:Path) throws {
self.init(templateString: path.read() ?? "")
}
/// Create a template with a template string
@@ -58,7 +45,7 @@ public class Template {
public func render(context:Context) -> Result {
switch parser.parse() {
case .Success(let nodes):
return renderNodes(nodes, context)
return renderNodes(nodes, context: context)
case .Error(let error):
return .Error(error)

View File

@@ -32,8 +32,8 @@ public class TemplateLoader {
for templateName in templateNames {
let templatePath = path + Path(templateName)
if templatePath.exists() {
if let template = Template(path: templatePath) {
if templatePath.exists {
if let template = try? Template(path: templatePath) {
return template
}
}

View File

@@ -30,9 +30,9 @@ public class IncludeNode : Node {
return template.render(context)
}
let paths:String = join(", ", loader.paths.map { path in
let paths:String = loader.paths.map { path in
return path.description
})
}.joinWithSeparator(", ")
let error = "Template '\(templateName)' not found in \(paths)"
return .Error(error)
}

View File

@@ -41,9 +41,9 @@ class ExtendsNode : Node {
return .Error(error:"'extends' cannot appear more than once in the same template")
}
let blockNodes = filter(nodes) { node in node is BlockNode }
let blockNodes = nodes.filter { node in node is BlockNode }
let nodes = reduce(blockNodes, [String:BlockNode](), { (accumulator, node:Node) -> [String:BlockNode] in
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
@@ -71,9 +71,9 @@ class ExtendsNode : Node {
return result
}
let paths:String = join(", ", loader.paths.map { path in
let paths:String = loader.paths.map { path in
return path.description
})
}.joinWithSeparator(", ")
let error = "Template '\(templateName)' not found in \(paths)"
return .Error(error)
}
@@ -119,6 +119,6 @@ class BlockNode : Node {
}
}
return renderNodes(nodes, context)
return renderNodes(nodes, context: context)
}
}

View File

@@ -28,14 +28,14 @@ public struct Variable : Equatable {
} else if let dictionary = current as? Dictionary<String, AnyObject> {
current = dictionary[bit]
} else if let array = current as? [AnyObject] {
if let index = bit.toInt() {
if let index = Int(bit) {
current = array[index]
} else if bit == "first" {
current = array.first
} else if bit == "last" {
current = array.last
} else if bit == "count" {
current = count(array)
current = array.count
}
} else if let object = current as? NSObject {
current = object.valueForKey(bit)