-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
416 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
|
||
import Foundation | ||
|
||
enum MetalBuilderParserError: Error{ | ||
case syntaxError(String) | ||
} | ||
|
||
// find function "prefix some_type name" and returns range of "("... | ||
func findFunction(_ function: MetalFunction, in source: String) throws -> Range<Substring.Index>{ | ||
let prefix = function.prefix | ||
let name = function.name | ||
|
||
// find prefix(kernel, vertex, fragment) | ||
var s = source[source.startIndex..<source.endIndex] | ||
var nameId: String.Index? | ||
while true{ | ||
guard let prefixId = findPrefix(prefix, in: s) | ||
else { | ||
throw MetalBuilderParserError | ||
.syntaxError("no "+prefix+" "+name+" function in source!") | ||
} | ||
// find "{" | ||
guard let curlyId = s[prefixId...].range(of: "{")?.upperBound | ||
else { | ||
throw MetalBuilderParserError | ||
.syntaxError("no '{' after '"+prefix+"'!") | ||
} | ||
// find name between prefix and "{" | ||
guard let nameRange = s[prefixId...curlyId].range(of: name) | ||
else { | ||
guard let skipped = skipCurlies(in: s[curlyId...]) | ||
else { | ||
throw MetalBuilderParserError | ||
.syntaxError("no closing '}' after '"+name+"'!") | ||
} | ||
s = skipped | ||
continue } | ||
nameId = nameRange.lowerBound | ||
break | ||
} | ||
guard let nameId = nameId | ||
else { | ||
throw MetalBuilderParserError | ||
.syntaxError("no "+prefix+" "+name+" function in source!") | ||
} | ||
guard let bracketRange = source[nameId...].range(of: "(") | ||
else { | ||
throw MetalBuilderParserError | ||
.syntaxError("expected '(' after '"+function.name+"'!") | ||
} | ||
return bracketRange | ||
} | ||
|
||
|
||
// takes substring starting with '{' | ||
// return index after {...} expression | ||
// or nil if there's no ending '}' | ||
func skipCurlies(in s: String.SubSequence) -> String.SubSequence?{ | ||
var curlyCount = 1 | ||
for index in s.dropFirst().indices{ | ||
switch s[index]{ | ||
case "{": curlyCount += 1 | ||
case "}": curlyCount -= 1 | ||
default: break | ||
} | ||
if curlyCount == 0{ return s[index...].dropFirst() } | ||
} | ||
return nil | ||
} | ||
// takes substring starting with | ||
// return index after (...) expression | ||
// or nil if there's no ending ')' | ||
func skipRounds(in s: String.SubSequence) -> String.SubSequence?{ | ||
var roundCount = 1 | ||
for index in s.dropFirst().indices{ | ||
switch s[index]{ | ||
case "(": roundCount += 1 | ||
case ")": roundCount -= 1 | ||
default: break | ||
} | ||
if roundCount == 0{ return s[index...].dropFirst() } | ||
} | ||
return nil | ||
} | ||
|
||
// find prefix (it should be outside any {...} or (...)) | ||
func findPrefix(_ prefix: String, in string: String.SubSequence) -> String.Index?{ | ||
|
||
var s = string[string.startIndex..<string.endIndex] | ||
while true{ | ||
guard let firstCurly = s.range(of: "{")?.lowerBound | ||
else { return nil } | ||
guard let firstRound = s.range(of: "(")?.lowerBound | ||
else { return nil } | ||
guard let prefixRange = s.range(of: prefix) | ||
else{ return nil } | ||
if firstCurly<prefixRange.upperBound{ | ||
guard let skipped = skipCurlies(in: s[firstCurly...]) | ||
else{ return nil } | ||
s = skipped | ||
continue | ||
} | ||
if firstRound<prefixRange.upperBound{ | ||
guard let skipped = skipRounds(in: s[firstRound...]) | ||
else{ return nil } | ||
s = skipped | ||
continue | ||
} | ||
return prefixRange.lowerBound | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
import Foundation | ||
|
||
enum MetalFunction{ | ||
case vertex(String), fragment(String), compute(String) | ||
var prefix: String{ | ||
switch self{ | ||
case .vertex(_): return "vertex" | ||
case .fragment(_): return "fragment" | ||
case .compute(_): return "kernel" | ||
} | ||
} | ||
var name: String{ | ||
switch self{ | ||
case .vertex(let name): return name | ||
case .fragment(let name): return name | ||
case .compute(let name): return name | ||
} | ||
} | ||
} |
Oops, something went wrong.