Skip to content

Commit

Permalink
adds #many plus unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
animatedlew committed Mar 25, 2016
1 parent 35021fe commit 70ed809
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const mocha = require('gulp-mocha');
const shell = require('gulp-shell');

gulp.task('test', ['build'], () => gulp.src('test/**/*.js', { read: false }).pipe(mocha({ reporter: 'spec' })));
gulp.task('build', () => gulp.src('./parser.ts').pipe(shell(['tsc -p .'])));
gulp.task('run', ['build'], () => gulp.src('./parser.js').pipe(shell(['node <%= file.path %>'])));
gulp.task('build', () => gulp.src('./src/parser.ts').pipe(shell(['tsc -p .'])));
gulp.task('run', ['build'], () => gulp.src('./src/parser.js').pipe(shell(['node <%= file.path %>'])));
gulp.task('default', ['run', 'test'], () => {});

3 changes: 3 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,8 @@ export default class Parser<T> {
Parser.unit("")
);
}
static many(p: Parser<string>): Parser<string> {
return Parser.plus(Parser.bind(p, x => Parser.bind(Parser.many(p), xs => Parser.unit(x + xs))), Parser.unit(""));
}
apply = (input: string): Result<T>[] => this.f(input);
}
6 changes: 6 additions & 0 deletions test/parserSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,10 @@ describe("Parser", () => {
expect(Parser.string(phrase).apply("dog")).to.have.deep.property("0.lexeme", "dog");
});
});
describe("#many", () => {
it("should parse a sequence of items", () => {
expect(Parser.many(Parser.char("a")).apply("aaaaabbb")).to.have.deep.property("0.lexeme", "aaaaa");
expect(Parser.many(Parser.char("b")).apply("bbaaa")).to.have.deep.property("0.lexeme", "bb");
});
});
});

0 comments on commit 70ed809

Please sign in to comment.