usfm
Version:
A [USFM](http://paratext.org/about/usfm) parser for JavaScript (written in TypeScript).
32 lines • 1.02 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const lexer_1 = require("perplex/lib/lexer");
class UsfmLexer {
constructor(s) {
this.lexer = new lexer_1.default(s);
this.lexer.token('TAG', /\\\+?[a-z0-9]+\*?\s*/i);
this.lexer.token('TEXT', /[^\\]+/);
}
_tkn(t) {
if (t.type == 'TAG')
t.type = t.match.replace(/^\\/, '').trim();
return t;
}
peek() {
return this._tkn(this.lexer.peek());
}
next() {
return this._tkn(this.lexer.next());
}
expect(type) {
const token = this.lexer.next();
const surrogateType = token.type == 'TAG' ? token.match.replace(/^\\/, '').trim() : token.type;
if (surrogateType != type) {
const { start } = token.strpos();
throw new Error(`Expected ${type}, got ${surrogateType} (at ${start.line}:${start.column})`);
}
return token;
}
}
exports.UsfmLexer = UsfmLexer;
//# sourceMappingURL=lexer.js.map
;