Skip to content

Commit

Permalink
remove parsing methods on Environment in favour of TokenParser methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyapuchka committed May 10, 2018
1 parent 8cd8639 commit 8e1e7b6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 deletions.
4 changes: 2 additions & 2 deletions Sources/IfTag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ final class IfExpressionParser {
let tokens: [IfToken]
var position: Int = 0

init(components: [String], environment: Environment) throws {
init(components: [String], parser: TokenParser) throws {
self.tokens = try components.map { component in
if let op = findOperator(name: component) {
switch op {
Expand All @@ -111,7 +111,7 @@ final class IfExpressionParser {
}
}

return .variable(try environment.compileResolvable(component))
return .variable(try parser.compileResolvable(component))
}
}

Expand Down
19 changes: 7 additions & 12 deletions Sources/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ public class TokenParser {
case .text(let text):
nodes.append(TextNode(text: text))
case .variable:
let variable = try environment.compileResolvable(token.contents)
nodes.append(VariableNode(variable: variable))
nodes.append(VariableNode(variable: try compileResolvable(token.contents)))
case .block:
if let parse_until = parse_until , parse_until(self, token) {
prependToken(token)
Expand Down Expand Up @@ -72,12 +71,16 @@ public class TokenParser {
tokens.insert(token, at: 0)
}

func findFilter(_ name: String) throws -> FilterType {
return try environment.findFilter(name)
}

public func compileFilter(_ token: String) throws -> Resolvable {
return try FilterExpression(token: token, environment: environment)
return try FilterExpression(token: token, parser: self)
}

func compileExpression(components: [String]) throws -> Expression {
return try IfExpressionParser(components: components, environment: environment).parse()
return try IfExpressionParser(components: components, parser: self).parse()
}

public func compileResolvable(_ token: String) throws -> Resolvable {
Expand Down Expand Up @@ -128,14 +131,6 @@ extension Environment {
return filtersWithDistance.filter({ $0.distance == minDistance }).map({ $0.filterName })
}

public func compileFilter(_ token: String) throws -> Resolvable {
return try FilterExpression(token: token, environment: self)
}

public func compileResolvable(_ token: String) throws -> Resolvable {
return try TokenParser(tokens: [], environment: self).compileResolvable(token)
}

}

// https://en.wikipedia.org/wiki/Levenshtein_distance#Iterative_with_two_matrix_rows
Expand Down
4 changes: 2 additions & 2 deletions Sources/Variable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class FilterExpression : Resolvable {
let filters: [(FilterType, [Variable])]
let variable: Variable

init(token: String, environment: Environment) throws {
init(token: String, parser: TokenParser) throws {
let bits = token.smartSplit(separator: "|").map({ String($0).trim(character: " ") })
if bits.isEmpty {
filters = []
Expand All @@ -22,7 +22,7 @@ class FilterExpression : Resolvable {
do {
filters = try filterBits.map {
let (name, arguments) = parseFilterComponents(token: $0)
let filter = try environment.findFilter(name)
let filter = try parser.findFilter(name)
return (filter, arguments)
}
} catch {
Expand Down

0 comments on commit 8e1e7b6

Please sign in to comment.