parser-combinator
Version:
Parser combinators
512 lines (435 loc) • 20.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _index = require('../../lib/stream/index');
var _index2 = _interopRequireDefault(_index);
var _index3 = require('../../lib/parsec/index');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/*
======== A Handy Little Nodeunit Reference ========
https://github.com/caolan/nodeunit
Test methods:
test.expect(numAssertions)
test.done()
Test assertions:
test.ok(value, [message])
test.equal(actual, expected, [message])
test.notEqual(actual, expected, [message])
test.deepEqual(actual, expected, [message])
test.notDeepEqual(actual, expected, [message])
test.strictEqual(actual, expected, [message])
test.notStrictEqual(actual, expected, [message])
test.throws(block, [error], [message])
test.doesNotThrow(block, [error], [message])
test.ifError(value)
*/
exports.default = {
setUp: function setUp(done) {
done();
},
'expect (map) to be accepted': function expectMapToBeAccepted(test) {
test.expect(1);
// tests here
test.equal(_index3.C.char('a').map(function (a) {
return a + 'b';
}).parse(_index2.default.ofString('a'), 0).isAccepted(), true, 'should be accepted.');
test.done();
},
'expect (map) to be rejected': function expectMapToBeRejected(test) {
test.expect(1);
// tests here
test.equal(_index3.C.char('a').map(function (a) {
return a + 'b';
}).parse(_index2.default.ofString('b'), 0).isAccepted(), false, 'should be rejected.');
test.done();
},
'expect (map) to be return ab': function expectMapToBeReturnAb(test) {
test.expect(1);
// tests here
test.equal(_index3.C.char('a').map(function (a) {
return a + 'b';
}).parse(_index2.default.ofString('a'), 0).value, 'ab', 'should be accepted.');
test.done();
},
'expect (map) to be return 5x8': function expectMapToBeReturn5x8(test) {
test.expect(1);
var st = _index2.default.ofString('5x8');
var combinator = _index3.N.integer.thenLeft(_index3.C.char('x')).then(_index3.N.integer).map(function (values) {
return values[0] * values[1];
});
test.equal(combinator.parse(st).value, 40, 'should be accepted.');
test.done();
},
'expect (flatmap) to be accepted': function expectFlatmapToBeAccepted(test) {
test.expect(1);
// tests here
test.equal(_index3.C.char('a').flatmap(function () {
return _index3.F.returns('b');
}).parse(_index2.default.ofString('a'), 0).isAccepted(), true, 'should be accepted.');
test.done();
},
'expect (flatmap) to be rejected ': function expectFlatmapToBeRejected(test) {
test.expect(1);
// tests here
test.equal(_index3.C.char('a').flatmap(function () {
return _index3.F.returns('b');
}).parse(_index2.default.ofString('b'), 0).isAccepted(), false, 'should be rejected.');
test.done();
},
'expect (flatmap) to be return ab': function expectFlatmapToBeReturnAb(test) {
test.expect(1);
// tests here
test.equal(_index3.C.char('a').flatmap(function () {
return _index3.C.char('b');
}).parse(_index2.default.ofString('ab'), 0).value, 'b', 'should be accepted.');
test.done();
},
'expect (filter) to be accepted': function expectFilterToBeAccepted(test) {
test.expect(1);
// tests here
test.equal(_index3.C.char('a').filter(function (a) {
return a === 'a';
}).parse(_index2.default.ofString('a'), 0).isAccepted(), true, 'should be accepted.');
test.done();
},
'expect (filter) to be rejected': function expectFilterToBeRejected(test) {
test.expect(1);
// tests here
test.equal(_index3.C.char('a').filter(function (a) {
return a === 'b';
}).parse(_index2.default.ofString('a'), 0).isAccepted(), false, 'should be rejected.');
test.done();
},
'expect (match) to be accepted': function expectMatchToBeAccepted(test) {
test.expect(1);
// tests here
test.equal(_index3.C.char('a').match('a').parse(_index2.default.ofString('a'), 0).isAccepted(), true, 'should be accepted.');
test.done();
},
'expect (match) to be rejected': function expectMatchToBeRejected(test) {
test.expect(1);
// tests here
test.equal(_index3.C.char('a').match('b').parse(_index2.default.ofString('a'), 0).isAccepted(), false, 'should be rejected.');
test.done();
},
'expect (then) to be accepted': function expectThenToBeAccepted(test) {
test.expect(1);
// tests here
test.equal(_index3.C.char('a').then(_index3.C.char('b')).parse(_index2.default.ofString('ab'), 0).isAccepted(), true, 'should be accepted.');
test.done();
},
'expect (then) left to be rejected': function expectThenLeftToBeRejected(test) {
test.expect(1);
// tests here
test.equal(_index3.C.char('a').then(_index3.C.char('b')).parse(_index2.default.ofString('cb'), 0).isAccepted(), false, 'should be rejected.');
test.done();
},
'expect (then) right to be rejected': function expectThenRightToBeRejected(test) {
test.expect(1);
// tests here
test.equal(_index3.C.char('a').then(_index3.C.char('b')).parse(_index2.default.ofString('ac'), 0).isAccepted(), false, 'should be rejected.');
test.done();
},
'expect (then) to return [a,b]': function expectThenToReturnAB(test) {
test.expect(1);
// tests here
// const stream = stream.ofString("ab");
test.deepEqual(_index3.C.char('a').then(_index3.C.char('b')).parse(_index2.default.ofString('ab'), 0).value, ['a', 'b'], 'should be accepted.');
test.done();
},
'expect (then) to return [a,b,d]': function expectThenToReturnABD(test) {
test.expect(1);
// tests here
// const stream = stream.ofString("ab");
test.deepEqual(_index3.C.char('a').then(_index3.C.char('b').then(_index3.C.char('c').drop()).then(_index3.C.char('d'))).parse(_index2.default.ofString('abcd'), 0).value, ['a', 'b', 'd'], 'should be accepted.');
test.done();
},
'expect (then) to be associative ': function expectThenToBeAssociative(test) {
test.expect(1);
// tests here
// const stream = stream.ofString("ab");
test.deepEqual(_index3.C.char('a').then(_index3.C.char('b')).then(_index3.C.char('c').drop()).then(_index3.C.char('d')).parse(_index2.default.ofString('abcd'), 0).value, ['a', 'b', 'd'], 'should be accepted.');
test.done();
},
'expect (then) to be replaced by concat ': function expectThenToBeReplacedByConcat(test) {
test.expect(1);
// tests here
// const stream = stream.ofString("ab");
test.deepEqual(_index3.C.char('a').concat(_index3.C.char('b')).then(_index3.C.char('c').drop()).concat(_index3.C.char('d')).parse(_index2.default.ofString('abcd'), 0).value, ['a', 'b', 'd'], 'should be accepted.');
test.done();
},
'expect (thenLeft) to be accepted': function expectThenLeftToBeAccepted(test) {
test.expect(1);
// tests here
test.equal(_index3.C.char('a').thenLeft(_index3.C.char('b')).parse(_index2.default.ofString('ab'), 0).isAccepted(), true, 'should be accepted.');
test.done();
},
'expect (thenLeft) to return a': function expectThenLeftToReturnA(test) {
test.expect(1);
// tests here
test.deepEqual(_index3.C.char('a').thenLeft(_index3.C.char('b')).parse(_index2.default.ofString('ab'), 0).value, 'a', 'should be accepted.');
test.done();
},
'expect (thenLeft) to be rejected': function expectThenLeftToBeRejected(test) {
test.expect(1);
// tests here
test.equal(_index3.C.char('a').thenLeft(_index3.C.char('b')).parse(_index2.default.ofString('b'), 0).isAccepted(), false, 'should be accepted.');
test.done();
},
'expect (thenRight) to be accepted': function expectThenRightToBeAccepted(test) {
test.expect(1);
// tests here
test.equal(_index3.C.char('a').thenRight(_index3.C.char('b')).parse(_index2.default.ofString('ab'), 0).isAccepted(), true, 'should be accepted.');
test.done();
},
'expect (thenRight) to return a': function expectThenRightToReturnA(test) {
test.expect(1);
// tests here
test.deepEqual(_index3.C.char('a').thenRight(_index3.C.char('b')).parse(_index2.default.ofString('ab'), 0).value, 'b', 'should be accepted.');
test.done();
},
'expect (thenRight) to be rejected': function expectThenRightToBeRejected(test) {
test.expect(1);
// tests here
test.equal(_index3.C.char('a').thenRight(_index3.C.char('b')).parse(_index2.default.ofString('b'), 0).isAccepted(), false, 'should be rejected.');
test.done();
},
'expect (drop/then) to be accepted': function expectDropThenToBeAccepted(test) {
test.expect(1);
// tests here
test.equal(_index3.C.char('a').drop().then(_index3.C.char('b')).parse(_index2.default.ofString('ab'), 0).isAccepted(), true, 'should be accepted.');
test.done();
},
'expect (drop/then) to be return b': function expectDropThenToBeReturnB(test) {
test.expect(1);
// tests here
test.equal(_index3.C.char('a').drop().then(_index3.C.char('b')).parse(_index2.default.ofString('ab'), 0).value, 'b', 'should be accepted.');
test.done();
},
'expect (then/drop) to be accepted': function expectThenDropToBeAccepted(test) {
test.expect(1);
// tests here
test.equal(_index3.C.char('a').then(_index3.C.char('b').drop()).parse(_index2.default.ofString('ab'), 0).isAccepted(), true, 'should be accepted.');
test.done();
},
'expect (drop/then) to be return a': function expectDropThenToBeReturnA(test) {
test.expect(1);
// tests here
test.equal(_index3.C.char('a').then(_index3.C.char('b').drop()).parse(_index2.default.ofString('ab'), 0).value, 'a', 'should be accepted.');
test.done();
},
'expect (thenReturns) to be accepted': function expectThenReturnsToBeAccepted(test) {
test.expect(1);
// tests here
test.equal(_index3.C.char('a').thenReturns('b').parse(_index2.default.ofString('ab'), 0).isAccepted(), true, 'should be accepted.');
test.done();
},
'expect (thenReturns) to return b': function expectThenReturnsToReturnB(test) {
test.expect(1);
// tests here
test.equal(_index3.C.char('a').thenReturns('b').parse(_index2.default.ofString('ab'), 0).value, 'b', 'should be accepted.');
test.done();
},
'expect (thenReturns) not to eat char': function expectThenReturnsNotToEatChar(test) {
test.expect(1);
// tests here
test.deepEqual(_index3.C.char('a').thenReturns('X').then(_index3.C.char('b')).parse(_index2.default.ofString('ab'), 0).value, ['X', 'b'], 'should be accepted.');
test.done();
},
'expect (thenReturns) to be rejected': function expectThenReturnsToBeRejected(test) {
test.expect(1);
// tests here
test.equal(_index3.C.char('a').thenReturns('b').parse(_index2.default.ofString('b'), 0).isAccepted(), false, 'should be rejected.');
test.done();
},
'expect (or) to be accepted': function expectOrToBeAccepted(test) {
test.expect(1);
// tests here
test.equal(_index3.C.char('a').or(_index3.C.char('b')).parse(_index2.default.ofString('a'), 0).isAccepted(), true, 'should be accepted.');
test.done();
},
'expect (or) to be rejected': function expectOrToBeRejected(test) {
test.expect(1);
// tests here
test.equal(_index3.C.char('a').or(_index3.C.char('b')).parse(_index2.default.ofString('c'), 0).isAccepted(), false, 'should be rejected.');
test.done();
},
'expect (or) LL(1) to be rejected': function expectOrLL1ToBeRejected(test) {
test.expect(1);
// tests here
test.equal(_index3.C.char('a').then(_index3.C.char('b')).or(_index3.C.char('a')).parse(_index2.default.ofString('ac'), 0).isAccepted(), false, 'should be rejected.');
test.done();
},
'expect (or) to return a': function expectOrToReturnA(test) {
test.expect(1);
// tests here
test.deepEqual(_index3.C.char('a').or(_index3.C.char('b')).parse(_index2.default.ofString('a'), 0).value, 'a', 'should be accepted.');
test.done();
},
'expect (or) to return b': function expectOrToReturnB(test) {
test.expect(1);
// tests here
test.deepEqual(_index3.C.char('a').or(_index3.C.char('b')).parse(_index2.default.ofString('b'), 0).value, 'b', 'should be accepted.');
test.done();
},
'expect (then.or) left to be rejected': function expectThenOrLeftToBeRejected(test) {
test.expect(1);
// tests here
test.equal(_index3.C.char('a').then(_index3.C.char('b').or(_index3.C.char('c'))).parse(_index2.default.ofString('ad'), 0).isAccepted(), false, 'should be rejected.');
test.done();
},
'expect (then.or) left to be consumed': function expectThenOrLeftToBeConsumed(test) {
test.expect(1);
// tests here
test.equal(_index3.C.char('a').then(_index3.C.char('b').or(_index3.C.char('c'))).parse(_index2.default.ofString('ad'), 0).consumed, true, 'should be consumed.');
test.done();
},
'expect (opt) some to accepted': function expectOptSomeToAccepted(test) {
test.expect(1);
// tests here
test.deepEqual(_index3.C.char('a').opt().parse(_index2.default.ofString('a'), 0).isAccepted(), true, 'should be accepted.');
test.done();
},
'expect (opt) some to return some a': function expectOptSomeToReturnSomeA(test) {
test.expect(1);
// tests here
test.deepEqual(_index3.C.char('a').opt().parse(_index2.default.ofString('a'), 0).value.get(), 'a', 'should be a.');
test.done();
},
'expect (opt) none to accepted': function expectOptNoneToAccepted(test) {
test.expect(1);
// tests here
test.deepEqual(_index3.C.char('a').opt().parse(_index2.default.ofString('b'), 0).isAccepted(), true, 'should be accepted.');
test.done();
},
'expect (opt) none to return none': function expectOptNoneToReturnNone(test) {
test.expect(1);
// tests here
test.deepEqual(_index3.C.char('a').opt().parse(_index2.default.ofString('b'), 0).value.isPresent(), false, 'should be accepted but none.');
test.done();
},
'expect (rep) to accepted': function expectRepToAccepted(test) {
test.expect(1);
// tests here
test.deepEqual(_index3.C.char('a').rep().parse(_index2.default.ofString('a'), 0).isAccepted(), true, 'should be accepted.');
test.done();
},
'expect (rep) to rejected': function expectRepToRejected(test) {
test.expect(1);
// tests here
test.deepEqual(_index3.C.char('a').rep().parse(_index2.default.ofString('b'), 0).isAccepted(), false, 'should be rejected.');
test.done();
},
'expect (rep) mutiple to accepted': function expectRepMutipleToAccepted(test) {
test.expect(1);
// tests here
test.deepEqual(_index3.C.char('a').rep().parse(_index2.default.ofString('aaaabbb'), 0).isAccepted(), true, 'should be accepted.');
test.done();
},
'expect (rep) mutiple to return [a,a,a,a]': function expectRepMutipleToReturnAAAA(test) {
test.expect(1);
// tests here
test.deepEqual(_index3.C.char('a').rep().parse(_index2.default.ofString('aaaabbb'), 0).value.array(), ['a', 'a', 'a', 'a'], 'should be accepted.');
test.done();
},
'expect (optrep) to accepted': function expectOptrepToAccepted(test) {
test.expect(1);
// tests here
test.deepEqual(_index3.C.char('a').optrep().parse(_index2.default.ofString('a'), 0).isAccepted(), true, 'should be accepted.');
test.done();
},
'expect (optrep) none to accepted': function expectOptrepNoneToAccepted(test) {
test.expect(1);
// tests here
test.deepEqual(_index3.C.char('a').optrep().parse(_index2.default.ofString('b'), 0).isAccepted(), true, 'should be rejected.');
test.done();
},
'expect (optrep) mutiple to accepted': function expectOptrepMutipleToAccepted(test) {
test.expect(1);
// tests here
test.deepEqual(_index3.C.char('a').optrep().parse(_index2.default.ofString('aaaabbb'), 0).isAccepted(), true, 'should be accepted.');
test.done();
},
'expect (optrep) mutiple to return some [a,a,a,a]': function expectOptrepMutipleToReturnSomeAAAA(test) {
test.expect(1);
// tests here
test.deepEqual(_index3.C.char('a').optrep().parse(_index2.default.ofString('aaaabbb'), 0).value.array(), ['a', 'a', 'a', 'a'], 'should be accepted.');
test.done();
},
'expect (optrep) to return none': function expectOptrepToReturnNone(test) {
test.expect(1);
// tests here
test.deepEqual(_index3.C.char('a').optrep().parse(_index2.default.ofString('bbb'), 0).value.array(), [], 'should be accepted.');
test.done();
},
'expect (optrep) to return [b,b,b]': function expectOptrepToReturnBBB(test) {
test.expect(1);
// tests here
test.deepEqual(_index3.C.notChar('a').optrep().parse(_index2.default.ofString('bbba'), 0).value.array(), ['b', 'b', 'b'], 'should be accepted.');
test.done();
},
'expect two (optrep) to return [[b,b,b],[a]]': function expectTwoOptrepToReturnBBBA(test) {
test.expect(1);
// tests here
test.deepEqual(_index3.C.char('b').optrep().then(_index3.C.char('a').optrep()).map(function (r) {
return r.map(function (e) {
return e.array();
});
}).parse(_index2.default.ofString('bbba'), 0).value, [['b', 'b', 'b'], ['a']], 'should be accepted.');
test.done();
},
'expect two (rep) to return [[b,b,b],[a]]': function expectTwoRepToReturnBBBA(test) {
test.expect(1);
// tests here
test.deepEqual(_index3.C.char('b').rep().then(_index3.C.char('a').rep()).map(function (r) {
return r.map(function (e) {
return e.array();
});
}).parse(_index2.default.ofString('bbba'), 0).value, [['b', 'b', 'b'], ['a']], 'should be accepted.');
test.done();
},
'expect debug() to make side effect': function expectDebugToMakeSideEffect(test) {
test.expect(1);
// tests here
var original = console.log;
var sideEffect = false;
console.log = function () {
sideEffect = true;
};
// side effect action
_index3.C.char('a').debug('found').optrep().parse(_index2.default.ofString('aaa'), 0);
console.log = original;
test.deepEqual(sideEffect, true, 'should make side effect.');
test.done();
},
'expect debug(param, false) to make side effect': function expectDebugParamFalseToMakeSideEffect(test) {
test.expect(1);
// tests here
var original = console.log;
var sideEffect = false;
console.log = function () {
sideEffect = true;
};
// side effect action : will hide the details
_index3.C.char('a').debug('found', false).optrep().parse(_index2.default.ofString('aaa'), 0);
console.log = original;
test.deepEqual(sideEffect, true, 'should make side effect.');
test.done();
},
'expect (debug) to not make side effect': function expectDebugToNotMakeSideEffect(test) {
test.expect(1);
// tests here
var original = console.log;
var sideEffect = false;
console.log = function () {
sideEffect = true;
};
// side effect action
_index3.C.char('a').debug('found').optrep().parse(_index2.default.ofString('xxxx'), 0);
console.log = original;
test.deepEqual(sideEffect, false, 'should make side effect.');
test.done();
}
};
//# sourceMappingURL=parser_core_test.js.map