start
= cond_or
cond_or
= left:cond_and [ ]+ "OR" [ ]+ right:cond_or {
var temp = {}, i;
temp['$or'] = [];
if(left.hasOwnProperty('$and')){temp['$or'].push(left);}
else{
if(left.hasOwnProperty('$or')){left = left['$or']}
for(i in left){
temp['$or'].push(left[i]);
}}
if(right.hasOwnProperty('$and')){temp['$or'].push(right);}
else{
if(right.hasOwnProperty('$or')){right = right['$or']}
for(i in right){
temp['$or'].push(right[i]);
}}
return temp;
}
/ cond_and
cond_and
= left:primary [ ]+ "AND" [ ]+ right:cond_and {
var temp = {}, i;
temp['$and'] = [];
if(left.hasOwnProperty('$or')){temp['$and'].push(left);}
else{
if(left.hasOwnProperty('$and')){left = left['$and']}
for(i in left){
temp['$and'].push(left[i]);
}}
if(right.hasOwnProperty('$or')){temp['$and'].push(right);}
else{
if(right.hasOwnProperty('$and')){right = right['$and']}
for(i in right){
temp['$and'].push(right[i]);
}}
return temp;
}
/ primary
primary
= comparison
/ "(" expr:cond_or ")" { return expr; }
comparison = attr:attribute [ ]* op:op [ ]* val:value {
var t = {};
attr = attr.toUpperCase();
t[attr] = {};
t[attr][op] = val;
return [t];}
attribute = alpha:[0-9a-zA-Z]+ {return alpha.join("");}
value
= str1
/ str2
/ num
str1 = '"' alpha:[a-zA-Z0-9 !#$%*()-_+=;:\[\]<>,./?]+ '"' {return alpha.join("");}
str2 = "'" alpha:[a-zA-Z0-9 !#$%*()-_+=;:\[\]<>,./?]+ "'" {return alpha.join("");}
num = int:[0-9]+ {return parseInt(int.join(""), 10)}
op = operator:[<=>]+ {
operator = operator.join("");
switch(operator){
case '<':
return '$lt';
case '<=':
return '$lte';
case '=':
return '$eq';
case '<>':
return '$ne';
case '>=':
return '$gte';
case '>':
return '$gt';
default:
return '$eq';
}
}
start
= multiple
multiple
= left:attribute [, ]* right:multiple { return left.concat(right); }
/ attribute
attribute
= [ ]* attr:[0-9a-zA-Z]+ [ ]*{ return [attr.join("")]; }
start
= multiple
multiple
= left:rename [, ]+ right:multiple {
left.push.apply(left, right);
return left}
/rename
rename
= left:value [ ]+ right:attribute { return [[left, right]]; }
/ value
value
= integer
/ attribute
integer = digits:[0-9]+ { return parseInt(digits.join(""), 10); }
attribute = alpha:[a-zA-Z0-9]+ {return alpha.join("");}