diff --git a/Sources/ForTag.swift b/Sources/ForTag.swift index 74eb3ab3..5628bbe3 100644 --- a/Sources/ForTag.swift +++ b/Sources/ForTag.swift @@ -16,7 +16,7 @@ class ForNode : NodeType { var emptyNodes = [NodeType]() - let forNodes = try parser.parse(token.whitespace?.trailing, until(["endfor", "empty"])) + let forNodes = try parser.parse(until(["endfor", "empty"])) guard let token = parser.nextToken() else { throw TemplateSyntaxError("`endfor` was not found.") diff --git a/Sources/IfTag.swift b/Sources/IfTag.swift index 9476b393..bd89b5e6 100644 --- a/Sources/IfTag.swift +++ b/Sources/IfTag.swift @@ -171,14 +171,14 @@ class IfNode : NodeType { var trueNodes = [NodeType]() var falseNodes = [NodeType]() - trueNodes = try parser.parse(token.whitespace?.trailing, until(["endif", "else"])) + trueNodes = try parser.parse(until(["endif", "else"])) guard let token = parser.nextToken() else { throw TemplateSyntaxError("`endif` was not found.") } if token.contents == "else" { - falseNodes = try parser.parse(token.whitespace?.trailing, until(["endif"])) + falseNodes = try parser.parse(until(["endif"])) _ = parser.nextToken() } @@ -195,14 +195,14 @@ class IfNode : NodeType { var trueNodes = [NodeType]() var falseNodes = [NodeType]() - falseNodes = try parser.parse(token.whitespace?.trailing, until(["endif", "else"])) + falseNodes = try parser.parse(until(["endif", "else"])) guard let token = parser.nextToken() else { throw TemplateSyntaxError("`endif` was not found.") } if token.contents == "else" { - trueNodes = try parser.parse(token.whitespace?.trailing, until(["endif"])) + trueNodes = try parser.parse(until(["endif"])) _ = parser.nextToken() } diff --git a/Sources/Parser.swift b/Sources/Parser.swift index 7559f647..d612aeaa 100644 --- a/Sources/Parser.swift +++ b/Sources/Parser.swift @@ -19,6 +19,7 @@ public class TokenParser { fileprivate var tokens: [Token] fileprivate let environment: Environment + private var previousWhiteSpace: WhitespaceBehavior.Behavior? public init(tokens: [Token], environment: Environment) { self.tokens = tokens @@ -27,16 +28,11 @@ public class TokenParser { /// Parse the given tokens into nodes public func parse() throws -> [NodeType] { - return try parse(nil, nil) + return try parse(nil) } public func parse(_ parse_until:((_ parser:TokenParser, _ token:Token) -> (Bool))?) throws -> [NodeType] { - return try parse(nil, parse_until) - } - - public func parse(_ leadingWhiteSpace: WhitespaceBehavior.Behavior?, _ parse_until:((_ parser:TokenParser, _ token:Token) -> (Bool))?) throws -> [NodeType] { var nodes = [NodeType]() - var previousWhiteSpace:WhitespaceBehavior.Behavior? = leadingWhiteSpace while tokens.count > 0 { let token = nextToken()! @@ -50,6 +46,7 @@ public class TokenParser { case .variable: nodes.append(VariableNode(variable: try compileFilter(token.contents))) case .block: + previousWhiteSpace = token.whitespace?.trailing if let parse_until = parse_until , parse_until(self, token) { prependToken(token) return nodes @@ -62,7 +59,6 @@ public class TokenParser { case .comment: continue } - previousWhiteSpace = token.whitespace?.trailing } return nodes