UNPKG

@informalsystems/quint

Version:

Core tool for the Quint specification language

106 lines 4.37 kB
"use strict"; /* ---------------------------------------------------------------------------------- * Copyright 2022 Informal Systems * Licensed under the Apache License, Version 2.0. * See LICENSE in the project root for license information. * --------------------------------------------------------------------------------- */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseEffectOrThrow = exports.parseEffect = void 0; /** * Parsing for effects. To be used internally for the moment. * * @author Gabriela Moreira * * @module */ const antlr4ts_1 = require("antlr4ts"); const EffectLexer_1 = require("../generated/EffectLexer"); const p = __importStar(require("../generated/EffectParser")); const ParseTreeWalker_1 = require("antlr4ts/tree/ParseTreeWalker"); const ToEffectVisitor_1 = require("./ToEffectVisitor"); const either_1 = require("@sweet-monads/either"); /** * Parses an effect string into an Effect * * @param effectString the string to be parsed * * @returns the parsed effect when the string is a valid effect. * Otherwise, a list of parsing errors. */ function parseEffect(effectString) { const errorMessages = []; // error listener to report lexical and syntax errors const errorListener = { syntaxError: (_, offendingSymbol, line, charPositionInLine, msg) => { const len = offendingSymbol ? offendingSymbol.stopIndex - offendingSymbol.startIndex : 0; const index = offendingSymbol ? offendingSymbol.startIndex : 0; const start = { line: line - 1, col: charPositionInLine, index }; const end = { line: line - 1, col: charPositionInLine + len, index: index + len }; errorMessages.push({ explanation: msg, locs: [{ start, end }] }); }, }; // Create the lexer and parser const inputStream = antlr4ts_1.CharStreams.fromString(effectString); const lexer = new EffectLexer_1.EffectLexer(inputStream); // remove the console listener and add our listener lexer.removeErrorListeners(); lexer.addErrorListener(errorListener); const tokenStream = new antlr4ts_1.CommonTokenStream(lexer); const parser = new p.EffectParser(tokenStream); // remove the console listener and add our listener parser.removeErrorListeners(); parser.addErrorListener(errorListener); // run the parser const tree = parser.effect(); if (errorMessages.length > 0) { // report the errors return (0, either_1.left)(errorMessages); } else { // walk through the AST and construct the Effect const listener = new ToEffectVisitor_1.ToEffectVisitor(); ParseTreeWalker_1.ParseTreeWalker.DEFAULT.walk(listener, tree); if (listener.effect !== undefined) { return (0, either_1.right)(listener.effect); } else { throw new Error('this should be impossible: effect is undefined'); } } } exports.parseEffect = parseEffect; function parseEffectOrThrow(effect) { const result = parseEffect(effect); if (result.isRight()) { const { value } = result; return value; } else { throw new Error(`Could not parse generated effect: ${effect} `); } } exports.parseEffectOrThrow = parseEffectOrThrow; //# sourceMappingURL=parser.js.map