Skip to content

Commit

Permalink
Keeping trailing whitespace state inside parser
Browse files Browse the repository at this point in the history
This simplifies the parsing code for each node and gives every block
token automatic support for the "-" and "+" whitespace control
characters.
  • Loading branch information
Miguel Bejar committed Jan 18, 2017
1 parent 3fc963b commit 0a93d76
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Sources/ForTag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
8 changes: 4 additions & 4 deletions Sources/IfTag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand All @@ -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()
}

Expand Down
10 changes: 3 additions & 7 deletions Sources/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()!
Expand All @@ -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
Expand All @@ -62,7 +59,6 @@ public class TokenParser {
case .comment:
continue
}
previousWhiteSpace = token.whitespace?.trailing
}

return nodes
Expand Down

0 comments on commit 0a93d76

Please sign in to comment.