Another ill-fated attempt at filters, this breaks many tests and does

not mesh with the fiendishly tricky filterset logic.
This commit is contained in:
Tom MacWright 2012-06-27 16:41:13 -04:00
parent d57006dd97
commit 880b0e8f0a
4 changed files with 28 additions and 49 deletions

View File

@ -655,10 +655,17 @@ carto.Parser = function Parser(env) {
save(); save();
var key, op, val; var key, op, val;
if (! $('[')) return; if (! $('[')) return;
if (key = $(/^[a-zA-Z0-9\-_]+/) || $(this.entities.quoted) || $(this.entities.variable)) { if (key = $(/^[a-zA-Z0-9\-_]+/) ||
$(this.expression)) {
if ((op = $(this.entities.comparison)) && if ((op = $(this.entities.comparison)) &&
(val = $(this.entities.quoted) || $(this.entities.variable) || $(/^[\w\-\.]+/))) { (val = $(this.expression))) {
if (! $(']')) return; if (! $(']')) return;
// Support legacy-style non-wrapped field names,
// like [NAME = 5]
if (!key.is) {
key = new tree.Field(key);
}
return new tree.Filter(key, op, val, memo, env.filename); return new tree.Filter(key, op, val, memo, env.filename);
} }
} }

View File

@ -9,7 +9,10 @@ tree.Field.prototype = {
toString: function() { toString: function() {
return '[' + this.value + ']'; return '[' + this.value + ']';
}, },
'eval': function() { 'eval': function(env) {
if (this.value.eval) {
this.value = this.value.eval(env);
}
return this; return this;
} }
}; };

View File

@ -1,27 +1,12 @@
(function(tree) { (function(tree) {
tree.Filter = function Filter(key, op, val, index, filename) { tree.Filter = function Filter(key, op, val, index, filename) {
if (key.is) { // TODO: figure out what the f
this.key = key.value; this.key = key;
this._key = key;
} else {
this.key = key;
}
this.op = op; this.op = op;
this.index = index; this.index = index;
this.filename = filename; this.filename = filename;
this.val = val;
if (val.is) {
this.val = val.value;
this._val = val;
} else {
this.val = val;
}
if (ops[this.op][1] == 'numeric') {
this.val = 1 * this.val;
}
this.id = this.key + this.op + this.val; this.id = this.key + this.op + this.val;
}; };
@ -39,23 +24,12 @@ var ops = {
}; };
tree.Filter.prototype.toXML = function(env) { tree.Filter.prototype.toXML = function(env) {
if (this.val.eval) this._val = this.val.eval(env); return (this.key.eval(env).toString(true)) +
if (this.key.eval) this._key = this.key.eval(env); ops[this.op][0] + '' +
if (this._key) var key = this._key.toString(false); (this.val.eval(env).toString(true)) +
if (this._val) var val = this._val.toString(this._val.is == 'string'); // This section is only for operators
// which can wrap, like .match
if ( (ops[this.op][2] || '');
(ops[this.op][1] == 'numeric' && isNaN(this.val)) ||
(ops[this.op][1] == 'string' && (val || this.val)[0] != "'")
) {
env.error({
message: 'Cannot use operator "' + this.op + '" with value ' + this.val,
index: this.index,
filename: this.filename
});
}
return '[' + (key || this.key) + ']' + ops[this.op][0] + '' + (val || this.val) + (ops[this.op][2] || '');
}; };
tree.Filter.prototype.toString = function() { tree.Filter.prototype.toString = function() {

View File

@ -43,7 +43,7 @@ Object.defineProperty(tree.Filterset.prototype, 'clone', {
Object.defineProperty(tree.Filterset.prototype, 'cloneWith', { Object.defineProperty(tree.Filterset.prototype, 'cloneWith', {
enumerable: false, enumerable: false,
value: function(other) { value: function(other) {
var additions; var additions = [];
for (var id in other) { for (var id in other) {
var status = this.addable(other[id]); var status = this.addable(other[id]);
if (status === false) { if (status === false) {
@ -51,14 +51,13 @@ Object.defineProperty(tree.Filterset.prototype, 'cloneWith', {
} }
if (status === true) { if (status === true) {
// Adding the filter will override another value. // Adding the filter will override another value.
if (!additions) additions = [];
additions.push(other[id]); additions.push(other[id]);
} }
} }
// Adding the other filters doesn't make this filterset invalid, but it // Adding the other filters doesn't make this filterset invalid, but it
// doesn't add anything to it either. // doesn't add anything to it either.
if (!additions) return null; if (!additions.length) return null;
// We can successfully add all filters. Now clone the filterset and add the // We can successfully add all filters. Now clone the filterset and add the
// new rules. // new rules.
@ -79,13 +78,11 @@ Object.defineProperty(tree.Filterset.prototype, 'cloneWith', {
} }
}); });
/** // Returns true when the new filter can be added, false otherwise.
* Returns true when the new filter can be added, false otherwise.
*/
Object.defineProperty(tree.Filterset.prototype, 'addable', { Object.defineProperty(tree.Filterset.prototype, 'addable', {
enumerable: false, enumerable: false,
value: function(filter) { value: function(filter) {
var key = filter.key, value = filter.val; var key = filter.key.toString(), value = filter.val;
switch (filter.op) { switch (filter.op) {
case '=': case '=':
@ -141,13 +138,11 @@ Object.defineProperty(tree.Filterset.prototype, 'addable', {
} }
}); });
/** // Only call this function for filters that have been cleared by .addable().
* Only call this function for filters that have been cleared by .addable().
*/
Object.defineProperty(tree.Filterset.prototype, 'add', { Object.defineProperty(tree.Filterset.prototype, 'add', {
enumerable: false, enumerable: false,
value: function(filter) { value: function(filter) {
var key = filter.key; var key = filter.key.toString();
switch (filter.op) { switch (filter.op) {
case '=': case '=':