parjs
Version:
Library for building parsers using combinators.
121 lines • 4.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.whitespace = exports.uniLower = exports.upper = exports.lower = exports.hex = exports.uniUpper = exports.uniDecimal = exports.digit = exports.uniLetter = exports.letter = exports.noCharOf = exports.anyCharOf = exports.spaces1 = exports.space = exports.anyChar = void 0;
const char_info_1 = require("char-info");
const combinators_1 = require("../combinators");
const char_where_1 = require("./char-where");
const string_len_1 = require("./string-len");
/** Returns a parser that parses any single character. Equivalent to `stringLen(1)`. */
function anyChar() {
return (0, string_len_1.stringLen)(1);
}
exports.anyChar = anyChar;
/** Returns a parser that parses a single space (`[ \t]`). */
function space() {
return (0, char_where_1.charWhere)(x => (0, char_info_1.isSpace)(x) || { reason: "expecting a space" });
}
exports.space = space;
/** Returns a parser that parses at least one ASCII inline space. */
function spaces1() {
return space().pipe((0, combinators_1.many)(), (0, combinators_1.must)(x => x.length > 0 || {
kind: "Soft"
}), (0, combinators_1.stringify)());
}
exports.spaces1 = spaces1;
/**
* Returns a parser that parses any character in the string `options`.
*
* @param options A string of alternative characters.
*/
function anyCharOf(options) {
return (0, char_where_1.charWhere)(c => options.includes(c) || {
reason: `any char of ${options}`
});
}
exports.anyCharOf = anyCharOf;
/**
* Returns a parser that parses any character not in the string `exclusions`.
*
* @param exclusions A string of characters to exclude.
*/
function noCharOf(exclusions) {
return (0, char_where_1.charWhere)(c => !exclusions.includes(c) || {
reason: `no char of: ${exclusions}`
});
}
exports.noCharOf = noCharOf;
/** Returns a parser that parses a single ASCII letter. */
function letter() {
return (0, char_where_1.charWhere)(x => (0, char_info_1.isLetter)(x) || {
reason: "expecting a letter"
});
}
exports.letter = letter;
/** Returns a parser that parses a single Unicode letter. */
function uniLetter() {
return (0, char_where_1.charWhere)(x => char_info_1.uniIsLetter.char(x) || {
reason: "expecting a Unicode letter"
});
}
exports.uniLetter = uniLetter;
/** Returns a parser that parses a single digit in base `base`. */
function digit(base = 10) {
return (0, char_where_1.charWhere)(x => (0, char_info_1.isDigit)(x, base) || {
reason: `expecting a digit in base ${base}`
});
}
exports.digit = digit;
/** Returns a parser that parses a single Unicode decimal digit. */
function uniDecimal() {
return (0, char_where_1.charWhere)(x => char_info_1.uniIsDecimal.char(x) || {
reason: `expecting a Unicode decimal digit`
});
}
exports.uniDecimal = uniDecimal;
/** Returns a parser that parses a single Unicode upper-case character. */
function uniUpper() {
return (0, char_where_1.charWhere)(x => char_info_1.uniIsUpper.char(x) || {
reason: `expecting a Unicode upper-case letter`
});
}
exports.uniUpper = uniUpper;
/** Returns a parser that parses a single hex digit. */
function hex() {
return (0, char_where_1.charWhere)(x => (0, char_info_1.isHex)(x) || {
reason: "expecting a hex digit"
});
}
exports.hex = hex;
/** Returns a parser that parses a single ASCII lowercase letter. */
function lower() {
return (0, char_where_1.charWhere)(x => (0, char_info_1.isLower)(x) || {
reason: "expecting a lowercase letter"
});
}
exports.lower = lower;
/** Returns a parser that parses a single uppercase ASCII letter. */
function upper() {
return (0, char_where_1.charWhere)(x => (0, char_info_1.isUpper)(x) || {
reason: "expecting an uppercase letter"
});
}
exports.upper = upper;
/** Returns a parser that parses a single lowercase Unicode letter. */
function uniLower() {
return (0, char_where_1.charWhere)(x => char_info_1.uniIsLower.char(x) || {
reason: "expecting a Unicode lowercase letter"
});
}
exports.uniLower = uniLower;
/**
* Returns a parser that parses any amount of ASCII whitespace characters and yields the parsed
* text.
*/
function whitespace() {
return (0, char_where_1.charWhere)(char => (0, char_info_1.isSpace)(char) ||
(0, char_info_1.isNewline)(char) || {
reason: "expecting a whitespaced character"
}).pipe((0, combinators_1.many)(), (0, combinators_1.stringify)());
}
exports.whitespace = whitespace;
//# sourceMappingURL=char-types.js.map