-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbnt-quick-consumers.js
112 lines (94 loc) · 2.9 KB
/
bnt-quick-consumers.js
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
const {consumeExpressionP} = require("./consume-expression-p");
const consumeMethod = (template, getMethod, consumeExpression) => {
const match1 = /^\s*(\w+)\(\s*/.exec(template.strings[0]);
if (!match1) {
return null;
}
const methodName = match1[1];
template = {
strings: [template.strings[0].substring(match1[0].length), ...template.strings.slice(1)],
values: template.values,
};
const expression = consumeExpression(template);
if (!expression) {
return null;
}
template = expression.remains;
const match2 = /^\s*\)\s*/.exec(template.strings[0]);
if (!match2) {
return null;
}
return {
value: getMethod(methodName)(expression.value),
remains: {
strings: [template.strings[0].substring(match2[0].length), ...template.strings.slice(1)],
values: template.values,
},
};
};
const consumeGroup = (template, consumeExpression) => {
const match1 = /^\s*\(\s*/.exec(template.strings[0]);
if (!match1) {
return null;
}
template = {
strings: [template.strings[0].substring(match1[0].length), ...template.strings.slice(1)],
values: template.values,
};
const expression = consumeExpression(template);
if (!expression) {
return null;
}
template = expression.remains;
const match2 = /^\s*\)\s*/.exec(template.strings[0]);
if (!match2) {
return null;
}
return {
value: expression.value,
remains: {
strings: [template.strings[0].substring(match2[0].length), ...template.strings.slice(1)],
values: template.values,
},
};
};
const consumeStrNumber = (template, negate) => {
const match = /^\s*(-\s*)?([\d.e]+)\s*/.exec(template.strings[0]);
if (!match) {
return null;
}
return {
value: !match[1] ? match[2] : negate(match[2]),
remains: {
strings: [template.strings[0].substring(match[0].length), ...template.strings.slice(1)],
values: template.values,
},
};
};
const consumeOperator = (template) => {
const match = /^\s*(==|\*\*|!=|<=|>=|[-+*/<>])\s*/.exec(template.strings[0]);
if (!match) {
return null;
}
return {
value: match[1],
remains: {
strings: [template.strings[0].substring(match[0].length), ...template.strings.slice(1)],
values: template.values,
},
};
};
const consumeTemplateValue = (template) => {
const match = /^\s*$/.exec(template.strings[0]);
if (!match) {
return null;
}
return {
value: template.values[0],
remains: {
strings: template.strings.slice(1),
values: template.values.slice(1),
},
};
};
exports.consumeExpression = consumeExpressionP({consumeStrNumber, consumeTemplateValue, consumeGroup, consumeMethod, consumeOperator});