Skip to content

Commit

Permalink
better typing for tokens (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
wcho21 authored Jan 24, 2024
1 parent d035838 commit 1e0b789
Show file tree
Hide file tree
Showing 17 changed files with 691 additions and 287 deletions.
56 changes: 29 additions & 27 deletions src/lexer/char-buffer/char-reader/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,7 @@ export default class CharReader {
this.col = 0;
}

private peekNewLine(): string | null {
// return if end of input
if (this.index === this.chars.length) {
return null;
}

// return if no new line
const char = this.chars[this.index];
if (char !== "\r" && char !== "\n") {
return null;
}

// return if the last character
if (this.index+1 === this.chars.length) {
return char;
}

// return if single-character new line
const nextChar = this.chars[this.index+1];
if (nextChar !== "\r" && nextChar !== "\n") {
return char;
}

// return two-character new line
return char + nextChar;
}

/** return character at current position */
readChar(): SourceChar {
// return fallback character if end of input
if (this.index === this.chars.length) {
Expand Down Expand Up @@ -85,6 +59,7 @@ export default class CharReader {
return sourceChar;
}

/** advance to next character */
advance(): void {
if (this.index === this.chars.length) {
return;
Expand All @@ -104,6 +79,33 @@ export default class CharReader {
++this.col;
}

private peekNewLine(): string | null {
// return if end of input
if (this.index === this.chars.length) {
return null;
}

// return if no new line
const char = this.chars[this.index];
if (char !== "\r" && char !== "\n") {
return null;
}

// return if the last character
if (this.index+1 === this.chars.length) {
return char;
}

// return if single-character new line
const nextChar = this.chars[this.index+1];
if (nextChar !== "\r" && nextChar !== "\n") {
return char;
}

// return two-character new line
return char + nextChar;
}

/** @deprecated Returns current character; if end of input, return fallback character */
read(): string {
if (this.index === this.chars.length) {
Expand Down
Loading

0 comments on commit 1e0b789

Please sign in to comment.