Fixed all issues found in spec so far

parse has got quite complex might look to simplify
This commit is contained in:
Adam Fowler
2021-03-17 17:02:59 +00:00
parent 753079fa9d
commit b4d6a518c7
4 changed files with 129 additions and 21 deletions

View File

@@ -15,6 +15,7 @@ struct HBParser {
case unexpected case unexpected
case emptyString case emptyString
case invalidUTF8 case invalidUTF8
case invalidPosition
} }
/// Create a Parser object /// Create a Parser object
@@ -282,6 +283,9 @@ extension HBParser {
{ {
unsafeAdvance() unsafeAdvance()
} }
if startIndex == index {
return subParser(startIndex..<startIndex)
}
return subParser(startIndex ..< index) return subParser(startIndex ..< index)
} }
@@ -396,6 +400,15 @@ extension HBParser {
amount -= 1 amount -= 1
} }
} }
func getPosition() -> Int {
return index
}
mutating func setPosition(_ index: Int) throws {
guard range.contains(index) else { throw Error.invalidPosition }
guard validateUTF8Character(at: index).0 != nil else { throw Error.invalidPosition }
_setPosition(index)
}
} }
/// extend Parser to conform to Sequence /// extend Parser to conform to Sequence

View File

@@ -13,73 +13,136 @@ extension HBMustacheTemplate {
} }
/// parse section in mustache text /// parse section in mustache text
static func parse(_ parser: inout HBParser, sectionName: String?) throws -> [Token] { static func parse(_ parser: inout HBParser, sectionName: String?, newLine: Bool = true) throws -> [Token] {
var tokens: [Token] = [] var tokens: [Token] = []
var newLine = newLine
var whiteSpaceBefore: String = ""
while !parser.reachedEnd() { while !parser.reachedEnd() {
let text = try parser.read(untilString: "{{", throwOnOverflow: false, skipToEnd: true) // if new line read whitespace
if newLine {
whiteSpaceBefore = parser.read(while: Set(" \t")).string
}
// read until we hit either a newline or "{"
let text = try parser.read(until: Set("{\n"), throwOnOverflow: false)
// if new line append all text read plus newline
if parser.current() == "\n" {
tokens.append(.text(whiteSpaceBefore + text.string + "\n"))
newLine = true
parser.unsafeAdvance()
continue
} else if parser.current() == "{" {
parser.unsafeAdvance()
if parser.current() != "{" {
if text.count > 0 { if text.count > 0 {
tokens.append(.text(text.string)) tokens.append(.text(whiteSpaceBefore + text.string + "{"))
whiteSpaceBefore = ""
}
continue
} else {
parser.unsafeAdvance()
}
}
if text.count > 0 {
tokens.append(.text(whiteSpaceBefore + text.string))
whiteSpaceBefore = ""
newLine = false
} }
if parser.reachedEnd() { if parser.reachedEnd() {
break break
} }
switch parser.current() { switch parser.current() {
case "#": case "#":
// section
parser.unsafeAdvance() parser.unsafeAdvance()
let (name, method) = try parseName(&parser) let (name, method) = try parseName(&parser)
if newLine && hasLineFinished(&parser) {
newLine = true
if parser.current() == "\n" { if parser.current() == "\n" {
parser.unsafeAdvance() parser.unsafeAdvance()
} }
let sectionTokens = try parse(&parser, sectionName: name) } else if whiteSpaceBefore.count > 0 {
tokens.append(.text(whiteSpaceBefore))
}
let sectionTokens = try parse(&parser, sectionName: name, newLine: newLine)
tokens.append(.section(name: name, method: method, template: HBMustacheTemplate(sectionTokens))) tokens.append(.section(name: name, method: method, template: HBMustacheTemplate(sectionTokens)))
case "^": case "^":
// inverted section
parser.unsafeAdvance() parser.unsafeAdvance()
let (name, method) = try parseName(&parser) let (name, method) = try parseName(&parser)
if newLine && hasLineFinished(&parser) {
newLine = true
if parser.current() == "\n" { if parser.current() == "\n" {
parser.unsafeAdvance() parser.unsafeAdvance()
} }
let sectionTokens = try parse(&parser, sectionName: name) } else if whiteSpaceBefore.count > 0 {
tokens.append(.text(whiteSpaceBefore))
}
let sectionTokens = try parse(&parser, sectionName: name, newLine: newLine)
tokens.append(.invertedSection(name: name, method: method, template: HBMustacheTemplate(sectionTokens))) tokens.append(.invertedSection(name: name, method: method, template: HBMustacheTemplate(sectionTokens)))
case "/": case "/":
// end of section
parser.unsafeAdvance() parser.unsafeAdvance()
let (name, _) = try parseName(&parser) let (name, _) = try parseName(&parser)
guard name == sectionName else { guard name == sectionName else {
throw Error.sectionCloseNameIncorrect throw Error.sectionCloseNameIncorrect
} }
if newLine && hasLineFinished(&parser) {
newLine = true
if parser.current() == "\n" { if parser.current() == "\n" {
parser.unsafeAdvance() parser.unsafeAdvance()
} }
} else if whiteSpaceBefore.count > 0 {
tokens.append(.text(whiteSpaceBefore))
}
return tokens return tokens
case "!":
// comment
parser.unsafeAdvance()
_ = try parseComment(&parser)
if newLine && hasLineFinished(&parser) {
newLine = true
if !parser.reachedEnd() {
parser.unsafeAdvance()
}
}
case "{": case "{":
// unescaped variable
if whiteSpaceBefore.count > 0 {
tokens.append(.text(whiteSpaceBefore))
}
parser.unsafeAdvance() parser.unsafeAdvance()
let (name, method) = try parseName(&parser) let (name, method) = try parseName(&parser)
guard try parser.read("}") else { throw Error.unfinishedName } guard try parser.read("}") else { throw Error.unfinishedName }
tokens.append(.unescapedVariable(name: name, method: method)) tokens.append(.unescapedVariable(name: name, method: method))
case "&": case "&":
// unescaped variable
if whiteSpaceBefore.count > 0 {
tokens.append(.text(whiteSpaceBefore))
}
parser.unsafeAdvance() parser.unsafeAdvance()
let (name, method) = try parseName(&parser) let (name, method) = try parseName(&parser)
tokens.append(.unescapedVariable(name: name, method: method)) tokens.append(.unescapedVariable(name: name, method: method))
case "!":
parser.unsafeAdvance()
_ = try parseComment(&parser)
if parser.current() == "\n" {
parser.unsafeAdvance()
}
case ">": case ">":
// partial
if whiteSpaceBefore.count > 0 {
tokens.append(.text(whiteSpaceBefore))
}
parser.unsafeAdvance() parser.unsafeAdvance()
let (name, _) = try parseName(&parser) let (name, _) = try parseName(&parser)
tokens.append(.partial(name)) tokens.append(.partial(name))
if parser.current() == "\n" {
parser.unsafeAdvance()
}
default: default:
// variable
if whiteSpaceBefore.count > 0 {
tokens.append(.text(whiteSpaceBefore))
}
let (name, method) = try parseName(&parser) let (name, method) = try parseName(&parser)
tokens.append(.variable(name: name, method: method)) tokens.append(.variable(name: name, method: method))
} }
@@ -118,6 +181,17 @@ extension HBMustacheTemplate {
return text.string return text.string
} }
static func hasLineFinished(_ parser: inout HBParser) -> Bool {
var parser2 = parser
if parser.reachedEnd() { return true }
parser2.read(while: Set(" \t\r"))
if parser2.current() == "\n" {
try! parser.setPosition(parser2.getPosition())
return true
}
return false
}
private static let sectionNameCharsWithoutBrackets = Set<Unicode.Scalar>("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._?") private static let sectionNameCharsWithoutBrackets = Set<Unicode.Scalar>("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._?")
private static let sectionNameChars = Set<Unicode.Scalar>("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._?()") private static let sectionNameChars = Set<Unicode.Scalar>("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._?()")
} }

View File

@@ -18,11 +18,28 @@ final class MethodTests: XCTestCase {
XCTAssertEqual(template.render(object), "TEST") XCTAssertEqual(template.render(object), "TEST")
} }
func testNewline() throws {
let template = try HBMustacheTemplate(string: """
{{#repo}}
<b>{{name}}</b>
{{/repo}}
""")
let object: [String: Any] = ["repo": [["name": "resque"], ["name": "hub"], ["name": "rip"]]]
XCTAssertEqual(template.render(object), """
<b>resque</b>
<b>hub</b>
<b>rip</b>
""")
}
func testFirstLast() throws { func testFirstLast() throws {
let template = try HBMustacheTemplate(string: """ let template = try HBMustacheTemplate(string: """
{{#repo}} {{#repo}}
<b>{{#first()}}first: {{/}}{{#last()}}last: {{/}}{{ name }}</b> <b>{{#first()}}first: {{/}}{{#last()}}last: {{/}}{{ name }}</b>
{{/repo}} {{/repo}}
""") """)
let object: [String: Any] = ["repo": [["name": "resque"], ["name": "hub"], ["name": "rip"]]] let object: [String: Any] = ["repo": [["name": "resque"], ["name": "hub"], ["name": "rip"]]]
XCTAssertEqual(template.render(object), """ XCTAssertEqual(template.render(object), """
@@ -38,6 +55,7 @@ final class MethodTests: XCTestCase {
{{#repo}} {{#repo}}
<b>{{#index()}}{{plusone(.)}}{{/}}) {{ name }}</b> <b>{{#index()}}{{plusone(.)}}{{/}}) {{ name }}</b>
{{/repo}} {{/repo}}
""") """)
let object: [String: Any] = ["repo": [["name": "resque"], ["name": "hub"], ["name": "rip"]]] let object: [String: Any] = ["repo": [["name": "resque"], ["name": "hub"], ["name": "rip"]]]
XCTAssertEqual(template.render(object), """ XCTAssertEqual(template.render(object), """
@@ -53,6 +71,7 @@ final class MethodTests: XCTestCase {
{{#repo}} {{#repo}}
<b>{{index()}}) {{#even()}}even {{/}}{{#odd()}}odd {{/}}{{ name }}</b> <b>{{index()}}) {{#even()}}even {{/}}{{#odd()}}odd {{/}}{{ name }}</b>
{{/repo}} {{/repo}}
""") """)
let object: [String: Any] = ["repo": [["name": "resque"], ["name": "hub"], ["name": "rip"]]] let object: [String: Any] = ["repo": [["name": "resque"], ["name": "hub"], ["name": "rip"]]]
XCTAssertEqual(template.render(object), """ XCTAssertEqual(template.render(object), """
@@ -68,6 +87,7 @@ final class MethodTests: XCTestCase {
{{#reversed(repo)}} {{#reversed(repo)}}
<b>{{ name }}</b> <b>{{ name }}</b>
{{/repo}} {{/repo}}
""") """)
let object: [String: Any] = ["repo": [["name": "resque"], ["name": "hub"], ["name": "rip"]]] let object: [String: Any] = ["repo": [["name": "resque"], ["name": "hub"], ["name": "rip"]]]
XCTAssertEqual(template.render(object), """ XCTAssertEqual(template.render(object), """

View File

@@ -390,6 +390,7 @@ final class SpecInvertedTests: XCTestCase {
* first * first
* second * second
* third * third
""" """
try test(object, template, expected) try test(object, template, expected)
} }