Package Exports
- asty
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (asty) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
ASTy
Abstract Syntax Tree (AST) Data Structure
Installation
Node environments (with NPM package manager):
$ npm install astyBrowser environments (with Bower package manager):
$ bower install astyAbout
ASTy is a Abstract Syntax Tree (AST) Data Structure library for JavaScript, i.e., it provides a hierarchical data structure for holding the syntax abstraction of an arbitrary formal language. It is usually used in combination with a parser generator like PEG.js (and then especially with its utility class PEGUtil) to carry the results of the parsing step and to provide the vehicle for further processing of those results.
Usage
ASTy provides a context (ASTYCtx below) for the creation of AST node
(ASTYNode below). The tree of AST nodes is formed by linking child
AST nodes into a parent AST node. The ASTy API, here assumed to be
exposed through the variable ASTY, provides the following methods (in
a notation somewhat resembling TypeScript type definitions):
ASTy Context (ASTYCtx)
new ASTY(): ASTYCtx:
Create a new instance of the ASTy context. It internally captures the prototype (ASTYNode) of the AST nodes to be created.ASTYCtx#extend(object: { [methodName: String]: [methodFunc: Function] }): ASTYCtx:
Extend the internal ASTYNode prototype with additional methods which are then available on each ASTYNode instance when created withASTYCtx#create. This should be used by ASTy extension modules only.ASTYCtx#create(type: String, attrs?: {[name: String]: [value: Object]}, childs?: ASTY[]): ASTYNode:
Create a new ASTYNode instance oftypeand optionally already set attributes and add child nodes.ASTYCtx#isA(object: Object): Boolean:
Check whetherobjectis an ASTYNode instance.
ASTy Node (ASTYNode)
ASTYNode#merge(node: Node, takePos?: Boolean, attrMap?: {[from: String]: [to: (String|null)})): ASTYNode:
Merge attributes, childs and optionally the position of a node. The attributes can be renamed or skipped (if mapped ontonull).ASTYNode#type(type: String): Boolean:
ASTYNode#type(): String:
Set or get type of node.ASTYNode#pos(line: Number, column: Number, offset: Number): ASTYNode:
ASTYNode#pos(): Object:
Set or get the position for the node.ASTYNode#set(name: String, value: Object): ASTYNode:
Set a single attributenametovalue.ASTYNode#set({ [name: String]: [value: Object] }): ASTYNode:
Set multiple attributes, each consisting of name and value pairs.ASTYNode#get(name: String): Object:
Get value of attributename.`ASTYNode#attrs(): String[]:
Get names of all node attributes.ASTYNode#add(childs: ASTYNode[]): ASTYNode:
Add one or more childs to a node. The arraychildscan either contain ASTYNode objects or even arrays of ASTYNode objects.ASTYNode#del(childs: ASTYNode[]): ASTYNode:
Delete one or more childs from a node.ASTYNode#childs(): ASTYNode[]:
Get a nodes list of childs.ASTYNode#parent(): ASTYNode:
Get parent node.ASTYNode#walk(callback: (node: ASTYNode, depth: Number, parent: ASTYNode, when: String) => Void, when?: String): ASTYNode:
Recursively walk the AST starting at this node (at depth 0). For each visited node thecallbackfunction is called with the current node, the current node's tree depth, the current node's parent node and the current walking situation. By default (and ifwhenis eitherdownwardorboth), the callback is called in the downward phase, i.e., before(!) all child nodes will be visited, and withwhenset todownward. Ifwhenis set toupwardorboth, the callback is called in the upward phase, i.e., after(!) all child nodes were visited, and withwhenset toupward.ASTYNode#dump(maxDepth?: Number): String:
Returns a textual dump of the AST starting at the current node. By defaultmaxDepthisInfinityand this way the whole AST below the current node is dumped. IfmaxDepthis0only the current node is dumped. IfmaxDepthis1the current node and all its direct child nodes are dumped.
Implementation Notice
Although ASTy is written in ECMAScript 6, it is transpiled to ECMAScript 5 and this way runs in really all(!) current (as of 2015) JavaScript environments, of course.
Additionally, there are two transpilation results: first, there is
asty.browser.js (plus asty.browser.map) for Browser environments.
This is a size-compressed variant but still with source-map for
debugging. Second, there is asty.node.js for Node.js/IO.js
environments. This is a variant without compression and no source-maps.
License
Copyright (c) 2014-2015 Ralf S. Engelschall (http://engelschall.com/)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.