{"id":19681962,"url":"https://github.com/higherorderco/tspl","last_synced_at":"2025-07-21T12:32:57.146Z","repository":{"id":224117853,"uuid":"762473842","full_name":"HigherOrderCO/TSPL","owner":"HigherOrderCO","description":"The Simplest Parser Library (that works) in Rust","archived":false,"fork":false,"pushed_at":"2024-08-06T21:37:49.000Z","size":28,"stargazers_count":43,"open_issues_count":1,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-11T07:05:32.341Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"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/HigherOrderCO.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-02-23T21:20:19.000Z","updated_at":"2025-04-04T03:09:38.000Z","dependencies_parsed_at":"2024-03-15T22:41:55.219Z","dependency_job_id":"16dee282-a4e4-414d-829a-7908ac046cc0","html_url":"https://github.com/HigherOrderCO/TSPL","commit_stats":null,"previous_names":["higherorderco/tspl"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/HigherOrderCO/TSPL","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HigherOrderCO%2FTSPL","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HigherOrderCO%2FTSPL/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HigherOrderCO%2FTSPL/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HigherOrderCO%2FTSPL/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/HigherOrderCO","download_url":"https://codeload.github.com/HigherOrderCO/TSPL/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HigherOrderCO%2FTSPL/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266302511,"owners_count":23908168,"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","status":"online","status_checked_at":"2025-07-21T11:47:31.412Z","response_time":64,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2024-11-11T18:09:19.008Z","updated_at":"2025-07-21T12:32:57.043Z","avatar_url":"https://github.com/HigherOrderCO.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# The Simplest Parser Library (TSPL)\n\nTSPL is the The Simplest Parser Library that works in Rust.\n\n## Concept\n\nIn pure functional languages like Haskell, a Parser can be represented as a function:\n\n```\nParser\u003cA\u003e ::= String -\u003e Reply\u003cA, Error\u003e\n```\n\nThis allows us to implement a Monad instance for `Parser\u003cA\u003e`, letting us use the `do-notation` to\ncreate simple and elegant parsers for our own types. Sadly, Rust doesn't have an equivalent. Yet,\nwe can easily emulate it by:\n\n1. Using structs and `impl` to manage the cursor state internally.\n\n2. Returning a `Result`, which allows us to use Rust's `?` to emulate monadic blocks.\n\nThis library merely exposes some functions to implement parsers that way, and nothing else.\n\n## Example\n\nAs an example, let's create a λ-Term parser using TSPL.\n\n1. Implement the type you want to create a parser for.\n\n```rust\nenum Term {\n  Lam { name: String, body: Box\u003cTerm\u003e },\n  App { func: Box\u003cTerm\u003e, argm: Box\u003cTerm\u003e },\n  Var { name: String },\n}\n```\n\n2. Define your grammar. We'll use the following:\n\n```\n\u003cterm\u003e ::= \u003clam\u003e | \u003capp\u003e | \u003cvar\u003e\n\u003clam\u003e  ::= \"λ\" \u003cname\u003e \" \" \u003cterm\u003e\n\u003capp\u003e  ::= \"(\" \u003cterm\u003e \" \" \u003cterm\u003e \")\"\n\u003cvar\u003e  ::= alphanumeric_string\n```\n\n3. Create a new Parser with the `new_parser()!` macro.\n\n```rust\nTSPL::new_parser!(TermParser);\n```\n\n4. Create an `impl TermParser`, with your grammar:\n\n```rust\nimpl\u003c'i\u003e TermParser\u003c'i\u003e {\n  fn parse(\u0026mut self) -\u003e Result\u003cTerm, String\u003e {\n    self.skip_trivia();\n    match self.peek_one() {\n      Some('λ') =\u003e {\n        self.consume(\"λ\")?;\n        let name = self.parse_name()?;\n        let body = Box::new(self.parse()?);\n        Ok(Term::Lam { name, body })\n      }\n      Some('(') =\u003e {\n        self.consume(\"(\")?;\n        let func = Box::new(self.parse()?);\n        let argm = Box::new(self.parse()?);\n        self.consume(\")\")?;\n        Ok(Term::App { func, argm })\n      }\n      _ =\u003e {\n        let name = self.parse_name()?;\n        Ok(Term::Var { name })\n      }\n    }\n  }\n}\n```\n\n5. Use your parser!\n\n```rust\nfn main() {\n  let mut parser = TermParser::new(\"λx(λy(x y) λz z)\");\n  match parser.parse() {\n    Ok(term) =\u003e println!(\"{:?}\", term),\n    Err(err) =\u003e eprintln!(\"{}\", err),\n  }\n}\n```\n\nThe complete example is available in [./examples/lambda_term.rs](./examples/lambda_term.rs). Run it with:\n\n```\ncargo run --example lambda_term\n```\n\n## Credit\n\nThis design is based on T6's new parser for\n[HVM-Core](https://github.com/HigherOrderCO/HVM-Core), and is much cleaner than\nthe old [HOPA](https://github.com/HigherOrderCO/HOP) approach.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhigherorderco%2Ftspl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhigherorderco%2Ftspl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhigherorderco%2Ftspl/lists"}