parjs
Version:
A parser-combinator library for JavaScript.
146 lines (145 loc) • 4.33 kB
JavaScript
;
/** @module parjs/combinators */ /** */
Object.defineProperty(exports, "__esModule", { value: true });
const many_1 = require("../combinators/many");
const stringify_1 = require("../combinators/stringify");
const string_len_1 = require("./string-len");
const char_where_1 = require("./char-where");
const char_info_1 = require("char-info");
const must_1 = require("../combinators/must");
/**
* Returns a parser that parses any single character. Equivalent
* to `stringLen(1)`.
*/
function anyChar() {
return string_len_1.stringLen(1);
}
exports.anyChar = anyChar;
/**
* Returns a parser that parses a single ASCII inline space.
*/
function space() {
return char_where_1.charWhere(x => 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(many_1.many(), must_1.must(x => x.length > 0 || {
kind: "Soft"
}), stringify_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 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 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 char_where_1.charWhere(x => char_info_1.isLetter(x) || {
reason: "expecting a letter"
});
}
exports.letter = letter;
/**
* Returns a parser that parses a single Unicode letter.
*/
function uniLetter() {
return 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 char_where_1.charWhere(x => 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 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 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 char_where_1.charWhere(x => 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 char_where_1.charWhere(x => 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 char_where_1.charWhere(x => 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 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 char_where_1.charWhere(char => char_info_1.isSpace(char) || char_info_1.isNewline(char) || {
reason: "expecting a whitespaced character"
}).pipe(many_1.many(), stringify_1.stringify());
}
exports.whitespace = whitespace;
//# sourceMappingURL=char-types.js.map