Merge pull request #997 from krzkaczor/master

Added proper visit function in ParseTreeVisitor
This commit is contained in:
Terence Parr 2015-09-18 12:28:43 -07:00
commit dfe05b5f9f
3 changed files with 31 additions and 1 deletions

View File

@ -74,4 +74,5 @@ YYYY/MM/DD, github id, Full name, email
2015/05/12, Pursuit92, Josh Chase, jcjoshuachase@gmail.com 2015/05/12, Pursuit92, Josh Chase, jcjoshuachase@gmail.com
2015/05/20, peturingi, Pétur Ingi Egilsson, petur@petur.eu 2015/05/20, peturingi, Pétur Ingi Egilsson, petur@petur.eu
2015/05/27, jcbrinfo, Jean-Christophe Beaupré, jcbrinfo@users.noreply.github.com 2015/05/27, jcbrinfo, Jean-Christophe Beaupré, jcbrinfo@users.noreply.github.com
2015/06/29, jvanzyl, Jason van Zyl, jason@takari.io 2015/06/29, jvanzyl, Jason van Zyl, jason@takari.io
2015/08/18, krzkaczor, Krzysztof Kaczor, krzysztof@kaczor.io

View File

@ -191,6 +191,13 @@ function escapeWhitespace(s, escapeSpaces) {
return s; 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.Set = Set;
exports.BitSet = BitSet; exports.BitSet = BitSet;

View File

@ -35,6 +35,8 @@
var Token = require('./../Token').Token; var Token = require('./../Token').Token;
var Interval = require('./../IntervalSet').Interval; var Interval = require('./../IntervalSet').Interval;
var INVALID_INTERVAL = new Interval(-1, -2); var INVALID_INTERVAL = new Interval(-1, -2);
var Utils = require('../Utils.js');
function Tree() { function Tree() {
return this; return this;
@ -84,6 +86,26 @@ function ParseTreeVisitor() {
return this; 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() { function ParseTreeListener() {
return this; return this;
} }