{"id":15520659,"url":"https://github.com/turbolent/parsercombinators","last_synced_at":"2025-03-28T20:14:41.808Z","repository":{"id":63921165,"uuid":"96156102","full_name":"turbolent/ParserCombinators","owner":"turbolent","description":"A parser-combinator library for Swift","archived":false,"fork":false,"pushed_at":"2019-05-12T20:42:11.000Z","size":154,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-03T07:46:06.227Z","etag":null,"topics":["parser","parser-combinators","parsing","swift"],"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/turbolent.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":"2017-07-03T22:52:33.000Z","updated_at":"2021-03-29T10:00:26.000Z","dependencies_parsed_at":"2023-01-14T14:15:38.558Z","dependency_job_id":null,"html_url":"https://github.com/turbolent/ParserCombinators","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/turbolent%2FParserCombinators","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/turbolent%2FParserCombinators/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/turbolent%2FParserCombinators/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/turbolent%2FParserCombinators/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/turbolent","download_url":"https://codeload.github.com/turbolent/ParserCombinators/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246093180,"owners_count":20722402,"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","parser-combinators","parsing","swift"],"created_at":"2024-10-02T10:28:32.392Z","updated_at":"2025-03-28T20:14:41.781Z","avatar_url":"https://github.com/turbolent.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/turbolent/ParserCombinators.svg?branch=master)](https://travis-ci.org/turbolent/ParserCombinators)\n\n# ParserCombinators\n\nA *parser-combinator* library for Swift. \n\nParsers are simply functions that accept any kind of input, such as strings or custom data structures, and return an output.\n\nParser combinators are higher-order functions which allow the composition of parsers to create more expressive parsers.\n\n## Examples\n\nFor example, a simple calculator with the grammar in BNF can be implemented as follows:\n\n```\n\u003cexpr\u003e   ::= \u003cterm\u003e \u003caddop\u003e \u003cterm\u003e     | \u003cterm\u003e\n\u003cterm\u003e   ::= \u003cfactor\u003e \u003cmulop\u003e \u003cfactor\u003e | \u003cfactor\u003e\n\u003cfactor\u003e ::= '(' \u003cexpr\u003e ')' | \u003cnum\u003e\n\u003cdigit\u003e  ::= '0' | '1' | ...\n\u003cnum\u003e    ::= \u003cdigit\u003e | \u003cnum\u003e \u003cdigit\u003e\n\u003caddop\u003e  ::= '+' | '-'\n\u003cmulop\u003e  ::= '*' | '/'\n```\n\n```swift\n\nimport ParserCombinators\nimport ParserCombinatorOperators\n\ntypealias Op = (Int, Int) -\u003e Int\ntypealias OpParser = Parser\u003cOp, Character\u003e\n\nlet addOp: OpParser =\n    char(\"+\") ^^^ (+) ||\n    char(\"-\") ^^^ (-)\n\nlet mulOp: OpParser =\n    char(\"*\") ^^^ (*) ||\n    char(\"/\") ^^^ (/)\n\nlet digit = `in`(.decimalDigits, kind: \"digit\")\nlet num = digit.rep(min: 1) ^^ { Int(String($0))! }\n\nlet expr: Parser\u003cInt, Character\u003e =\n    Parser.recursive { expr in\n        let factor = (char(\"(\") ~\u003e expr) \u003c~ char(\")\")\n            || num\n\n        let term = factor.chainLeft(\n            separator: mulOp,\n            min: 1\n        ).map { $0 ?? 0 }\n\n        return term.chainLeft(\n            separator: addOp,\n            min: 1\n        ).map { $0 ?? 0 }\n    }\n\nlet r = CollectionReader(collection: \"(23+42)*3\")\n\nguard case .success(let value, _) = expr.parse(r) else {\n    fatalError()\n}\n\nassert(value == 195)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fturbolent%2Fparsercombinators","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fturbolent%2Fparsercombinators","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fturbolent%2Fparsercombinators/lists"}