parjs
Version:
Library for building parsers using combinators.
55 lines • 1.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.uniNewline = exports.newline = void 0;
const char_info_1 = require("char-info");
const parser_1 = require("../parser");
const result_1 = require("../result");
class Newline extends parser_1.ParjserBase {
constructor(_unicodeRecognizer) {
super();
this._unicodeRecognizer = _unicodeRecognizer;
this.type = "newline";
this.expecting = "expecting newline";
}
_apply(ps) {
const { position, input } = ps;
if (position >= input.length) {
ps.kind = result_1.ResultKind.SoftFail;
return;
}
const pair = input.slice(position, position + 2);
if (pair === "\r\n") {
ps.position += 2;
ps.value = pair;
ps.kind = result_1.ResultKind.Ok;
return;
}
const firstChar = pair.charCodeAt(0);
if (firstChar === char_info_1.AsciiCodes.newline ||
firstChar === char_info_1.AsciiCodes.carriageReturn ||
this._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 new Newline();
}
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 new Newline(char_info_1.uniIsNewline.code);
}
exports.uniNewline = uniNewline;
//# sourceMappingURL=newline.js.map