{"id":24558190,"url":"https://github.com/rse/asty","last_synced_at":"2025-07-23T14:07:29.739Z","repository":{"id":25118160,"uuid":"28539764","full_name":"rse/asty","owner":"rse","description":"Abstract Syntax Tree (AST) Data Structure","archived":false,"fork":false,"pushed_at":"2024-03-08T22:05:30.000Z","size":447,"stargazers_count":36,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-07-19T00:32:50.116Z","etag":null,"topics":["abstract","ast","syntax","tree"],"latest_commit_sha":null,"homepage":"https://npmjs.com/asty","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/rse.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}},"created_at":"2014-12-27T15:29:00.000Z","updated_at":"2025-02-01T18:54:08.000Z","dependencies_parsed_at":"2024-03-13T12:02:26.871Z","dependency_job_id":null,"html_url":"https://github.com/rse/asty","commit_stats":{"total_commits":310,"total_committers":1,"mean_commits":310.0,"dds":0.0,"last_synced_commit":"ce340d6876706a4f8ad2bf5dfb1ac0432a507165"},"previous_names":[],"tags_count":98,"template":false,"template_full_name":null,"purl":"pkg:github/rse/asty","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rse%2Fasty","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rse%2Fasty/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rse%2Fasty/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rse%2Fasty/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rse","download_url":"https://codeload.github.com/rse/asty/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rse%2Fasty/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266691580,"owners_count":23969182,"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-23T02:00:09.312Z","response_time":66,"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":["abstract","ast","syntax","tree"],"created_at":"2025-01-23T05:47:36.125Z","updated_at":"2025-07-23T14:07:29.713Z","avatar_url":"https://github.com/rse.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\nASTy\n====\n\nAbstract Syntax Tree (AST) Data Structure\n\n[![github (author stars)](https://img.shields.io/github/stars/rse?logo=github\u0026label=author%20stars\u0026color=%233377aa)](https://github.com/rse)\n[![github (author followers)](https://img.shields.io/github/followers/rse?label=author%20followers\u0026logo=github\u0026color=%234477aa)](https://github.com/rse)\n\u003cbr/\u003e\n[![npm (project release)](https://img.shields.io/npm/v/asty?logo=npm\u0026label=npm%20release\u0026color=%23cc3333)](https://npmjs.com/asty)\n[![npm (project downloads)](https://img.shields.io/npm/dm/asty?logo=npm\u0026label=npm%20downloads\u0026color=%23cc3333)](https://npmjs.com/asty)\n\nInstallation\n------------\n\n```shell\n$ npm install asty\n```\n\nAbout\n-----\n\nASTy is a Abstract Syntax Tree (AST) Data Structure library for JavaScript,\ni.e., it provides a hierarchical data structure for holding the syntax\nabstraction of an arbitrary formal language. It is usually used\nin combination with a parser generator like [PEG.js](http://pegjs.org/)\n(and then especially with its utility class [PEGUtil](http://github.com/rse/pegjs-util))\nto carry the results of the parsing step and to provide the vehicle\nfor further processing those results.\n\nUsage\n-----\n\nASTy provides a context (`ASTYCtx` below) for the creation of AST node\n(`ASTYNode` below). The tree of AST nodes is formed by linking child\nAST nodes into a parent AST node. The ASTy API, here assumed to be\nexposed through the variable `ASTY`, provides the following methods (in\na notation somewhat resembling TypeScript type definitions):\n\n### ASTy Context (ASTYCtx)\n\n- `new ASTY(): ASTYCtx`:\u003cbr/\u003e\n  Create a new instance of the ASTy context.\n  It internally captures the prototype (`ASTYNode`) of the AST nodes to be created.\n\n- `ASTYCtx#version(): { major: Number, minor: Number, micro: Number, date: Number }`:\u003cbr/\u003e\n  Return the ASTy version detals. The date is in numeric format `YYYYMMDD`.\n\n- `ASTYCtx#extend(object: { [methodName: String]: [methodFunc: Function] }): ASTYCtx`:\u003cbr/\u003e\n  Extend the internal ASTYNode prototype with additional methods which are then available on each\n  ASTYNode instance when created with `ASTYCtx#create`. This should be used by ASTy extension modules only.\n\n- `ASTYCtx#create(type: String, attrs?: {[name: String]: [value: Object]}, childs?: ASTY[]): ASTYNode`:\u003cbr/\u003e\n  Create a new ASTYNode instance of `type` and optionally already set attributes and add child nodes.\n\n- `ASTYCtx#isA(object: Object): Boolean`:\u003cbr/\u003e\n  Check whether `object` is an ASTYNode instance.\n\n- `static ASTYCtx::serialize(node: ASTYNode): String`:\u003cbr/\u003e\n  Serializes (formats) ASTy nodes to JSON string. Use this for exporting an AST.\n\n- `static ASTYCtx::unserialize(json: String): ASTYNode`:\u003cbr/\u003e\n  Unserializes (parses) JSON string to ASTy nodes. Use this for importing an AST.\n\n### ASTy Node (ASTYNode)\n\n- `ASTYNode#create(type: String, attrs?: {[name: String]: [value: Object]}, childs?: ASTY[]): ASTYNode`:\u003cbr/\u003e\n  Create a new ASTYNode instance of `type` and optionally already set attributes and add child nodes.\n\n- `ASTYNode#merge(node: Node, takePos?: Boolean, attrMap?: {[from: String]: [to: (String|null)})): ASTYNode`:\u003cbr/\u003e\n  Merge attributes, childs and optionally the position of a node.\n  The attributes can be renamed or skipped (if mapped onto `null`).\n\n- `ASTYNode#type(type: String): Boolean`:\u003cbr/\u003e\n  `ASTYNode#type(): String`:\u003cbr/\u003e\n  Set or get type of node.\n\n- `ASTYNode#pos(line: Number, column: Number, offset: Number): ASTYNode`:\u003cbr/\u003e\n  `ASTYNode#pos(): { line: Number, column: Number, offset: Number }`:\u003cbr/\u003e\n  Set or get the position for the node.\n\n- `ASTYNode#set(name: String, value: Object): ASTYNode`:\u003cbr/\u003e\n  `ASTYNode#set({ [String]: Object }): ASTYNode`:\u003cbr/\u003e\n  Set a single attribute `name` to `value` or set multiple\n  attributes to their corresponding value.\n\n- `ASTYNode#unset(name: String): ASTYNode`:\u003cbr/\u003e\n  `ASTYNode#unset(names: String[]): ASTYNode`:\u003cbr/\u003e\n  Unset a single attribute `name` or unset multiple attributes.\n\n- `ASTYNode#set({ [name: String]: [value: Object] }): ASTYNode`:\u003cbr/\u003e\n  Set multiple attributes, each consisting of name and value pairs.\n\n- `ASTYNode#get(name: String): Object`:\u003cbr/\u003e\n  `ASTYNode#get(names: String[]): Object[]`:\u003cbr/\u003e\n  Get value of a particular attribute `name`,\n  or get array of values corresponding to each name in `names`.\n\n- `ASTYNode#attrs(): String[]`:\u003cbr/\u003e\n  Get names of all node attributes.\n\n- `ASTYNode#nth(): Number`:\u003cbr/\u003e\n  Get position among sibling nodes in parent's child node list.\n  The positions start at 0.\n\n- `ASTYNode#ins(pos: Number, childs: ASTYNode[]): ASTYNode`:\u003cbr/\u003e\n  Add one or more childs to a node, at a fixed position `pos`. The array `childs`\n  can either contain ASTYNode objects or even arrays of ASTYNode objects.\n  If `pos` is negative it counts from the end of child list,\n  with `-1` the position after the last existing child.\n\n- `ASTYNode#add(childs: ASTYNode[]): ASTYNode`:\u003cbr/\u003e\n  Add one or more childs to a node, at the end of the child list. The array `childs`\n  can either contain ASTYNode objects or even arrays of ASTYNode objects.\n\n- `ASTYNode#del(childs: ASTYNode[]): ASTYNode`:\u003cbr/\u003e\n  Delete one or more childs from a node.\n\n- `ASTYNode#childs(begin?: Number, end?: Number): ASTYNode[]`:\u003cbr/\u003e\n  Get a nodes list of all or some childs. The `begin` and `end` parameters\n  are passed-through to `Array::slice`. If the range from `begin` to `end` is\n  out of range, an empty array is returned.\n\n- `ASTYNode#child(pos: Number): ASTYNode`:\u003cbr/\u003e\n  Get a particular child node. If `pos` is out of range, `null` is returned.\n\n- `ASTYNode#parent(): ASTYNode`:\u003cbr/\u003e\n  Get parent node.\n\n- `ASTYNode#walk(callback: (node: ASTYNode, depth: Number, parent: ASTYNode, when: String) =\u003e Void, when?: String): ASTYNode`:\u003cbr/\u003e\n  Recursively walk the AST starting at this node (at depth 0). For\n  each visited node the `callback` function is called with the\n  current node, the current node's tree depth, the current node's\n  parent node and the current walking situation.  By default (and\n  if `when` is either `downward` or `both`), the callback is called\n  in the downward phase, i.e., before(!) all child nodes will be\n  visited, and with `when` set to `downward`. If `when` is set to\n  `upward` or `both`, the callback is called in the upward phase,\n  i.e., after(!) all child nodes were visited, and with `when` set\n  to `upward`.\n\n- `ASTYNode#dump(maxDepth?: Number, colorize?: (type: String, text: String) =\u003e String, unicode?: Boolean): String`:\u003cbr/\u003e\n  Returns a textual dump of the AST starting at the current node. By\n  default `maxDepth` is `Infinity` and this way the whole AST below the\n  current node is dumped. If `maxDepth` is `0` only the current node is\n  dumped. If `maxDepth` is `1` the current node and all its direct child\n  nodes are dumped. The parameter `colorize` is an optional callback function,\n  intended to colorize the output `text` fragments according to their `type`.\n  The following `type` strings are supported: `tree`, `type`, `parenthesis`, `comma`,\n  `key`, `colon`, `value`, `position`, `bracket`, `line`, `slash`, and `column`.\n  If `unicode` is set to `false`, ASCII substitution characters are used\n  for the tree structure.\n\n- `ASTYNode#serialize(): String`:\u003cbr/\u003e\n  Recursively serializes the AST node to JSON.\n  Use this for exporting.\n\nImplementation Notice\n---------------------\n\nAlthough ASTy is written in ECMAScript 2018, it is transpiled to older\nenvironments and this way runs in really all current (as of 2018)\nJavaScript environments, of course.\n\nAdditionally, there are two transpilation results: first, there is a\ncompressed `asty.browser.js` for Browser environments. Second, there is\nan uncompressed `asty.node.js` for Node.js environments.\n\nAlternatives\n------------\n\nA few decent alternatives to ASTy exist:\n\n- [UniST](https://github.com/syntax-tree/unist) and [UniST Builder](https://github.com/syntax-tree/unist-builder)\n- [estree](https://github.com/estree/estree)\n- [AST-Types](https://www.npmjs.com/package/ast-types)\n\nLicense\n-------\n\nCopyright \u0026copy; 2014-2024 Dr. Ralf S. Engelschall (http://engelschall.com/)\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n\"Software\"), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be included\nin all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\nIN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\nTORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\nSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frse%2Fasty","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frse%2Fasty","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frse%2Fasty/lists"}