UNPKG

@masala/parser

Version:
137 lines (112 loc) 8.46 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); var _option = require('../data/option'); var _option2 = _interopRequireDefault(_option); 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 * 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(ParserStream, _Stream); function ParserStream(parser, lowerStream) { _classCallCheck(this, ParserStream); //TODO: why the parser is called source ? var _this = _possibleConstructorReturn(this, (ParserStream.__proto__ || Object.getPrototypeOf(ParserStream)).call(this)); _this.source = parser; _this.input = lowerStream; _this.offsets = [0]; return _this; } _createClass(ParserStream, [{ key: 'getOffset', value: function getOffset(index) { if (index < this.offsets.length) { // TODO logger console.log('found offset', this.offsets[index]); return _option2.default.some(this.offsets[index]); } return _option2.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 source is at result.offset this.offsets.push(result.offset); return result.value; } else { // TODO logger throw new Error(); } } }]); return ParserStream; }(_stream2.default); function factory(parser, lowerStream) { return new ParserStream(parser, lowerStream); } exports.default = factory; //# sourceMappingURL=parserstream.js.map