slige/src/Lexer.ts

41 lines
897 B
TypeScript
Raw Normal View History

2024-11-01 11:21:40 +00:00
import { Pos, Token } from "./Token.ts";
class Lexer {
private index = 0;
private line = 1;
private col = 1;
public constructor (private text: string) {}
public next(): Token | null { return null }
private done(): boolean { return this.index >= this.text.length; }
private current(): string { return this.text[this.index]; }
private step() {
if (this.done())
return;
if (this.current() === "\n") {
this.line += 1;
this.col = 1;
} else {
this.col += 1;
}
this.index += 1;
}
private pos(): Pos {
return {
index: this.index,
line: this.line,
col: this.col
}
}
private token(type: string, pos: Pos): Token {
const length = this.index - pos.index;
return { type, pos, length };
}
}