{"id":21131311,"url":"https://github.com/jrop/catastrophejs","last_synced_at":"2025-03-14T12:22:33.924Z","repository":{"id":57194918,"uuid":"63193125","full_name":"jrop/catastrophejs","owner":"jrop","description":null,"archived":false,"fork":false,"pushed_at":"2017-09-01T17:28:50.000Z","size":16,"stargazers_count":0,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-11T10:49:31.039Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/jrop.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":"2016-07-12T21:21:23.000Z","updated_at":"2016-07-12T21:22:53.000Z","dependencies_parsed_at":"2022-09-16T05:21:46.111Z","dependency_job_id":null,"html_url":"https://github.com/jrop/catastrophejs","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/jrop%2Fcatastrophejs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jrop%2Fcatastrophejs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jrop%2Fcatastrophejs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jrop%2Fcatastrophejs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jrop","download_url":"https://codeload.github.com/jrop/catastrophejs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243573679,"owners_count":20312917,"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-11-20T05:53:36.837Z","updated_at":"2025-03-14T12:22:33.904Z","avatar_url":"https://github.com/jrop.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# catASTrophe\n\n\u003e parse source text into an Abstract Syntax Tree (AST)\n\n# Installation\n\n```sh\n$ npm install catastrophe\n```\n\n# Use\n\n1. Create a matcher\n2. Call `yourMatcher.parse(src)`\n3. Do stuff with the generated AST\n\n```js\n'use strict'\n\nconst match = require('catastrophe')\n\nconst numberList = match.many('NumberList',\n\tmatch.regex('Number', /^\\s*(\\d+)\\s*/),\n\t',')\n\nconst ast = numberList.parse('1, 2, 3,').toObject()\n/*\n{ type: 'NumberList',\n  parts:\n   [ { type: 'Item',\n       parts:\n        [ { type: 'Number', parts: [ '1' ] },\n          { type: 'String', parts: [ ',' ] } ] },\n     { type: 'Item',\n       parts:\n        [ { type: 'Number', parts: [ '2' ] },\n          { type: 'String', parts: [ ',' ] } ] },\n     { type: 'Item',\n       parts:\n        [ { type: 'Number', parts: [ '3' ] },\n          { type: 'String', parts: [ ',' ] } ] } ] }\n*/\n```\n\n# API\n\n## Matcher\n\nA \"Matcher\" has two different forms, depending on how it is being referred to below: 1) as a parameter, and 2) as a return value.  When it is being passed as a parameter, it will be normalized to its more formal definition.  The pseudo-code for normalization is:\n\n```js\nfunction normalize(matcher) {\n\treturn match.string(matcher) if matcher is string\n\treturn match.regex(matcher) if matcher is regex\n\treturn matcher if matcher is function\n\tthrow new Error('Invalid matcher: ' + matcher)\n}\n```\n\nThe normalized matcher is just a function that takes the form:\n\n```js\nfunction matcher(ctx: SourceContext) {\n\treturn undefined if no match\n\treturn {\n\t\tctx: SourceContext after match,\n\t\tnode: AstNode after match\n\t}\n}\nmatcher.match = str =\u003e matcher(new SourceContext(str))\nmatcher.parse = str =\u003e matcher.match(str).node\n```\n\n## Functions\n\n### match.any(...matchers: Array\u003cMatcher\u003e): Matcher\n\nReturns a matcher that matches the first matching matcher given.\n\nExample:\n\n```js\nmatch.any(numberMatcher, alphaMatcher).parse('123')\n// =\u003e { type: 'Number', ... }\n```\n\n### match.many(type: string, item: Matcher, separator: Matcher): Matcher\n\nReturns a matcher that matches zero-or-more items as specified by the given item-matcher and separator-matcher.\n\nExample:\n\n```js\nmatch.many('NumberList', match.regex('Number', /^\\s*(\\d+)\\s*/), ',')\n// =\u003e { type: 'NumberList',\n//      parts: [ { type: 'Item', parts: [ { type: 'Number', parts: [ '1' ] } ] } ] }\n```\n\n### match.optional(noneType: string, matcher: Matcher): Matcher\n\nMatches zero or one item specified by the given matcher.  If it matches, it returns the AST node as returned by `matcher`.  If there is no match, it returns an empty AST node with the type specified by `noneType`.\n\n### match.plus(type: string, item: Matcher, separator: Matcher): Matcher\n\nSame as `match.many`, except that it requires a minimum of one (1) match.\n\n### match.regex(type: string, regex: RegExp): Matcher\n\nCreates a matcher based on a Regular Expression.\n\nExample:\n\n```js\nmatch.regex('Number', /^\\s*(\\d+)\\s*/)\n```\n\n### match.sequence(type: string, ...matchers: Array\u003cMatcher\u003e): Matcher\n\nMatches a sequence of matchers.\n\nExample:\n\n```js\nmatch.sequence('Block',\n\tmatch.string('LeftBrace', '{'),\n\tmatch.many(statementMatcher),\n\tmatch.string('RightBrace'),)\n```\n\n### match.string(type: string, s: string): Matcher\n\nMatches a string, disregarding whitespace.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjrop%2Fcatastrophejs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjrop%2Fcatastrophejs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjrop%2Fcatastrophejs/lists"}