版博士V2.0程序
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

160 wiersze
5.1 KiB

  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.convertProgramNode = exports.TokenConvertor = void 0;
  4. const validate_1 = require("./validate");
  5. const errors_1 = require("./errors");
  6. const acorn_1 = require("./modules/acorn");
  7. class TokenConvertor {
  8. constructor(code) {
  9. this.templateBuffer = [];
  10. this.code = code;
  11. this.tokTypes = (0, acorn_1.getAcorn)().tokTypes;
  12. }
  13. convertToken(token) {
  14. const { tokTypes } = this;
  15. let type, value;
  16. const additional = {};
  17. if (token.type === tokTypes.string) {
  18. type = "String";
  19. value = this.code.slice(...token.range);
  20. }
  21. else if (token.type === tokTypes.num) {
  22. type = "Numeric";
  23. value = this.code.slice(...token.range);
  24. }
  25. else if (token.type.keyword) {
  26. if (token.type.keyword === "true" ||
  27. token.type.keyword === "false") {
  28. type = "Boolean";
  29. }
  30. else if (token.type.keyword === "null") {
  31. type = "Null";
  32. }
  33. else {
  34. type = "Keyword";
  35. }
  36. value = token.value;
  37. }
  38. else if (token.type === tokTypes.braceL ||
  39. token.type === tokTypes.braceR ||
  40. token.type === tokTypes.bracketL ||
  41. token.type === tokTypes.bracketR ||
  42. token.type === tokTypes.colon ||
  43. token.type === tokTypes.comma ||
  44. token.type === tokTypes.plusMin) {
  45. type = "Punctuator";
  46. value = this.code.slice(...token.range);
  47. }
  48. else if (token.type === tokTypes.name) {
  49. type = "Identifier";
  50. value = token.value;
  51. }
  52. else if (token.type === tokTypes.backQuote) {
  53. if (this.templateBuffer.length > 0) {
  54. const first = this.templateBuffer[0];
  55. this.templateBuffer.length = 0;
  56. return {
  57. type: "Template",
  58. value: this.code.slice(first.start, token.end),
  59. range: [first.start, token.end],
  60. loc: {
  61. start: first.loc.start,
  62. end: token.loc.end,
  63. },
  64. };
  65. }
  66. this.templateBuffer.push(token);
  67. return null;
  68. }
  69. else if (token.type === tokTypes.template) {
  70. if (this.templateBuffer.length === 0) {
  71. return (0, errors_1.throwUnexpectedTokenError)(this.code.slice(...token.range), token);
  72. }
  73. this.templateBuffer.push(token);
  74. return null;
  75. }
  76. else if (token.type === tokTypes.regexp) {
  77. const reValue = token.value;
  78. type = "RegularExpression";
  79. additional.regex = {
  80. flags: reValue.flags,
  81. pattern: reValue.pattern,
  82. };
  83. value = `/${reValue.pattern}/${reValue.flags}`;
  84. }
  85. else {
  86. return (0, errors_1.throwUnexpectedTokenError)(this.code.slice(...token.range), token);
  87. }
  88. ;
  89. token.type = type;
  90. token.value = value;
  91. for (const k in additional) {
  92. ;
  93. token[k] = additional[k];
  94. }
  95. return token;
  96. }
  97. }
  98. exports.TokenConvertor = TokenConvertor;
  99. function convertProgramNode(node, tokens, ctx, code) {
  100. if (node.type !== "JSONObjectExpression" &&
  101. node.type !== "JSONArrayExpression" &&
  102. node.type !== "JSONLiteral" &&
  103. node.type !== "JSONUnaryExpression" &&
  104. node.type !== "JSONIdentifier" &&
  105. node.type !== "JSONTemplateLiteral") {
  106. return (0, errors_1.throwUnexpectedNodeError)(node, tokens);
  107. }
  108. if (node.type === "JSONIdentifier") {
  109. if (!(0, validate_1.isStaticValueIdentifier)(node, ctx)) {
  110. return (0, errors_1.throwUnexpectedNodeError)(node, tokens);
  111. }
  112. }
  113. const body = Object.assign(Object.assign({ type: "JSONExpressionStatement", expression: node }, cloneLocation(node)), { parent: null });
  114. setParent(node, body);
  115. const end = code.length;
  116. const endLoc = (0, acorn_1.getAcorn)().getLineInfo(code, end);
  117. const nn = {
  118. type: "Program",
  119. body: [body],
  120. comments: [],
  121. tokens: [],
  122. range: [0, end],
  123. loc: {
  124. start: {
  125. line: 1,
  126. column: 0,
  127. },
  128. end: {
  129. line: endLoc.line,
  130. column: endLoc.column,
  131. },
  132. },
  133. parent: null,
  134. };
  135. setParent(body, nn);
  136. return nn;
  137. }
  138. exports.convertProgramNode = convertProgramNode;
  139. function cloneLocation(node) {
  140. const range = node.range;
  141. const loc = node.loc;
  142. return {
  143. range: [range[0], range[1]],
  144. loc: {
  145. start: {
  146. line: loc.start.line,
  147. column: loc.start.column,
  148. },
  149. end: {
  150. line: loc.end.line,
  151. column: loc.end.column,
  152. },
  153. },
  154. };
  155. }
  156. function setParent(prop, parent) {
  157. ;
  158. prop.parent = parent;
  159. }