tree-visitor

Visit nodes in the tree

Tree Visitor

Visit nodes in the abstract syntax tree.

Visit a single node:

var Visitor = require('tree-visitor');
 
var node = { type: 'number', value: 1 };
var visitor = new Visitor({
    numberfunction (number) {
        console.log(number.value);
    }
});
visitor.visit(node); // 1 

Visit an array of nodes:

var Visitor = require('tree-visitor');
 
var nodes = [
    { type: 'number', value: 1 },
    { type: 'string', value: 'abc', quote: '"' }
];
var visitor = new Visitor({
    numberfunction (number) {
        console.log(number.value);
    },
    stringfunction (string) {
        console.log(string.quote + string.value + string.quote);
    }
});
visitor.visit(nodes); // 1 "abc" 

Visit nested nodes:

var Visitor = require('tree-visitor');
 
var tree = {
        type: 'binaryExpression',
        operator: '+',
        left: { type: 'number', value: 1 },
        right: { type: 'string', value: 'abc' }
};
var visitor = new Visitor({
    binaryExpressionfunction (binaryExpression) {
        this.visit(binaryExpression.left);
        console.log(binaryExpression.operator);
        this.visit(binaryExpression.right);
    },
    numberfunction (number) {
        console.log(number.value);
    },
    stringfunction (string) {
        console.log(string.quote + string.value + string.quote);
    }
});
visitor.visit(tree); // 1 + "abc" 

One function to rule them all:

var Visitor = require('tree-visitor');
 
var nodes = [
    { type: 'number', value: 1 },
    { type: 'string', value: 'abc', quote: '"' }
];
var visitor = new Visitor({
    nodefunction (node) {
        console.log(node.value);
    }
});
visitor.visit(nodes); // 1 abc 
var visitor = new Visitor(actions);

actions is an object containing functions (each one is call an "action"). If the key and the value of the node's type property are equal, that node will be passed to the action. If no such key exists for an node, an action of key node will be called, if there is one. Otherwise, the node is silently ignored. Nodes don't have a type property are also ignored.

var nodes = [
    { type: 'number', value: 1 },
    { type: 'string', value: 'abc', quote: '"' }
    { type: 'boolean', value: true }
];
var visitor = new Visitor({
    numberfunction (number) {
        // number nodes are passed here 
    },
    nodefunction (string) {
        // string and boolean nodes are passed here 
        // number nodes are NOT passed here 
    }
});
visitor.visit(nodes);

visitor has a method visit(). You can pass a single node or an array of nodes, in the latter case, each node in the array will be visited sequentially.

this keyword in each action refers to the visitor object in the previous example. Note however, actions are not added to the visitor object, so visitor.number does not exist (nor does this.number in each action for that matter).

When visiting a single node, visit(node) returns the returned value of the corresponding action.

When visiting an array of nodes, the original array is returned.