@bitloops/bl-transpiler
Version:
The one and only Bitloops Language transpiler
74 lines • 3.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AntlerParser = void 0;
const tslib_1 = require("tslib");
/**
* Bitloops Language CLI
* Copyright (C) 2022 Bitloops S.A.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* For further information you can contact legal(at)bitloops.com.
*/
const antlr4_1 = tslib_1.__importDefault(require("antlr4"));
// import md5 from 'md5';
const BitloopsLexer_js_1 = tslib_1.__importDefault(require("./BitloopsLexer.js"));
const BitloopsParser_js_1 = tslib_1.__importDefault(require("./BitloopsParser.js"));
const BitloopsParserListener_js_1 = tslib_1.__importDefault(require("./BitloopsParserListener.js"));
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const getAllParentsPath = (parser, node, tree = []) => {
if (!node.parentCtx) {
return tree;
}
const nodeType = parser.ruleNames[node.ruleIndex];
// tree.push({type: nodeType, value: node.getText(), hash: md5(node), numOfChildren: node.children.length});
tree.push({ type: nodeType, value: node.getText(), numOfChildren: node.children.length });
return getAllParentsPath(parser, node.parentCtx, tree);
};
const getAllChildrensTree = (parser, node, tree = []) => {
const nodeType = parser.ruleNames[node.ruleIndex] ? parser.ruleNames[node.ruleIndex] : 'LEAF';
if (!node.children) {
// return {type: nodeType, value: node.getText(), hash: md5(node)};
return { type: nodeType, value: node.getText() };
}
else {
const children = node.children.map((child) => getAllChildrensTree(parser, child, tree));
// return {type: nodeType, value: node.getText(), hash: md5(node), numOfChildren: node.children.length, children};
return { type: nodeType, value: node.getText(), numOfChildren: node.children.length, children };
}
};
class AntlerParser extends BitloopsParserListener_js_1.default {
constructor(string) {
super();
const lexer = new BitloopsLexer_js_1.default(new antlr4_1.default.InputStream(string));
const tokenStream = new antlr4_1.default.CommonTokenStream(lexer);
this.parser = new BitloopsParser_js_1.default(tokenStream);
this.parser.buildParseTrees = true;
this._tree = this.parser.program();
}
get test() {
return this._test;
}
get tree() {
return this._tree;
}
get bitloopsTree() {
return this._bitloopsTree;
}
enterProgram(ctx) {
this._bitloopsTree = getAllChildrensTree(this.parser, ctx, []);
}
}
exports.AntlerParser = AntlerParser;
//# sourceMappingURL=AntlerParser.js.map