commit 98f5d19e91dd8fe2a2ebdc20b5ac552d1b8668a4 Author: Adam Fowler Date: Thu Mar 11 13:59:28 2021 +0000 Initial commit, code not working diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5b60bb5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +.DS_Store +/.build +/Packages +/*.xcodeproj +xcuserdata/ +/.swiftpm +/Package.resolved \ No newline at end of file diff --git a/Package.swift b/Package.swift new file mode 100644 index 0000000..837d459 --- /dev/null +++ b/Package.swift @@ -0,0 +1,20 @@ +// swift-tools-version:5.3 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "hummingbird-mustache", + products: [ + .library(name: "HummingbirdMustache", targets: ["HummingbirdMustache"]), + ], + dependencies: [ + .package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "0.6.0"), + ], + targets: [ + .target(name: "HummingbirdMustache", dependencies: [ + .product(name: "Hummingbird", package: "hummingbird") + ]), + .testTarget(name: "HummingbirdMustacheTests", dependencies: ["HummingbirdMustache"]), + ] +) diff --git a/README.md b/README.md new file mode 100644 index 0000000..fcbc7e5 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# hummingbird-mustache + +A description of this package. diff --git a/Sources/HummingbirdMustache/template.swift b/Sources/HummingbirdMustache/template.swift new file mode 100644 index 0000000..5d8b1d3 --- /dev/null +++ b/Sources/HummingbirdMustache/template.swift @@ -0,0 +1,77 @@ +import Hummingbird + +enum HBMustacheError: Error { + case sectionCloseNameIncorrect + case unfinishedSectionName +} + +class HBTemplate { + init(_ string: String) throws { + self.tokens = try Self.parse(string) + } + + static func parse(_ string: String) throws -> [Token] { + var parser = Parser(string) + return try parse(&parser, sectionName: nil) + } + + static func parse(_ parser: inout Parser, sectionName: String?) throws -> [Token] { + var tokens: [Token] = [] + while !parser.reachedEnd() { + let text = try parser.read(untilString: "{{", throwOnOverflow: false, skipToEnd: true) + tokens.append(.text(text.string)) + switch parser.current() { + case "#": + let name = try readSectionName(&parser) + let sectionTokens = try parse(&parser, sectionName: name) + tokens.append(.section(name, sectionTokens)) + + case "^": + let name = try readSectionName(&parser) + let sectionTokens = try parse(&parser, sectionName: name) + tokens.append(.invertedSection(name, sectionTokens)) + + case "/": + let name = try readSectionName(&parser) + if name != sectionName { + throw HBMustacheError.sectionCloseNameIncorrect + } + return tokens + + case "{": + let name = try readSectionName(&parser) + guard try parser.read("}") else { throw HBMustacheError.unfinishedSectionName } + tokens.append(.variable(name)) + + case "!": + _ = try readSection(&parser) + + default: + let name = try readSectionName(&parser) + tokens.append(.variable(name)) + } + } + return tokens + } + + static func readSectionName(_ parser: inout Parser) throws -> String { + let text = parser.read(while: { $0.isLetter || $0.isNumber } ) + guard try parser.read("}"), try parser.read("}") else { throw HBMustacheError.unfinishedSectionName } + return text.string + } + + static func readSection(_ parser: inout Parser) throws -> String { + let text = try parser.read(untilString: "}}", throwOnOverflow: true, skipToEnd: true) + return text.string + } + + enum Token { + case text(String) + case variable(String) + case section(String, [Token]) + case invertedSection(String, [Token]) + } + + let tokens: [Token] +} + diff --git a/Tests/HummingbirdMustacheTests/hummingbird_mustacheTests.swift b/Tests/HummingbirdMustacheTests/hummingbird_mustacheTests.swift new file mode 100644 index 0000000..9202bf6 --- /dev/null +++ b/Tests/HummingbirdMustacheTests/hummingbird_mustacheTests.swift @@ -0,0 +1,15 @@ +import XCTest +@testable import hummingbird_mustache + +final class hummingbird_mustacheTests: XCTestCase { + func testExample() { + // This is an example of a functional test case. + // Use XCTAssert and related functions to verify your tests produce the correct + // results. + XCTAssertEqual(hummingbird_mustache().text, "Hello, World!") + } + + static var allTests = [ + ("testExample", testExample), + ] +} diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift new file mode 100644 index 0000000..d7de6c3 --- /dev/null +++ b/Tests/LinuxMain.swift @@ -0,0 +1,7 @@ +import XCTest + +import hummingbird_mustacheTests + +var tests = [XCTestCaseEntry]() +tests += hummingbird_mustacheTests.allTests() +XCTMain(tests)