AST

AST

The ghast abstract syntax tree.

Constructor

new AST(opt)

Source:
Parameters:
Name Type Description
opt ASTConf

an ASTConf object

Members

goDown

Return first (leftmost) child. If this node is a leaf, return self.

Source:

goLeft

Return leftward sibling. If root, return self.

Source:

goRight

Return rightward sibling. If root, return self.

Source:

goUp

Return parent. If root, return self.

Source:

hasAttributes

True if this node has any attributes.

Source:

hasTags

True if this node has any tags.

Source:

image

Return captured syntax as a string.

Source:

isLeaf

True if this node has no children.

Source:

isRoot

True if this node has no parent.

Source:

isStem

True if this node is not a leaf or root.

Source:

leftmostLeaf

Return leftmost descendant node. If this node is a leaf it will return itself.

Source:

leftmostNode

Return leftmost child node or null if this node has no children.

Source:

rightmostLeaf

Return rightmost descendant node. If this node is a leaf it will return itself.

Source:

rightmostNode

Return rightmost child node or null if this node has no children.

Source:

Methods

ancestor(traverseopt, callbackopt) → {Nodes}

Traverse this nodes ancestors. Same as calling each with up specified.

Source:
Parameters:
Name Type Attributes Description
traverse TraverseLike <optional>

A Traverse literal

callback eachNode <optional>

A function to be called on the selected node, if any

Returns:
Type:
Nodes

An array of selected nodes, or a single node if first or last are specified. Returns null if first or last are specified and nothing was selected.

Example
// select any 'A' ancestors
node.ancestor('A')
// same as above
node.each({id: 'A', up: true})

attr(a, b) → {ast}

Assign an attribute

Source:
Parameters:
Name Type Description
a string | Object

the key to assign, or an object to merge with the nodes attributes

b *

the value to assign to key a

Returns:
Type:
ast

Returns this node

climb(nopt, callbackopt) → (nullable) {AST}

Return the nth parent node.

Source:
Parameters:
Name Type Attributes Default Description
n number <optional>
0

ancestor to select

callback eachNode <optional>

A function to be called on the selected node, if any

Returns:
Type:
AST

The selected node or null if nothing was selected

Example
// return third ancestor
node.climb(2)
// same as above
node.each({up: true, last: true, depth: 2})

each(traverseopt, callbackopt) → {Nodes}

Perform a single traversal of the tree.

Source:
Parameters:
Name Type Attributes Description
traverse TraverseLike <optional>

A Traverse literal

callback eachNode <optional>

A function to be called on each selected node

Returns:
Type:
Nodes

An array of selected nodes, or a single node if first or last are specified. Returns null if first or last are specified and nothing was selected.

Example
node.each()                       // return all descendants of `node`
node.each({self: true})           // return `node` and all of its descendants
node.each('Section')              // return all descendants with id `Section`

first(traverseopt, callbackopt) → (nullable) {AST}

Return the first descendant selected by the given traverse. Same as calling each with first specified.

Source:
Parameters:
Name Type Attributes Description
traverse TraverseLike <optional>

A Traverse literal

callback eachNode <optional>

A function to be called on the selected node, if any

Returns:
Type:
AST

The selected node or null if nothing was selected

Example
// select first 'A' node
node.first('A')
// same as above
node.each({id: 'A', first: true})

hasTag(…tags) → {boolean}

Returns true if the node has all of the given tags.

Source:
Parameters:
Name Type Attributes Description
tags string <repeatable>

zero or more tags to check

Returns:
Type:
boolean

loc(data) → {AST}

Set location data for this node

Source:
Parameters:
Name Type Description
data Object

location data to be assigned

Returns:
Type:
AST

Returns this node

match(…query) → {boolean}

Match this node against multiple queries

Source:
Parameters:
Name Type Attributes Description
query QueryLike <repeatable>

one or more QueryLikes to match against

Returns:
Type:
boolean

Returns true if any of the queries match

mutate(opt) → {AST}

Mutate this node in place, replacing any of its properties with those specified in the config object

Source:
Parameters:
Name Type Description
opt ASTConf

an ASTConf object

Returns:
Type:
AST

Returns the new replacement node

read(k) → {*}

Merge the attributes of this node with all of its descendant nodes, and return the value of k

Source:
Parameters:
Name Type Description
k string

the key to read

Returns:
Type:
*

The value of k

remove(nopt)

Remove a child node n. If n is not given, remove this node. with those specified in the config object

Source:
Parameters:
Name Type Attributes Description
n AST <optional>

an AST node

replace(a, bopt) → {AST}

Replace child node a with node b If b is not given, replace self with a

Source:
Parameters:
Name Type Attributes Description
a AST

the child node to replaced, or the node to replace self with

b AST <optional>

the node to replace child node a with

Returns:
Type:
AST

Returns the replacement node

select(…traverseopt, callbackopt) → {Array.<AST>}

Perform selections on the tree from a sequence of traversals. Returns the selected nodes if no callback is given.

Source:
Parameters:
Name Type Attributes Description
traverse TraverseLike <optional>
<repeatable>

Zero-or-more Traverse literals

callback eachNode <optional>

A function to be called on each selected node

Returns:
Type:
Array.<AST>

An array of any AST nodes that were selected

Example
// similar to the css selector `A .foo > B`
node.select('A', {tag: 'foo'}, {id: 'B', depth: 0})

tag(…tags) → {AST}

Assign one or more tags

Source:
Parameters:
Name Type Attributes Description
tags Tags <repeatable>

one or more tags to assign

Returns:
Type:
AST

Returns this node

when(…visitors)

Walk the tree and match each node against the given visitors. When a visitor matches a node its callback is called with the node. Multiple visitors may match each node.

Source:
Parameters:
Name Type Attributes Description
visitors VisitorLike <repeatable>

One-or-more Visitor literals, or Visitor objects

Example
node.when(
  ['A', 'B', n=> n.foo()],
  [{id: 'C', tag: 'x y'}, {tag: 'c'}, n=> n.bar()],
  [{id: 'Y', leaf: true}, {id: 'Z', leaf: true}, n=> n.baz()],
)