UNPKG

web-gash

Version:

Terminal web game framework in React.

1,032 lines (829 loc) 250 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var jsxRuntime = require('react/jsx-runtime'); var React = require('react'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var React__default = /*#__PURE__*/_interopDefaultLegacy(React); /*! ***************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ var __assign = function() { __assign = Object.assign || function __assign(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; function __spreadArray(to, from) { for (var i = 0, il = from.length, j = to.length; i < il; i++, j++) to[j] = from[i]; return to; } var brown600 = '#6d4c41'; var cyan600 = '#00acc1'; var deepOrange600 = '#f4511e'; var deepPurple400 = '#7e57c2'; var green600 = '#43a047'; var grey200 = '#eeeeee'; var grey600 = '#757575'; var indigo400 = '#5c6bc0'; var lightBlue600 = '#039be5'; var red600 = '#e53935'; var yellow600 = '#fdd835'; var white0 = '#ffffff'; var black900 = '#000000'; /** * Simple component that changes color of its content, optionally changing the background color as well. */ function Colored$1(props) { var foreground = props.foreground, background = props.background, children = props.children; var style = { color: foreground }; if (background !== undefined) { style = __assign(__assign({}, style), { backgroundColor: background }); } return (jsxRuntime.jsx("span", __assign({ style: style }, { children: children }), void 0)); } /** * Text styled to look as a built-in commands references */ function CommandColored(props) { return jsxRuntime.jsx(Colored$1, __assign({}, props, { foreground: cyan600 }), void 0); } /** * Component that formats a single line of output. It is recommended to wrap each `Gash.writeLine` content with this component. */ function Line(props) { var children = props.children, systemMessage = props.systemMessage, tabs = props.tabs; var styles = { marginTop: '3px', marginBottom: '3px', color: (systemMessage !== undefined && systemMessage) ? grey600 : white0, }; if (tabs !== undefined) { styles = __assign(__assign({}, styles), { marginLeft: tabs * 15 + "px" }); } if (children === undefined) { styles = __assign(__assign({}, styles), { minHeight: '20px' }); } return (jsxRuntime.jsx("div", __assign({ style: styles }, { children: children }), void 0)); } // Inspired by https://github.com/ai/nanoevents with which my jest was struggling var Emitter = /** @class */ (function () { function Emitter() { this.events = {}; } Emitter.prototype.emit = function (event) { var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } var listeners = this.events[event]; if (listeners !== undefined) { for (var _a = 0, listeners_1 = listeners; _a < listeners_1.length; _a++) { var listener = listeners_1[_a]; listener.apply(void 0, args); } } }; Emitter.prototype.on = function (event, cb) { var _this = this; var listeners = this.events[event]; if (listeners === undefined) { listeners = []; } listeners.push(cb); this.events[event] = listeners; return function () { listeners = _this.events[event]; if (listeners !== undefined) { listeners = listeners.filter(function (c) { return c !== cb; }); _this.events[event] = listeners; } }; }; return Emitter; }()); exports.AutoCompleteResultType = void 0; (function (AutoCompleteResultType) { /** The input already fully matches the attempted solution. */ AutoCompleteResultType[AutoCompleteResultType["AlreadyMatching"] = 0] = "AlreadyMatching"; /** The input can be auto-completed and there is only on choice. */ AutoCompleteResultType[AutoCompleteResultType["SingleMatchFound"] = 1] = "SingleMatchFound"; /** The input can be auto-completed but there are multiple options. */ AutoCompleteResultType[AutoCompleteResultType["MultipleMatchesFound"] = 2] = "MultipleMatchesFound"; /** The input does not match the attempted solution. */ AutoCompleteResultType[AutoCompleteResultType["NotMatching"] = 3] = "NotMatching"; })(exports.AutoCompleteResultType || (exports.AutoCompleteResultType = {})); /** * Reason for a parsing failure. */ exports.ParsingFailureReason = void 0; (function (ParsingFailureReason) { /** The input line does not correspond to the command. */ ParsingFailureReason[ParsingFailureReason["WrongCommand"] = 0] = "WrongCommand"; /** A mandatory parameter missing in the input line for this command. */ ParsingFailureReason[ParsingFailureReason["MissingParam"] = 1] = "MissingParam"; /** The input line contains an option unknown by the command. */ ParsingFailureReason[ParsingFailureReason["UnrecognizedOption"] = 2] = "UnrecognizedOption"; })(exports.ParsingFailureReason || (exports.ParsingFailureReason = {})); var lib = {}; var data = {}; var option = {}; Object.defineProperty(option, "__esModule", { value: true }); var _createClass$a = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck$b(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /* * Parsec * https://github.com/d-plaindoux/parsec * * Copyright (c) 2016 Didier Plaindoux * Licensed under the LGPL2 license. */ /** * Need a reference object in memory for uniqueness. Symbols causes troubles. */ var empty = { __MASALA_EMPTY__: 'empty' }; /** * Private class Option, accessible from someOrNone() or none() */ var Option = function () { function Option(value) { _classCallCheck$b(this, Option); this.value = value; } _createClass$a(Option, [{ key: 'isPresent', value: function isPresent() { return this.value !== empty; } }, { key: 'map', value: function map(bindCall) { if (this.isPresent()) { return someOrNone(bindCall(this.value)); } else { return this; } } }, { key: 'flatMap', value: function flatMap(bindCall) { if (this.isPresent()) { return bindCall(this.value); } else { return this; } } }, { key: 'filter', value: function filter(f) { if (this.isPresent() && f(this.value)) { return this; } // equivalent of return none() without cyclic creation // eslint : no-use-before-define return new Option(empty); } }, { key: 'get', value: function get() { return this.value; } }, { key: 'orElse', value: function orElse(value) { if (this.isPresent()) { return this.value; } else { return value; } } }, { key: 'orLazyElse', value: function orLazyElse(value) { if (this.isPresent()) { return this.value; } else { return value(); } } }]); return Option; }(); function someOrNone(value) { return new Option(value); } function none() { return new Option(empty); } option.default = { some: someOrNone, none: none }; var _try$3 = {}; Object.defineProperty(_try$3, "__esModule", { value: true }); var _createClass$9 = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck$a(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /* * Parsec * https://github.com/d-plaindoux/parsec * * Copyright (c) 2016 Didier Plaindoux * Licensed under the LGPL2 license. */ var Try = function () { function Try(value, error) { _classCallCheck$a(this, Try); this.value = value; this.error = error; } _createClass$9(Try, [{ key: 'isSuccess', value: function isSuccess() { return this.error === null; } }, { key: 'isFailure', value: function isFailure() { return !this.isSuccess(); } }, { key: 'onSuccess', value: function onSuccess(bindCall) { if (this.isSuccess()) { bindCall(this.value); } return this; } }, { key: 'onFailure', value: function onFailure(bindCall) { if (this.isFailure()) { bindCall(this.error); } return this; } }, { key: 'map', value: function map(bindCall) { if (this.isSuccess()) { try { return success(bindCall(this.value)); } catch (e) { return failure(e); } } else { return this; } } }, { key: 'flatMap', value: function flatMap(bindCall) { if (this.isSuccess()) { try { return bindCall(this.value); } catch (e) { return failure(e); } } else { return this; } } }, { key: 'success', value: function success() { return this.value; } }, { key: 'failure', value: function failure() { return this.error; } }, { key: 'recoverWith', value: function recoverWith(value) { if (this.isSuccess()) { return this.value; } else { return value; } } }, { key: 'lazyRecoverWith', value: function lazyRecoverWith(value) { if (this.isSuccess()) { return this.value; } else { return value(this.error); } } }, { key: 'filter', value: function filter(f) { if (this.isSuccess()) { if (f(this.value)) { return this; } else { return failure(new Error('invalid filter')); } } return this; } }]); return Try; }(); function success(value) { return new Try(value, null); } function failure(error) { return new Try(null, error); } _try$3.default = { success: success, failure: failure }; var unit = {}; Object.defineProperty(unit, "__esModule", { value: true }); function _classCallCheck$9(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /* * Parsec * https://github.com/d-plaindoux/parsec * * Copyright (c) 2016 Didier Plaindoux * Licensed under the LGPL2 license. */ var Unit = function Unit() { _classCallCheck$9(this, Unit); }; unit.default = new Unit(); Object.defineProperty(data, "__esModule", { value: true }); var _option$3 = option; var _option2$3 = _interopRequireDefault$f(_option$3); var _try$2 = _try$3; var _try2$2 = _interopRequireDefault$f(_try$2); var _unit$2 = unit; var _unit2$2 = _interopRequireDefault$f(_unit$2); function _interopRequireDefault$f(obj) { return obj && obj.__esModule ? obj : { default: obj }; } data.default = { option: _option2$3.default, atry: _try2$2.default, unit: _unit2$2.default }; /* * Parsec * https://github.com/d-plaindoux/parsec * * Copyright (c) 2016 Didier Plaindoux * Licensed under the LGPL2 license. */ var genlex$1 = {}; var response$1 = {}; Object.defineProperty(response$1, "__esModule", { value: true }); response$1.reject = response$1.accept = undefined; var _createClass$8 = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /* * Parsec * https://github.com/d-plaindoux/parsec * * Copyright (c) 2016 Didier Plaindoux * Licensed under the LGPL2 license. */ var _try$1 = _try$3; var _try2$1 = _interopRequireDefault$e(_try$1); function _interopRequireDefault$e(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _possibleConstructorReturn$4(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits$4(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } function _classCallCheck$8(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /** * 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 offest has moved forward. It doesn't mean * that the stream is fully consumed. See parser.eos() for that. */ var ParserResponse = function () { function ParserResponse(input, offset, consumed) { _classCallCheck$8(this, ParserResponse); this.input = input; this.offset = offset; this.consumed = consumed; } // Response 'a 'c => unit -> bool _createClass$8(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 _try2$1.default.success(accept.value); }, function (reject) { return _try2$1.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 */ }]); return ParserResponse; }(); /** * Reject response class */ var Reject = function (_ParserResponse) { _inherits$4(Reject, _ParserResponse); function Reject(input, offset, consumed) { _classCallCheck$8(this, Reject); return _possibleConstructorReturn$4(this, (Reject.__proto__ || Object.getPrototypeOf(Reject)).call(this, input, offset, consumed)); } // Response 'a 'c => (Accept 'a 'c -> 'a) -> (Reject 'a 'c -> 'a) -> 'a _createClass$8(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); } }]); return Reject; }(ParserResponse); /** * Accept response class */ var Accept = function (_ParserResponse2) { _inherits$4(Accept, _ParserResponse2); function Accept(value, input, offset, consumed) { _classCallCheck$8(this, Accept); var _this2 = _possibleConstructorReturn$4(this, (Accept.__proto__ || Object.getPrototypeOf(Accept)).call(this, input, offset, consumed)); _this2.value = value; return _this2; } _createClass$8(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.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); } // 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); } } }]); return Accept; }(ParserResponse); /** * Constructors */ var accept = function accept(value, input, offset, consumed) { return new Accept(value, input, offset, consumed); }; var reject = function reject(input, offset, consumed) { return new Reject(input, offset, consumed); }; var response = { accept: accept, reject: reject }; response$1.default = response; response$1.accept = accept; response$1.reject = reject; var parsec = {}; var parser = {}; var stream$1 = {}; var stringstream = {}; var stream = {}; Object.defineProperty(stream, "__esModule", { value: true }); var _createClass$7 = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /* * Parsec * https://github.com/d-plaindoux/parsec * * Copyright (c) 2016 Didier Plaindoux * Licensed under the LGPL2 license. */ var _try = _try$3; var _try2 = _interopRequireDefault$d(_try); function _interopRequireDefault$d(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck$7(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /** * Abstract methods: * - unsafeGet(index) */ var Stream = function () { function Stream() { _classCallCheck$7(this, Stream); } // Stream 'a => number -> number _createClass$7(Stream, [{ key: 'location', value: function location(index) { return index; } // Stream 'a => number -> Try 'a }, { key: 'get', value: function get(index) { try { if (this.endOfStream(index)) { return _try2.default.failure(new Error('End of stream reached')); } else { return _try2.default.success(this.unsafeGet(index)); } } catch (e) { return _try2.default.failure(e); } } // Stream 'a => [Comparable 'a] -> number -> boolean }, { key: 'subStreamAt', value: function subStreamAt(s, index) { for (var i = 0; i < s.length; i++) { var value = this.get(i + index); if (!value.isSuccess() || value.success() !== s[i]) { return false; } } return true; } }]); return Stream; }(); stream.default = Stream; Object.defineProperty(stringstream, "__esModule", { value: true }); var _createClass$6 = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _stream$3 = stream; var _stream2$3 = _interopRequireDefault$c(_stream$3); function _interopRequireDefault$c(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck$6(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn$3(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits$3(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /* * Parsec * https://github.com/d-plaindoux/parsec * * Copyright (c) 2016 Didier Plaindoux * Licensed under the LGPL2 license. */ /** * String stream class */ var StringStream = function (_Stream) { _inherits$3(StringStream, _Stream); function StringStream(source) { _classCallCheck$6(this, StringStream); var _this = _possibleConstructorReturn$3(this, (StringStream.__proto__ || Object.getPrototypeOf(StringStream)).call(this)); _this.source = source; return _this; } // StringStream 'a => unit -> boolean _createClass$6(StringStream, [{ key: 'endOfStream', value: function endOfStream(index) { return this.source.length <= index; } // StringStream 'a => number -> 'a <+> error }, { key: 'unsafeGet', value: function unsafeGet(index) { return this.source.charAt(index); } }]); return StringStream; }(_stream2$3.default); function factory$3(source) { return new StringStream(source); } stringstream.default = factory$3; var arraystream = {}; Object.defineProperty(arraystream, "__esModule", { value: true }); var _createClass$5 = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _stream$2 = stream; var _stream2$2 = _interopRequireDefault$b(_stream$2); function _interopRequireDefault$b(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck$5(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn$2(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits$2(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /* * Parsec * https://github.com/d-plaindoux/parsec * * Copyright (c) 2016 Didier Plaindoux * Licensed under the LGPL2 license. */ /** * Array stream class */ var ArrayStream = function (_Stream) { _inherits$2(ArrayStream, _Stream); function ArrayStream(source) { _classCallCheck$5(this, ArrayStream); var _this = _possibleConstructorReturn$2(this, (ArrayStream.__proto__ || Object.getPrototypeOf(ArrayStream)).call(this)); _this.source = source; return _this; } // ArrayStream 'a => unit -> boolean _createClass$5(ArrayStream, [{ key: 'endOfStream', value: function endOfStream(index) { return index >= this.source.length; } // ArrayStream 'a => number -> 'a <+> error }, { key: 'unsafeGet', value: function unsafeGet(index) { return this.source[index]; } }]); return ArrayStream; }(_stream2$2.default); function factory$2(source) { return new ArrayStream(source); } arraystream.default = factory$2; var parserstream = {}; Object.defineProperty(parserstream, "__esModule", { value: true }); var _createClass$4 = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _stream$1 = stream; var _stream2$1 = _interopRequireDefault$a(_stream$1); var _option$2 = option; var _option2$2 = _interopRequireDefault$a(_option$2); function _interopRequireDefault$a(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck$4(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn$1(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits$1(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /* * Parsec * https://github.com/d-plaindoux/parsec * * Copyright (c) 2016 Didier Plaindoux * 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 = function (_Stream) { _inherits$1(ParserStream, _Stream); function ParserStream(parser, lowerStream) { _classCallCheck$4(this, ParserStream); //TODO: why the parser is called source ? var _this = _possibleConstructorReturn$1(this, (ParserStream.__proto__ || Object.getPrototypeOf(ParserStream)).call(this)); _this.source = parser; _this.input = lowerStream; _this.offsets = [0]; return _this; } _createClass$4(ParserStream, [{ key: 'getOffset', value: function getOffset(index) { if (index < this.offsets.length) { // TODO logger console.log('found offset', this.offsets[index]); return _option2$2.default.some(this.offsets[index]); } return _option2$2.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 = void 0; 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 s