版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

95 строки
4.3 KiB

  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.throwUnexpectedNodeError = exports.throwInvalidNumberError = exports.throwUnexpectedSpaceError = exports.throwUnexpectedCommentError = exports.throwUnexpectedTokenError = exports.throwUnexpectedError = exports.throwExpectedTokenError = exports.ParseError = void 0;
  4. const utils_1 = require("./utils");
  5. class ParseError extends SyntaxError {
  6. constructor(message, offset, line, column) {
  7. super(message);
  8. this.index = offset;
  9. this.lineNumber = line;
  10. this.column = column;
  11. }
  12. }
  13. exports.ParseError = ParseError;
  14. function throwExpectedTokenError(name, beforeToken) {
  15. const locs = getLocation(beforeToken);
  16. const err = new ParseError(`Expected token '${name}'.`, locs.end, locs.loc.end.line, locs.loc.end.column + 1);
  17. throw err;
  18. }
  19. exports.throwExpectedTokenError = throwExpectedTokenError;
  20. function throwUnexpectedError(name, token) {
  21. const locs = getLocation(token);
  22. const err = new ParseError(`Unexpected ${name}.`, locs.start, locs.loc.start.line, locs.loc.start.column + 1);
  23. throw err;
  24. }
  25. exports.throwUnexpectedError = throwUnexpectedError;
  26. function throwUnexpectedTokenError(name, token) {
  27. return throwUnexpectedError(`token '${name}'`, token);
  28. }
  29. exports.throwUnexpectedTokenError = throwUnexpectedTokenError;
  30. function throwUnexpectedCommentError(token) {
  31. return throwUnexpectedError("comment", token);
  32. }
  33. exports.throwUnexpectedCommentError = throwUnexpectedCommentError;
  34. function throwUnexpectedSpaceError(beforeToken) {
  35. const locs = getLocation(beforeToken);
  36. const err = new ParseError("Unexpected whitespace.", locs.end, locs.loc.end.line, locs.loc.end.column + 1);
  37. throw err;
  38. }
  39. exports.throwUnexpectedSpaceError = throwUnexpectedSpaceError;
  40. function throwInvalidNumberError(text, token) {
  41. const locs = getLocation(token);
  42. const err = new ParseError(`Invalid number ${text}.`, locs.start, locs.loc.start.line, locs.loc.start.column + 1);
  43. throw err;
  44. }
  45. exports.throwInvalidNumberError = throwInvalidNumberError;
  46. function throwUnexpectedNodeError(node, tokens, offset) {
  47. if (node.type === "Identifier" || node.type === "JSONIdentifier") {
  48. const locs = getLocation(node);
  49. const err = new ParseError(`Unexpected identifier '${node.name}'.`, locs.start, locs.loc.start.line, locs.loc.start.column + 1);
  50. throw err;
  51. }
  52. if (node.type === "Literal" || node.type === "JSONLiteral") {
  53. const type = node.bigint
  54. ? "bigint"
  55. : (0, utils_1.isRegExpLiteral)(node)
  56. ? "regex"
  57. : node.value === null
  58. ? "null"
  59. : typeof node.value;
  60. const locs = getLocation(node);
  61. const err = new ParseError(`Unexpected ${type} literal.`, locs.start, locs.loc.start.line, locs.loc.start.column + 1);
  62. throw err;
  63. }
  64. if (node.type === "TemplateLiteral" ||
  65. node.type === "JSONTemplateLiteral") {
  66. const locs = getLocation(node);
  67. const err = new ParseError("Unexpected template literal.", locs.start, locs.loc.start.line, locs.loc.start.column + 1);
  68. throw err;
  69. }
  70. if (node.type.endsWith("Expression") &&
  71. node.type !== "FunctionExpression") {
  72. const name = node.type
  73. .replace(/^JSON/u, "")
  74. .replace(/\B([A-Z])/gu, " $1")
  75. .toLowerCase();
  76. const locs = getLocation(node);
  77. const err = new ParseError(`Unexpected ${name}.`, locs.start, locs.loc.start.line, locs.loc.start.column + 1);
  78. throw err;
  79. }
  80. const index = node.range[0] + (offset || 0);
  81. const t = tokens.findTokenByOffset(index);
  82. const name = (t === null || t === void 0 ? void 0 : t.value) || "unknown";
  83. const locs = getLocation(t || node);
  84. const err = new ParseError(`Unexpected token '${name}'.`, locs.start, locs.loc.start.line, locs.loc.start.column + 1);
  85. throw err;
  86. }
  87. exports.throwUnexpectedNodeError = throwUnexpectedNodeError;
  88. function getLocation(token) {
  89. var _a, _b, _c, _d;
  90. const start = (_b = (_a = token.range) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : token.start;
  91. const end = (_d = (_c = token.range) === null || _c === void 0 ? void 0 : _c[1]) !== null && _d !== void 0 ? _d : token.end;
  92. const loc = token.loc;
  93. return { start, end, loc };
  94. }