Last active
December 21, 2015 13:42
-
-
Save benbenbenbenbenben/fa426e8800036767d9fc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%{ | |
var indexed = []; | |
function log() { | |
console.log.apply(console, arguments); | |
} | |
function index(k, q) { | |
var i = k.indexOf(q); | |
if (i >= 0) { | |
if (typeof(k) === 'string') | |
indexed.push({ value: q, start: i, end: i + q.length }); | |
else if (typeof(k) === 'object') | |
indexed.push({ value: k.value, start: i, end: i + q.length }); | |
} | |
return i >= 0; | |
} | |
%} | |
/* lexical grammar */ | |
%lex | |
%options case-insensitive | |
%% | |
\s+ /* skip whitespace */ | |
\'[^']+\' return 'SQTERM' | |
\"[^"]+\" return 'DQTERM' | |
"and" return 'AND' | |
"or" return 'OR' | |
"," return ',' | |
"not" return 'NOT' | |
^[^'",\(\)]+ return 'TERM' | |
"(" return '(' | |
")" return ')' | |
<<EOF>> return 'EOF' | |
. return 'INVALID' | |
/lex | |
/* operator associations and precedence */ | |
%left 'AND' 'OR' ',' | |
%left 'NOT' | |
%start query | |
%% /* language grammar */ | |
query | |
: expr EOF | |
{ var query = $1; | |
return function(k, callback) { | |
indexed = []; | |
var result = query(k); | |
if (callback) | |
callback(result, indexed); | |
return result; | |
}; | |
} | |
; | |
args | |
: expr | |
{$$ = [$1];} | |
| expr ',' expr | |
{$$ = Array.prototype.concat($1, $3);} | |
; | |
func | |
: TERM '(' ')' | |
{$$ = (function(f){ var c = f; | |
console.warn('[textsearch]: no function like "' + c + '()", using text search'); | |
return function(k) { return k.indexOf(c) > -1; }; })($1); } | |
| TERM '(' args ')' | |
{$$ = (function(f,a){ var c = f, p = a; | |
return c == 'in' ? function(k) { while(p.length > 0) if(p.shift()(k)) return true; return false; } | |
: c == 'match' ? | |
p.length < 1 ? (function() { console.warn('[textsearch]: no function like "' + c + '()", using text search'); return function(k) { return k.indexOf(c) > -1; }; })() | |
: function(k) { | |
var func = p[0]; // function(string):int | |
var h = { | |
indexOf: function(pattern) { | |
// sanitize and replace * with .*, spaces with 1 or more spaces | |
pattern = pattern.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); | |
pattern = pattern.split('\\*').join('[^\\s]*'); | |
pattern = pattern.replace(/\s+/g, '\\s+'); | |
var match = RegExp(pattern).exec(k); | |
this.value = match ? match[0] : null; | |
return match ? match.index : -1; | |
} | |
}; | |
return func(h); | |
} | |
: function(k) { return k.indexOf(c) > -1; } | |
})($1, $3); } | |
; | |
expr | |
: func | |
{$$ = (function(a){ var x = a; return function(k) { return x(k); }; })($1); } | |
| expr 'AND' expr | |
{$$ = (function(a,b){ var x = a, y = b; return function(k) { return x(k) && y(k); }; })($1, $3); } | |
| expr 'OR' expr | |
{$$ = (function(a,b){ var x = a, y = b; return function(k) { return x(k) || y(k); }; })($1, $3); } | |
| 'NOT' expr | |
{$$ = (function(a){ var x = a; return function(k) { return !x(k); }; })($2); } | |
| '(' expr ')' | |
{$$ = (function(a){ var x = a; return function(k) { return x(k); }; })($2); } | |
| SQTERM | |
{$$ = (function(a){ var q = a; return function(k) { return index(k, q); }; })(yytext.substr(1, yytext.length - 2)); } | |
| DQTERM | |
{$$ = (function(a){ var q = a; return function(k) { return index(k, q); }; })(yytext.substr(1, yytext.length - 2)); } | |
| TERM | |
{$$ = (function(a){ var q = a; return function(k) { return index(k, q); }; })(yytext); } | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment