{"id":16010571,"url":"https://github.com/kasei/swift-sparql-syntax","last_synced_at":"2026-03-14T06:02:44.377Z","repository":{"id":63279772,"uuid":"130610326","full_name":"kasei/swift-sparql-syntax","owner":"kasei","description":"SPARQL 1.1 Parser and Abstract Syntax","archived":false,"fork":false,"pushed_at":"2024-07-30T18:41:32.000Z","size":492,"stargazers_count":17,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-28T01:36:19.937Z","etag":null,"topics":["parser","sparql"],"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/kasei.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-04-22T21:45:28.000Z","updated_at":"2024-08-26T21:53:42.000Z","dependencies_parsed_at":"2023-10-14T17:07:28.101Z","dependency_job_id":"8bcdf964-3452-466e-b0cd-7be15d0a953b","html_url":"https://github.com/kasei/swift-sparql-syntax","commit_stats":{"total_commits":299,"total_committers":2,"mean_commits":149.5,"dds":0.01672240802675584,"last_synced_commit":"9ebf4abe84e326bd9f9d89be9a80712f5c6fa8dc"},"previous_names":[],"tags_count":146,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kasei%2Fswift-sparql-syntax","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kasei%2Fswift-sparql-syntax/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kasei%2Fswift-sparql-syntax/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kasei%2Fswift-sparql-syntax/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kasei","download_url":"https://codeload.github.com/kasei/swift-sparql-syntax/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243871108,"owners_count":20361304,"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":["parser","sparql"],"created_at":"2024-10-08T13:06:25.784Z","updated_at":"2026-03-14T06:02:44.306Z","avatar_url":"https://github.com/kasei.png","language":"Swift","funding_links":[],"categories":["Programming"],"sub_categories":["Swift"],"readme":"# SPARQLSyntax\n\n## SPARQL 1.1 Parser and Abstract Syntax\n\n - [Features](#features)\n - [Building](#building)\n - [Swift Package Manager](#swift-package-manager)\n - [Command Line Usage](#command-line-usage)\n - [API](#api)\n   - [Term](#term)\n   - [Triple, Quad, TriplePattern and QuadPattern](#triple-quad-triplepattern-and-quadpattern)\n   - [Algebra](#algebra)\n   - [Expression](#expression)\n   - [Query](#query)\n   - [SPARQLParser](#sparqlparser)\n   - [SPARQLSerializer](#sparqlserializer)\n - [Extensions](#extensions)\n   - [Window Functions](#window-functions)\n\n### Features\n\n* [SPARQL 1.1] Parser, Tokenizer, and Serializer available via both API and command line tool\n* Abstract syntax representation of SPARQL queries, aligned with the [SPARQL Algebra]\n* Supported extensions:\n  - [Window Functions](#window-functions)\n\n### Building\n\n```\n% swift build -c release\n```\n\n### Swift Package Manager\n\nTo use SPARQLSyntax with projects using the [Swift Package Manager],\nadd the following to your project's `Package.swift` file:\n\n  ```swift\n  dependencies: [\n    .package(url: \"https://github.com/kasei/swift-sparql-syntax.git\", .upToNextMinor(from: \"0.0.91\"))\n  ]\n  ```\n\n### Command Line Usage\n\nA command line tool, `sparql-parser`, is provided to parse a SPARQL query and\nprint its parsed query algebra, its tokenization, or a pretty-printed SPARQL\nstring:\n\n```\n% ./.build/release/sparql-parser \nUsage: ./.build/release/sparql-parser [-v] COMMAND [ARGUMENTS]\n       ./.build/release/sparql-parser parse query.rq\n       ./.build/release/sparql-parser lint query.rq\n       ./.build/release/sparql-parser tokens query.rq\n```\n\nTo \"lint\", or \"pretty print\", a SPARQL query:\n\n```\n% cat examples/messy.rq\nprefix geo: \u003chttp://www.w3.org/2003/01/geo/wgs84_pos#\u003e\nselect    ?s\nwhere{\n?s geo:lat ?lat ;geo:long ?long   ;\n\tFILTER(?long \u003c -117.0)\nFILTER(?lat \u003e= 31.0)\n  FILTER(?lat \u003c= 33.0)\n} ORDER BY ?s\n\n% ./.build/release/sparql-parser lint examples/messy.rq \nPREFIX geo: \u003chttp://www.w3.org/2003/01/geo/wgs84_pos#\u003e\nSELECT ?s WHERE {\n    ?s geo:lat ?lat ;\n        geo:long ?long ;\n    FILTER (?long \u003c - 117.0)\n    FILTER (?lat \u003e= 31.0)\n    FILTER (?lat \u003c= 33.0)\n}\nORDER BY ?s\n\n```\n\nTo parse the query and print the resulting query algebra:\n\n```\n% ./.build/release/sparql-parser parse examples/messy.rq\nQuery\n  Select { ?s }\n        Project { ?s }\n          OrderBy { ?s }\n            Filter (((?long \u003c -117.0) \u0026\u0026 (?lat \u003e= 31.0)) \u0026\u0026 (?lat \u003c= 33.0))\n              BGP\n                ?s \u003chttp://www.w3.org/2003/01/geo/wgs84_pos#lat\u003e ?lat .\n                ?s \u003chttp://www.w3.org/2003/01/geo/wgs84_pos#long\u003e ?long .\n\n```\n\n### API\n\nThe `SPARQLSyntax` library provides an API for parsing SPARQL queries\nand accessing the resulting abstract data structures.\nThe primary components of this API are:\n\n* `struct Term` - A representation of an RDF Term (IRI, Literal, or Blank node)\n* `enum Algebra` - A representation of the query pattern closely aligned with the formal SPARQL Algebra\n* `enum Expression` - A representation of a logical expression\n* `struct Query` - A representation of a SPARQL query including: a query form (`SELECT`, `ASK`, `DESCRIBE`, or `CONSTRUCT`), a query `Algebra`, and optional base URI and dataset specification\n* `struct SPARQLParser` - Parses a SPARQL query String/Data and returns a `Query`\n* `struct SPARQLSerializer` - Provides the ability to serialize a query, optionally applying \"pretty printing\" formatting\n\n#### `Term`\n\n`struct Term` represents an [RDF Term] (an IRI, a blank node, or an RDF Literal).\n`Term` also provides some support for XSD numeric types,\nbridging between `Term`s and `enum NumericValue` which provides numeric functions and [type-promoting operators](https://www.w3.org/TR/xpath20/#promotion).\n\n#### `Triple`, `Quad`, `TriplePattern`, and `QuadPattern`\n\n`struct Triple` and `struct Quad` combine `Term`s into RDF triples and quads.\n`struct TriplePattern` and `struct QuadPattern` represent patterns which can be matched by concrete `Triple`s and `Quad`s.\nInstead of `Term`s, patterns are comprised of a `enum Node` which can be either a bound `Term`, or a named `variable`.\n\n#### `Algebra`\n\n`enum Algebra` is an representation of a query pattern aligned with the [SPARQL Algebra].\nCases include simple graph pattern matching such as `triple`, `quad`, and `bgp`,\nand more complex operators that can be used to join other `Algebra` values\n(e.g. `innerJoin`, `union`, `project`, `distinct`).\n\n`Algebra` provides functions and properties to access features of graph patterns including:\nvariables used; and in-scope, projectable, and \"necessarily bound\" variables.\nThe structure of `Algebra` values can be modified using a rewriting API that can:\nbind values to specific variables; replace entire `Algebra` sub-trees; and rewrite `Expression`s used within the `Algebra`.\n\n#### `Expression`\n\n`enum Expression` represents a logical expression of variables, values, operators, and functions\nthat can be evaluated within the context of a query result to produce a  `Term` value.\n`Expression`s are used in the following `Algebra` operations: filter, left outer join (\"OPTIONAL\"), extend (\"BIND\"), and aggregate.\n\n`Expression`s may be modified using a similar rewriting API to that provided by `Algebra` that can:\nbind values to specific variables; and replace entire `Expression` sub-trees.\n\n#### `Query`\n\n`struct Query` represents a SPARQL Query and includes:\n\n* a query form (`SELECT`, `ASK`, `DESCRIBE`, or `CONSTRUCT`, and any associated data such as projected variables, or triple patterns used to `CONSTRUCT` a result graph)\n* a graph pattern (`Algebra`)\n* an optional base URI\n* an optional dataset specification\n\n#### `SPARQLParser`\n\n`struct SPARQLParser` provides an API for parsing a SPARQL 1.1 query string and producing a `Query`.\n\n#### `SPARQLSerializer`\n\n`struct SPARQLSerializer` provides an API for serializing SPARQL 1.1 queries, optionally applying \"pretty printing\" rules to produce consistently formatted output.\nIt can serialize both structured queries (`Query` and `Algebra`) and unstructured queries (a query `String`).\nIn the latter case, serialization can be used even if the query contains syntax errors (with data after the error being serialized as-is).\n\n### Extensions\n\n#### Window Functions\n\nParsing of window functions is supported as an extension to the SPARQL 1.1 syntax.\nA SQL-like syntax is supported for projecting window functions in a `SELECT` clause, as well as in a `HAVING` clause.\nIn addition to the built-in aggregate functions, the following window functions are supported:\n`RANK`, `ROW_NUMBER`.\n\nShown below are some examples of the supported syntax.\n\n```swift\n# \"Limit By Resource\"\n# This query limits results to two name/school pairs per person\nPREFIX foaf: \u003chttp://xmlns.com/foaf/0.1/\u003e\nSELECT ?name ?school WHERE {\n\t?s a foaf:Person ;\n\t\tfoaf:name ?name ;\n\t\tfoaf:schoolHomepage ?school\n}\nHAVING (RANK() OVER (PARTITION BY ?s) \u003c 2)\n```\n\n```swift\n# Use window framing to compute a moving average over the trailing four results\nPREFIX : \u003chttp://example.org/\u003e\nSELECT (AVG(?value) OVER (ORDER BY ?date ROWS BETWEEN 3 PRECEDING AND CURRENT ROW) AS ?movingAverage) WHERE {\n\tVALUES (?date ?value) {\n\t\t(1 1.0)\n\t\t(2 2.0)\n\t\t(3 3.0)\n\t\t(4 2.0)\n\t\t(5 0.0)\n\t\t(6 0.0)\n\t\t(7 1.0)\n\t}\n}\n```\n\n\n[SPARQL 1.1]: https://www.w3.org/TR/sparql11-query\n[SPARQL Algebra]: https://www.w3.org/TR/sparql11-query/#sparqlAlgebra\n[Swift Package Manager]: https://swift.org/package-manager\n[RDF Term]: https://www.w3.org/TR/sparql11-query/#sparqlBasicTerms\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkasei%2Fswift-sparql-syntax","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkasei%2Fswift-sparql-syntax","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkasei%2Fswift-sparql-syntax/lists"}