Skip to content

Commit

Permalink
refactor (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi authored Nov 8, 2023
1 parent 45d909e commit 9a84691
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
7 changes: 7 additions & 0 deletions src/tokenizer/code-point-iterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number> {
let index = this.end.offset;
while (true) {
Expand Down
19 changes: 7 additions & 12 deletions src/tokenizer/tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}

Expand All @@ -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) {
Expand Down Expand Up @@ -706,7 +706,7 @@ export class Tokenizer {
return "DATA";
}
}
out += this.currChar(cp);
out += this.currChar();
cp = this.nextCode();
}
return this.reportParseError("unterminated-string");
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
}
}

Expand Down

0 comments on commit 9a84691

Please sign in to comment.