@masala/parser
Version:
322 lines (244 loc) • 11.9 kB
JavaScript
'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 _index = require('../../lib/stream/index');
var _index2 = _interopRequireDefault(_index);
var _index3 = require('../../lib/parsec/index');
var _lib = require('../../lib');
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"); } }
var value = undefined;
var accepted = undefined;
function testParser(parser, string) {
var stream = _index2.default.ofString(string);
var parsing = parser.parse(stream);
value = parsing.value;
accepted = parsing.isAccepted();
}
exports.default = {
setUp: function setUp(done) {
done();
},
'subStream is ok on string stream': function subStreamIsOkOnStringStream(test) {
var text = 'Hello World';
var parser = _index3.F.subStream(6).then(_index3.C.string('World'));
var response = parser.parse(_index2.default.ofString(text));
test.ok(response.isAccepted());
test.equal(7, response.value.size()); // 6 for the stream, one for World
test.done();
},
'subStream is ok on genlex stream': function subStreamIsOkOnGenlexStream(test) {
var genlex = new _lib.GenLex();
genlex.setSeparatorsParser(_index3.F.not(_index3.C.charIn('+-<>[],.')));
genlex.keywords(['+', '-', '<', '>', '[', ']', ',', '.']);
var grammar = _index3.F.subStream(4).drop().then(_index3.F.any().rep());
var parser = genlex.use(grammar);
var text = '++++ and then >>';
var response = parser.parse(_index2.default.ofString(text));
test.ok(response.isAccepted());
test.equal(2, response.value.size());
test.done();
},
'not parser should not eat offset': function notParserShouldNotEatOffset(test) {
var text = 'this is a line';
var line = text + '\n';
var eol = _index3.C.char('\n');
var parser = _index3.F.not(eol).rep();
var response = parser.parse(_index2.default.ofString(line));
test.ok(response.isAccepted());
test.equal(text.length, response.offset);
var withParser = _index3.F.not(eol).rep().then(eol);
response = withParser.parse(_index2.default.ofString(line));
test.ok(response.isAccepted());
test.equal(line.length, response.offset);
test.done();
},
'expect flatten result to be ok': function expectFlattenResultToBeOk(test) {
var string = 'foobar';
// tests here
var parser = _index3.C.char('f').then(_index3.C.char('o')).then(_index3.C.char('o')).then(_index3.C.string('bar')).array();
testParser(parser, string);
test.deepEqual(value, ['f', 'o', 'o', 'bar'], 'flatten result not ok');
test.done();
},
'expect returns to be ok when empty': function expectReturnsToBeOkWhenEmpty(test) {
var string = 'some';
// tests here
var parser = _index3.F.any().rep().then(_index3.F.eos()).returns([]);
testParser(parser, string);
test.ok(accepted);
test.deepEqual(value, [], 'flatten result not ok');
test.done();
},
'expect startWith to start': function expectStartWithToStart(test) {
var string = ' world';
var object = 'hello';
// tests here
var parser = _index3.F.startWith(object).then(_index3.C.string(' world')).then(_index3.F.eos().drop());
testParser(parser, string);
test.ok(accepted);
test.equals(value.join(''), 'hello world');
test.done();
},
'test moveUntilFast string': function testMoveUntilFastString(test) {
var line = _index2.default.ofString('soXYZso');
var combinator = _index3.F.moveUntil('XYZ');
var parser = combinator.parse(line);
var value = parser.value;
var offset = parser.offset;
test.equals(value, 'so');
test.equals(offset, 2);
test.done();
},
'test moveUntilFast string with continuation': function testMoveUntilFastStringWithContinuation(test) {
var document = 'start-detect-XYZ-continues';
var line = _index2.default.ofString(document);
var start = _index3.C.string('start-');
var combinator = start.drop().then(_index3.F.moveUntil('XYZ')).then(_index3.C.string('XYZ-continues').drop()).single();
var parser = combinator.parse(line);
var value = parser.value;
var offset = parser.offset;
test.equals(value, 'detect-');
test.equals(offset, document.length);
test.done();
},
'test moveUntilFast array of string with continuation': function testMoveUntilFastArrayOfStringWithContinuation(test) {
var document = 'start-detect-XYZ-continues';
var line = _index2.default.ofString(document);
var start = _index3.C.string('start-');
var combinator = start.drop().then(_index3.F.moveUntil(['ABC', 'ZE', 'XYZ'])).then(_index3.C.string('XYZ-continues').drop()).single();
var parser = combinator.parse(line);
var value = parser.value;
var offset = parser.offset;
test.equals(value, 'detect-');
test.equals(offset, document.length);
test.done();
},
'test moveUntilFast string fails': function testMoveUntilFastStringFails(test) {
var document = 'start-detect-XYZ-continues';
var line = _index2.default.ofString(document);
var start = _index3.C.string('start-');
var combinator = start.drop().then(_index3.F.moveUntil('EEE')).then(_index3.C.string('XYZ-continues').drop());
var parsing = combinator.parse(line);
test.ok(!parsing.isAccepted());
test.done();
},
'test moveUntilFast array of string fails': function testMoveUntilFastArrayOfStringFails(test) {
var document = 'start-detect-XYZ-continues';
var line = _index2.default.ofString(document);
var start = _index3.C.string('start-');
var combinator = start.drop().then(_index3.F.moveUntil(['ABC', 'ZE', 'EEE'])).then(_index3.C.string('XYZ-continues').drop());
var parsing = combinator.parse(line);
test.ok(!parsing.isAccepted());
test.done();
},
'test moveUntilFast fails if array stream': function testMoveUntilFastFailsIfArrayStream(test) {
var document = ['More', 'XYZ'];
var line = _index2.default.ofArray(document);
var combinator = _index3.F.moveUntil(['ABC', 'ZE', 'XYZ']);
var found = false;
try {
combinator.parse(line);
} catch (e) {
if (e === 'Input source must be a String') {
found = true;
}
}
test.ok(found);
test.done();
},
'test moveUntilFastString fails if array stream': function testMoveUntilFastStringFailsIfArrayStream(test) {
var document = ['More', 'XYZ'];
var line = _index2.default.ofArray(document);
var combinator = _index3.F.moveUntil('XYZ');
var found = false;
try {
combinator.parse(line);
} catch (e) {
if (e === 'Input source must be a String') {
found = true;
}
}
test.ok(found);
test.done();
},
'test moveUntil': function testMoveUntil(test) {
var line = _index2.default.ofString('I write until James appears');
var combinator = _index3.F.moveUntil(_index3.C.string('James')).then(_index3.F.any().drop()).single();
var value = combinator.parse(line).value;
test.equals(value, 'I write until ');
test.done();
},
'test moveUntil Not found': function testMoveUntilNotFound(test) {
var line = _index2.default.ofString('I write until James appears');
var combinator = _index3.F.moveUntil(_index3.C.string('Indiana')).then(_index3.C.string('I')).then(_index3.F.any().drop());
var accepted = combinator.parse(line).isAccepted();
test.ok(!accepted);
test.done();
},
'test moveUntil found with failing parser': function testMoveUntilFoundWithFailingParser(test) {
var line = _index2.default.ofString('I write until James Bond appears');
var combinator = _index3.F.moveUntil(_index3.C.string('James')).then(_index3.F.dropTo(_index3.F.eos()));
var accepted = combinator.parse(line).isAccepted();
test.ok(!accepted);
test.done();
},
'test dropTo with string': function testDropToWithString(test) {
var line = _index2.default.ofString('I write until James Bond appears');
var combinator = _index3.F.dropTo('James').then(_index3.C.string(' Bond appears')).then(_index3.F.eos());
var accepted = combinator.parse(line).isAccepted();
test.ok(accepted);
test.done();
},
'test dropTo with string fail': function testDropToWithStringFail(test) {
var line = _index2.default.ofString('I write until James Bond appears');
var combinator = _index3.F.dropTo('James').then(_index3.C.string(' Bond appears')).then(_index3.F.eos());
var accepted = combinator.parse(line).isAccepted();
test.ok(accepted);
test.done();
},
'test dropTo with parser': function testDropToWithParser(test) {
var line = _index2.default.ofString('I write until James Bond appears');
var combinator = _index3.F.dropTo(_index3.C.string('James')).then(_index3.C.string(' Bond appears')).then(_index3.F.eos());
var accepted = combinator.parse(line).isAccepted();
test.ok(accepted);
test.done();
},
'test moveUntil found with more parsers': function testMoveUntilFoundWithMoreParsers(test) {
var line = _index2.default.ofString('I write until James Bond appears');
var combinator = _index3.F.moveUntil(_index3.C.string('James')).then(_index3.F.dropTo('appears')).then(_index3.F.eos().drop()).single();
var value = combinator.parse(line).value;
test.equals(value, 'I write until ');
test.done();
},
'lazy with a class': function lazyWithAClass(test) {
var SomeLazyParser = function () {
function SomeLazyParser(char) {
_classCallCheck(this, SomeLazyParser);
this.char = char;
}
_createClass(SomeLazyParser, [{
key: 'first',
value: function first() {
return _index3.C.char(this.char).then(this.second().opt().map(function (opt) {
return opt.orElse('');
}));
}
}, {
key: 'second',
value: function second() {
return _index3.C.char('b').then(_index3.F.lazy(this.first, ['a'], this));
}
}]);
return SomeLazyParser;
}();
var line = _index2.default.ofString('ababa');
var combinator = new SomeLazyParser('a').first().then(_index3.F.eos().drop());
var value = combinator.parse(line).value;
test.equals(value.join(''), 'ababa');
test.done();
}
};
//# sourceMappingURL=flow-bundle-test.js.map