Skip to content

Commit 54065be

Browse files
committed
Fix commenting tokenization
1 parent dc92cd0 commit 54065be

File tree

3 files changed

+20
-29
lines changed

3 files changed

+20
-29
lines changed

src/parser/parser.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,7 @@ export class Parser {
2121
}
2222

2323
public parse() {
24-
this.tokenStream = new TokenStream(
25-
this.tokenizer.tokenize().filter((t) => {
26-
return (
27-
t.type !== TokenType.LineComment && t.type !== TokenType.BlockComment
28-
);
29-
})
30-
);
24+
this.tokenStream = new TokenStream(this.tokenizer.tokenize());
3125
const obj = this.parseValue();
3226
this.eat(TokenType.EOF);
3327
this.lateReferences.forEach((ref) => {

src/tokenizer/tokenizer.ts

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class Tokenizer {
6666
tokens.push(this.tokenizeNull());
6767
break;
6868
case char === "/":
69-
tokens.push(this.tokenizeComment());
69+
this.tokenizeComment();
7070
break;
7171
default:
7272
this.throwUnexpectedCharacter(char);
@@ -254,20 +254,16 @@ export class Tokenizer {
254254
return this.makeToken(TokenType.ObjectReference, name);
255255
}
256256

257-
private tokenizeComment(): Token {
257+
private tokenizeComment() {
258258
this.next();
259-
const nextChar = this.next();
259+
const nextChar = this.input.peek();
260260
if (nextChar === "/") {
261-
return this.makeToken(
262-
TokenType.LineComment,
263-
"//" + this.readSingleLineComment()
264-
);
261+
this.readSingleLineComment();
262+
return;
265263
}
266264
if (nextChar === "*") {
267-
return this.makeToken(
268-
TokenType.BlockComment,
269-
"/*" + this.readMultiLineComment()
270-
);
265+
this.readMultiLineComment();
266+
return;
271267
}
272268
this.throwUnexpectedCharacter(nextChar);
273269
}
@@ -312,32 +308,31 @@ export class Tokenizer {
312308
}
313309

314310
private readSingleLineComment() {
315-
let value = "";
316311
while (!this.input.eof()) {
317312
const char = this.next();
318313
if (Characters.isLineBreak(char)) {
319-
break;
314+
this.nextLine();
315+
return;
320316
}
321-
value += char;
322317
}
323-
324-
return value;
325318
}
326319

327320
private readMultiLineComment() {
328-
let value = "";
321+
this.next();
329322
while (!this.input.eof()) {
330323
const char = this.next();
324+
if (Characters.isLineBreak(char)) {
325+
this.nextLine();
326+
continue;
327+
}
331328
if (char === "*") {
332329
const nextChar = this.input.peek();
333330
if (nextChar === "/") {
334331
this.next();
335-
break;
332+
return;
336333
}
337334
}
338-
value += char;
339335
}
340-
return value;
341336
}
342337

343338
private makeToken(type: TokenType, value: string): Token {

tests/simple.rson

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@
1313
$namedObject, // This is a reference to the named object
1414
5,
1515
],
16-
true,
16+
true, // This is a reference to the named object
1717
false,
1818
null
1919
/**
2020
* This is a comment
2121
*/
22+
/**/
2223
],
2324
"object": {
2425
"key": "value"
2526
}(namedObject),
2627
"objectRef": $namedObject,
2728
"objectRef2": $ref1,
28-
}
29+
}
30+
//

0 commit comments

Comments
 (0)