Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 52 additions & 14 deletions index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 51 additions & 13 deletions rule.jison
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
%start expressions
%parse-param data
%parse-param validator
%parse-param grammar

%% /* language grammar */

Expand All @@ -70,27 +71,27 @@ expressions

e
: e OR e
{$$ = $1 || $3;}
{$$ = !grammar ? ($1 || $3) : [$1, 'or', $3];}
| e AND e
{$$ = $1 && $3;}
{$$ = !grammar ? ($1 && $3) : [$1, 'and', $3];}
| NOT e
{$$ = !$2;}
{$$ = !grammar ? !$2 : [$2, 'not', null];}
| term '=' term
{$$ = $1 === $3;}
{$$ = !grammar ? ($1 === $3) : [$1, 'eq', $3];}
| term '<>' term
{$$ = $1 !== $3;}
{$$ = !grammar ? ($1 !== $3) : [$1, 'neq', $3];}
| term IN term
{$$ = validator ? validator.in($1, $3) : $3.indexOf($1) !== -1}
{$$ = validator ? validator.in($1, $3) : (!grammar ? ($3.indexOf($1) !== -1) : [$1, 'in', $3])}
| term NOT_IN term
{$$ = validator ? validator.not_in($1, $3) : $3.indexOf($1) === -1}
{$$ = validator ? validator.not_in($1, $3) : (!grammar ? ($3.indexOf($1) === -1) : [$1, 'nin', $3])}
| term '>' term
{$$ = $1 > $3;}
{$$ = !grammar ? $1 > $3 : [$1, 'gt', $3];}
| term '>=' term
{$$ = $1 >= $3;}
{$$ = !grammar ? $1 >= $3 : [$1, 'gte', $3];}
| term '<' term
{$$ = $1 < $3;}
{$$ = !grammar ? $1 < $3 : [$1, 'lt', $3];}
| term '<=' term
{$$ = $1 <= $3;}
{$$ = !grammar ? $1 <= $3 : [$1, 'lte', $3];}
| '(' e ')'
{$$ = $2;}
;
Expand All @@ -99,7 +100,7 @@ term
: NUMBER
{$$ = validator ? validator.number(Number(yytext)) : Number(yytext);}
| VARIABLE
{$$ = validator ? validator.variable(yytext, data) : data[yytext];}
{$$ = validator ? validator.variable(yytext, data) : !grammar ? data[yytext]: yytext;}
| STRING
{$$ = validator ? validator.string(JSON.parse(yytext)) : JSON.parse(yytext);}
| ARRAY
Expand All @@ -112,4 +113,41 @@ term

function splitArray(input){
return input.replace(/\[|\]+/g,'').split(',').map(function(item){return item ? JSON.parse(item.trim()): ''});
}
}

function _g() { return typeof grammar === 'undefined'; }
function _v() { return typeof validator !== 'undefined'; }

function isExpr(input) {
return Array.isArray(input) && input.length === 3;
}

function toValue(input) {
if (input instanceof Date) return (input.getMonth() + 1) + '/' + input.getDate() + '/' + input.getFullYear();
return JSON.stringify(input);
}

function generate(input) {
var lhs = isExpr(input[0]) ? generate(input[0]) : input[0];
var op = input[1];
var rhs = isExpr(input[2]) ? generate(input[2]) : toValue(input[2]);

var _s = function(str) { return str; };
switch (op) {
case 'not' : return _s('!(' + lhs + ')');
case 'eq' : return _s(lhs + ' = ' + rhs);
case 'neq' : return _s(lhs + ' <> ' + rhs);
case 'in' : return _s(lhs + ' in ' + rhs);
case 'nin' : return _s(lhs + ' not in ' + rhs);
case 'gt' : return _s(lhs + ' > ' + rhs);
case 'gte' : return _s(lhs + ' >= ' + rhs);
case 'lt' : return _s(lhs + ' < ' + rhs);
case 'lte' : return _s(lhs + ' <= ' + rhs);
case 'and' : return _s('(' + lhs + ' and ' + rhs + ')');
case 'or' : return _s('(' + lhs + ' or ' + rhs + ')');
default:
throw new Error('do not recognize:' + lhs + ', ' + op + ', ' + rhs);
}
}

exports.generate = generate;