parseJson.js 5.1 KB

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