UNPKG

@parser-generator/definition

Version:

A Parser Generator that supports LL,SLR,LR1,LALR

83 lines 2.32 kB
import { Queue, assert } from "@light0x00/shim"; export class CommonAbstractRegexpLexer { constructor(text, patterns) { this.lastIndex = 0; this.hasMore = true; this.buffer = new Queue(); assert(text != undefined); this.text = text; this.patterns = patterns; } peek() { return this.peekFor(0); } peekFor(i) { if (this.fill(i)) { return this.buffer.get(i); } else { return this.getEOF(); } } next() { if (this.fill(0)) return this.buffer.removeFirst(); else return this.getEOF(); } /** * 填充直到队列达到指定数量,或没有足够的输入符号. * 如果填充达到要求的数量,返回true,否则false * @param i */ fill(i) { while (this.buffer.size() < i + 1 && this.hasMore) { this.addToken(); } return i < this.buffer.size(); } /** * 填充 */ addToken() { assert(this.hasMore); //determine lexeme and type let type; let lexeme; let match = null; for (let p of this.patterns) { p.regexp.lastIndex = this.lastIndex; match = p.regexp.exec(this.text); if (match != null) { type = p.type; lexeme = match[0]; this.lastIndex = p.regexp.lastIndex; //reach the end if (this.lastIndex >= this.text.length) { this.hasMore = false; } break; } } if (type == undefined || lexeme == undefined || match == null) { this.hasMore = false; this.onMatchFaild(this.text, this.lastIndex); return; } //create token let t = this.createToken(lexeme, type, match); if (t == undefined) { return false; } else { this.buffer.addLast(t); return true; } } onMatchFaild(text, lastIndex) { throw new Error(`Unrecognized charactor(${lastIndex}):${text[lastIndex]} `); } } export class AbstractRegexpLexer extends CommonAbstractRegexpLexer { } //# sourceMappingURL=lexical.js.map