"use strict";

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); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _stream = _interopRequireDefault(require("./stream.js"));
var _option = _interopRequireDefault(require("../data/option.js"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
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); }
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); } /*
 * Masala Parser
 * https://github.com/masala/masala-parser
 *
 * Copyright (c) 2016-2025 Didier Plaindoux & Nicolas Zozol
 * Licensed under the LGPL2 license.
 */
/**
 * ParserStream stream class
 * Compared to StringStream, it is NOT a RandomAccess.
 * You must use a substream before making an access to a unreached point.
 */
var ParserStream = /*#__PURE__*/function (_Stream) {
  function ParserStream(parser, lowerStream) {
    var _this;
    _classCallCheck(this, ParserStream);
    _this = _callSuper(this, ParserStream);
    //TODO: why the parser is called source ?
    _this.source = parser;
    _this.input = lowerStream;
    _this.offsets = [0];
    return _this;
  }
  _inherits(ParserStream, _Stream);
  return _createClass(ParserStream, [{
    key: "getOffset",
    value: function getOffset(index) {
      if (index < this.offsets.length) {
        // TODO logger console.log('found offset', this.offsets[index]);
        return _option["default"].some(this.offsets[index]);
      }
      return _option["default"].none();
    }

    // Stream 'a => number -> number
  }, {
    key: "location",
    value: function location(index) {
      if (index === 0) {
        return 0;
      }
      var option = this.getOffset(index);
      if (option.isPresent()) {
        // TODO: check the +1 ; the -1 has disappeared
        return this.input.location(option.get());
      } else {
        // TODO logger
        throw 'No location has been found yet for index ' + index;
        // TODO: What is the use case ?
        // return this.input.location(this.getOffset(index - 1) + 1);
      }
    }

    // ParserStream 'a => unit -> boolean
  }, {
    key: "endOfStream",
    value: function endOfStream(index) {
      var option = this.getOffset(index);
      if (option.isPresent()) {
        return this.input.endOfStream(option.get());
      } else {
        // If last was the last befor end of stream, then yes
        var last = this.offsets[this.offsets.length - 1];
        return this.input.endOfStream(last + 1);
      }
    }

    // ParserStream 'a => number -> 'a <+> error
    /**
     * index is the token index ; It uses getOffset(index) to retrieve the location
     * in the stringStream
     * @param index  token index
     */
  }, {
    key: "unsafeGet",
    value: function unsafeGet(index) {
      // the wrapped parser parses the StringStream
      var sourceIndex;
      var option = this.getOffset(index);
      if (option.isPresent()) {
        // we have already read it. Should be caught by the bufferedStream
        sourceIndex = option.get();
      } else {
        // TODO logger
        throw new Error();
      }
      var result = this.source.parse(this.input, sourceIndex);
      if (result.isAccepted()) {
        // Why index+1 ? Because we succeeded, and the source is at result.offset
        this.offsets.push(result.offset);
        return result.value;
      } else {
        // TODO logger
        throw new Error();
      }
    }
  }]);
}(_stream["default"]);
function factory(parser, lowerStream) {
  return new ParserStream(parser, lowerStream);
}
var _default = exports["default"] = factory;
//# sourceMappingURL=parserstream.js.map