UNPKG

1.65 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6Object.defineProperty(exports, "Errors", {
7 enumerable: true,
8 get: function () {
9 return _errorMessage.ErrorMessages;
10 }
11});
12exports.default = void 0;
13
14var _location = require("../util/location");
15
16var _comments = _interopRequireDefault(require("./comments"));
17
18var _errorMessage = require("./error-message.js");
19
20function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
22class ParserError extends _comments.default {
23 getLocationForPosition(pos) {
24 let loc;
25 if (pos === this.state.start) loc = this.state.startLoc;else if (pos === this.state.lastTokStart) loc = this.state.lastTokStartLoc;else if (pos === this.state.end) loc = this.state.endLoc;else if (pos === this.state.lastTokEnd) loc = this.state.lastTokEndLoc;else loc = (0, _location.getLineInfo)(this.input, pos);
26 return loc;
27 }
28
29 raise(pos, errorTemplate, ...params) {
30 return this.raiseWithData(pos, undefined, errorTemplate, ...params);
31 }
32
33 raiseWithData(pos, data, errorTemplate, ...params) {
34 const loc = this.getLocationForPosition(pos);
35 const message = errorTemplate.replace(/%(\d+)/g, (_, i) => params[i]) + ` (${loc.line}:${loc.column})`;
36 return this._raise(Object.assign({
37 loc,
38 pos
39 }, data), message);
40 }
41
42 _raise(errorContext, message) {
43 const err = new SyntaxError(message);
44 Object.assign(err, errorContext);
45
46 if (this.options.errorRecovery) {
47 if (!this.isLookahead) this.state.errors.push(err);
48 return err;
49 } else {
50 throw err;
51 }
52 }
53
54}
55
56exports.default = ParserError;
\No newline at end of file