-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReGenerator.js
171 lines (126 loc) · 5.06 KB
/
ReGenerator.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
class ReGenerator {
constructor(samples, start_idx = 0) {
let sample_keys = Object.keys(samples);
if(samples[sample_keys[start_idx]].parsed) {
return samples[sample_keys[start_idx]].parsed
}
if(!samples[sample_keys[start_idx]].fill) {
throw new Error('sample must have a substitute: ' + sample_keys[start_idx] + " = " + JSON.stringify(samples[sample_keys[start_idx]]));
}
else if(samples[sample_keys[start_idx]].fill.endsWith('.')) {
this.getContext = (new Function("return (_) => _." + samples[sample_keys[start_idx]].fill.substr(0, samples[sample_keys[start_idx]].fill.length - 1)))();
} else if(samples[sample_keys[start_idx]].fill.endsWith('[]')) {
this.getContext = (new Function("return (_) => _." + samples[sample_keys[start_idx]].fill.substr(0, samples[sample_keys[start_idx]].fill.length - 2)))();
this.for_array = true;
this.between = samples[sample_keys[start_idx]].between;
} else {
let cb = samples[sample_keys[start_idx]].parsed = (new Function("return (_) => '' + _." + samples[sample_keys[start_idx]].fill))();
if(samples[sample_keys[start_idx]].fn) {
cb.fn = samples[sample_keys[start_idx]].fn;
}
return cb;
}
samples[sample_keys[start_idx]].parsed = this;
// ni`пройтись по всем семплам и подставить все значения`;
var string = sample_keys[start_idx];
if(this.for_array) {
// console.log('fount stplit');
string = string.split(samples[sample_keys[start_idx]].between)[0];
// console.log('fount stplit', string);
}
for(var i = start_idx + 1; i < sample_keys.length; i++) {
let sample = samples[sample_keys[i]];
if(!sample.parsed) {
new ReGenerator(samples, i)
}
let idx;
while((idx = string.indexOf(sample_keys[i])) > -1 ) {
string = string.substr(0, idx) + "\x00\x01\x02" + i + "\x02\x01\x00" + string.substr(idx + sample_keys[i].length)
}
}
let values = [];
string.replace(/\x00\x01\x02[0-9]+\x02\x01\x00/g, function(a, num) {
num = Number(a.match(/[0-9]+/)[0]);
values.push(samples[sample_keys[num]].parsed);
});
this.sequence = string.split(/\x00\x01\x02[0-9]+\x02\x01\x00/);
for(let i = values.length; i--;) {
this.sequence.splice(i + 1, 0, values[i]);
}
// console.log(this.sequence);
// ni`распарсить полученную строку на готовые сэмплы`;
}
generate(values) {
let string = "";
// console.log('xuxu', values, this.getContext.toString(), this.getContext(values));
let context = this.getContext(values);
let gen = (context) => {
for(let val of this.sequence) {
if(val instanceof ReGenerator) {
// console.log('1 string+=', val.generate(context));
string += val.generate(context);
} else if(val instanceof Function) {
// console.log('2 string+=', val(context), val, context);
string += val.fn ? val.fn(val(context)) : val(context);
} else {
// console.log('3 string+=', val);
string += "" + val;
}
}
};
if(this.for_array) {
// console.log('for_array');
for(var i = 0; console.log(i, context[i]) , context[i]; i++) {
gen(context[i]);
if(context[i+1]) {
string += this.between
}
}
} else {
gen(context)
}
return string;
}
}
module.exports = ReGenerator;
// let aop = require('../yucalc3/aop.js');
//
// aop.test(function() {
// let generator = new ReGenerator({
// "table.Lookup(({col1}, n) => col1(n), ((_) => ({col2}, n) => col2(n) == _ )(val2) , ((_) => ({col3}) => col3(n) >= _ )(val3) )": { _: "lookup." },
// "table" : { _: "result.table" },
// "Lookup" : { _: "name" },
// "col1" : { _: "result.column" },
// "((_) => ({col2}, n) => col2(n) == _ )(val2) , ((_) => ({col3}) => col3(n) >= _ )(val3)"
// : { _: "filter[]", between: " , ", keep_spaces: true },
// "col2" : { _: "column" },
// "==" : { _: "operator" },
// "val2" : { _: "value" },
// });
//
// console.log('zzz', generator.generate({lookup: {name: 'SumIf', result: {table: "tab_zizi", column: "Col1"}, filter: [{column: 'colX', operator: "===", value: "zuzu"},{column: 'colP', operator: ">=", value: "puzu"},]}}));
//
// });
/*
проектирование по CORE:
объекты:
GeneratorSampleParser
события:
GeneratorSampleParser
GeneratorSampleParser_FoundNewExpression
GeneratorSampleParser_
действия:
GeneratorSampleParser.goThroughAllSamplesAndSubstitute on GeneratorSampleParser_FoundNewExpression
*/
function ni(descr) {
throw new Error("Not fully implemented: " + descr);
}
function pni(descr) {
if(!pni[descr]) {
pni[descr] = true;
console.warn(new Error("Warning - not fully implemented: " + descr).stack);
}
}
function sb(descr, ...args) {
throw new Error("Strange behaviour: " + descr + (args && args.length ? JSON.stringify(args) : ""));
}