Emit Listener - dickensas/smartlexer GitHub Wiki
Create a JavaScript file with below code
var myarray = [];
exports.end = function(flow, eobj) {
console.log("end callback")
return eobj;
}
exports.start = function(flow, eobj) {
console.log("start callback")
return eobj;
}
exports.listener = function(flow, eobj, prev, parent) {
if(eobj.type == "SequenceExpression"){
//do something with SequenceExpression
}else if(eobj.type == "Identifier"){
//do something with Identifier
}else if(eobj.type == "MemberExpression"){
//do something with MemberExpression
}else if(eobj.type == "AssignmentExpression"){
//do something with AssignmentExpression
}
return eobj;
};
While calling the command line pass this file as argument
node smartlexer.js <source file> <emit listener js file>
Example command line
node smartlexer.js myfile.c EmitListener.js
eobj
This object is your corrent walking object for each ExpressionStatement
prev
This object is the previous ExpressionStatement
parent
This object is the parent object of the current object
flow
The flow parser instance, if you want to parse your own piece of code and attach to the AST
exports.end
Will be invoked once the traversing is completed
exports.start
Will be invoked once before the traversing starts
exports.listener
Will be invoked for each ExpressionStatement instance of the AST
Modifying the AST
return eobj;
You can return any object in the format of standard EsTree Object as per ESPrima documentation
Example ESTree Object
{
"type" : "Identifier",
"loc" : {
"source" : null,
"start" : {
"line" : 1,
"column" : 1
},
"end" : {
"line" : 1,
"column" : 8
}
},
"range" : [ 1, 8 ],
"name" : "include",
"typeAnnotation" : null,
"optional" : false
}