JavaScript Visitor

223 views
Skip to first unread message

Bruno Vollmer

unread,
Feb 21, 2016, 4:59:56 AM2/21/16
to antlr-discussion

I'm currently trying to develop a JavaScript compiler with the help of an Antlr4 Visitor. I've got this already implemented with Java but cannot figure out how to do this in JavaScript. Probably somebody can answer me a few questions?

1: In Java there is a Visitor.visit function. If im right this isn't possible with JavaScript. Is there a work around for this?

2: My JavaScript Visitor got all the generated visiting functions but when I use console.log(ctx) the context is undefined. Any idea why?

Extract from the SimpleVisitor.js:

// Visit a parse tree produced by SimpleParser#parse.
SimpleVisitor.prototype.visitParse = function(ctx) {
        console.log(ctx);
};

Main js file:

var antlr4 = require('lib/antlr4/index');
var SimpleLexer = require('antlr4/SimpleLexer');
var SimpleParser = require('antlr4/SimpleParser');
var SimpleVisitor = require('antlr4/SimpleVisitor');    

var input = "double hallo = 1;";
var chars = new antlr4.InputStream(input);
var lexer = new SimpleLexer.SimpleLexer(chars);
var tokens = new antlr4.CommonTokenStream(lexer);
var parser = new SimpleParser.SimpleParser(tokens);
var visitor = new SimpleVisitor.SimpleVisitor();
parser.buildParseTrees = true;
var tree = parser.parse();

visitor.visitParse();

This is probably enough to start with ...

Bruno

Edit:

If I call my visitor with tree as parameter how to I call the next visitor? 

Eric Vergnaud

unread,
Feb 23, 2016, 6:27:51 PM2/23/16
to antlr-discussion
Mmm...
There is a visitor.visit function, which will actually populate the context when traversing the tree nodes.
(However there is a bug when using alternative labels, which some contributors have proposed to fix)
Reply all
Reply to author
Forward
0 new messages