Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better typing for tokens #49

Merged
merged 2 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading