{"id":13872303,"url":"https://github.com/viktorstrate/swift-tree-sitter","last_synced_at":"2025-05-07T03:23:40.758Z","repository":{"id":41565215,"uuid":"266520756","full_name":"viktorstrate/swift-tree-sitter","owner":"viktorstrate","description":"Swift bindings for the tree-sitter parsing library","archived":false,"fork":false,"pushed_at":"2022-03-03T18:39:12.000Z","size":872,"stargazers_count":32,"open_issues_count":0,"forks_count":6,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-15T08:05:14.099Z","etag":null,"topics":["bindings","ios","macos","swift","tree-sitter"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/viktorstrate.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-05-24T10:49:06.000Z","updated_at":"2024-09-10T18:36:48.000Z","dependencies_parsed_at":"2022-09-21T13:32:07.580Z","dependency_job_id":null,"html_url":"https://github.com/viktorstrate/swift-tree-sitter","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/viktorstrate%2Fswift-tree-sitter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/viktorstrate%2Fswift-tree-sitter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/viktorstrate%2Fswift-tree-sitter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/viktorstrate%2Fswift-tree-sitter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/viktorstrate","download_url":"https://codeload.github.com/viktorstrate/swift-tree-sitter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252804941,"owners_count":21806903,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bindings","ios","macos","swift","tree-sitter"],"created_at":"2024-08-05T23:00:39.206Z","updated_at":"2025-05-07T03:23:40.741Z","avatar_url":"https://github.com/viktorstrate.png","language":"Swift","funding_links":[],"categories":["Swift"],"sub_categories":[],"readme":"# Swift Tree Sitter\n\nThis module provides Swift bindings for the [tree-sitter](https://tree-sitter.github.io) parsing library\n\n## Installation\n\n### Using Swift Package Manager\n\nAdd it as a dependency in the `Package.swift` file.\n\n```swift\n.package(url: \"https://github.com/viktorstrate/swift-tree-sitter\", from: \"1.0.0\")\n```\n\nOr from Xcode navigate to `File` -\u003e `Swift Packages` -\u003e `Add Package Dependency...`, then enter this url:\n\n```\nhttps://github.com/viktorstrate/swift-tree-sitter\n```\n\n### Import directly to Xcode\n\nIf you want to load languages from `.bundles` dynamically at runtime, you'll have to import it directly to Xcode as Mac bundles aren't supported using the Swift Package Manager.\n\nTo do this, download the project and drag the folder with the `SwiftTreeSitter.xcodeproj` file into the sidebar of your Xcode project.\n\n## Usage\n\nFirst you'll need to setup the Parser and specify what language to use.\n\n```swift\nlet javascript = try STSLanguage(fromPreBundle: .javascript)\nlet parser = STSParser(language: javascript)\n```\n\nThen you can parse some source code.\n\n```swift\nlet sourceCode = \"let x = 1; console.log(x);\";\nlet tree = parser.parse(string: sourceCode, oldTree: nil)!\nprint(tree.rootNode.sExpressionString!)\n\n// (program\n//   (lexical_declaration\n//     (variable_declarator name: (identifier) value: (number)))\n//   (expression_statement\n//     (call_expression function:\n//       (member_expression object: (identifier)\n//         property: (property_identifier))\n//         arguments: (arguments (identifier)))))\n```\n\nInspect the syntax tree.\n\n```swift\nlet callExpression = tree.rootNode.child(at: 1).firstChild(forOffset: 0)\nprint(\"type:\\t\\(callExpression.type)\")\nprint(\"start point:\\t\\(callExpression.startPoint)\")\nprint(\"end point:\\t\\(callExpression.endPoint)\")\nprint(\"start byte:\\t\\(callExpression.startByte)\")\nprint(\"end byte:\\t\\(callExpression.endByte)\")\n\n// type:        call_expression\n// start point: STSPoint(row: 0, column: 11)\n// end point:   STSPoint(row: 0, column: 25)\n// start byte:  11\n// end byte:    25\n```\n\nIf your source code changes you can update the syntax tree.\nThis will take less time than to recompute the tree from scratch again.\n\n```swift\n// replace let with const\nlet newSourceCode = \"const x = 1; console.log(x);\";\n\ntree.edit(\n  STSInputEdit(\n    startByte: 0,\n    oldEndByte: 3,\n    newEndByte: 5,\n    startPoint: STSPoint(row: 0, column: 0),\n    oldEndPoint: STSPoint(row: 0, column: 3),\n    newEndPoint: STSPoint(row: 0, column: 5)\n))\n\nlet newTree = parser.parse(string: newSourceCode, oldTree: tree)\n```\n\n### Parsing text from a custom data source\n\nIf your text is stored in a custom data source,\nyou can parse it by passing a callback to `.parse()` instead of a `String`.\n\n```swift\nlet sourceLines = [\n  \"let x = 1;\\n\",\n  \"console.log(x);\\n\"\n]\n\nlet tree = parser.parse(callback: { (byte, point) -\u003e [Int8] in\n  if (point.row \u003e= sourceLines.count) {\n    return []\n  }\n\n  let line = sourceLines[Int(point.row)]\n\n  let index = line.index(line.startIndex, offsetBy: Int(point.column))\n  let slice = line[index...]\n  let array = Array(slice.utf8).map { Int8($0) }\n\n  return array\n}, oldTree: nil)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fviktorstrate%2Fswift-tree-sitter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fviktorstrate%2Fswift-tree-sitter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fviktorstrate%2Fswift-tree-sitter/lists"}