Skip to content

Commit 6fc1cf9

Browse files
committed
pw
1 parent a4fc3a0 commit 6fc1cf9

File tree

4 files changed

+26
-14
lines changed

4 files changed

+26
-14
lines changed

build/index.cjs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,19 @@ var assert = (condition, message = "Assertion failed") => {
4040
var assertEq = (a, b, message = "Assertion failed") => {
4141
if (a !== b) throw new Error(`${message}: '${a}' !== '${b}'`);
4242
};
43+
console.log("v2");
4344
var JsonParser = class {
4445
#queue;
4546
#text = "";
4647
#index = 0;
4748
#stream;
4849
constructor(queue) {
50+
console.log("v2");
4951
this.#queue = queue.pipe((r) => [...r]).flat();
50-
this.#stream = this.parseValue();
52+
this.#stream = (async () => {
53+
await this.#skipWhiteSpaces();
54+
return await this.parseValue();
55+
})();
5156
}
5257
#isWhitespace(char) {
5358
return char === " " || char === "\n" || char === " " || char === "\r";
@@ -77,8 +82,8 @@ var JsonParser = class {
7782
return chunk;
7883
}
7984
async #skipWhiteSpaces() {
80-
for (let char = await this.#peekNonEof(1, "skipWhiteSpaces"); this.#isWhitespace(char); char = await this.#peekNonEof(1, "skipWhiteSpaces")) {
81-
this.#nextNonEof();
85+
while (this.#isWhitespace(await this.#peekNonEof())) {
86+
await this.#nextNonEof();
8287
}
8388
}
8489
async #expectNext(expected) {
@@ -112,8 +117,7 @@ var JsonParser = class {
112117
};
113118
return result;
114119
}
115-
async parseValue(skip = true) {
116-
if (skip) await this.#skipWhiteSpaces();
120+
async parseValue() {
117121
const next = await this.#peekNonEof();
118122
switch (next) {
119123
case "{":
@@ -141,6 +145,7 @@ var JsonParser = class {
141145
case "9":
142146
return this.parseNumber();
143147
default:
148+
console.error(this.#text.slice(this.#index - 10));
144149
throw new Error(`Unexpected token ${next} at index ${this.#index} while parsing value in JSON`);
145150
}
146151
}
@@ -154,6 +159,7 @@ var JsonParser = class {
154159
await key.wait;
155160
await this.#skipWhiteSpaces();
156161
await this.#expectNext(":");
162+
await this.#skipWhiteSpaces();
157163
const val = await this.parseValue();
158164
update((data) => void (data[key.data] = val), true);
159165
await val.wait;
@@ -170,7 +176,7 @@ var JsonParser = class {
170176
do {
171177
await this.#skipWhiteSpaces();
172178
if (await this.#peekNonEof() === "]") break;
173-
const val = await this.parseValue(false);
179+
const val = await this.parseValue();
174180
update((data) => void data.push(val), true);
175181
await val.wait;
176182
await this.#skipWhiteSpaces();

build/index.d.cts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type JSONArrayStream = Array<JSONStreamResult<JSONStreamValue>>;
1212
declare class JsonParser<T> {
1313
#private;
1414
constructor(queue: Queue<string>);
15-
parseValue(skip?: boolean): Promise<JSONStreamResult<JSONObjectStream> | JSONStreamResult<JSONArrayStream> | JSONStreamResult<string> | JSONStreamResult<boolean> | JSONStreamResult<null> | JSONStreamResult<number>>;
15+
parseValue(): Promise<JSONStreamResult<JSONObjectStream> | JSONStreamResult<JSONArrayStream> | JSONStreamResult<string> | JSONStreamResult<boolean> | JSONStreamResult<null> | JSONStreamResult<number>>;
1616
parseObject(): JSONStreamResult<JSONObjectStream>;
1717
parseArray(): JSONStreamResult<JSONArrayStream>;
1818
parseNumber(): JSONStreamResult<number>;

build/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type JSONArrayStream = Array<JSONStreamResult<JSONStreamValue>>;
1212
declare class JsonParser<T> {
1313
#private;
1414
constructor(queue: Queue<string>);
15-
parseValue(skip?: boolean): Promise<JSONStreamResult<JSONObjectStream> | JSONStreamResult<JSONArrayStream> | JSONStreamResult<string> | JSONStreamResult<boolean> | JSONStreamResult<null> | JSONStreamResult<number>>;
15+
parseValue(): Promise<JSONStreamResult<JSONObjectStream> | JSONStreamResult<JSONArrayStream> | JSONStreamResult<string> | JSONStreamResult<boolean> | JSONStreamResult<null> | JSONStreamResult<number>>;
1616
parseObject(): JSONStreamResult<JSONObjectStream>;
1717
parseArray(): JSONStreamResult<JSONArrayStream>;
1818
parseNumber(): JSONStreamResult<number>;

build/index.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,19 @@ var assert = (condition, message = "Assertion failed") => {
66
var assertEq = (a, b, message = "Assertion failed") => {
77
if (a !== b) throw new Error(`${message}: '${a}' !== '${b}'`);
88
};
9+
console.log("v2");
910
var JsonParser = class {
1011
#queue;
1112
#text = "";
1213
#index = 0;
1314
#stream;
1415
constructor(queue) {
16+
console.log("v2");
1517
this.#queue = queue.pipe((r) => [...r]).flat();
16-
this.#stream = this.parseValue();
18+
this.#stream = (async () => {
19+
await this.#skipWhiteSpaces();
20+
return await this.parseValue();
21+
})();
1722
}
1823
#isWhitespace(char) {
1924
return char === " " || char === "\n" || char === " " || char === "\r";
@@ -43,8 +48,8 @@ var JsonParser = class {
4348
return chunk;
4449
}
4550
async #skipWhiteSpaces() {
46-
for (let char = await this.#peekNonEof(1, "skipWhiteSpaces"); this.#isWhitespace(char); char = await this.#peekNonEof(1, "skipWhiteSpaces")) {
47-
this.#nextNonEof();
51+
while (this.#isWhitespace(await this.#peekNonEof())) {
52+
await this.#nextNonEof();
4853
}
4954
}
5055
async #expectNext(expected) {
@@ -78,8 +83,7 @@ var JsonParser = class {
7883
};
7984
return result;
8085
}
81-
async parseValue(skip = true) {
82-
if (skip) await this.#skipWhiteSpaces();
86+
async parseValue() {
8387
const next = await this.#peekNonEof();
8488
switch (next) {
8589
case "{":
@@ -107,6 +111,7 @@ var JsonParser = class {
107111
case "9":
108112
return this.parseNumber();
109113
default:
114+
console.error(this.#text.slice(this.#index - 10));
110115
throw new Error(`Unexpected token ${next} at index ${this.#index} while parsing value in JSON`);
111116
}
112117
}
@@ -120,6 +125,7 @@ var JsonParser = class {
120125
await key.wait;
121126
await this.#skipWhiteSpaces();
122127
await this.#expectNext(":");
128+
await this.#skipWhiteSpaces();
123129
const val = await this.parseValue();
124130
update((data) => void (data[key.data] = val), true);
125131
await val.wait;
@@ -136,7 +142,7 @@ var JsonParser = class {
136142
do {
137143
await this.#skipWhiteSpaces();
138144
if (await this.#peekNonEof() === "]") break;
139-
const val = await this.parseValue(false);
145+
const val = await this.parseValue();
140146
update((data) => void data.push(val), true);
141147
await val.wait;
142148
await this.#skipWhiteSpaces();

0 commit comments

Comments
 (0)