parseJson.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.parseJsonString = exports.parseJsonNumber = exports.parseJson = void 0;
  4. const rxParseJson = /position\s(\d+)(?: \(line \d+ column \d+\))?$/;
  5. function parseJson(s, pos) {
  6. let endPos;
  7. parseJson.message = undefined;
  8. let matches;
  9. if (pos)
  10. s = s.slice(pos);
  11. try {
  12. parseJson.position = pos + s.length;
  13. return JSON.parse(s);
  14. }
  15. catch (e) {
  16. matches = rxParseJson.exec(e.message);
  17. if (!matches) {
  18. parseJson.message = "unexpected end";
  19. return undefined;
  20. }
  21. endPos = +matches[1];
  22. const c = s[endPos];
  23. s = s.slice(0, endPos);
  24. parseJson.position = pos + endPos;
  25. try {
  26. return JSON.parse(s);
  27. }
  28. catch (e1) {
  29. parseJson.message = `unexpected token ${c}`;
  30. return undefined;
  31. }
  32. }
  33. }
  34. exports.parseJson = parseJson;
  35. parseJson.message = undefined;
  36. parseJson.position = 0;
  37. parseJson.code = 'require("ajv/dist/runtime/parseJson").parseJson';
  38. function parseJsonNumber(s, pos, maxDigits) {
  39. let numStr = "";
  40. let c;
  41. parseJsonNumber.message = undefined;
  42. if (s[pos] === "-") {
  43. numStr += "-";
  44. pos++;
  45. }
  46. if (s[pos] === "0") {
  47. numStr += "0";
  48. pos++;
  49. }
  50. else {
  51. if (!parseDigits(maxDigits)) {
  52. errorMessage();
  53. return undefined;
  54. }
  55. }
  56. if (maxDigits) {
  57. parseJsonNumber.position = pos;
  58. return +numStr;
  59. }
  60. if (s[pos] === ".") {
  61. numStr += ".";
  62. pos++;
  63. if (!parseDigits()) {
  64. errorMessage();
  65. return undefined;
  66. }
  67. }
  68. if (((c = s[pos]), c === "e" || c === "E")) {
  69. numStr += "e";
  70. pos++;
  71. if (((c = s[pos]), c === "+" || c === "-")) {
  72. numStr += c;
  73. pos++;
  74. }
  75. if (!parseDigits()) {
  76. errorMessage();
  77. return undefined;
  78. }
  79. }
  80. parseJsonNumber.position = pos;
  81. return +numStr;
  82. function parseDigits(maxLen) {
  83. let digit = false;
  84. while (((c = s[pos]), c >= "0" && c <= "9" && (maxLen === undefined || maxLen-- > 0))) {
  85. digit = true;
  86. numStr += c;
  87. pos++;
  88. }
  89. return digit;
  90. }
  91. function errorMessage() {
  92. parseJsonNumber.position = pos;
  93. parseJsonNumber.message = pos < s.length ? `unexpected token ${s[pos]}` : "unexpected end";
  94. }
  95. }
  96. exports.parseJsonNumber = parseJsonNumber;
  97. parseJsonNumber.message = undefined;
  98. parseJsonNumber.position = 0;
  99. parseJsonNumber.code = 'require("ajv/dist/runtime/parseJson").parseJsonNumber';
  100. const escapedChars = {
  101. b: "\b",
  102. f: "\f",
  103. n: "\n",
  104. r: "\r",
  105. t: "\t",
  106. '"': '"',
  107. "/": "/",
  108. "\\": "\\",
  109. };
  110. const CODE_A = "a".charCodeAt(0);
  111. const CODE_0 = "0".charCodeAt(0);
  112. function parseJsonString(s, pos) {
  113. let str = "";
  114. let c;
  115. parseJsonString.message = undefined;
  116. // eslint-disable-next-line no-constant-condition, @typescript-eslint/no-unnecessary-condition
  117. while (true) {
  118. c = s[pos++];
  119. if (c === '"')
  120. break;
  121. if (c === "\\") {
  122. c = s[pos];
  123. if (c in escapedChars) {
  124. str += escapedChars[c];
  125. pos++;
  126. }
  127. else if (c === "u") {
  128. pos++;
  129. let count = 4;
  130. let code = 0;
  131. while (count--) {
  132. code <<= 4;
  133. c = s[pos];
  134. // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
  135. if (c === undefined) {
  136. errorMessage("unexpected end");
  137. return undefined;
  138. }
  139. c = c.toLowerCase();
  140. if (c >= "a" && c <= "f") {
  141. code += c.charCodeAt(0) - CODE_A + 10;
  142. }
  143. else if (c >= "0" && c <= "9") {
  144. code += c.charCodeAt(0) - CODE_0;
  145. }
  146. else {
  147. errorMessage(`unexpected token ${c}`);
  148. return undefined;
  149. }
  150. pos++;
  151. }
  152. str += String.fromCharCode(code);
  153. }
  154. else {
  155. errorMessage(`unexpected token ${c}`);
  156. return undefined;
  157. }
  158. // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
  159. }
  160. else if (c === undefined) {
  161. errorMessage("unexpected end");
  162. return undefined;
  163. }
  164. else {
  165. if (c.charCodeAt(0) >= 0x20) {
  166. str += c;
  167. }
  168. else {
  169. errorMessage(`unexpected token ${c}`);
  170. return undefined;
  171. }
  172. }
  173. }
  174. parseJsonString.position = pos;
  175. return str;
  176. function errorMessage(msg) {
  177. parseJsonString.position = pos;
  178. parseJsonString.message = msg;
  179. }
  180. }
  181. exports.parseJsonString = parseJsonString;
  182. parseJsonString.message = undefined;
  183. parseJsonString.position = 0;
  184. parseJsonString.code = 'require("ajv/dist/runtime/parseJson").parseJsonString';
  185. //# sourceMappingURL=parseJson.js.map