Build infrastructure for parsing block tokens

This commit is contained in:
Kyle Fuller
2014-10-24 14:02:57 +01:00
parent 576e952488
commit bf67ea3e5f
3 changed files with 38 additions and 1 deletions

View File

@@ -82,3 +82,14 @@ public class VariableNode : Node {
return (nil, nil)
}
}
public class NowNode : Node {
public class func parse(parser:TokenParser, token:Token) -> Node {
return NowNode()
}
public func render(context: Context) -> (String?, Error?) {
let date = NSDate()
return ("\(date)", nil)
}
}

View File

@@ -10,9 +10,11 @@ import Foundation
public class TokenParser {
private var tokens:[Token]
private var tags = Dictionary<String, ((TokenParser, Token) -> (Node))>()
public init(tokens:[Token]) {
self.tokens = tokens
tags["now"] = NowNode.parse
}
public func parse() -> [Node] {
@@ -31,7 +33,21 @@ public class TokenParser {
case .Variable(let variable):
nodes.append(VariableNode(variable: variable))
case .Block(let value):
continue
let tag = token.components().first
if let parse_until = parse_until {
if parse_until(parser: self, token: token) {
prependToken(token)
return nodes
}
}
if let tag = tag {
if let parser = self.tags[tag] {
let node = parser(self, token)
nodes.append(node)
}
}
case .Comment(let value):
continue
}

View File

@@ -45,4 +45,14 @@ class TokenParserTests: XCTestCase {
XCTAssertEqual(nodes.count, 0)
}
func testParsingTagToken() {
let parser = TokenParser(tokens: [
Token.Block(value: "now"),
])
let nodes = parser.parse()
let node = nodes.first as NowNode!
XCTAssertEqual(nodes.count, 1)
}
}