Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix formatting #9

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions Sources/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ enum JinjaError: Error, LocalizedError {
case todo(String)
case syntaxNotSupported(String)

var errorDescription: String? {
switch self {
case .syntax(let message): return "Syntax error: \(message)"
case .parser(let message): return "Parser error: \(message)"
case .runtime(let message): return "Runtime error: \(message)"
case .todo(let message): return "Todo error: \(message)"
case .syntaxNotSupported(let string): return "Syntax not supported: \(string)"
var errorDescription: String? {
switch self {
case .syntax(let message): return "Syntax error: \(message)"
case .parser(let message): return "Parser error: \(message)"
case .runtime(let message): return "Runtime error: \(message)"
case .todo(let message): return "Todo error: \(message)"
case .syntaxNotSupported(let string): return "Syntax not supported: \(string)"
}
}
}
}
6 changes: 4 additions & 2 deletions Sources/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,14 @@ func parse(tokens: [Token]) throws -> Program {
var filter = try parsePrimaryExpression()
if let boolLiteralFilter = filter as? BoolLiteral {
filter = Identifier(value: String(boolLiteralFilter.value))
} else if filter is NullLiteral {
}
else if filter is NullLiteral {
filter = Identifier(value: "none")
}
if let test = filter as? Identifier {
operand = TestExpression(operand: operand as! Expression, negate: negate, test: test)
} else {
}
else {
throw JinjaError.syntax("Expected identifier for the test")
}
}
Expand Down
39 changes: 23 additions & 16 deletions Sources/Runtime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ struct Interpreter {
throw JinjaError.runtime("Cannot unpack non-iterable type: \(type(of:current))")
}
default:
throw JinjaError.syntaxNotSupported(String(describing: node.loopvar))
throw JinjaError.syntaxNotSupported(String(describing: node.loopvar))
}

let evaluated = try self.evaluateBlock(statements: node.body, environment: scope)
Expand Down Expand Up @@ -429,18 +429,22 @@ struct Interpreter {

if left is StringValue, right is ObjectValue {
switch node.operation.value {
case "in":
if let leftString = (left as? StringValue)?.value,
let rightObject = right as? ObjectValue {
return BooleanValue(value: rightObject.value.keys.contains(leftString))
}
case "not in":
if let leftString = (left as? StringValue)?.value,
let rightObject = right as? ObjectValue {
return BooleanValue(value: !rightObject.value.keys.contains(leftString))
}
default:
throw JinjaError.runtime("Unsupported operation '\(node.operation.value)' between StringValue and ObjectValue")
case "in":
if let leftString = (left as? StringValue)?.value,
let rightObject = right as? ObjectValue
{
return BooleanValue(value: rightObject.value.keys.contains(leftString))
}
case "not in":
if let leftString = (left as? StringValue)?.value,
let rightObject = right as? ObjectValue
{
return BooleanValue(value: !rightObject.value.keys.contains(leftString))
}
default:
throw JinjaError.runtime(
"Unsupported operation '\(node.operation.value)' between StringValue and ObjectValue"
)
}
}

Expand Down Expand Up @@ -683,7 +687,8 @@ struct Interpreter {
if let testFunction = environment.tests[node.test.value] {
let result = try testFunction(operand)
return BooleanValue(value: node.negate ? !result : result)
} else {
}
else {
throw JinjaError.runtime("Unknown test: \(node.test.value)")
}
}
Expand Down Expand Up @@ -718,11 +723,13 @@ struct Interpreter {
case let statement as FilterExpression:
return try self.evaluateFilterExpression(node: statement, environment: environment)
case let statement as TestExpression:
return try self.evaluateTestExpression(node: statement, environment: environment)
return try self.evaluateTestExpression(node: statement, environment: environment)
case is NullLiteral:
return NullValue()
default:
throw JinjaError.runtime("Unknown node type: \(type(of:statement)), statement: \(String(describing: statement))")
throw JinjaError.runtime(
"Unknown node type: \(type(of:statement)), statement: \(String(describing: statement))"
)
}
}
else {
Expand Down
Loading