Initial commit, code not working
This commit is contained in:
7
.gitignore
vendored
Normal file
7
.gitignore
vendored
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
.DS_Store
|
||||||
|
/.build
|
||||||
|
/Packages
|
||||||
|
/*.xcodeproj
|
||||||
|
xcuserdata/
|
||||||
|
/.swiftpm
|
||||||
|
/Package.resolved
|
||||||
20
Package.swift
Normal file
20
Package.swift
Normal file
@@ -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"]),
|
||||||
|
]
|
||||||
|
)
|
||||||
3
README.md
Normal file
3
README.md
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
# hummingbird-mustache
|
||||||
|
|
||||||
|
A description of this package.
|
||||||
77
Sources/HummingbirdMustache/template.swift
Normal file
77
Sources/HummingbirdMustache/template.swift
Normal file
@@ -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]
|
||||||
|
}
|
||||||
|
|
||||||
@@ -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),
|
||||||
|
]
|
||||||
|
}
|
||||||
7
Tests/LinuxMain.swift
Normal file
7
Tests/LinuxMain.swift
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import XCTest
|
||||||
|
|
||||||
|
import hummingbird_mustacheTests
|
||||||
|
|
||||||
|
var tests = [XCTestCaseEntry]()
|
||||||
|
tests += hummingbird_mustacheTests.allTests()
|
||||||
|
XCTMain(tests)
|
||||||
Reference in New Issue
Block a user