{"id":17087884,"url":"https://github.com/geraintluff/parsley","last_synced_at":"2025-03-23T15:16:38.886Z","repository":{"id":43609126,"uuid":"11988770","full_name":"geraintluff/parsley","owner":"geraintluff","description":"A parsing engine for JavaScript","archived":false,"fork":false,"pushed_at":"2013-08-09T17:47:25.000Z","size":124,"stargazers_count":1,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-28T21:18:22.804Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/geraintluff.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}},"created_at":"2013-08-08T23:21:15.000Z","updated_at":"2018-09-25T02:33:09.000Z","dependencies_parsed_at":"2022-07-12T18:19:12.880Z","dependency_job_id":null,"html_url":"https://github.com/geraintluff/parsley","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/geraintluff%2Fparsley","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/geraintluff%2Fparsley/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/geraintluff%2Fparsley/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/geraintluff%2Fparsley/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/geraintluff","download_url":"https://codeload.github.com/geraintluff/parsley/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245119592,"owners_count":20563763,"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":[],"created_at":"2024-10-14T13:35:16.412Z","updated_at":"2025-03-23T15:16:38.847Z","avatar_url":"https://github.com/geraintluff.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# parsley\n\nShift-reduce-style parsing engine, written in JavaScript, with configurable behaviour for replacement of results, and lookahead-capable \"but not\" rules.\n\nWritten because it's fun, so don't expect impressive performance or other real-world concerns. :)\n\n## Short example\n\nHere's a parser for an example grammar involving brackets, commas and letters, e.g.:\n\n```\n((a,b),c,d)\n```\n\n```javascript\n\tvar expression = new Parser('Expression');\n\n\tvar brackets = new Parser('Brackets');\n\tvar items = new Parser('Bracket items');\n\titems.option(expression).replace([0]);   // replaces result with [expression]\n\titems.option(expression, \",\", items).replace(function (first, comma, others) {\n\t\treturn [first].concat(others);\n\t}); // custom replacement function, concatenating entries\n\tbrackets.option(\"(\", items, \")\").replace(1);  // replace with the second argument\n\n\texpression.option(brackets).butNot(brackets.padded(), \",\", brackets.padded()).replace();\n\texpression.option(/^[a-zA-Z]/).replace();\n```\n\nNote the penultimate line where it *disallows* having two bracket clauses next to each other.\n\nYou then run the parser like:\n\n```javascript\n\tvar parsed = expression.parse(\"((a,b),c,d)\");\n\tconsole.log(parsed.result); // the result, after any replacements\n\tconsole.log(parsed.failure); // the parse error that was furthest along in the input\n```\n\n## Replacement\n\n`.replace()` declares replacement rules for the most recently-created option, and it can take custom functions, indices, arrays of indices, or blank (default replacement behaviour).\n\nWithout replacement, the result looks something like:\n\n```json\n{\n\t\"parser\": \"Brackets\",\n\t\"name\": \"Brackets: 0\",\n\t\"success\": true,\n\t\"sequence\": [\n\t\t\"(\",\n\t\t{...},\n\t\t\")\"\n\t],\n\t\"length\": 11\n}\n```\n\nCustom replacement functions are called with the above structure as `this`, and the arguments taken from `this.sequence`.\n\n## More stuff\n\nThere are some shorthand modifiers like `parser.optional()`, `parser.repeat()`, and construction functions like `Parser.sequence()`.  I'm sure I'll document them if this ever becomes a real project.\n\n`generator.html` is a utility for code-generation.  I'm testing the library out by copy-and-pasting the grammar rules from the [ECMAScript spec](http://www.ecma-international.org/ecma-262/5.1/#sec-A), and it's just a utility to get thing going.\n\nHopefully in the future, it'll be able to read ABNF grammars.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgeraintluff%2Fparsley","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgeraintluff%2Fparsley","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgeraintluff%2Fparsley/lists"}