This repository has been archived by the owner on Jul 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjouch.jison
94 lines (81 loc) · 2.29 KB
/
jouch.jison
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* description: parses couchdb 2.0 querys and constructs query objects */
/* lexical grammar */
%lex
%%
\s+ /* skip whitespace */
\"(\\.|[^"])*\" yytext = yytext.substr(1, yyleng-2); return 'STRING';
\'(\\.|[^'])*\' yytext = yytext.substr(1, yyleng-2); return 'STRING';
"(" return '(';
")" return ')';
"[" return '[';
"]" return ']';
"," return ',';
"==" return '==';
"!=" return '!=';
">=" return '>=';
"<=" return '<=';
">" return '>';
"<" return '<';
and[^\w] return 'and';
or[^\w] return 'or';
not[^\w] return 'not';
has[^\w] return 'has';
[0-9]+(?:\.[0-9]+)?\b return 'NUMBER';
[a-zA-Z][\.a-zA-Z0-9_]* return 'SYMBOL';
<<EOF>> return 'EOF';
/lex
/* operator associations and precedence */
%left 'or'
%left 'and'
%left 'not'
%left '==' '!=' 'has'
%left '<' '<=' '>' '>='
%left UMINUS
%start expressions
%% /* language grammar */
expressions
: e EOF
{return JSON.stringify($1);}
;
e
: 'not' e
{$$ = {'%not': $2}; }
| e 'and' e
{$$ = {'%and': [$1, $3]};}
| e 'or' e
{$$ = {'%or': [$1, $3]};}
| property 'has' value
{$$ = {}; $$[$1] = {'$elemMatch': {'$eq': $3}}; }
| property '==' value
{$$ = {}; $$[$1] = {'$eq': $3};}
| property '!=' value
{$$ = {}; $$[$1] = {'$neq': $3};}
| property '>=' value
{$$ = {}; $$[$1] = {'$gte': $3};}
| property '<=' value
{$$ = {}; $$[$1] = {'$lte': $3};}
| property '>' value
{$$ = {}; $$[$1] = {'$gt': $3};}
| property '<' value
{$$ = {}; $$[$1] = {'$lt': $3};}
| '(' e ')'
{$$ = $2;}
;
property
: SYMBOL
{$$ = $1;}
;
value
: NUMBER
{$$ = Number(yytext);}
| STRING
{$$ = yytext; }
| '[' ElemList ']'
{$$ = $2; }
;
ElemList
: ElemList ',' value
{$$ = $1; $$.push($3); }
| value
{$$ = [$1]; }
;