From e8366e0230cd47fc533b24fe8c21454f76524294 Mon Sep 17 00:00:00 2001 From: Mikkel Kongsted Date: Fri, 1 Nov 2024 12:21:40 +0100 Subject: [PATCH] some lexer stuff --- .vscode/settings.json | 3 +++ README.md | 2 +- deno.jsonc | 5 +++++ src/Lexer.ts | 40 ++++++++++++++++++++++++++++++++++++++++ src/Token.ts | 14 ++++++++++++++ 5 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 .vscode/settings.json create mode 100644 deno.jsonc create mode 100644 src/Lexer.ts create mode 100644 src/Token.ts diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..f7f606b --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "deno.enable": true +} diff --git a/README.md b/README.md index ff95c79..f9f9a1b 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -# Slige \ No newline at end of file +# Slige diff --git a/deno.jsonc b/deno.jsonc new file mode 100644 index 0000000..3fecf5f --- /dev/null +++ b/deno.jsonc @@ -0,0 +1,5 @@ +{ + "fmt": { + "indentWidth": 4 + } +} diff --git a/src/Lexer.ts b/src/Lexer.ts new file mode 100644 index 0000000..e69e070 --- /dev/null +++ b/src/Lexer.ts @@ -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 }; + } +} diff --git a/src/Token.ts b/src/Token.ts new file mode 100644 index 0000000..6cbc56e --- /dev/null +++ b/src/Token.ts @@ -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; +};