@bitloops/bl-transpiler
Version:
The one and only Bitloops Language transpiler
114 lines • 4.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BitloopsParser = void 0;
const tslib_1 = require("tslib");
const antlr4_1 = tslib_1.__importDefault(require("antlr4"));
const BitloopsLexer_js_1 = tslib_1.__importDefault(require("./grammar/BitloopsLexer.js"));
const BitloopsParser_js_1 = tslib_1.__importDefault(require("./grammar/BitloopsParser.js"));
const index_js_1 = require("./guards/index.js");
const VerboseErrorListener_js_1 = require("./VerboseErrorListener.js");
class BitloopsParser {
parse(inputData) {
const errors = [];
const parseResult = { core: {} };
const ASTCore = this.parseCore(inputData.core);
if ((0, index_js_1.isParserErrors)(ASTCore)) {
errors.push(...ASTCore);
}
else {
parseResult.core = ASTCore;
}
if (inputData.setup) {
const ASTSetup = this.parseSetup(inputData.setup);
if ((0, index_js_1.isParserErrors)(ASTSetup)) {
errors.push(...ASTSetup);
}
else {
parseResult.setup = ASTSetup;
}
}
if (errors.length > 0) {
return errors;
}
return parseResult;
}
parseCore(inputData) {
/**
* For each file, we need to create its tree.
*/
const boundedContexts = {};
const errors = [];
for (const data of inputData) {
const { boundedContext, module, fileId, fileContents } = data;
const ASTContextOrError = BitloopsParser.getInitialAST(fileContents, fileId);
if ((0, index_js_1.isParserErrors)(ASTContextOrError)) {
errors.push(...ASTContextOrError);
}
else {
const ASTContext = ASTContextOrError;
if (boundedContexts[boundedContext] === null ||
boundedContexts[boundedContext] === undefined) {
// first time we come across this boundedContext
boundedContexts[boundedContext] = {
[module]: {
[fileId]: { ASTContext },
},
};
}
else if (boundedContexts[boundedContext][module] === undefined) {
// first time we come across this module
boundedContexts[boundedContext][module] = {
[fileId]: { ASTContext },
};
}
else {
boundedContexts[boundedContext][module][fileId] = { ASTContext };
}
}
}
if (errors.length > 0) {
return errors;
}
return boundedContexts;
}
parseSetup(setupData) {
const setupContext = {};
const errors = [];
for (const data of setupData) {
const { fileContents, fileId } = data;
const ASTContextOrError = BitloopsParser.getInitialAST(fileContents, fileId);
if ((0, index_js_1.isParserErrors)(ASTContextOrError)) {
errors.push(...ASTContextOrError);
}
else {
const ASTContext = ASTContextOrError;
setupContext[fileId] = { ASTContext };
}
}
if (errors.length > 0) {
return errors;
}
return setupContext;
}
/**
* Initial AST.
* @param blCode string
* @returns Parser.ProgramContext
*/
static getInitialAST(blCode, fileId) {
const chars = new antlr4_1.default.InputStream(blCode);
const lexer = new BitloopsLexer_js_1.default(chars);
const tokens = new antlr4_1.default.CommonTokenStream(lexer);
const parser = new BitloopsParser_js_1.default(tokens);
parser.removeErrorListeners();
const errorListener = new VerboseErrorListener_js_1.VerboseErrorListener(fileId);
parser.addErrorListener(errorListener);
const tree = parser.program();
if (errorListener.hasErrors()) {
return errorListener.getErrors();
}
return tree;
}
}
exports.BitloopsParser = BitloopsParser;
//# sourceMappingURL=index.js.map