From 7a7480911c8ea67a35cc23ed027711af95fe3ad4 Mon Sep 17 00:00:00 2001 From: Krzysztof Kaczor Date: Thu, 17 Sep 2015 21:21:49 +0200 Subject: [PATCH 1/4] added proper visit function in ParseTreeVisitor --- runtime/JavaScript/src/antlr4/Utils.js | 7 +++++++ runtime/JavaScript/src/antlr4/tree/Tree.js | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/runtime/JavaScript/src/antlr4/Utils.js b/runtime/JavaScript/src/antlr4/Utils.js index f06760c3c..b47e75872 100644 --- a/runtime/JavaScript/src/antlr4/Utils.js +++ b/runtime/JavaScript/src/antlr4/Utils.js @@ -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; diff --git a/runtime/JavaScript/src/antlr4/tree/Tree.js b/runtime/JavaScript/src/antlr4/tree/Tree.js index 7a8aed68a..9295d3034 100644 --- a/runtime/JavaScript/src/antlr4/tree/Tree.js +++ b/runtime/JavaScript/src/antlr4/tree/Tree.js @@ -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; } From b2d771e01c9d07dee641c9ef79c1d9d018b64a1d Mon Sep 17 00:00:00 2001 From: Krzysztof Kaczor Date: Thu, 17 Sep 2015 21:25:37 +0200 Subject: [PATCH 2/4] fixed bug that prevented run on nodejs --- runtime/JavaScript/src/antlr4/FileStream.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/JavaScript/src/antlr4/FileStream.js b/runtime/JavaScript/src/antlr4/FileStream.js index 8359c0333..7b916d912 100644 --- a/runtime/JavaScript/src/antlr4/FileStream.js +++ b/runtime/JavaScript/src/antlr4/FileStream.js @@ -34,7 +34,7 @@ // when you construct the object. // var InputStream = require('./InputStream').InputStream; -var isNodeJs = typeof window === 'undefined' && importScripts === 'undefined'; +var isNodeJs = typeof window === 'undefined' && typeof(importScripts) === 'undefined'; var fs = isNodeJs ? require("fs") : null; function FileStream(fileName) { From 55fce54f4e3e9b41bee1d89546dba7e6db3377dd Mon Sep 17 00:00:00 2001 From: Krzysztof Kaczor Date: Fri, 18 Sep 2015 17:29:55 +0200 Subject: [PATCH 3/4] reverted fix --- runtime/JavaScript/src/antlr4/FileStream.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/JavaScript/src/antlr4/FileStream.js b/runtime/JavaScript/src/antlr4/FileStream.js index 7b916d912..8359c0333 100644 --- a/runtime/JavaScript/src/antlr4/FileStream.js +++ b/runtime/JavaScript/src/antlr4/FileStream.js @@ -34,7 +34,7 @@ // when you construct the object. // var InputStream = require('./InputStream').InputStream; -var isNodeJs = typeof window === 'undefined' && typeof(importScripts) === 'undefined'; +var isNodeJs = typeof window === 'undefined' && importScripts === 'undefined'; var fs = isNodeJs ? require("fs") : null; function FileStream(fileName) { From e2554805de42075e5e0f78a173b3276829e1522f Mon Sep 17 00:00:00 2001 From: Krzysztof Kaczor Date: Fri, 18 Sep 2015 17:30:11 +0200 Subject: [PATCH 4/4] added myself as contributor --- contributors.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/contributors.txt b/contributors.txt index 75e3b8b92..ccc915883 100644 --- a/contributors.txt +++ b/contributors.txt @@ -74,4 +74,5 @@ YYYY/MM/DD, github id, Full name, email 2015/05/12, Pursuit92, Josh Chase, jcjoshuachase@gmail.com 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 \ No newline at end of file +2015/06/29, jvanzyl, Jason van Zyl, jason@takari.io +2015/08/18, krzkaczor, Krzysztof Kaczor, krzysztof@kaczor.io