-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparser.jison
130 lines (110 loc) · 4.92 KB
/
parser.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* lexer tokens */
/* The single character tokens don't have any special names
* This is to make error messages easier to understand. */
%lex
%%
\s+ { /* whitespace no action */ }
'(' { return '('; }
')' { return ')'; }
'[' { return '['; }
']' { return ']'; }
'{' { return '{'; }
'}' { return '}'; }
'=' { return '='; }
'!=' { return '!='; }
'~' { return '~'; }
'!~' { return '!~'; }
'<=' { return '<='; }
'>=' { return '>='; }
'>' { return '>'; }
'<' { return '<'; }
'*' { return '*'; }
'#' { return '#'; }
'not' { return 'not'; }
'and' { return 'and'; }
'or' { return 'or'; }
'true' { return 'Boolean'; }
'false' { return 'Boolean'; }
"-"?\d+ { return 'Number'; }
\"(\\.|[^\\"])*\" { yytext = eval(yytext); return 'String'; }
"'"(\\.|[^\\'])*"'" { yytext = eval(yytext); return 'String'; }
'.' { return '.'; }
[-\w_:\?]+ { return 'String'; }
'@@' { return '@@'; }
'@' { return '@'; }
<<EOF>> { return 'EOF'; }
/lex
/* Helper functions */
%{
var astlib = require('./ast');
var ast = astlib.builders;
var evaluator = require('./evaluator');
var loc = require('./util').loc;
%}
/* operator precedence */
%left 'or'
%left 'and'
%left '=' '!=' '~' '!~' '<' '<=' '>' '>='
%right 'not'
%start query
%% /* grammar */
query
: expression EOF { $$ = ast.query($1); $$.loc = loc(@$); return $$; }
| EOF { $$ = ast.query(); $$.loc = loc(@$); return $$;}
;
expression
: identifier_path { $$ = ast.regexpNodeMatch($1); $$.loc = loc(@$); }
| 'not' expression { $$ = ast.notExpression($2); $$.loc = loc(@$); }
| expression 'and' expression { $$ = ast.andExpression($1, $3); $$.loc = loc(@$); }
| expression 'or' expression { $$ = ast.orExpression($1, $3); $$.loc = loc(@$); }
| '(' expression ')' { $$ = ast.parentesizedExpression($2); $$.loc = loc(@$); }
| resource_expression
| comparison_expression
| subquery
;
literal
: boolean { $$ = ast.boolean($1); $$.loc = loc(@$); }
| string { $$ = ast.string($1); $$.loc = loc(@$); }
| integer { $$ = ast.number($1); $$.loc = loc(@$); }
| float { $$ = ast.number($1); $$.loc = loc(@$); }
| '@' string { $$ = ast.date($2); $$.loc = loc(@$); }
;
comparison_op: '~' | '!~' | '=' | '!=' | '>' | '>=' | '<' | '<=' ;
comparison_expression
: identifier_path comparison_op literal { $$ = ast.comparison($2, $1, $3); $$.loc = loc(@$); }
;
literal_identifier
: string { $$ = ast.identifier($1); $$.loc = loc(@$); }
| integer { $$ = ast.identifier($1); $$.loc = loc(@$); }
;
regexp_identifier
: '~' string { $$ = ast.regexpIdentifier($2); $$.loc = loc(@$); }
| '*' { $$ = ast.regexpIdentifier(".*"); $$.loc = loc(@$); }
;
identifier
: literal_identifier
| regexp_identifier
;
identifier_path
: literal_identifier { $$ = ast.identifierPath([$1], false); $$.loc = loc(@$); }
| regexp_identifier { $$ = ast.identifierPath([$1], true); $$.loc = loc(@$); }
| identifier_path '.' literal_identifier { $1.components.push($3); $$ = $1; $$.loc = loc(@$); }
| identifier_path '.' regexp_identifier { $1.components.push($3); $1.regexp = true; $$ = $1; $$.loc = loc(@$); }
;
subquery
: '#' string '.' comparison_expression { $$ = ast.subquery($2, $4); $$.loc = loc(@$); }
| '#' string block_expression { $$ = ast.subquery($2, $3); $$.loc = loc(@$); }
;
block_expression
: '{' expression '}' { $$ = ast.blockExpression($2); $$.loc = loc(@$); }
;
resource_expression
: string '[' identifier ']' { $$ = ast.resource($1, $3, false); $$.loc = loc(@$); }
| string '[' identifier ']' block_expression { $$ = ast.resource($1, $3, false, $5); $$.loc = loc(@$); }
| '@@' string '[' identifier ']' { $$ = ast.resource($2, $4, true); $$.loc = loc(@$); }
| '@@' string '[' identifier ']' block_expression { $$ = ast.resource($2, $4, true, $6); $$.loc = loc(@$); }
;
boolean : Boolean { $$ = yytext === 'true' ? true: false; } ;
integer : Number { $$ = parseInt(yytext, 10); } ;
string : String { $$ = yytext; } ;
float : Number '.' Number { $$ = parseFloat($1 + '.' + $3) } ;