"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.reject = exports["default"] = exports.accept = void 0;
var _try = _interopRequireDefault(require("../data/try.js"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /*
 * Masala Parser
 * https://github.com/masala/masala-parser
 *
 * Copyright (c) 2016-2025 Didier Plaindoux & Nicolas Zozol
 * Licensed under the LGPL2 license.
 */
/**
 * ParserResponse basic type
 * fold() is an abstract method implemented in Accept and Reject
 * A Response has two markers: offset and location().
 * Offset is the token count from the start of the input stream, where
 * location() is the character count of the input stream.
 *
 *
 * consumed is a boolean describing if the offset has moved forward. It doesn't mean
 * that the stream is fully consumed. See parser.eos() for that.
 */
var ParserResponse = /*#__PURE__*/function () {
  function ParserResponse(input, offset, consumed) {
    _classCallCheck(this, ParserResponse);
    this.input = input;
    this.offset = offset;
    this.consumed = consumed;
  }

  // Response 'a 'c => unit -> bool
  return _createClass(ParserResponse, [{
    key: "isAccepted",
    value: function isAccepted() {
      return this.fold(function () {
        return true;
      }, function () {
        return false;
      });
    }

    // Response 'a 'c => unit -> bool
  }, {
    key: "toTry",
    value: function toTry() {
      return this.fold(function (accept) {
        return _try["default"].success(accept.value);
      }, function (reject) {
        return _try["default"].failure(new Error('parser error at ' + reject.offset));
      });
    }
  }, {
    key: "isEos",
    value: function isEos() {
      return false; //overridden by Accept
    }
  }, {
    key: "getOffset",
    value: function getOffset() {
      return this.offset;
    }
  }, {
    key: "location",
    value: function location() {
      return this.input.location(this.getOffset());
    }

    /**
     * fold takes a function to map the value depending on result
     * Abstract function fold(accept, reject)
     *
     * flatMap is a specialization of fold
     */
  }]);
}();
/**
 * Reject response class
 */
var Reject = /*#__PURE__*/function (_ParserResponse) {
  function Reject(input, offset, consumed) {
    _classCallCheck(this, Reject);
    return _callSuper(this, Reject, [input, offset, consumed]);
  }

  // Response 'a 'c => (Accept 'a 'c -> 'a) -> (Reject 'a 'c -> 'a) -> 'a
  _inherits(Reject, _ParserResponse);
  return _createClass(Reject, [{
    key: "fold",
    value: function fold(_, reject) {
      return reject(this);
    }

    // Response 'a 'c => ('a -> 'b) -> Response 'b 'c
  }, {
    key: "map",
    value: function map() {
      return this;
    }

    // Response 'a 'c => ('a -> Response 'b 'c) -> Response 'b 'c
  }, {
    key: "flatMap",
    value: function flatMap() {
      return this;
    }

    // Response 'a 'c => ('a -> bool) -> Response 'b 'c
  }, {
    key: "filter",
    value: function filter() {
      return new Reject(this.input, this.offset, false);
    }
  }]);
}(ParserResponse);
/**
 * Accept response class
 */
var Accept = /*#__PURE__*/function (_ParserResponse2) {
  function Accept(value, input, offset, consumed) {
    var _this;
    _classCallCheck(this, Accept);
    _this = _callSuper(this, Accept, [input, offset, consumed]);
    _this.value = value;
    return _this;
  }
  _inherits(Accept, _ParserResponse2);
  return _createClass(Accept, [{
    key: "isEos",
    value: function isEos() {
      return this.input.endOfStream(this.offset);
    }

    // Response 'a 'c => (Accept 'a 'c -> 'a) -> (Reject 'a 'c -> 'a) -> 'a
  }, {
    key: "fold",
    value: function fold(accept) {
      return accept(this);
    }

    // Response 'a 'c => ('a -> 'b) -> Response 'b 'c
  }, {
    key: "map",
    value: function map(callback) {
      return new Accept(callback(this.value, this), this.input, this.offset, this.consumed);
    }

    // Response 'a 'c => ('a -> Response 'b 'c) -> Response 'b 'c
  }, {
    key: "flatMap",
    value: function flatMap(callback) {
      return callback(this.value, this);
    }

    // Response 'a 'c => ('a -> bool) -> Response 'b 'c
  }, {
    key: "filter",
    value: function filter(predicate) {
      if (predicate(this.value)) {
        return this;
      } else {
        return new Reject(this.input, this.offset, false);
      }
    }
  }]);
}(ParserResponse);
/**
 * Constructors
 */
var accept = exports.accept = function accept(value, input, offset, consumed) {
  return new Accept(value, input, offset, consumed);
};
var reject = exports.reject = function reject(input, offset, consumed) {
  return new Reject(input, offset, consumed);
};
var response = {
  accept: accept,
  reject: reject
};
var _default = exports["default"] = response;
//# sourceMappingURL=response.js.map