Create lexer and template
This commit is contained in:
21
Stencil/Lexer.swift
Normal file
21
Stencil/Lexer.swift
Normal file
@@ -0,0 +1,21 @@
|
||||
//
|
||||
// Lexer.swift
|
||||
// Stencil
|
||||
//
|
||||
// Created by Kyle Fuller on 24/10/2014.
|
||||
// Copyright (c) 2014 Cocode. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public struct Lexer {
|
||||
public let templateString:String
|
||||
|
||||
public init(templateString:String) {
|
||||
self.templateString = templateString
|
||||
}
|
||||
|
||||
public func tokenize() -> [Token] {
|
||||
return [Token.Text(value: templateString)]
|
||||
}
|
||||
}
|
||||
@@ -35,7 +35,7 @@ extension Array {
|
||||
}
|
||||
}
|
||||
|
||||
public func render(nodes:[Node], context:Context) -> (String?, Error?) {
|
||||
public func renderNodes(nodes:[Node], context:Context) -> (String?, Error?) {
|
||||
let result:(results:[String]?, error:Error?) = nodes.map {
|
||||
return $0.render(context)
|
||||
}
|
||||
|
||||
24
Stencil/Template.swift
Normal file
24
Stencil/Template.swift
Normal file
@@ -0,0 +1,24 @@
|
||||
//
|
||||
// Template.swift
|
||||
// Stencil
|
||||
//
|
||||
// Created by Kyle Fuller on 23/10/2014.
|
||||
// Copyright (c) 2014 Cocode. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public struct Template {
|
||||
let nodes:[Node]
|
||||
|
||||
public init(templateString:String) {
|
||||
let lexer = Lexer(templateString: templateString)
|
||||
let tokens = lexer.tokenize()
|
||||
let parser = TokenParser(tokens: tokens)
|
||||
nodes = parser.parse()
|
||||
}
|
||||
|
||||
public func render(context:Context) -> (String?, Error?) {
|
||||
return renderNodes(nodes, context)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user