Skip to content

Commit

Permalink
#18: remove var-args from , . use arrays instead
Browse files Browse the repository at this point in the history
  • Loading branch information
mattfenwick committed Sep 25, 2016
1 parent 39da691 commit 5c7fbc1
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 32 deletions.
18 changes: 8 additions & 10 deletions lib/combinators.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,10 @@ function many1(parser) {
return check(function(x) {return x.length > 0;}, many0(parser));
}

function seq() {
function seq(parsers) {
/*
[Parser e s (m t) a] -> Parser e s (m t) [a]
*/
var parsers = F.getArgs(arguments, 0);
parsers.forEach(checkParser.bind(null, 'seq'));
function f(xs, s) {
var vals = [],
Expand Down Expand Up @@ -246,7 +245,7 @@ function appP(p) {
function g(args) {
return f.apply(undefined, args);
}
return fmap(g, seq.apply(undefined, parsers));
return fmap(g, seq(parsers));
});
}

Expand Down Expand Up @@ -284,7 +283,7 @@ function lookahead(parser) {
return bind(get, function(xs) {
return bind(getState, function(s) {
return seq2L(parser,
seq(put(xs), putState(s)));
seq([put(xs), putState(s)]));
});
});
}
Expand All @@ -307,11 +306,10 @@ function not0(parser) {
return new Parser(f);
}

function alt() {
function alt(parsers) {
/*
[Parser e s (m t) a] -> Parser e s (m t) a
*/
var parsers = F.getArgs(arguments, 0);
parsers.forEach(checkParser.bind(null, 'alt'));
function f(xs, s) {
var r = M.zero;
Expand All @@ -336,7 +334,7 @@ function optional(parser, default_v) {
default_v = null;
}
checkParser('optional', parser);
return alt(parser, pure(default_v));
return alt([parser, pure(default_v)]);
}

function repeat(count, parser) {
Expand All @@ -348,15 +346,15 @@ function repeat(count, parser) {
for (var i = 0; i < count; i++) {
ps.push(parser);
}
return seq.apply(null, ps);
return seq(ps);
}

function commit(e, parser) {
/*
e -> Parser e s (m t) a -> Parser e s (m t) a
*/
checkParser('commit', parser);
return alt(parser, error(e));
return alt([parser, error(e)]);
}

function sepBy1(parser, separator) {
Expand Down Expand Up @@ -431,7 +429,7 @@ function Itemizer(f) {
for(var i = 0; i < elems.length; i++) { // have to do this b/c strings don't have a `map` method
ps.push(literal(elems[i]));
}
var matcher = seq.apply(undefined, ps);
var matcher = seq(ps);
return seq2R(matcher, pure(elems));
}

Expand Down
2 changes: 1 addition & 1 deletion lib/cst.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function node(name /* _pairs_: 0+ 2-element arrays */) {
return addError(name,
app(action,
getState,
seq.apply(undefined, pairs.map(f)),
seq(pairs.map(f)),
getState));
}

Expand Down
8 changes: 4 additions & 4 deletions lib/operators-alternates.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function chainR(operator, parser) {
return C.pure(g(x));
});
});
return C.alt(ops, parser);
return C.alt([ops, parser]);
}

function chainR2(operator, parser) {
Expand Down Expand Up @@ -70,7 +70,7 @@ function chainL(operator, parser) {
};
}
var rest = C.error('undefined');
rest.parse = C.alt(C.app(f, operator, parser, rest), C.pure(id)).parse;
rest.parse = C.alt([C.app(f, operator, parser, rest), C.pure(id)]).parse;
return C.app(flipApply, parser, rest);
}

Expand Down Expand Up @@ -110,7 +110,7 @@ function prefix(operator, parser) {
var recurse = C.bind(operator, function(f) {
return C.fmap(f, prefix(operator, parser));
});
return C.alt(recurse, parser);
return C.alt([recurse, parser]);
}

function prefix2(operator, parser) {
Expand All @@ -136,7 +136,7 @@ function postfix(operator, parser) {
};
}
var rest = C.error('undefined');
rest.parse = C.alt(C.app(f, operator, rest), C.pure(id)).parse;
rest.parse = C.alt([C.app(f, operator, rest), C.pure(id)]).parse;
return C.app(flipApply, parser, rest);
}

Expand Down
34 changes: 17 additions & 17 deletions test/combinators.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,27 +171,27 @@ testModule('combinators', function() {
r1 = good(3, 'abc', null),
r3 = M.zero,
r4 = M.error('oops');
deepEqual(C.alt(g1, g2).parse('abc', null), r1);
deepEqual(C.alt(g1, b).parse('abc', null), r1);
deepEqual(C.alt(g1, e).parse('abc', null), r1);
deepEqual(C.alt(b, g1).parse('abc', null), r1);
deepEqual(C.alt(b, b).parse('abc', null), r3);
deepEqual(C.alt(b, e).parse('abc', null), r4);
deepEqual(C.alt(e, g1).parse('abc', null), r4);
deepEqual(C.alt(e, b).parse('abc', null), r4);
deepEqual(C.alt(e, e2).parse('abc', null), r4);
deepEqual(C.alt([g1, g2]).parse('abc', null), r1);
deepEqual(C.alt([g1, b]).parse('abc', null), r1);
deepEqual(C.alt([g1, e]).parse('abc', null), r1);
deepEqual(C.alt([b, g1]).parse('abc', null), r1);
deepEqual(C.alt([b, b]).parse('abc', null), r3);
deepEqual(C.alt([b, e]).parse('abc', null), r4);
deepEqual(C.alt([e, g1]).parse('abc', null), r4);
deepEqual(C.alt([e, b]).parse('abc', null), r4);
deepEqual(C.alt([e, e2]).parse('abc', null), r4);
});

test("AltCornerCases", function() {
deepEqual(C.alt().parse([1,2,3], null),
deepEqual(C.alt([]).parse([1,2,3], null),
M.zero);
deepEqual(C.alt(C.pure('h')).parse([1,2,3], null),
deepEqual(C.alt([C.pure('h')]).parse([1,2,3], null),
good('h', [1,2,3], null));
deepEqual(C.alt(C.error('oops')).parse([1,2,3], null),
deepEqual(C.alt([C.error('oops')]).parse([1,2,3], null),
M.error('oops'));
deepEqual(C.alt(C.zero).parse([1,2,3], null),
deepEqual(C.alt([C.zero]).parse([1,2,3], null),
M.zero);
var p1 = C.alt(C.zero, iz1.literal(1), iz1.literal(2), C.error('d'));
var p1 = C.alt([C.zero, iz1.literal(1), iz1.literal(2), C.error('d')]);
deepEqual(p1.parse([1,3,4], null), good(1, [3,4], null));
deepEqual(p1.parse([2,3,4], null), good(2, [3,4], null));
deepEqual(p1.parse([3,3,4], null), M.error('d'));
Expand Down Expand Up @@ -261,7 +261,7 @@ testModule('combinators', function() {
});

test("Seq", function() {
var val = C.seq(iz1.item, iz1.literal(2), iz1.literal(8));
var val = C.seq([iz1.item, iz1.literal(2), iz1.literal(8)]);
deepEqual(val.parse([3,2,4], {}), M.zero);
deepEqual(val.parse([3,2,8,16], {}), good([3,2,8], [16], {}));
});
Expand Down Expand Up @@ -410,7 +410,7 @@ testModule('combinators', function() {
test("when using function where Parser is expected, the 'actual' key appears in error message", function() {
var p = iz1.literal(2);
try {
C.seq(function() {}, p);
C.seq([function() {}, p]);
deepEqual(0, 1, "expected exception");
} catch(e) {
deepEqual(JSON.parse(e.message).actual, 'function');
Expand All @@ -419,7 +419,7 @@ testModule('combinators', function() {

test("when using non-function where Parser is expected, the 'actual' key appears in error message", function() {
try {
C.seq(3);
C.seq([3]);
deepEqual(0, 1, "expected exception");
} catch(e) {
deepEqual(JSON.parse(e.message).actual, '3');
Expand Down

0 comments on commit 5c7fbc1

Please sign in to comment.