mirror of
https://git.sfja.dk/Mikkel/slige.git
synced 2025-01-18 12:46:31 +00:00
some lexer stuff
This commit is contained in:
parent
25df78fd85
commit
e8366e0230
3
.vscode/settings.json
vendored
Normal file
3
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"deno.enable": true
|
||||
}
|
5
deno.jsonc
Normal file
5
deno.jsonc
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"fmt": {
|
||||
"indentWidth": 4
|
||||
}
|
||||
}
|
40
src/Lexer.ts
Normal file
40
src/Lexer.ts
Normal 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
14
src/Token.ts
Normal 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;
|
||||
};
|
Loading…
Reference in New Issue
Block a user