Permalink
Browse files

add expect and list primitives

  • Loading branch information...
Floby committed Apr 13, 2011
1 parent 6d7951d commit e4461bbbd388c1ecdace2949c5675bc3054a0470
Showing with 27 additions and 1 deletion.
  1. +27 −1 lib/Parser.js
View
@@ -90,11 +90,37 @@ Parser.prototype.end = function end(data, encoding) {
module.exports = Parser;
// common primitives
-Parse.checkType(type_to_check) {
+Parser.checkType = function checkType(type_to_check) {
return function checkType(token, type, next) {
assert.equal(type, type_to_check,
"unexpect token "+token+"("+type+"). expected "+type_to_check);
return true; // expand this
}
}
+Parser.expect = function expect(expected) {
+ return function expecting(token, type, next) {
+ assert.equal(type, expected,
+ "unexpected token "+token+". expecting "+expected);
+ }
+}
+Parser.list = function list(separator, handler, end) {
+ function expectSeparator(token, type, next) {
+ if(type === separator) {
+ // if we have or separator
+ next(handler, expectSeparator);
+ return;
+ }
+ if(type === end) {
+ // if we reached the token ending the list
+ // then our job is over
+ return true;
+ }
+ // if it's something else then it is an unexpected token
+ throw new SyntaxError("unexpected token "+token+". expecting "+separator);
+ }
+ return function listExpander(token, type, next) {
+ next(handler, expectSeparator);
+ return true;
+ }
+}

0 comments on commit e4461bb

Please sign in to comment.