AST

AST

The ghast abstract syntax tree.

Constructor

new AST(opt)

Source:
Parameters:
Name Type Description
opt ASTConf

an ASTConf object

Members

goDown

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

Source:

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

goLeft

Description:
  • Return leftward sibling. If root, return self.

Source:

Return leftward sibling. If root, return self.

goRight

Description:
  • Return rightward sibling. If root, return self.

Source:

Return rightward sibling. If root, return self.

goUp

Description:
  • Return parent. If root, return self.

Source:

Return parent. If root, return self.

hasAttributes

Description:
  • True if this node has any attributes.

Source:

True if this node has any attributes.

hasTags

Description:
  • True if this node has any tags.

Source:

True if this node has any tags.

image

Description:
  • Return captured syntax as a string.

Source:

Return captured syntax as a string.

isLeaf

Description:
  • True if this node has no children.

Source:

True if this node has no children.

isRoot

Description:
  • True if this node has no parent.

Source:

True if this node has no parent.

isStem

Description:
  • True if this node is not a leaf or root.

Source:

True if this node is not a leaf or root.

leftmostLeaf

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

Source:

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

leftmostNode

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

Source:

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

rightmostLeaf

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

Source:

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

rightmostNode

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

Source:

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

Methods

ancestor(traverseopt, callbackopt) → {Nodes}

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

Source:
Example
// select any 'A' ancestors
node.ancestor('A')
// same as above
node.each({id: 'A', up: true})
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:

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.

Type
Nodes

attr(a, b) → {ast}

Description:
  • 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:

Returns this node

Type
ast

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

Description:
  • Return the nth parent node.

Source:
Example
// return third ancestor
node.climb(2)
// same as above
node.each({up: true, last: true, depth: 2})
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:

The selected node or null if nothing was selected

Type
AST

each(traverseopt, callbackopt) → {Nodes}

Description:
  • Perform a single traversal of the tree.

Source:
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`
Parameters:
Name Type Attributes Description
traverse TraverseLike <optional>

A Traverse literal

callback eachNode <optional>

A function to be called on each selected node

Returns:

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.

Type
Nodes

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

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

Source:
Example
// select first 'A' node
node.first('A')
// same as above
node.each({id: 'A', first: true})
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:

The selected node or null if nothing was selected

Type
AST

hasTag(…tags) → {boolean}

Description:
  • 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}

Description:
  • Set location data for this node

Source:
Parameters:
Name Type Description
data Object

location data to be assigned

Returns:

Returns this node

Type
AST

match(…query) → {boolean}

Description:
  • Match this node against multiple queries

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

one or more QueryLikes to match against

Returns:

Returns true if any of the queries match

Type
boolean

mutate(opt) → {AST}

Description:
  • 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:

Returns the new replacement node

Type
AST

read(k) → {*}

Description:
  • 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:

The value of k

Type
*

remove(nopt)

Description:
  • 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}

Description:
  • 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:

Returns the replacement node

Type
AST

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

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

Source:
Example
// similar to the css selector `A .foo > B`
node.select('A', {tag: 'foo'}, {id: 'B', depth: 0})
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:

An array of any AST nodes that were selected

Type
Array.<AST>

tag(…tags) → {AST}

Description:
  • Assign one or more tags

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

one or more tags to assign

Returns:

Returns this node

Type
AST

when(…visitors)

Description:
  • 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:
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()],
)
Parameters:
Name Type Attributes Description
visitors VisitorLike <repeatable>

One-or-more Visitor literals, or Visitor objects