cbon
Version:
Common Bracket Object Notation
82 lines (81 loc) • 3.02 kB
JavaScript
;
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
Object.defineProperty(exports, "__esModule", { value: true });
const parser_1 = require("./parser");
const toJsObj_1 = require("./toJsObj");
const utils_1 = require("./utils");
const tokenizer_1 = require("./tokenizer");
const state_machine_1 = require("./state_machine");
const canceller_1 = require("./canceller");
__export(require("./stringify"));
function parser(code, config) {
var _a, _b, _c, _d, _e, _f, _g;
const cancel = (_b = (_a = config) === null || _a === void 0 ? void 0 : _a.cancel, (_b !== null && _b !== void 0 ? _b : canceller_1.AlwaysFalse));
const show_all_err = (_d = (_c = config) === null || _c === void 0 ? void 0 : _c.show_all_err, (_d !== null && _d !== void 0 ? _d : false));
const async = (_f = (_e = config) === null || _e === void 0 ? void 0 : _e.async, (_f !== null && _f !== void 0 ? _f : false));
let isCancel = false;
const errmsg = [];
let root;
function* main(cancel) {
var _a, _b;
try {
const r = yield parser_1.parser(yield tokenizer_1.tokenizer(code, {
show_all_err,
iterable: true,
async,
cancel
}), {
show_all_err,
async,
cancel
});
if (r.err != null) {
errmsg.push(...utils_1.getErrorsMsgs(r.err));
}
root = r.val;
}
catch (e) {
if (e instanceof state_machine_1.WhenError) {
errmsg.push(e.err.msg);
}
else
throw e;
}
if (errmsg.length !== 0) {
throw new SyntaxError(`\n ${errmsg.join('\n ')}\n`);
}
if (yield /** is cancel */)
return null;
const o = yield toJsObj_1.toJsObj(root, { async });
if (_b = (_a = config) === null || _a === void 0 ? void 0 : _a.alwaysDocs, (_b !== null && _b !== void 0 ? _b : false))
return o;
if (o.length == 1) {
return o[0];
}
else
return o;
}
const def = ((_g = config) === null || _g === void 0 ? void 0 : _g.async) ? async function () {
async function Cancel() {
if (!isCancel)
isCancel = await cancel();
return isCancel;
}
const g = main(Cancel);
g.next(await g.next(await g.next().value).value);
return g.next(await g.next(await Cancel()).value).value;
} : function () {
function Cancel() {
if (!isCancel)
isCancel = cancel();
return isCancel;
}
const g = main(Cancel);
g.next(g.next(g.next().value).value);
return g.next(g.next(Cancel()).value).value;
};
return def();
}
exports.parser = parser;