"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _genlex = require("../../genlex/genlex.js");
var _index = require("../../parsec/index.js");
/*
 * Masala Parser
 * https://github.com/masala/masala-parser
 *
 * Copyright (c) 2016-2025 Didier Plaindoux & Nicolas Zozol
 * Licensed under the LGPL2 license.
 */

//
// Facilities
//

var genlex = new _genlex.GenLex();
genlex.keywords(['null', 'false', 'true', '{', '}', '[', ']', ':', ',']);
var number = genlex.tokenize(_index.N.number(), 'number', 1100);
var string = genlex.tokenize(_index.C.stringLiteral(), 'string', 800);
function tkKey(s) {
  return genlex.get(s);
}

// unit -> Parser ? Token
function arrayOrNothing() {
  // FIXME: ES2015 code not great
  var value = [],
    addValue = function addValue(e) {
      value = value.concat(e);
    },
    getValue = function getValue() {
      return value;
    },
    item = _index.F.lazy(expr).map(addValue);
  return item.then(tkKey(',').thenRight(item).optrep().array()).opt().map(getValue);
}

// unit -> Parser ? Token
function objectOrNothing() {
  // FIXME: ES2015 code not great
  var value = {},
    addValue = function addValue(e) {
      value[e[0]] = e[1];
    },
    getValue = function getValue() {
      return value;
    },
    attribute = string.thenLeft(tkKey(':')).then(_index.F.lazy(expr)).array().map(addValue);
  return attribute.thenLeft(tkKey(',').then(attribute).optrep()).array().opt().map(getValue);
}

// unit -> Parser ? Token
function expr() {
  return number.or(string).or(tkKey('null').returns(null)).or(tkKey('true').returns(true)).or(tkKey('false').returns(false)).or(tkKey('[').thenRight(_index.F.lazy(arrayOrNothing)).thenLeft(tkKey(']')).single()).or(tkKey('{').thenRight(_index.F.lazy(objectOrNothing)).thenLeft(tkKey('}')).single());
}

//const parse =
var _default = exports["default"] = {
  parse: function parse(source) {
    var parser = genlex.use(expr().thenLeft(_index.F.eos()).single());
    return parser.parse(source, 0);
  }
};
//# sourceMappingURL=jsonparser.js.map