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 GitHub actions #10

Merged
merged 4 commits into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
22 changes: 16 additions & 6 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,33 @@ on:

jobs:
build:
name: Swift ${{ matrix.swift }}
name: Swift ${{ matrix.swift }} on Xcode ${{ matrix.xcode }}
strategy:
matrix:
swift: ["5.8", "5.9", "5.10"]
include:
- swift: "5.9"
xcode: "15.2"
- swift: "5.10"
xcode: "15.2"
runs-on: macos-14

steps:
- uses: actions/checkout@v4

- name: Setup Swift
- name: Select Xcode
run: sudo xcode-select -s /Applications/Xcode_${{ matrix.xcode }}.app

Comment on lines +27 to +29
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe remove Select Xcode? Previously, it was needed to specify xcode for version 5.8. @DePasqualeOrg

- name: Set up Swift
uses: swift-actions/setup-swift@v2
with:
swift-version: ${{ matrix.swift }}

- if: ${{ matrix.swift == '5.8' }}
name: Xcode Select
run: sudo xcode-select -s /Applications/Xcode_14.3.1.app
- name: Install swift-format
run: brew install swift-format

- name: Run swift-format
if: ${{ matrix.swift == '5.9' }}
run: swift-format lint --recursive . --strict

- name: Build
run: swift build -v
Expand Down
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
repos:
- repo: https://github.com/slessans/pre-commit-swift-format
rev: ""
hooks:
- id: swift-format
args: ["--configuration", ".swift-format"]
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
73 changes: 37 additions & 36 deletions Tests/LexerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ final class LexerTests: XCTestCase {
"UNDEFINED_ACCESS": "{{ object.undefined_attribute }}",

// Null
"NULL_VARIABLE": "{% if not null_val is defined %}{% set null_val = none %}{% endif %}{% if null_val is not none %}{{ 'fail' }}{% else %}{{ 'pass' }}{% endif %}",
"NULL_VARIABLE":
"{% if not null_val is defined %}{% set null_val = none %}{% endif %}{% if null_val is not none %}{{ 'fail' }}{% else %}{{ 'pass' }}{% endif %}",

// Ternary operator
"TERNARY_OPERATOR":
Expand Down Expand Up @@ -2182,41 +2183,41 @@ final class LexerTests: XCTestCase {

// Null
"NULL_VARIABLE": [
Token(value: "{%", type: .openStatement),
Token(value: "if", type: .if),
Token(value: "not", type: .not),
Token(value: "null_val", type: .identifier),
Token(value: "is", type: .is),
Token(value: "defined", type: .identifier),
Token(value: "%}", type: .closeStatement),
Token(value: "{%", type: .openStatement),
Token(value: "set", type: .set),
Token(value: "null_val", type: .identifier),
Token(value: "=", type: .equals),
Token(value: "none", type: .nullLiteral),
Token(value: "%}", type: .closeStatement),
Token(value: "{%", type: .openStatement),
Token(value: "endif", type: .endIf),
Token(value: "%}", type: .closeStatement),
Token(value: "{%", type: .openStatement),
Token(value: "if", type: .if),
Token(value: "null_val", type: .identifier),
Token(value: "is", type: .is),
Token(value: "not", type: .not),
Token(value: "none", type: .nullLiteral),
Token(value: "%}", type: .closeStatement),
Token(value: "{{", type: .openExpression),
Token(value: "fail", type: .stringLiteral),
Token(value: "}}", type: .closeExpression),
Token(value: "{%", type: .openStatement),
Token(value: "else", type: .else),
Token(value: "%}", type: .closeStatement),
Token(value: "{{", type: .openExpression),
Token(value: "pass", type: .stringLiteral),
Token(value: "}}", type: .closeExpression),
Token(value: "{%", type: .openStatement),
Token(value: "endif", type: .endIf),
Token(value: "%}", type: .closeStatement),
Token(value: "{%", type: .openStatement),
Token(value: "if", type: .if),
Token(value: "not", type: .not),
Token(value: "null_val", type: .identifier),
Token(value: "is", type: .is),
Token(value: "defined", type: .identifier),
Token(value: "%}", type: .closeStatement),
Token(value: "{%", type: .openStatement),
Token(value: "set", type: .set),
Token(value: "null_val", type: .identifier),
Token(value: "=", type: .equals),
Token(value: "none", type: .nullLiteral),
Token(value: "%}", type: .closeStatement),
Token(value: "{%", type: .openStatement),
Token(value: "endif", type: .endIf),
Token(value: "%}", type: .closeStatement),
Token(value: "{%", type: .openStatement),
Token(value: "if", type: .if),
Token(value: "null_val", type: .identifier),
Token(value: "is", type: .is),
Token(value: "not", type: .not),
Token(value: "none", type: .nullLiteral),
Token(value: "%}", type: .closeStatement),
Token(value: "{{", type: .openExpression),
Token(value: "fail", type: .stringLiteral),
Token(value: "}}", type: .closeExpression),
Token(value: "{%", type: .openStatement),
Token(value: "else", type: .else),
Token(value: "%}", type: .closeStatement),
Token(value: "{{", type: .openExpression),
Token(value: "pass", type: .stringLiteral),
Token(value: "}}", type: .closeExpression),
Token(value: "{%", type: .openStatement),
Token(value: "endif", type: .endIf),
Token(value: "%}", type: .closeStatement),
],

// Ternary operator
Expand Down