cordbuilder
Version:
Simple dsl to create discord builders simple.
59 lines (58 loc) • 1.86 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const lexer_1 = require("../constants/lexer");
const ast_1 = require("../types/ast");
const Token_1 = __importDefault(require("../structs/Token"));
class Lexer {
constructor(code) {
this.failed = false;
this.cursor = 0;
this.line = 1;
this.col = 0;
this.code = code.trim();
}
;
lex() {
const tokens = [];
while (this.hasMoreTokens()) {
const token = this.getNextToken();
tokens.push(token);
}
tokens.push(Token_1.default.eof(this.line, this.col + 1));
return tokens;
}
getNextToken() {
const currentCode = this.code.slice(this.cursor);
let token;
for (const [regex, kind] of lexer_1.LEXER_SPEC) {
const lexeme = regex.exec(currentCode)?.[0];
if (lexeme) {
this.cursor += lexeme.length;
this.col += lexeme.length;
if (kind == ast_1.TokenKind.Skippable) {
if (lexeme !== ' ')
this.breakLine();
return this.getNextToken();
}
token = new Token_1.default(kind, lexeme, this.line, this.col);
break;
}
}
if (!token) {
this.failed = true;
throw new Error(`lexer (l:${this.line}, c:${this.col}): Invalid token found during lexing -> ${currentCode[0]}`);
}
return token;
}
breakLine() {
this.line++;
this.col = 0;
}
hasMoreTokens() {
return this.cursor != this.code.length;
}
}
exports.default = Lexer;