parser-combinator
Version:
Parser combinators
157 lines (118 loc) • 8.27 kB
JavaScript
;
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; }; }(); /*
* Parsec
* https://github.com/d-plaindoux/parsec
*
* Copyright (c) 2016 Didier Plaindoux
* Licensed under the LGPL2 license.
*/
var _flowBundle = require('../parsec/flow-bundle');
var _flowBundle2 = _interopRequireDefault(_flowBundle);
var _charsBundle = require('../../lib/parsec/chars-bundle');
var _charsBundle2 = _interopRequireDefault(_charsBundle);
var _numbersBundle = require('../../lib/parsec/numbers-bundle');
var _numbersBundle2 = _interopRequireDefault(_numbersBundle);
var _unit = require('../data/unit.js');
var _unit2 = _interopRequireDefault(_unit);
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"); } }
// (string -> 'a,string -> 'a,number -> 'a,string -> 'a,char -> 'a) -> GenlexFactory 'a
function GenlexFactory(keyword, ident, number, string, char) {
this.keyword = keyword;
this.ident = ident;
this.number = number;
this.string = string;
this.char = char;
}
var Genlex = function () {
// [String] -> Genlex
function Genlex() {
var keywords = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
_classCallCheck(this, Genlex);
var idletter = _charsBundle2.default.letter.or(_charsBundle2.default.char('_')).or(_numbersBundle2.default.digit);
this.identParser = _charsBundle2.default.letter.then(idletter.optrep()).map(function (r) {
return [r[0]].concat(r[1].array()).join('');
});
this.keywordParser = keywords.reduce(function (p, s) {
return _charsBundle2.default.string(s).or(p);
}, _flowBundle2.default.error);
}
// unit -> Parser char char
_createClass(Genlex, [{
key: 'space',
value: function space() {
return _charsBundle2.default.charIn(' \r\n\f\t');
}
// unit -> Parser unit char
}, {
key: 'spaces',
value: function spaces() {
return this.space().optrep().map(function () {
return _unit2.default;
});
}
// GenLexFactory 'a -> Parser 'a char
}, {
key: 'keyword',
value: function keyword(f) {
return this.keywordParser.map(f.keyword);
}
// GenLexFactory 'a -> Parser 'a char
}, {
key: 'ident',
value: function ident(f) {
return this.identParser.map(f.ident);
}
// GenLexFactory 'a -> Parser 'a char
}, {
key: 'number',
value: function number(f) {
return _numbersBundle2.default.numberLiteral.map(f.number);
}
// GenLexFactory 'a -> Parser 'a char
}, {
key: 'string',
value: function string(f) {
return _charsBundle2.default.stringLiteral.map(f.string);
}
// GenLexFactory 'a -> Parser 'a char
}, {
key: 'char',
value: function char(f) {
return _charsBundle2.default.charLiteral.map(f.char);
}
// GenLexFactory 'a -> Parser 'a char
}, {
key: 'token',
value: function token(f) {
return this.keyword(f).or(this.ident(f)).or(this.number(f)).or(this.string(f)).or(this.char(f));
}
// GenLexFactory 'a -> Parser 'a char
}, {
key: 'tokenBetweenSpaces',
value: function tokenBetweenSpaces(f) {
return this.spaces().thenRight(this.token(f)).thenLeft(this.spaces());
}
// GenLexFactory 'a -> Parser ['a] char
}, {
key: 'tokens',
value: function tokens(f) {
return this.tokenBetweenSpaces(f).optrep().thenLeft(_flowBundle2.default.eos).map(function (r) {
return r.array();
});
}
}]);
return Genlex;
}();
exports.default = {
factory: function factory(keyword, ident, number, string, char) {
return new GenlexFactory(keyword, ident, number, string, char);
},
generator: function generator(keywords) {
return new Genlex(keywords);
}
};
//# sourceMappingURL=genlex.js.map