From 9a8469179570b2713da8cd43bd934798660323a5 Mon Sep 17 00:00:00 2001 From: Yosuke Ota Date: Thu, 9 Nov 2023 08:31:25 +0900 Subject: [PATCH] refactor (#168) --- src/tokenizer/code-point-iterator.ts | 7 +++++++ src/tokenizer/tokenizer.ts | 19 +++++++------------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/tokenizer/code-point-iterator.ts b/src/tokenizer/code-point-iterator.ts index 9367522..0f0773e 100644 --- a/src/tokenizer/code-point-iterator.ts +++ b/src/tokenizer/code-point-iterator.ts @@ -63,6 +63,13 @@ export class CodePointIterator { return (this.lastCodePoint = cp); } + public currChar(): string { + const cp = this.lastCodePoint; + if (cp === CodePoint.LINE_FEED) return "\n"; + if (cp < 0x10000) return this.text[this.start.offset]; + return this.text.slice(this.start.offset, this.end.offset); + } + public *iterateSubCodePoints(): IterableIterator { let index = this.end.offset; while (true) { diff --git a/src/tokenizer/tokenizer.ts b/src/tokenizer/tokenizer.ts index 79f4dc5..c9d58f2 100644 --- a/src/tokenizer/tokenizer.ts +++ b/src/tokenizer/tokenizer.ts @@ -543,7 +543,7 @@ export class Tokenizer { } return this.reportParseError("invalid-char-in-escape-sequence"); } - out += this.currChar(cp); + out += this.currChar(); cp = this.nextCode(); } if (cp !== CodePoint.QUOTATION_MARK) { @@ -640,7 +640,7 @@ export class Tokenizer { } return this.reportParseError("invalid-char-in-escape-sequence"); } - out += this.currChar(cp); + out += this.currChar(); cp = this.nextCode(); } @@ -665,7 +665,7 @@ export class Tokenizer { if (isControlOtherThanTab(cp)) { return this.reportParseErrorControlChar(); } - out += this.currChar(cp); + out += this.currChar(); cp = this.nextCode(); } if (cp !== CodePoint.SINGLE_QUOTE) { @@ -706,7 +706,7 @@ export class Tokenizer { return "DATA"; } } - out += this.currChar(cp); + out += this.currChar(); cp = this.nextCode(); } return this.reportParseError("unterminated-string"); @@ -1208,7 +1208,7 @@ export class Tokenizer { return this.reportParseError("invalid-underscore"); } } else { - out += this.currChar(cp); + out += this.currChar(); } before = cp; cp = this.nextCode(); @@ -1249,13 +1249,8 @@ export class Tokenizer { return this.reportParseError("invalid-control-character"); } - private currChar(cp: number): string { - if (cp === CodePoint.LINE_FEED) return "\n"; - if (cp < 0x10000) return this.text[this.codePointIterator.start.offset]; - return this.text.slice( - this.codePointIterator.start.offset, - this.codePointIterator.end.offset, - ); + private currChar(): string { + return this.codePointIterator.currChar(); } }