SGFKit is a library for manipulating a SGF FF[4] file in Swift. You can manipulate a SGF in a type-safe manner.
Please refer documents.
SGFKit supports only Swift Package Manager.
.package(url:"https://github.com/mtj0928/SGFKit",.upToNextMinor(from:"0.4.0"))You can extract a property from the SGF text in the type-safe manner, when the game is specified,
letgo=tryGo(input:"(;FF[4]C[root](;B[ab]))")letrootNode= go.tree.nodes[0] // ;FF[4]C[root]
letfileFormatVersion:Int?= rootNode.propertyValue(of:.fileFormat) // 4
letcomment:String?= rootNode.propertyValue(of:.comment) // "root"
letnodeA= rootNode.children[0] // ;B[ab]
letpointA:Go.Point?= nodeA.propertyValue(of:.black) // (1, 2)You can update a property value and remove a property from a node.
Note
A node is a reference type due to a tree structure.
letgo=tryGo(input:"(;FF[4];B[ab]C[a];B[ba])")letrootNode= go.tree.nodes[0]letnode= rootNode.children[0] // ;B[ab]C[a]
letpoint=GoPoint(column:4, row:3)
node.addProperty(point, to:.black) // dc
node.removeProperty(of:.comment)print(go.tree.convertToSGF()) // "(;FF[4];B[dc];B[ba])"You can parse the SGF based on the official EBNF. The returned value is non terminal symbols of the EBNF.
letinput="(;FF[4]C[root](;C[a];C[b](;C[c])(;C[d];C[e]))(;C[f](;C[g];C[h];C[i])(;C[j])))"letcollection=tryParser.parse(input: input)letfirstNode= collection.gameTrees[0].sequence.nodes[0] // ;FF[4]C[root]
letfirstProperty= firstNode.properties[0] // FF[4]
print(firstProperty.identifier.letters) // "FF"
print(firstProperty.values[0].type.first.value ??"none") // "4"