Skip to content

Instantly share code, notes, and snippets.

@geobde
Created February 16, 2021 10:13
Show Gist options
  • Save geobde/4b5e4716ba91904dd0a1400415f3155a to your computer and use it in GitHub Desktop.
Save geobde/4b5e4716ba91904dd0a1400415f3155a to your computer and use it in GitHub Desktop.
%lex
%%
\s+
\"[^"]+\" return 'WORD'
"or" return 'OR'
"and" return 'AND'
"not" return 'NOT'
"(" return '('
")" return ')'
<<EOF>> return 'EOF'
. return 'INVALID'
/lex
%left 'OR' 'AND'
%left 'NOT'
%start expressions
%%
expressions
: e EOF
{
var query = $1;
return query;
}
;
e
: e 'OR' e
{$$ = {_or: [$1, $3]};}
| e 'AND' e
{$$ = {_and: [$1, $3]};}
| 'NOT' e %prec NOT
{$$ = {_not: $2};}
| '(' e ')'
{$$ = $2;}
| WORD
{
$$ = "%" + yytext.substr(1, yytext.length-2) + "%";
$$ = {text: {_like: $$}};
}
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment