parjs
Version:
A parser-combinator library for JavaScript.
58 lines (57 loc) • 1.88 kB
JavaScript
;
/**
* @module parjs
*/
/** */
Object.defineProperty(exports, "__esModule", { value: true });
const result_1 = require("../result");
const parser_1 = require("../parser");
const char_info_1 = require("char-info");
function innerNewline(unicodeRecognizer) {
return new class Newline extends parser_1.ParjserBase {
constructor() {
super(...arguments);
this.expecting = "expecting newline";
this.type = "newline";
}
_apply(ps) {
let { position, input } = ps;
if (position >= input.length) {
ps.kind = result_1.ResultKind.SoftFail;
return;
}
let pair = input.slice(position, position + 2);
if (pair === "\r\n") {
ps.position += 2;
ps.value = pair;
ps.kind = result_1.ResultKind.Ok;
return;
}
let firstChar = pair.charCodeAt(0);
if (firstChar === char_info_1.AsciiCodes.newline || firstChar === char_info_1.AsciiCodes.carriageReturn || (unicodeRecognizer && unicodeRecognizer(firstChar))) {
ps.position++;
ps.value = pair[0];
ps.kind = result_1.ResultKind.Ok;
return;
}
ps.kind = result_1.ResultKind.SoftFail;
}
}();
}
/**
* Parses an ASCII newline, which can be a single character or the sequence
* `\r\n`. Yields the text that was parsed.
*/
function newline() {
return innerNewline();
}
exports.newline = newline;
/**
* Parses a Unicode newline, which includes ASCII newline strings as well as
* other vertical separators such as PARAGRAPH SEPARATOR.
*/
function uniNewline() {
return innerNewline(char_info_1.uniIsNewline.code);
}
exports.uniNewline = uniNewline;
//# sourceMappingURL=newline.js.map