Build infrastructure for parsing block tokens
This commit is contained in:
@@ -82,3 +82,14 @@ public class VariableNode : Node {
|
|||||||
return (nil, nil)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,9 +10,11 @@ import Foundation
|
|||||||
|
|
||||||
public class TokenParser {
|
public class TokenParser {
|
||||||
private var tokens:[Token]
|
private var tokens:[Token]
|
||||||
|
private var tags = Dictionary<String, ((TokenParser, Token) -> (Node))>()
|
||||||
|
|
||||||
public init(tokens:[Token]) {
|
public init(tokens:[Token]) {
|
||||||
self.tokens = tokens
|
self.tokens = tokens
|
||||||
|
tags["now"] = NowNode.parse
|
||||||
}
|
}
|
||||||
|
|
||||||
public func parse() -> [Node] {
|
public func parse() -> [Node] {
|
||||||
@@ -31,7 +33,21 @@ public class TokenParser {
|
|||||||
case .Variable(let variable):
|
case .Variable(let variable):
|
||||||
nodes.append(VariableNode(variable: variable))
|
nodes.append(VariableNode(variable: variable))
|
||||||
case .Block(let value):
|
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):
|
case .Comment(let value):
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,4 +45,14 @@ class TokenParserTests: XCTestCase {
|
|||||||
|
|
||||||
XCTAssertEqual(nodes.count, 0)
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user