From 7c25967e5edf2dce8b6f6eb7c4fa2b875ce611a6 Mon Sep 17 00:00:00 2001 From: Rafael Caricio Date: Fri, 5 Sep 2014 10:28:40 +0200 Subject: [PATCH] Add error function --- gradient-parser.js | 17 ++++++++++++++++- spec/gradient-parser.spec.js | 1 - 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/gradient-parser.js b/gradient-parser.js index 20b2a1a..36fae2d 100644 --- a/gradient-parser.js +++ b/gradient-parser.js @@ -27,6 +27,14 @@ var GradientParser = module.exports = (function() { comma: /^,/ }; + function error(input, cursor, msg) { + var err = new Error(input + ':' + cursor + ': ' + msg); + err.position = cursor; + err.message = msg; + err.source = input; + throw err; + } + function Constructor(input) { this.input = input; this.cursor = 0; @@ -38,7 +46,14 @@ var GradientParser = module.exports = (function() { if (input) { this.input = input; } - return this.listDefinitions(); + + var ast = this.listDefinitions(); + + if (this.input) { + error(input, cursor, 'Invalid input not EOF'); + } + + return ast; }; def.listDefinitions = function() { diff --git a/spec/gradient-parser.spec.js b/spec/gradient-parser.spec.js index 794720b..b139168 100644 --- a/spec/gradient-parser.spec.js +++ b/spec/gradient-parser.spec.js @@ -42,7 +42,6 @@ describe('gradient-parser.js', function () { beforeEach(function() { var parser = new GradientParser(); ast = parser.parse('linear-gradient(to right bottom, red, blue)'); - console.log(ast); }); it('should get the gradient type', function () {