fruitsconfits
Version:
FruitsConfits - A well typed and sugared parser combinator framework for TypeScript/JavaScript.
31 lines • 1.74 kB
JavaScript
;
// Copyright (c) 2019 Shellyl_N and Authors
// license: ISC
// https://github.com/shellyln
Object.defineProperty(exports, "__esModule", { value: true });
exports.parse = void 0;
const types_1 = require("../../lib/types");
const parser_1 = require("../../lib/parser");
const string_parser_1 = require("../../lib/string-parser");
const $s = string_parser_1.getStringParsers({
rawToToken: rawToken => rawToken,
concatTokens: tokens => (tokens.length ?
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
[tokens.reduce((a, b) => a + b)] : []),
});
const { seq, cls, notCls, classes, cat, repeat, end, first, combine, erase, trans, ahead, makeProgram } = $s;
const quoted = trans(input => input.length ? input : [''])(erase(repeat(classes.spaceWithinSingleLine), cls('"')), cat(repeat(first(trans(input => ['"'])(seq('""')), notCls('"')))), erase(cls('"'), repeat(erase(classes.spaceWithinSingleLine))));
const nakid = trans(input => input.length ? input : [''])(erase(repeat(classes.spaceWithinSingleLine)), cat(repeat(first(erase(classes.spaceWithinSingleLine, ahead(cls(',', '\r\n', '\n', '\r'))), notCls(',', '\r\n', '\n', '\r')))));
const cell = first(quoted, nakid);
const row = trans(input => [input])(cell, repeat(combine(erase(seq(',')), cell)));
const rows = makeProgram(combine(row, repeat(combine(erase(classes.newline), row)), end()));
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
function parse(s) {
const z = rows(types_1.parserInput(s));
if (!z.succeeded) {
throw new Error(parser_1.formatErrorMessage(z));
}
return z.tokens;
}
exports.parse = parse;
//# sourceMappingURL=index.js.map