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