[Project] Use 2 spaces for indentation
This commit is contained in:
@@ -1,304 +1,304 @@
|
||||
import Foundation
|
||||
|
||||
struct NodeError : Error {
|
||||
let token:Token
|
||||
let message:String
|
||||
let token:Token
|
||||
let message:String
|
||||
|
||||
init(token:Token, message:String) {
|
||||
self.token = token
|
||||
self.message = message
|
||||
}
|
||||
init(token:Token, message:String) {
|
||||
self.token = token
|
||||
self.message = message
|
||||
}
|
||||
|
||||
var description:String {
|
||||
return "\(token.components().first!): \(message)"
|
||||
}
|
||||
var description:String {
|
||||
return "\(token.components().first!): \(message)"
|
||||
}
|
||||
}
|
||||
|
||||
public protocol Node {
|
||||
/// Return the node rendered as a string, or returns a failure
|
||||
func render(context:Context) -> Result
|
||||
/// Return the node rendered as a string, or returns a failure
|
||||
func render(context:Context) -> Result
|
||||
}
|
||||
|
||||
extension Array {
|
||||
func map<U>(block:((Element) -> (U?, Error?))) -> ([U]?, Error?) {
|
||||
var results = [U]()
|
||||
func map<U>(block:((Element) -> (U?, Error?))) -> ([U]?, Error?) {
|
||||
var results = [U]()
|
||||
|
||||
for item in self {
|
||||
let (result, error) = block(item)
|
||||
for item in self {
|
||||
let (result, error) = block(item)
|
||||
|
||||
if let error = error {
|
||||
return (nil, error)
|
||||
} else if (result != nil) {
|
||||
// let result = result exposing a bug in the Swift compier :(
|
||||
results.append(result!)
|
||||
}
|
||||
}
|
||||
|
||||
return (results, nil)
|
||||
if let error = error {
|
||||
return (nil, error)
|
||||
} else if (result != nil) {
|
||||
// let result = result exposing a bug in the Swift compier :(
|
||||
results.append(result!)
|
||||
}
|
||||
}
|
||||
|
||||
return (results, nil)
|
||||
}
|
||||
}
|
||||
|
||||
public func renderNodes(nodes:[Node], context:Context) -> Result {
|
||||
var result = ""
|
||||
var result = ""
|
||||
|
||||
for item in nodes {
|
||||
switch item.render(context) {
|
||||
case .Success(let string):
|
||||
result += string
|
||||
case .Error(let error):
|
||||
return .Error(error)
|
||||
}
|
||||
for item in nodes {
|
||||
switch item.render(context) {
|
||||
case .Success(let string):
|
||||
result += string
|
||||
case .Error(let error):
|
||||
return .Error(error)
|
||||
}
|
||||
}
|
||||
|
||||
return .Success(result)
|
||||
return .Success(result)
|
||||
}
|
||||
|
||||
public class SimpleNode : Node {
|
||||
let handler:(Context) -> (Result)
|
||||
let handler:(Context) -> (Result)
|
||||
|
||||
public init(handler:((Context) -> (Result))) {
|
||||
self.handler = handler
|
||||
}
|
||||
public init(handler:((Context) -> (Result))) {
|
||||
self.handler = handler
|
||||
}
|
||||
|
||||
public func render(context:Context) -> Result {
|
||||
return handler(context)
|
||||
}
|
||||
public func render(context:Context) -> Result {
|
||||
return handler(context)
|
||||
}
|
||||
}
|
||||
|
||||
public class TextNode : Node {
|
||||
public let text:String
|
||||
public let text:String
|
||||
|
||||
public init(text:String) {
|
||||
self.text = text
|
||||
}
|
||||
public init(text:String) {
|
||||
self.text = text
|
||||
}
|
||||
|
||||
public func render(context:Context) -> Result {
|
||||
return .Success(self.text)
|
||||
}
|
||||
public func render(context:Context) -> Result {
|
||||
return .Success(self.text)
|
||||
}
|
||||
}
|
||||
|
||||
public class VariableNode : Node {
|
||||
public let variable:Variable
|
||||
public let variable:Variable
|
||||
|
||||
public init(variable:Variable) {
|
||||
self.variable = variable
|
||||
public init(variable:Variable) {
|
||||
self.variable = variable
|
||||
}
|
||||
|
||||
public init(variable:String) {
|
||||
self.variable = Variable(variable)
|
||||
}
|
||||
|
||||
public func render(context:Context) -> Result {
|
||||
let result:AnyObject? = variable.resolve(context)
|
||||
|
||||
if let result = result as? String {
|
||||
return .Success(result)
|
||||
} else if let result = result as? NSObject {
|
||||
return .Success(result.description)
|
||||
}
|
||||
|
||||
public init(variable:String) {
|
||||
self.variable = Variable(variable)
|
||||
}
|
||||
|
||||
public func render(context:Context) -> Result {
|
||||
let result:AnyObject? = variable.resolve(context)
|
||||
|
||||
if let result = result as? String {
|
||||
return .Success(result)
|
||||
} else if let result = result as? NSObject {
|
||||
return .Success(result.description)
|
||||
}
|
||||
|
||||
return .Success("")
|
||||
}
|
||||
return .Success("")
|
||||
}
|
||||
}
|
||||
|
||||
public class NowNode : Node {
|
||||
public let format:Variable
|
||||
public let format:Variable
|
||||
|
||||
public class func parse(parser:TokenParser, token:Token) -> TokenParser.Result {
|
||||
var format:Variable?
|
||||
public class func parse(parser:TokenParser, token:Token) -> TokenParser.Result {
|
||||
var format:Variable?
|
||||
|
||||
let components = token.components()
|
||||
if components.count == 2 {
|
||||
format = Variable(components[1])
|
||||
}
|
||||
|
||||
return .Success(node:NowNode(format:format))
|
||||
let components = token.components()
|
||||
if components.count == 2 {
|
||||
format = Variable(components[1])
|
||||
}
|
||||
|
||||
public init(format:Variable?) {
|
||||
if let format = format {
|
||||
self.format = format
|
||||
} else {
|
||||
self.format = Variable("\"yyyy-MM-dd 'at' HH:mm\"")
|
||||
}
|
||||
return .Success(node:NowNode(format:format))
|
||||
}
|
||||
|
||||
public init(format:Variable?) {
|
||||
if let format = format {
|
||||
self.format = format
|
||||
} else {
|
||||
self.format = Variable("\"yyyy-MM-dd 'at' HH:mm\"")
|
||||
}
|
||||
}
|
||||
|
||||
public func render(context: Context) -> Result {
|
||||
let date = NSDate()
|
||||
let format: AnyObject? = self.format.resolve(context)
|
||||
var formatter:NSDateFormatter?
|
||||
|
||||
if let format = format as? NSDateFormatter {
|
||||
formatter = format
|
||||
} else if let format = format as? String {
|
||||
formatter = NSDateFormatter()
|
||||
formatter!.dateFormat = format
|
||||
} else {
|
||||
return .Success("")
|
||||
}
|
||||
|
||||
public func render(context: Context) -> Result {
|
||||
let date = NSDate()
|
||||
let format: AnyObject? = self.format.resolve(context)
|
||||
var formatter:NSDateFormatter?
|
||||
|
||||
if let format = format as? NSDateFormatter {
|
||||
formatter = format
|
||||
} else if let format = format as? String {
|
||||
formatter = NSDateFormatter()
|
||||
formatter!.dateFormat = format
|
||||
} else {
|
||||
return .Success("")
|
||||
}
|
||||
|
||||
return .Success(formatter!.stringFromDate(date))
|
||||
}
|
||||
return .Success(formatter!.stringFromDate(date))
|
||||
}
|
||||
}
|
||||
|
||||
public class ForNode : Node {
|
||||
let variable:Variable
|
||||
let loopVariable:String
|
||||
let nodes:[Node]
|
||||
let variable:Variable
|
||||
let loopVariable:String
|
||||
let nodes:[Node]
|
||||
|
||||
public class func parse(parser:TokenParser, token:Token) -> TokenParser.Result {
|
||||
let components = token.components()
|
||||
public class func parse(parser:TokenParser, token:Token) -> TokenParser.Result {
|
||||
let components = token.components()
|
||||
|
||||
if count(components) == 4 && components[2] == "in" {
|
||||
let loopVariable = components[1]
|
||||
let variable = components[3]
|
||||
if count(components) == 4 && components[2] == "in" {
|
||||
let loopVariable = components[1]
|
||||
let variable = components[3]
|
||||
|
||||
var forNodes:[Node]!
|
||||
var emptyNodes = [Node]()
|
||||
var forNodes:[Node]!
|
||||
var emptyNodes = [Node]()
|
||||
|
||||
switch parser.parse(until(["endfor", "empty"])) {
|
||||
case .Success(let nodes):
|
||||
forNodes = nodes
|
||||
case .Error(let error):
|
||||
return .Error(error: error)
|
||||
}
|
||||
switch parser.parse(until(["endfor", "empty"])) {
|
||||
case .Success(let nodes):
|
||||
forNodes = nodes
|
||||
case .Error(let error):
|
||||
return .Error(error: error)
|
||||
}
|
||||
|
||||
if let token = parser.nextToken() {
|
||||
if token.contents == "empty" {
|
||||
switch parser.parse(until(["endfor"])) {
|
||||
case .Success(let nodes):
|
||||
emptyNodes = nodes
|
||||
case .Error(let error):
|
||||
return .Error(error: error)
|
||||
}
|
||||
if let token = parser.nextToken() {
|
||||
if token.contents == "empty" {
|
||||
switch parser.parse(until(["endfor"])) {
|
||||
case .Success(let nodes):
|
||||
emptyNodes = nodes
|
||||
case .Error(let error):
|
||||
return .Error(error: error)
|
||||
}
|
||||
|
||||
parser.nextToken()
|
||||
}
|
||||
} else {
|
||||
return .Error(error: NodeError(token: token, message: "`endfor` was not found."))
|
||||
}
|
||||
|
||||
return .Success(node:ForNode(variable: variable, loopVariable: loopVariable, nodes: forNodes, emptyNodes:emptyNodes))
|
||||
parser.nextToken()
|
||||
}
|
||||
} else {
|
||||
return .Error(error: NodeError(token: token, message: "`endfor` was not found."))
|
||||
}
|
||||
|
||||
return .Error(error: NodeError(token: token, message: "Invalid syntax. Expected `for x in y`."))
|
||||
return .Success(node:ForNode(variable: variable, loopVariable: loopVariable, nodes: forNodes, emptyNodes:emptyNodes))
|
||||
}
|
||||
|
||||
public init(variable:String, loopVariable:String, nodes:[Node], emptyNodes:[Node]) {
|
||||
self.variable = Variable(variable)
|
||||
self.loopVariable = loopVariable
|
||||
self.nodes = nodes
|
||||
}
|
||||
return .Error(error: NodeError(token: token, message: "Invalid syntax. Expected `for x in y`."))
|
||||
}
|
||||
|
||||
public func render(context: Context) -> Result {
|
||||
let values = variable.resolve(context) as? [AnyObject]
|
||||
var output = ""
|
||||
public init(variable:String, loopVariable:String, nodes:[Node], emptyNodes:[Node]) {
|
||||
self.variable = Variable(variable)
|
||||
self.loopVariable = loopVariable
|
||||
self.nodes = nodes
|
||||
}
|
||||
|
||||
if let values = values {
|
||||
for item in values {
|
||||
context.push()
|
||||
context[loopVariable] = item
|
||||
let result = renderNodes(nodes, context)
|
||||
context.pop()
|
||||
public func render(context: Context) -> Result {
|
||||
let values = variable.resolve(context) as? [AnyObject]
|
||||
var output = ""
|
||||
|
||||
switch result {
|
||||
case .Success(let string):
|
||||
output += string
|
||||
case .Error(let error):
|
||||
return .Error(error)
|
||||
}
|
||||
}
|
||||
if let values = values {
|
||||
for item in values {
|
||||
context.push()
|
||||
context[loopVariable] = item
|
||||
let result = renderNodes(nodes, context)
|
||||
context.pop()
|
||||
|
||||
switch result {
|
||||
case .Success(let string):
|
||||
output += string
|
||||
case .Error(let error):
|
||||
return .Error(error)
|
||||
}
|
||||
|
||||
return .Success(output)
|
||||
}
|
||||
}
|
||||
|
||||
return .Success(output)
|
||||
}
|
||||
}
|
||||
|
||||
public class IfNode : Node {
|
||||
public let variable:Variable
|
||||
public let trueNodes:[Node]
|
||||
public let falseNodes:[Node]
|
||||
public let variable:Variable
|
||||
public let trueNodes:[Node]
|
||||
public let falseNodes:[Node]
|
||||
|
||||
public class func parse(parser:TokenParser, token:Token) -> TokenParser.Result {
|
||||
let variable = token.components()[1]
|
||||
var trueNodes = [Node]()
|
||||
var falseNodes = [Node]()
|
||||
public class func parse(parser:TokenParser, token:Token) -> TokenParser.Result {
|
||||
let variable = token.components()[1]
|
||||
var trueNodes = [Node]()
|
||||
var falseNodes = [Node]()
|
||||
|
||||
switch parser.parse(until(["endif", "else"])) {
|
||||
case .Success(let nodes):
|
||||
trueNodes = nodes
|
||||
case .Error(let error):
|
||||
return .Error(error: error)
|
||||
}
|
||||
|
||||
if let token = parser.nextToken() {
|
||||
if token.contents == "else" {
|
||||
switch parser.parse(until(["endif"])) {
|
||||
case .Success(let nodes):
|
||||
falseNodes = nodes
|
||||
case .Error(let error):
|
||||
return .Error(error: error)
|
||||
}
|
||||
parser.nextToken()
|
||||
}
|
||||
} else {
|
||||
return .Error(error:NodeError(token: token, message: "`endif` was not found."))
|
||||
}
|
||||
|
||||
return .Success(node:IfNode(variable: variable, trueNodes: trueNodes, falseNodes: falseNodes))
|
||||
switch parser.parse(until(["endif", "else"])) {
|
||||
case .Success(let nodes):
|
||||
trueNodes = nodes
|
||||
case .Error(let error):
|
||||
return .Error(error: error)
|
||||
}
|
||||
|
||||
public class func parse_ifnot(parser:TokenParser, token:Token) -> TokenParser.Result {
|
||||
let variable = token.components()[1]
|
||||
var trueNodes = [Node]()
|
||||
var falseNodes = [Node]()
|
||||
|
||||
switch parser.parse(until(["endif", "else"])) {
|
||||
if let token = parser.nextToken() {
|
||||
if token.contents == "else" {
|
||||
switch parser.parse(until(["endif"])) {
|
||||
case .Success(let nodes):
|
||||
falseNodes = nodes
|
||||
falseNodes = nodes
|
||||
case .Error(let error):
|
||||
return .Error(error: error)
|
||||
return .Error(error: error)
|
||||
}
|
||||
|
||||
if let token = parser.nextToken() {
|
||||
if token.contents == "else" {
|
||||
switch parser.parse(until(["endif"])) {
|
||||
case .Success(let nodes):
|
||||
trueNodes = nodes
|
||||
case .Error(let error):
|
||||
return .Error(error: error)
|
||||
}
|
||||
parser.nextToken()
|
||||
}
|
||||
} else {
|
||||
return .Error(error:NodeError(token: token, message: "`endif` was not found."))
|
||||
}
|
||||
|
||||
return .Success(node:IfNode(variable: variable, trueNodes: trueNodes, falseNodes: falseNodes))
|
||||
parser.nextToken()
|
||||
}
|
||||
} else {
|
||||
return .Error(error:NodeError(token: token, message: "`endif` was not found."))
|
||||
}
|
||||
|
||||
public init(variable:String, trueNodes:[Node], falseNodes:[Node]) {
|
||||
self.variable = Variable(variable)
|
||||
self.trueNodes = trueNodes
|
||||
self.falseNodes = falseNodes
|
||||
return .Success(node:IfNode(variable: variable, trueNodes: trueNodes, falseNodes: falseNodes))
|
||||
}
|
||||
|
||||
public class func parse_ifnot(parser:TokenParser, token:Token) -> TokenParser.Result {
|
||||
let variable = token.components()[1]
|
||||
var trueNodes = [Node]()
|
||||
var falseNodes = [Node]()
|
||||
|
||||
switch parser.parse(until(["endif", "else"])) {
|
||||
case .Success(let nodes):
|
||||
falseNodes = nodes
|
||||
case .Error(let error):
|
||||
return .Error(error: error)
|
||||
}
|
||||
|
||||
public func render(context: Context) -> Result {
|
||||
let result: AnyObject? = variable.resolve(context)
|
||||
var truthy = false
|
||||
|
||||
if let result = result as? [AnyObject] {
|
||||
if result.count > 0 {
|
||||
truthy = true
|
||||
}
|
||||
} else if let result: AnyObject = result {
|
||||
truthy = true
|
||||
if let token = parser.nextToken() {
|
||||
if token.contents == "else" {
|
||||
switch parser.parse(until(["endif"])) {
|
||||
case .Success(let nodes):
|
||||
trueNodes = nodes
|
||||
case .Error(let error):
|
||||
return .Error(error: error)
|
||||
}
|
||||
|
||||
context.push()
|
||||
let output = renderNodes(truthy ? trueNodes : falseNodes, context)
|
||||
context.pop()
|
||||
|
||||
return output
|
||||
parser.nextToken()
|
||||
}
|
||||
} else {
|
||||
return .Error(error:NodeError(token: token, message: "`endif` was not found."))
|
||||
}
|
||||
|
||||
return .Success(node:IfNode(variable: variable, trueNodes: trueNodes, falseNodes: falseNodes))
|
||||
}
|
||||
|
||||
public init(variable:String, trueNodes:[Node], falseNodes:[Node]) {
|
||||
self.variable = Variable(variable)
|
||||
self.trueNodes = trueNodes
|
||||
self.falseNodes = falseNodes
|
||||
}
|
||||
|
||||
public func render(context: Context) -> Result {
|
||||
let result: AnyObject? = variable.resolve(context)
|
||||
var truthy = false
|
||||
|
||||
if let result = result as? [AnyObject] {
|
||||
if result.count > 0 {
|
||||
truthy = true
|
||||
}
|
||||
} else if let result: AnyObject = result {
|
||||
truthy = true
|
||||
}
|
||||
|
||||
context.push()
|
||||
let output = renderNodes(truthy ? trueNodes : falseNodes, context)
|
||||
context.pop()
|
||||
|
||||
return output
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user