Permalink
Browse files

fix list helper to allow empty lists

  • Loading branch information...
1 parent e019efc commit 3d7d2b8226658a13ba4b9eb636e80c6f5584ca97 @Floby committed Jul 1, 2011
Showing with 18 additions and 2 deletions.
  1. +18 −2 lib/Parser.js
View
@@ -187,9 +187,25 @@ Parser.expect = function expect(expected) {
* @param end the token that should end the list (be it ')' or '\n' or whatever)
* this must be specified so that the handler for separators
* doesn't treat it as an unexpected token
+ * @param forbidEmpty forbid empty lists, defaults to false
* @return your handler, ready to use
*/
-Parser.list = function list(separator, handler, end) {
+Parser.list = function list(separator, handler, end, forbidEmpty) {
+ function checkIfEmpty(token, type, next) {
+ if(type !== end) {
+ // the list is not empty
+ next(handler, expectSeparator);
+ return true;
+ } else {
+ // the list is empty
+ if(forbidEmpty === true) {
+ throw new SyntaxError('unexpected '+ token+ '. empty lists not allowed');
+ }
+ else {
+ return true;
+ }
+ }
+ }
function expectSeparator(token, type, next) {
if(type === separator) {
// if we have our separator
@@ -205,7 +221,7 @@ Parser.list = function list(separator, handler, end) {
throw new SyntaxError("unexpected token "+token+". expecting "+separator);
}
return function listExpander(token, type, next) {
- next(handler, expectSeparator);
+ next(checkIfEmpty);
return true;
}
}

0 comments on commit 3d7d2b8

Please sign in to comment.