-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetalgebra.ne
87 lines (78 loc) · 1.72 KB
/
setalgebra.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
82
83
84
85
86
87
expression ->
set {% id %}
| bracketed {% id %}
| union_expression {% id %}
| intersect_expression {% id %}
| minus_expression {% id %}
bracketed ->
"(" space:* expression space:* ")" {%
d => d[2]
%}
@{%
function make_op(data, op_name) {
let op = {
type: "operation",
op: op_name,
args: []
}
for (let i=0; i < data.length; i++) {
let datum = data[i];
if (Array.isArray(datum)) {
datum = datum[0];
}
if(datum === null) {
continue;
} else {
op.args.push(datum);
}
}
return op;
}
%}
union_expression ->
argument space:+ union_op space:+ argument {%
d => {
return make_op(d, "union")
}
%}
| union_expression space:+ union_op space:+ argument {%
d => {
let op = d[0];
op.args.push(d[4][0]);
return op;
}
%}
intersect_expression ->
argument space:+ intersect_op space:+ argument {%
d => {
return make_op(d, "intersect")
}
%}
| intersect_expression space:+ intersect_op space:+ argument {%
d => {
let op = d[0];
op.args.push(d[4][0]);
return op;
}
%}
minus_expression ->
argument space:+ minus_op space:+ argument {%
d => {
return make_op(d, "minus")
}
%}
| minus_expression space:+ minus_op space:+ argument {%
d => {
let op = d[0];
op.args.push(d[4][0]);
return op;
}
%}
union_op -> ("union" | "|" | "+") {% d => null %}
intersect_op -> ("intersect" | "&" | "^") {% d => null %}
minus_op -> ("minus" | "-") {% d => null %}
argument -> (set | bracketed) {% id %}
set -> [a-zA-Z0-9]:+ {%
d => { return {type: "set", value: d[0][0]}}
%}
space -> [\s] {% d => null %}