some lexer stuff

This commit is contained in:
Mikkel Kongsted 2024-11-01 12:21:40 +01:00
parent 25df78fd85
commit e8366e0230
5 changed files with 63 additions and 1 deletions

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"deno.enable": true
}

View File

@ -1 +1 @@
# Slige # Slige

5
deno.jsonc Normal file
View File

@ -0,0 +1,5 @@
{
"fmt": {
"indentWidth": 4
}
}

40
src/Lexer.ts Normal file
View File

@ -0,0 +1,40 @@
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 };
}
}

14
src/Token.ts Normal file
View File

@ -0,0 +1,14 @@
export type Token = {
type: string;
pos: Pos;
length: number;
identValue?: string;
intValue?: number;
stringValue?: string;
};
export type Pos = {
index: number;
line: number;
col: number;
};