forked from jasder/antlr
Merge pull request #997 from krzkaczor/master
Added proper visit function in ParseTreeVisitor
This commit is contained in:
commit
dfe05b5f9f
|
@ -75,3 +75,4 @@ YYYY/MM/DD, github id, Full name, email
|
|||
2015/05/20, peturingi, Pétur Ingi Egilsson, petur@petur.eu
|
||||
2015/05/27, jcbrinfo, Jean-Christophe Beaupré, jcbrinfo@users.noreply.github.com
|
||||
2015/06/29, jvanzyl, Jason van Zyl, jason@takari.io
|
||||
2015/08/18, krzkaczor, Krzysztof Kaczor, krzysztof@kaczor.io
|
||||
|
|
|
@ -191,6 +191,13 @@ function escapeWhitespace(s, escapeSpaces) {
|
|||
return s;
|
||||
}
|
||||
|
||||
exports.isArray = function (entity) {
|
||||
return Object.prototype.toString.call( entity ) === '[object Array]'
|
||||
};
|
||||
|
||||
exports.titleCase = function(str) {
|
||||
return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1);});
|
||||
};
|
||||
|
||||
exports.Set = Set;
|
||||
exports.BitSet = BitSet;
|
||||
|
|
|
@ -35,6 +35,8 @@
|
|||
var Token = require('./../Token').Token;
|
||||
var Interval = require('./../IntervalSet').Interval;
|
||||
var INVALID_INTERVAL = new Interval(-1, -2);
|
||||
var Utils = require('../Utils.js');
|
||||
|
||||
|
||||
function Tree() {
|
||||
return this;
|
||||
|
@ -84,6 +86,26 @@ function ParseTreeVisitor() {
|
|||
return this;
|
||||
}
|
||||
|
||||
ParseTreeVisitor.prototype.visit = function(ctx) {
|
||||
if (Utils.isArray(ctx)) {
|
||||
var self = this;
|
||||
return ctx.map(function(child) { return visitAtom(self, child)});
|
||||
} else {
|
||||
return visitAtom(this, ctx);
|
||||
}
|
||||
};
|
||||
|
||||
var visitAtom = function(visitor, ctx) {
|
||||
if (ctx.parser === undefined) { //is terminal
|
||||
return;
|
||||
}
|
||||
|
||||
var name = ctx.parser.ruleNames[ctx.ruleIndex];
|
||||
var funcName = "visit" + Utils.titleCase(name);
|
||||
|
||||
return visitor[funcName](ctx);
|
||||
};
|
||||
|
||||
function ParseTreeListener() {
|
||||
return this;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue