How to make antlr rewriteTokenstream in javascript
I am trying to use antlr to generate JavaScript code. and use it to change markers (nodes) in my grammar.
I can visit the nodes, enter a node and print the value, or exit the node and print the value of the node. I want to replace the node token if it is an expression.
as if expression was + 1, i want to replace expression with replaceString + 1
/**
* @author:saurabh
* sample programme to run the parser for a sample string using antlr
*/
/**
* dependency injection
*/
var antlr4 = require('antlr4/index');
var helloListener = require('./helloListener').helloListener;
var helloLexer = require('./helloLexer');
var helloParser = require('./helloParser');
var helloVisitor = require('./helloVisitor').helloVisitor;
var toRun = function (ruleForEnterAndExitOverride,ruleForgeneratiingTree, inputExpressionString, changeTokenString) {
var chars = new antlr4.InputStream(inputExpressionString);
var lexer = new helloLexer.helloLexer(chars);
var tokens = new antlr4.CommonTokenStream(lexer);
var parser = new helloParser.helloParser(tokens);
console.log(tokens)
//var visitor = new helloVisitor.helloVisitor();
parser.buildParseTrees = true;
var tree = parser.expression();
var keyPrinter = function () {
helloListener.call(this);
return this;
};
keyPrinter.prototype = Object.create(helloListener.prototype);
keyPrinter.prototype.constructor = keyPrinter;
//visitor
var keyVisitor = function(){
helloVisitor.call(this);
return this;
}
keyVisitor.prototype = Object.create(helloVisitor.prototype);
keyVisitor.prototype.constructor = keyVisitor;
//var tree1 = parser.prog();
//override funciton
keyPrinter.prototype["enter" + ruleForEnterAndExitOverride] = function (ctx) {
//var newVal = ctx.getText().replace(ctx.getText(),changeTokenString);
console.log("enter"+ctx.getText());
console.log("enter",ctx.start,ctx.end)
//console.log(ctx.quotedIdentifier())
return "hi";
}
keyPrinter.prototype["exit"+ ruleForEnterAndExitOverride] = function (ctx) {
//var newVal = ctx.getText().replace(ctx.getText(),changeTokenString);
console.log("exit"+ctx.getText());
}
keyVisitor.prototype["visit"+ruleForEnterAndExitOverride] = function(ctx){
console.log("visit"+ctx.getText())
//var newString = ctx.getText().replace(ctx.getText(),changeTokenString);
//return newString;
//console.log(ctx)
}
var visitor = new keyVisitor();
var test = visitor["visit"+ruleForEnterAndExitOverride](tree);
var printer = new keyPrinter();
antlr4.tree.ParseTreeWalker.DEFAULT.walk(printer, tree);
//console.log("out",test)
}
/**
* @param: ruleFor appending to the listner funciton (starts with capital character of rule name)
* @param: rule name to generate tree. without any change
* @param: expression to test
* @param: new value to be replaced from the @param3 expression
*/
toRun("QuotedIdentifier","quotedIdentifier", "`a`+1", "p");
in the antlr java doc I mentioned use rewritabletokenstreem, but I can't seem to find a way to do the same in javascript.
Please, help
+3
source to share
No one has answered this question yet
Check out similar questions: