UNPKG

parjs

Version:

A parser-combinator library for JavaScript.

44 lines (43 loc) 1.41 kB
"use strict"; /** * @module parjs */ /** */ Object.defineProperty(exports, "__esModule", { value: true }); const result_1 = require("../result"); const parser_1 = require("../parser"); /** * Returns a parser that will parse the string `str` and yield the text * that was parsed. If it can't, it will fail softly without consuming input. * @param str The string to parse. */ function string(str) { return new class ParseString extends parser_1.ParjserBase { constructor() { super(...arguments); this.expecting = `expecting '${str}'`; this.type = "string"; } _apply(ps) { let { position, input } = ps; let i; if (position + str.length > input.length) { ps.kind = result_1.ResultKind.SoftFail; return; } // This should create a StringSlice object instead of actually // copying a whole string. let substr = input.slice(position, position + str.length); // Equality test is very very fast. if (substr !== str) { ps.kind = result_1.ResultKind.SoftFail; return; } ps.position += str.length; ps.value = str; ps.kind = result_1.ResultKind.Ok; } }(); } exports.string = string; //# sourceMappingURL=string.js.map