-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.ne
81 lines (63 loc) · 1.65 KB
/
grammar.ne
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
@builtin "whitespace.ne"
@builtin "string.ne"
@{%
const _ = require('lodash');
const join = d => d[0].join('');
const nuller = d => null;
const nth = n => d => d[n];
const notNull = d => d.filter(i => i !== null);
const {
fetch,
filter,
selector,
until,
fetchQuantity
} = require('./utils');
%}
# Upper level
query ->
fetch {% d => _.flattenDeep(notNull(d)) %}
| fetch __ filter {% d => _.flattenDeep(notNull(d)) %}
# Fetch stage
fetch ->
target __ selector {% fetch %}
| target {% fetch %}
| quantifier __ target __ selector {% fetchQuantity %}
| quantifier __ target {% fetchQuantity %}
target ->
"thngs"i {% d => '/thngs' %}
| "thng"i {% d => '/thngs' %}
| "products"i {% d => '/products' %}
quantifier ->
int {% id %}
selector ->
selectorField __ selectorValue {% selector %}
selectorField ->
"tagged"i {% d => 'tags' %}
| "with tags"i {% d => 'tags' %}
| "named"i {% d => 'name' %}
| "with name"i {% d => 'name' %}
selectorValue ->
string {% id %}
# Filter stage
filter ->
"where" __ filterExpression {% nth(2) %}
filterExpression ->
filterField _ filterOperator _ filterValue {% filter %}
filterField -> property {% id %}
filterValue -> string {% id %}
filterOperator ->
"=" {% id %}
| "~" {% id %}
| "^" {% id %}
| "$" {% id %}
property ->
[\w\.]:+ {% join %}
string ->
[\w\*]:+ {% join %}
int ->
[0-9]:+ {% d => parseInt(join(d)) %}
_ ->
[\s] {% nuller %}
__ ->
[ \t]:+ {% nuller %}