parser-ts
Version:
String parser combinators for TypeScript
166 lines (165 loc) • 5.79 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.run = exports.doubleQuotedString = exports.float = exports.int = exports.notSpaces1 = exports.notSpaces = exports.spaces1 = exports.spaces = exports.many1 = exports.many = exports.maybe = exports.fold = exports.oneOf = exports.string = void 0;
/**
* @since 0.6.0
*/
var E = __importStar(require("fp-ts/lib/Either"));
var function_1 = require("fp-ts/lib/function");
var M = __importStar(require("fp-ts/lib/Monoid"));
var O = __importStar(require("fp-ts/lib/Option"));
var string_1 = require("fp-ts/lib/string");
var C = __importStar(require("./char"));
var P = __importStar(require("./Parser"));
var S = __importStar(require("./Stream"));
// -------------------------------------------------------------------------------------
// constructors
// -------------------------------------------------------------------------------------
/**
* Matches the exact string provided.
*
* @category constructors
* @since 0.6.0
*/
var string = function (s) {
return P.expected(P.ChainRec.chainRec(s, function (acc) {
return (0, function_1.pipe)(charAt(0, acc), O.fold(function () { return P.of(E.right(s)); }, function (c) {
return (0, function_1.pipe)(C.char(c), P.chain(function () { return P.of(E.left(acc.slice(1))); }));
}));
}), JSON.stringify(s));
};
exports.string = string;
function oneOf(F) {
return function (ss) {
return F.reduce(ss, P.fail(), function (p, s) {
return (0, function_1.pipe)(p, P.alt(function () { return (0, exports.string)(s); }));
});
};
}
exports.oneOf = oneOf;
// -------------------------------------------------------------------------------------
// destructors
// -------------------------------------------------------------------------------------
/**
* @category destructors
* @since 0.6.0
*/
exports.fold = M.concatAll(P.getMonoid(string_1.Monoid));
// -------------------------------------------------------------------------------------
// combinators
// -------------------------------------------------------------------------------------
/**
* @category combinators
* @since 0.6.0
*/
exports.maybe = P.maybe(string_1.Monoid);
/**
* Matches the given parser zero or more times, returning a string of the
* entire match
*
* @category combinators
* @since 0.6.0
*/
var many = function (parser) { return (0, exports.maybe)((0, exports.many1)(parser)); };
exports.many = many;
/**
* Matches the given parser one or more times, returning a string of the
* entire match
*
* @category combinators
* @since 0.6.0
*/
var many1 = function (parser) {
return (0, function_1.pipe)(P.many1(parser), P.map(function (nea) { return nea.join(''); }));
};
exports.many1 = many1;
var charAt = function (index, s) {
return index >= 0 && index < s.length ? O.some(s.charAt(index)) : O.none;
};
/**
* Matches zero or more whitespace characters.
*
* @category combinators
* @since 0.6.0
*/
exports.spaces = C.many(C.space);
/**
* Matches one or more whitespace characters.
*
* @category combinators
* @since 0.6.0
*/
exports.spaces1 = C.many1(C.space);
/**
* Matches zero or more non-whitespace characters.
*
* @category combinators
* @since 0.6.0
*/
exports.notSpaces = C.many(C.notSpace);
/**
* Matches one or more non-whitespace characters.
*
* @category combinators
* @since 0.6.0
*/
exports.notSpaces1 = C.many1(C.notSpace);
var fromString = function (s) {
var n = +s;
return isNaN(n) || s === '' ? O.none : O.some(n);
};
/**
* @category combinators
* @since 0.6.0
*/
exports.int = P.expected((0, function_1.pipe)((0, exports.fold)([(0, exports.maybe)(C.char('-')), C.many1(C.digit)]), P.map(function (s) { return +s; })), 'an integer');
/**
* @category combinators
* @since 0.6.0
*/
exports.float = P.expected((0, function_1.pipe)((0, exports.fold)([(0, exports.maybe)(C.char('-')), C.many(C.digit), (0, exports.maybe)((0, exports.fold)([C.char('.'), C.many1(C.digit)]))]), P.chain(function (s) {
return (0, function_1.pipe)(fromString(s), O.fold(function () { return P.fail(); }, P.succeed));
})), 'a float');
/**
* Parses a double quoted string, with support for escaping double quotes
* inside it, and returns the inner string. Does not perform any other form
* of string escaping.
*
* @category combinators
* @since 0.6.0
*/
exports.doubleQuotedString = P.surroundedBy(C.char('"'))((0, exports.many)(P.either((0, exports.string)('\\"'), function () { return C.notChar('"'); })));
/**
* @summary
* Creates a stream from `string` and runs the parser.
*
* @category combinators
* @since 0.6.8
*/
function run(string) {
return function (p) { return p(S.stream(string.split(''))); };
}
exports.run = run;