Refactor and simplify

This commit is contained in:
Rafael Caricio 2014-09-05 18:56:36 +02:00
parent 06521bf0ad
commit 11398b2c37
2 changed files with 56 additions and 62 deletions

View file

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
var GradientParser = module.exports = (function() { module.exports = (function() {
var types = { var types = {
gradients: [ gradients: [
@ -27,7 +27,10 @@ var GradientParser = module.exports = (function() {
comma: /^,/ comma: /^,/
}; };
function error(input, cursor, msg) { var input = '',
cursor = 0;
function error(msg) {
var err = new Error(input + ':' + cursor + ': ' + msg); var err = new Error(input + ':' + cursor + ': ' + msg);
err.position = cursor; err.position = cursor;
err.message = msg; err.message = msg;
@ -35,37 +38,26 @@ var GradientParser = module.exports = (function() {
throw err; throw err;
} }
function Constructor(input) { function getAST() {
this.input = input; var ast = listDefinitions();
this.cursor = 0;
}
var def = Constructor.prototype; if (input.length > 0) {
def.parse = function(input) {
if (input) {
this.input = input;
}
var ast = this.listDefinitions();
if (this.input) {
error(input, cursor, 'Invalid input not EOF'); error(input, cursor, 'Invalid input not EOF');
} }
return ast; return ast;
}; };
def.listDefinitions = function() { function listDefinitions() {
var definitions = [], var definitions = [],
definition = this.definition(); currentDefinition = definition();
if (definition) { if (currentDefinition) {
definitions.push(definition); definitions.push(currentDefinition);
while (this.scan(tokens.comma)) { while (scan(tokens.comma)) {
definition = this.definition(); currentDefinition = definition();
if (definition) { if (currentDefinition) {
definitions.push(definition); definitions.push(currentDefinition);
} else { } else {
// throw error // throw error
} }
@ -73,24 +65,24 @@ var GradientParser = module.exports = (function() {
} }
return definitions; return definitions;
}; }
def.definition = function() { function definition() {
return this.linearGradient(); return linearGradient();
}; }
def.linearGradient = function() { function linearGradient() {
var captures = this.scan(tokens.linearGradient), var captures = scan(tokens.linearGradient),
orientation, orientation,
colorStops; colorStops;
if (captures) { if (captures) {
this.scan(tokens.startCall); scan(tokens.startCall);
orientation = this.orientation(); orientation = matchOrientation();
this.scan(tokens.comma); scan(tokens.comma);
colorStops = this.colorStops(); colorStops = matchColorStops();
this.scan(tokens.endCall); scan(tokens.endCall);
return { return {
type: 'linear-gradient', type: 'linear-gradient',
@ -98,31 +90,31 @@ var GradientParser = module.exports = (function() {
colorStops: colorStops colorStops: colorStops
}; };
} }
}; }
def.orientation = function() { function matchOrientation() {
return this.sideOrCorner(); return sideOrCorner();
}; }
def.sideOrCorner = function() { function sideOrCorner() {
var captures = this.scan(tokens.sideOrCorner); var captures = scan(tokens.sideOrCorner);
if (captures) { if (captures) {
return { return {
type: 'directional', type: 'directional',
value: captures[1].toLowerCase() value: captures[1].toLowerCase()
}; };
} }
}; }
def.colorStops = function() { function matchColorStops() {
var literalColors = /^([a-zA-Z]+)/, var literalColors = /^([a-zA-Z]+)/,
captures = this.scan(literalColors), captures = scan(literalColors),
colors = []; colors = [];
if (captures) { if (captures) {
colors.push(captures[0].toLowerCase()); colors.push(captures[0].toLowerCase());
while (this.scan(tokens.comma)) { while (scan(tokens.comma)) {
captures = this.scan(literalColors); captures = scan(literalColors);
if (captures) { if (captures) {
colors.push(captures[0].toLowerCase()); colors.push(captures[0].toLowerCase());
} else { } else {
@ -132,29 +124,32 @@ var GradientParser = module.exports = (function() {
} }
return colors; return colors;
}; }
def.scan = function(regexp) { function scan(regexp) {
var captures, var captures,
blankCaptures; blankCaptures;
blankCaptures = /^[\n\r\t\s]+/.exec(this.input); blankCaptures = /^[\n\r\t\s]+/.exec(input);
if (blankCaptures) { if (blankCaptures) {
this.consume(blankCaptures[0].length); consume(blankCaptures[0].length);
} }
captures = regexp.exec(this.input); captures = regexp.exec(input);
if (captures) { if (captures) {
this.consume(captures[0].length); consume(captures[0].length);
} }
return captures; return captures;
}; }
def.consume = function(size) { function consume(size) {
this.cursor += size; cursor += size;
this.input = this.input.substr(size); input = input.substr(size);
}; }
return Constructor; return function(code) {
input = code.toString();
return getAST();
};
})(); })();

View file

@ -1,7 +1,7 @@
'use strict'; 'use strict';
var expect = require('expect.js'); var expect = require('expect.js');
var GradientParser = require('gradient-parser'); var gradientParser = require('gradient-parser');
// [ // [
// { // {
@ -33,15 +33,14 @@ var GradientParser = require('gradient-parser');
describe('gradient-parser.js', function () { describe('gradient-parser.js', function () {
it('should exist', function () { it('should exist', function () {
expect(typeof GradientParser).to.equal('function'); expect(typeof gradientParser).to.equal('function');
}); });
describe('when parsing a simple definition', function(){ describe('when parsing a simple definition', function(){
var ast; var ast;
beforeEach(function() { beforeEach(function() {
var parser = new GradientParser(); ast = gradientParser('linear-gradient(to right bottom, red, blue)');
ast = parser.parse('linear-gradient(to right bottom, red, blue)');
}); });
it('should get the gradient type', function () { it('should get the gradient type', function () {