syntax-tree

Syntax Tree

Usage no npm install needed!

<script type="module">
  import syntaxTree from 'https://cdn.skypack.dev/syntax-tree';
</script>

README

Syntax Tree

npm npm downloads

Install via npm

$ npm install --save syntax-tree

Usage

var SyntaxTree = require( 'syntax-tree' )
// Create a new tree
var tree = new SyntaxTree()

// Create a new node
var childNode = new SyntaxTree.Node({
  type: 'ArbitraryNode'
})

// Add nodes to the tree
tree.root.appendChild( childNode )
tree.root.prependChild( otherChildNode )

// Replace a node
someNode.replaceChild( oldChild, newChild )

// Insertions
someNode.insertBefore( newChild, existingChild )
someNode.insertAfter( newChild, existingChild )

// Removals
someNode.removeChild( child )
otherNode.remove()

// Siblings
someNode.nextSibling.appendChild( newChild )
someNode.previousSibling.appendChild( newChild )

// Children
someNode.firstChild // -> child|null
someNode.lastChild // -> child|null

// Relationships
someNode.isParent // -> true|false
someNode.isChild // -> true|false

Interface Definition

Node

interface Node {
  
  readonly attribute isRoot: boolean;
  readonly attribute isParent: boolean;
  readonly attribute isChild: boolean;
  readonly attribute firstChild: Child | null;
  readonly attribute lastChild: Child | null;
  readonly attribute previousSibling: Node | null;
  readonly attribute nextSibling: Node | null;
  
  type: String;
  parent: Node?;
  children: [ Child ];
  data: Data | null;
  
};

Data

interface Data {};

Root

interface Root <: Node {
  type: "Root";
  parent: Root;
};