UNPKG

@masala/parser

Version:
230 lines (177 loc) 10.4 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.reject = exports.accept = undefined; 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; }; }(); /* * Parsec * https://github.com/d-plaindoux/parsec * * Copyright (c) 2016 Didier Plaindoux * Licensed under the LGPL2 license. */ var _try = require('../data/try.js'); var _try2 = _interopRequireDefault(_try); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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; } function _classCallCheck(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(this, ParserResponse); this.input = input; this.offset = offset; this.consumed = consumed; } // Response 'a 'c => unit -> bool _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 _try2.default.success(accept.value); }, function (reject) { return _try2.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(Reject, _ParserResponse); function Reject(input, offset, consumed) { _classCallCheck(this, Reject); return _possibleConstructorReturn(this, (Reject.__proto__ || Object.getPrototypeOf(Reject)).call(this, input, offset, consumed)); } // Response 'a 'c => (Accept 'a 'c -> 'a) -> (Reject 'a 'c -> 'a) -> 'a _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); } }]); return Reject; }(ParserResponse); /** * Accept response class */ var Accept = function (_ParserResponse2) { _inherits(Accept, _ParserResponse2); function Accept(value, input, offset, consumed) { _classCallCheck(this, Accept); var _this2 = _possibleConstructorReturn(this, (Accept.__proto__ || Object.getPrototypeOf(Accept)).call(this, input, offset, consumed)); _this2.value = value; return _this2; } _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.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 }; exports.default = response; exports.accept = accept; exports.reject = reject; //# sourceMappingURL=response.js.map