Basic method setup and calling
This commit is contained in:
@@ -24,7 +24,7 @@ extension HBMustacheTemplate {
|
||||
switch parser.current() {
|
||||
case "#":
|
||||
parser.unsafeAdvance()
|
||||
let name = try parseName(&parser)
|
||||
let (name, _) = try parseName(&parser)
|
||||
if parser.current() == "\n" {
|
||||
parser.unsafeAdvance()
|
||||
}
|
||||
@@ -33,7 +33,7 @@ extension HBMustacheTemplate {
|
||||
|
||||
case "^":
|
||||
parser.unsafeAdvance()
|
||||
let name = try parseName(&parser)
|
||||
let (name, _) = try parseName(&parser)
|
||||
if parser.current() == "\n" {
|
||||
parser.unsafeAdvance()
|
||||
}
|
||||
@@ -42,7 +42,7 @@ extension HBMustacheTemplate {
|
||||
|
||||
case "/":
|
||||
parser.unsafeAdvance()
|
||||
let name = try parseName(&parser)
|
||||
let (name, _) = try parseName(&parser)
|
||||
guard name == sectionName else {
|
||||
throw Error.sectionCloseNameIncorrect
|
||||
}
|
||||
@@ -53,7 +53,7 @@ extension HBMustacheTemplate {
|
||||
|
||||
case "{":
|
||||
parser.unsafeAdvance()
|
||||
let name = try parseName(&parser)
|
||||
let (name, _) = try parseName(&parser)
|
||||
guard try parser.read("}") else { throw Error.unfinishedName }
|
||||
tokens.append(.unescapedVariable(name))
|
||||
|
||||
@@ -63,12 +63,12 @@ extension HBMustacheTemplate {
|
||||
|
||||
case ">":
|
||||
parser.unsafeAdvance()
|
||||
let name = try parseName(&parser)
|
||||
let (name, _) = try parseName(&parser)
|
||||
tokens.append(.partial(name))
|
||||
|
||||
default:
|
||||
let name = try parseName(&parser)
|
||||
tokens.append(.variable(name))
|
||||
let (name, method) = try parseName(&parser)
|
||||
tokens.append(.variable(name, method))
|
||||
}
|
||||
}
|
||||
// should never get here if reading section
|
||||
@@ -78,12 +78,24 @@ extension HBMustacheTemplate {
|
||||
return tokens
|
||||
}
|
||||
|
||||
static func parseName(_ parser: inout HBParser) throws -> String {
|
||||
static func parseName(_ parser: inout HBParser) throws -> (String, String?) {
|
||||
parser.read(while: \.isWhitespace)
|
||||
let text = parser.read(while: sectionNameChars )
|
||||
var text = parser.read(while: sectionNameChars )
|
||||
parser.read(while: \.isWhitespace)
|
||||
guard try parser.read("}"), try parser.read("}") else { throw Error.unfinishedName }
|
||||
return text.string
|
||||
// does the name include brackets. If so this is a method call
|
||||
let string = text.read(while: sectionNameCharsWithoutBrackets)
|
||||
if text.reachedEnd() {
|
||||
return (text.string, nil)
|
||||
} else {
|
||||
guard text.current() == "(" else { throw Error.unfinishedName }
|
||||
text.unsafeAdvance()
|
||||
let string2 = text.read(while: sectionNameCharsWithoutBrackets)
|
||||
guard text.current() == ")" else { throw Error.unfinishedName }
|
||||
text.unsafeAdvance()
|
||||
guard text.reachedEnd() else { throw Error.unfinishedName }
|
||||
return (string2.string, string.string)
|
||||
}
|
||||
}
|
||||
|
||||
static func parseComment(_ parser: inout HBParser) throws -> String {
|
||||
@@ -91,5 +103,6 @@ extension HBMustacheTemplate {
|
||||
return text.string
|
||||
}
|
||||
|
||||
private static let sectionNameChars = Set<Unicode.Scalar>("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._?")
|
||||
private static let sectionNameCharsWithoutBrackets = Set<Unicode.Scalar>("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._?")
|
||||
private static let sectionNameChars = Set<Unicode.Scalar>("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._?()")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user