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

105 строки
4.0 KiB

  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getParser = void 0;
  4. const validate_1 = require("./validate");
  5. const acorn_1 = require("./modules/acorn");
  6. const errors_1 = require("./errors");
  7. const convert_1 = require("./convert");
  8. let parserCache;
  9. const PRIVATE = Symbol("ExtendParser#private");
  10. const PRIVATE_PROCESS_NODE = Symbol("ExtendParser#processNode");
  11. function getParser() {
  12. if (parserCache) {
  13. return parserCache;
  14. }
  15. parserCache = class ExtendParser extends (0, acorn_1.getAcorn)().Parser {
  16. constructor(options, code) {
  17. super((() => {
  18. const tokenConvertor = new convert_1.TokenConvertor(code);
  19. return {
  20. ecmaVersion: options.ecmaVersion,
  21. sourceType: options.sourceType,
  22. ranges: true,
  23. locations: true,
  24. allowReserved: true,
  25. onToken: (token) => {
  26. const t = tokenConvertor.convertToken(token);
  27. if (t) {
  28. this[PRIVATE].tokenStore.add(t);
  29. }
  30. },
  31. onComment: (block, text, start, end, startLoc, endLoc) => {
  32. const comment = {
  33. type: block ? "Block" : "Line",
  34. value: text,
  35. range: [start, end],
  36. loc: {
  37. start: startLoc,
  38. end: endLoc,
  39. },
  40. };
  41. if (!this[PRIVATE].ctx.comments) {
  42. throw (0, errors_1.throwUnexpectedCommentError)(comment);
  43. }
  44. this[PRIVATE].comments.push(comment);
  45. },
  46. };
  47. })(), code);
  48. this[PRIVATE] = {
  49. code,
  50. ctx: options.ctx,
  51. tokenStore: options.tokenStore,
  52. comments: options.comments,
  53. nodes: options.nodes,
  54. };
  55. }
  56. finishNode(...args) {
  57. const result = super.finishNode(...args);
  58. return this[PRIVATE_PROCESS_NODE](result);
  59. }
  60. finishNodeAt(...args) {
  61. const result = super.finishNodeAt(...args);
  62. return this[PRIVATE_PROCESS_NODE](result);
  63. }
  64. [PRIVATE_PROCESS_NODE](node) {
  65. const { tokenStore, ctx, nodes } = this[PRIVATE];
  66. (0, validate_1.validateNode)(node, tokenStore, ctx);
  67. nodes.push(node);
  68. return node;
  69. }
  70. raise(pos, message) {
  71. const loc = (0, acorn_1.getAcorn)().getLineInfo(this[PRIVATE].code, pos);
  72. const err = new errors_1.ParseError(message, pos, loc.line, loc.column + 1);
  73. throw err;
  74. }
  75. raiseRecoverable(pos, message) {
  76. this.raise(pos, message);
  77. }
  78. unexpected(pos) {
  79. if (pos != null) {
  80. this.raise(pos, "Unexpected token.");
  81. return;
  82. }
  83. const start = this.start;
  84. const end = this.end;
  85. const token = this[PRIVATE].code.slice(start, end);
  86. if (token) {
  87. const message = `Unexpected token '${token}'.`;
  88. this.raise(start, message);
  89. }
  90. else {
  91. if (!this[PRIVATE].nodes.length) {
  92. this.raise(0, "Expected to be an expression, but got empty.");
  93. }
  94. if (this[PRIVATE].tokenStore.tokens.length) {
  95. const last = this[PRIVATE].tokenStore.tokens[this[PRIVATE].tokenStore.tokens.length - 1];
  96. this.raise(last.range[0], `Unexpected token '${last.value}'.`);
  97. }
  98. this.raise(start, "Unexpected token.");
  99. }
  100. }
  101. };
  102. return parserCache;
  103. }
  104. exports.getParser = getParser;