{"id":21222468,"url":"https://github.com/eratio08/kpars","last_synced_at":"2025-03-15T01:17:35.741Z","repository":{"id":231934113,"uuid":"783077382","full_name":"eratio08/kpars","owner":"eratio08","description":"Very simple parser combinator library in Kotlin","archived":false,"fork":false,"pushed_at":"2024-04-08T15:59:53.000Z","size":74,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-21T17:14:46.599Z","etag":null,"topics":["applicative","functional-parsing","kotlin","monad","monadic-parser-combinators","parser","parser-combinators","parsing"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eratio08.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2024-04-06T22:01:02.000Z","updated_at":"2024-04-08T16:03:01.000Z","dependencies_parsed_at":"2024-04-06T23:20:56.270Z","dependency_job_id":"450bf4d6-7587-4817-8f9f-50188255a5d8","html_url":"https://github.com/eratio08/kpars","commit_stats":null,"previous_names":["eratio08/kpars"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eratio08%2Fkpars","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eratio08%2Fkpars/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eratio08%2Fkpars/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eratio08%2Fkpars/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eratio08","download_url":"https://codeload.github.com/eratio08/kpars/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243668236,"owners_count":20328042,"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":["applicative","functional-parsing","kotlin","monad","monadic-parser-combinators","parser","parser-combinators","parsing"],"created_at":"2024-11-20T22:44:30.294Z","updated_at":"2025-03-15T01:17:35.712Z","avatar_url":"https://github.com/eratio08.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Parser Combinator in Kotlin\n\nA very simple [parser combinator](https://www.cs.nott.ac.uk/~pszgmh/pearl.pdf) library implementation in Kotlin.\n\n## Example\n\nThe following grammar\n```\nexpr ::= expr addop term | term\nterm ::= term mulop factor | factor\nfactor ::= digit | ( expr )\ndigit ::= 0 | 1 | . . . | 9\naddop ::= + | -\nmulop ::= * | /\n```\n\nCan be parsed and evaluated like this\n\n```kotlin\n// Parsers\nfun add(a: Int, b: Int): Int = a + b\nfun sub(a: Int, b: Int): Int = a - b\nval addOp: Parser\u003c(Int, Int) -\u003e Int\u003e =\n    (symbol(\"+\") keepRight return_(::add)) or (symbol(\"-\") keepRight return_(::sub))\nfun mul(a: Int, b: Int): Int = a * b\nfun div(a: Int, b: Int): Int = a / b\nval mulOp: Parser\u003c(Int, Int) -\u003e Int\u003e =\n    (symbol(\"*\") keepRight return_(::mul)) or (symbol(\"/\") keepRight return_(::div))\nfun isDigit(c: Char): Boolean = when (c) {\n    in '0'..'9' -\u003e true\n    else -\u003e false\n}\nval digit = token(satisfies(::isDigit)) flatMap { x -\u003e return_(x.code - '0'.code) }\nfun expr(): Parser\u003cInt\u003e {\n    val factor = digit or (symbol(\"(\") flatMap { _ -\u003e expr() keepLeft symbol(\")\") })\n    val term = chainL1(factor, mulOp)\n    return chainL1(term, addOp)\n}\n// Evaluation\nval res = apply(expr())(\"1 + 3 * 3\").firstOrNull()?.first\n// -\u003e 10\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feratio08%2Fkpars","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feratio08%2Fkpars","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feratio08%2Fkpars/lists"}