Add support for Swift 3.0

This commit is contained in:
Kyle Fuller
2016-09-13 19:09:58 +01:00
parent f393efbd0b
commit feff3b18b1
41 changed files with 252 additions and 229 deletions

View File

@@ -1,4 +1,4 @@
public func until(tags: [String]) -> ((TokenParser, Token) -> Bool) {
public func until(_ tags: [String]) -> ((TokenParser, Token) -> Bool) {
return { parser, token in
if let name = token.components().first {
for tag in tags {
@@ -12,14 +12,14 @@ public func until(tags: [String]) -> ((TokenParser, Token) -> Bool) {
}
}
public typealias Filter = Any? throws -> Any?
public typealias Filter = (Any?) throws -> Any?
/// A class for parsing an array of tokens and converts them into a collection of Node's
public class TokenParser {
open class TokenParser {
public typealias TagParser = (TokenParser, Token) throws -> NodeType
private var tokens: [Token]
private let namespace: Namespace
fileprivate var tokens: [Token]
fileprivate let namespace: Namespace
public init(tokens: [Token], namespace: Namespace) {
self.tokens = tokens
@@ -27,25 +27,25 @@ public class TokenParser {
}
/// Parse the given tokens into nodes
public func parse() throws -> [NodeType] {
open func parse() throws -> [NodeType] {
return try parse(nil)
}
public func parse(parse_until:((parser:TokenParser, token:Token) -> (Bool))?) throws -> [NodeType] {
open func parse(_ parse_until:((_ parser:TokenParser, _ token:Token) -> (Bool))?) throws -> [NodeType] {
var nodes = [NodeType]()
while tokens.count > 0 {
let token = nextToken()!
switch token {
case .Text(let text):
case .text(let text):
nodes.append(TextNode(text: text))
case .Variable:
case .variable:
nodes.append(VariableNode(variable: try compileFilter(token.contents)))
case .Block:
case .block:
let tag = token.components().first
if let parse_until = parse_until where parse_until(parser: self, token: token) {
if let parse_until = parse_until , parse_until(self, token) {
prependToken(token)
return nodes
}
@@ -57,7 +57,7 @@ public class TokenParser {
throw TemplateSyntaxError("Unknown template tag '\(tag)'")
}
}
case .Comment:
case .comment:
continue
}
}
@@ -65,19 +65,19 @@ public class TokenParser {
return nodes
}
public func nextToken() -> Token? {
open func nextToken() -> Token? {
if tokens.count > 0 {
return tokens.removeAtIndex(0)
return tokens.remove(at: 0)
}
return nil
}
public func prependToken(token:Token) {
tokens.insert(token, atIndex: 0)
open func prependToken(_ token:Token) {
tokens.insert(token, at: 0)
}
public func findFilter(name: String) throws -> Filter {
open func findFilter(_ name: String) throws -> Filter {
if let filter = namespace.filters[name] {
return filter
}
@@ -85,7 +85,7 @@ public class TokenParser {
throw TemplateSyntaxError("Invalid filter '\(name)'")
}
func compileFilter(token: String) throws -> Resolvable {
func compileFilter(_ token: String) throws -> Resolvable {
return try FilterExpression(token: token, parser: self)
}
}