UNPKG

parser-combinator

Version:
90 lines (67 loc) 6.5 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = 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 = require('./stream'); var _stream2 = _interopRequireDefault(_stream); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(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(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 */ var ParserStream = function (_Stream) { _inherits(ParserStream, _Stream); function ParserStream(parser, source) { _classCallCheck(this, ParserStream); var _this = _possibleConstructorReturn(this, (ParserStream.__proto__ || Object.getPrototypeOf(ParserStream)).call(this)); _this.source = parser; _this.input = source; _this.offsets = {}; return _this; } _createClass(ParserStream, [{ key: 'getOffset', value: function getOffset(index) { return this.offsets[index] || index; } // Stream 'a => number -> number }, { key: 'location', value: function location(index) { return this.input.location(this.getOffset(index - 1) + 1); } // ParserStream 'a => unit -> boolean }, { key: 'endOfStream', value: function endOfStream(index) { return this.input.endOfStream(this.getOffset(index)); } // ParserStream 'a => number -> 'a <+> error }, { key: 'unsafeGet', value: function unsafeGet(index) { var result = this.source.parse(this.input, this.getOffset(index)); if (result.isAccepted()) { this.offsets[index + 1] = result.offset; return result.value; } else { throw new Error(); } } }]); return ParserStream; }(_stream2.default); function factory(parser, source) { return new ParserStream(parser, source); } exports.default = factory; //# sourceMappingURL=parserstream.js.map