@parser-generator/definition
Version:
A Parser Generator that supports LL,SLR,LR1,LALR
87 lines • 2.51 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const shim_1 = require("@light0x00/shim");
class CommonAbstractRegexpLexer {
constructor(text, patterns) {
this.lastIndex = 0;
this.hasMore = true;
this.buffer = new shim_1.Queue();
shim_1.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() {
shim_1.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]} `);
}
}
exports.CommonAbstractRegexpLexer = CommonAbstractRegexpLexer;
class AbstractRegexpLexer extends CommonAbstractRegexpLexer {
}
exports.AbstractRegexpLexer = AbstractRegexpLexer;
//# sourceMappingURL=lexical.js.map