UNPKG

5.28 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.parseJsonString = exports.parseJsonNumber = exports.parseJson = void 0;
4const rxParseJson = /position\s(\d+)$/;
5function 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}
34exports.parseJson = parseJson;
35parseJson.message = undefined;
36parseJson.position = 0;
37parseJson.code = 'require("ajv/dist/runtime/parseJson").parseJson';
38function 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}
96exports.parseJsonNumber = parseJsonNumber;
97parseJsonNumber.message = undefined;
98parseJsonNumber.position = 0;
99parseJsonNumber.code = 'require("ajv/dist/runtime/parseJson").parseJsonNumber';
100const escapedChars = {
101 b: "\b",
102 f: "\f",
103 n: "\n",
104 r: "\r",
105 t: "\t",
106 '"': '"',
107 "/": "/",
108 "\\": "\\",
109};
110const CODE_A = "a".charCodeAt(0);
111const CODE_0 = "0".charCodeAt(0);
112function 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 if (c === undefined) {
135 errorMessage("unexpected end");
136 return undefined;
137 }
138 c = c.toLowerCase();
139 if (c >= "a" && c <= "f") {
140 code += c.charCodeAt(0) - CODE_A + 10;
141 }
142 else if (c >= "0" && c <= "9") {
143 code += c.charCodeAt(0) - CODE_0;
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 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
158 }
159 else if (c === undefined) {
160 errorMessage("unexpected end");
161 return undefined;
162 }
163 else {
164 if (c.charCodeAt(0) >= 0x20) {
165 str += c;
166 }
167 else {
168 errorMessage(`unexpected token ${c}`);
169 return undefined;
170 }
171 }
172 }
173 parseJsonString.position = pos;
174 return str;
175 function errorMessage(msg) {
176 parseJsonString.position = pos;
177 parseJsonString.message = msg;
178 }
179}
180exports.parseJsonString = parseJsonString;
181parseJsonString.message = undefined;
182parseJsonString.position = 0;
183parseJsonString.code = 'require("ajv/dist/runtime/parseJson").parseJsonString';
184//# sourceMappingURL=parseJson.js.map
\No newline at end of file