const Node = require('snapdragon-node');
// either pass on object with "type" and (optional) "val"
const node1 = new Node({type: 'star', val: '*'});
// or pass "val" (first) and "type" (second) as string
const node2 = new Node('*', 'star');
// both result in => Node { type: 'star', val: '*' }
With [snapdragon][] v0.9.0 and higher, it's recommended that you use this.node()
to create a new Node
inside parser handlers (instead of doing new Node()
).
Example usage inside a [snapdragon][] parser handler function.
const Node = require('{%= name %}');
const Token = require('snapdragon-token');
// create a new AST node
const node = new Node({ type: 'star', value: '*' });
// convert a Lexer Token into an AST Node
const token = new Token({ type: 'star', value: '*' });
const node = new Node(token);
AST Nodes are represented as Node
objects that implement the following interface:
interface Node {
type: string;
value: string | undefined
nodes: array | undefined
}
type
{string} - A string representing the node variant type. This property is often used for classifying the purpose or nature of the node, so that parsers or compilers can determine what to do with it.value
{string|undefined} (optional) - In general, value should only be a string whennode.nodes
is undefined. This is not reinforced, but is considered good practice. Use a different property name to store arbitrary strings on the node whennode.nodes
is an array.nodes
{array|undefined} (optional) - array of child nodes
A number of useful methods and non-enumerable properties are also exposed for adding, finding and removing child nodes, etc.
Continue reading the API documentation for more details.
{%= apidocs("index.js") %}
node.isNode
{boolean} - this value is set totrue
when a node is created. This can be useful in situationas as a fast alternative to usinginstanceof Node
if you need to determine if a value is anode
object.node.size
{number} - the number of child nodes that have been pushed or unshifted ontonode.nodes
using the node's API. This is useful for determining if nodes were added tonode.nodes
without usingnode.push()
ornode.unshift()
(for example:if (node.nodes && node.size !== node.nodes.length)
)node.parent
{object} (instance of Node)
See the changelog.