UNPKG

ts-fusion-parser

Version:

Parser for Neos Fusion Files

76 lines (75 loc) 2.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AbstractLexer = void 0; const eolError_1 = require("./errors/eolError"); class AbstractLexer { constructor(text) { this.cursor = -1; this.tagStack = []; this.text = text; this.cursor = 0; } getRemainingText(info = undefined) { if (this.isEOF()) { // console.log("tagStack", this.tagStack, info) throw new eolError_1.EOLError("Hit EOL but was not expecting it"); } return this.text.substring(this.cursor); } getSnippet(begin, end) { return this.text.substring(begin, end); } getCursor() { return this.cursor; } isEOF(debug = false) { if (debug) console.log(`${this.cursor} >= ${this.text.length}`, this.cursor >= this.text.length); return this.cursor > this.text.length - 1; } lookAhead(tokenType) { if (this.isEOF()) return false; const text = this.getRemainingText(tokenType.name); const token = new tokenType(); if (token.regex.test(text)) { this.lookAheadTokenType = tokenType; return true; } return false; } consumeLookAhead() { if (this.lookAheadTokenType === undefined) throw new Error("Tried to consume lookAhead but is was undefined"); return this.consume(this.lookAheadTokenType); } consume(tokenType) { const text = this.getRemainingText(tokenType.name); const begin = this.cursor; const token = new tokenType(); const match = token.regex.exec(text); if (match === null) { throw new Error(`Trying to consume [${tokenType.name}]\nGot: ${this.getRemainingText().substring(0, 150)}...`); } const value = match[1]; token.value = value; this.cursor += value.length; const end = this.cursor; this.lookAheadTokenType = undefined; token.position = { begin, end }; this.tagStack.push(token); return token; } lazyConsume(tokenType) { if (this.lookAhead(tokenType)) { return this.consumeLookAhead(); } return undefined; } debug() { console.log("cursor: ", this.cursor); console.log("text length: ", this.text.length); console.log("rest|" + this.getRemainingText()); } } exports.AbstractLexer = AbstractLexer;