@makano/rew 1.5.7 → 1.5.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,752 @@
|
|
1
|
+
let straceLog, execOptions, path, existsSync, wait, CONFIG_PATH;
|
2
|
+
if(typeof window !== "undefined"){
|
3
|
+
straceLog = () => {}
|
4
|
+
execOptions = {};
|
5
|
+
path = { resolve: (...a) => a.join('/') };
|
6
|
+
wait = (a, ...args) => a(...args);
|
7
|
+
CONFIG_PATH = '/';
|
8
|
+
} else {
|
9
|
+
straceLog = require("../misc/strace").straceLog;
|
10
|
+
execOptions = require("../const/opt").execOptions;
|
11
|
+
path = require("path");
|
12
|
+
const fs = require("fs");
|
13
|
+
existsSync = fs.existsSync;
|
14
|
+
wait = require('../functions/wait').wait;
|
15
|
+
CONFIG_PATH = require('../const/config_path').CONFIG_PATH;
|
16
|
+
}
|
17
|
+
const { USING_DEFAULT } = require('../const/usage');
|
18
|
+
const { compile: compileCivet } = require('../../civet/main');
|
19
|
+
|
20
|
+
function tokenizeCoffeeScript(code) {
|
21
|
+
const tokens = [];
|
22
|
+
let currentToken = '';
|
23
|
+
let i = 0;
|
24
|
+
|
25
|
+
while (i < code.length) {
|
26
|
+
const char = code[i];
|
27
|
+
const prevChar = code[i - 1];
|
28
|
+
const nextChar = code[i + 1];
|
29
|
+
const nextNextChar = code[i + 2];
|
30
|
+
|
31
|
+
if (char === '#') {
|
32
|
+
const commentEnd = code.indexOf('\n', i);
|
33
|
+
const comment = code.substring(i, commentEnd < 0 ? code.length : commentEnd + 1);
|
34
|
+
tokens.push({ type: 'COMMENT', value: comment });
|
35
|
+
i += comment.length - 1;
|
36
|
+
} else if (char === '"' && nextChar === '"' && nextNextChar === '"') {
|
37
|
+
let string = '"""';
|
38
|
+
i += 3;
|
39
|
+
while (i < code.length && !(code[i] === '"' && code[i + 1] === '"' && code[i + 2] === '"')) {
|
40
|
+
string += code[i];
|
41
|
+
i++;
|
42
|
+
}
|
43
|
+
string += '"""';
|
44
|
+
tokens.push({ type: 'TRIPLE_STRING', value: string });
|
45
|
+
i += 2;
|
46
|
+
} else if (char === '"' || char === "'") {
|
47
|
+
let string = char;
|
48
|
+
let escaped = false;
|
49
|
+
i++;
|
50
|
+
while (i < code.length && (code[i] !== char || escaped)) {
|
51
|
+
string += code[i];
|
52
|
+
if (code[i] === '\\' && !escaped) {
|
53
|
+
escaped = true;
|
54
|
+
} else {
|
55
|
+
escaped = false;
|
56
|
+
}
|
57
|
+
i++;
|
58
|
+
}
|
59
|
+
string += char;
|
60
|
+
tokens.push({ type: 'STRING', value: string });
|
61
|
+
} else if (char === '/' && nextChar !== ' ' && nextChar !== '/' && nextChar !== '*' && prevChar !== '<') {
|
62
|
+
let regex = char;
|
63
|
+
i++;
|
64
|
+
while (i < code.length && (code[i] !== '/' || regex.endsWith('\\'))) {
|
65
|
+
regex += code[i];
|
66
|
+
i++;
|
67
|
+
}
|
68
|
+
regex += '/';
|
69
|
+
tokens.push({ type: 'REGEX', value: regex });
|
70
|
+
} else if (/\s/.test(char)) {
|
71
|
+
if (tokens[tokens.length - 1]?.type === 'WHITESPACE') {
|
72
|
+
tokens[tokens.length - 1].value += char;
|
73
|
+
} else {
|
74
|
+
tokens.push({ type: 'WHITESPACE', value: char });
|
75
|
+
}
|
76
|
+
} else if (/[a-zA-Z_$@]/.test(char)) {
|
77
|
+
let identifier = char;
|
78
|
+
i++;
|
79
|
+
while (i < code.length && /[a-zA-Z0-9_$]/.test(code[i])) {
|
80
|
+
identifier += code[i];
|
81
|
+
i++;
|
82
|
+
}
|
83
|
+
tokens.push({ type: 'IDENTIFIER', value: identifier });
|
84
|
+
i--;
|
85
|
+
} else if (/[a-f0-9.xn]/.test(char)) {
|
86
|
+
let num = char;
|
87
|
+
i++;
|
88
|
+
while (i < code.length && /[a-f0-9.nx]/.test(code[i])) {
|
89
|
+
num += code[i];
|
90
|
+
i++;
|
91
|
+
}
|
92
|
+
tokens.push({ type: 'NUMBER', value: num });
|
93
|
+
i--;
|
94
|
+
} else {
|
95
|
+
tokens.push({ type: 'OTHER', value: char });
|
96
|
+
}
|
97
|
+
i++;
|
98
|
+
}
|
99
|
+
|
100
|
+
return tokens;
|
101
|
+
}
|
102
|
+
|
103
|
+
const ValueIfy = (val) => {
|
104
|
+
if(!isNaN(parseFloat(val)) || !isNaN(parseInt(val))){
|
105
|
+
return isNaN(parseInt(val)) ? parseFloat(val) : parseInt(val);
|
106
|
+
} if(val == 'true' || val == 'false') {
|
107
|
+
return val == 'true' ? true : false;
|
108
|
+
} else {
|
109
|
+
return JSON.stringify(val);
|
110
|
+
}
|
111
|
+
}
|
112
|
+
|
113
|
+
function insertTokenAt(array, index, value) {
|
114
|
+
if (index < 0 || index > array.length) {
|
115
|
+
throw new RangeError('Index out of bounds');
|
116
|
+
}
|
117
|
+
array.splice(index, 0, value);
|
118
|
+
}
|
119
|
+
|
120
|
+
const gnextToken = (i, n, tokens) => {
|
121
|
+
return tokens[i + n] ? (tokens[i + n].type == 'WHITESPACE' ? gnextToken(i, n + 1, tokens) : { token: tokens[i + n], n, ti: i + n }) : null;
|
122
|
+
};
|
123
|
+
|
124
|
+
const fnextToken = (i, tokens, type, value) => {
|
125
|
+
return tokens
|
126
|
+
.map((t, ind) => {
|
127
|
+
t.ti = ind;
|
128
|
+
return t;
|
129
|
+
})
|
130
|
+
.slice(i, tokens.length - 1)
|
131
|
+
.map((t, ind) => {
|
132
|
+
t.ri = ind;
|
133
|
+
t.index = ind - i;
|
134
|
+
return t;
|
135
|
+
})
|
136
|
+
.find((t) => t.type == type && (value ? t.value == value : true));
|
137
|
+
};
|
138
|
+
|
139
|
+
function declareAlias(aliases, token) {
|
140
|
+
const regex = /^#declare(\*)?\s+(\w+)\s+"([^"]+)"\s*=\s*([\s\S]*);$/;
|
141
|
+
const match = token.value.trim().match(regex);
|
142
|
+
straceLog('declareCase()');
|
143
|
+
straceLog('==> WARN: Experimental feature detected');
|
144
|
+
|
145
|
+
if (match) {
|
146
|
+
const isPublic = !!match[1];
|
147
|
+
const type = match[2] == "key" ? 'IDENTIFIER' : match[2];
|
148
|
+
let name = match[3];
|
149
|
+
let value = match[4].trim();
|
150
|
+
straceLog('==> INFO !DECLARE', name, 'as', value);
|
151
|
+
|
152
|
+
let aliasValue = value.startsWith('${')
|
153
|
+
? new Function('token', 'tokens', 'code', 'hooks', 'index', 'setIndex', value.slice(2, -1))
|
154
|
+
: value;
|
155
|
+
|
156
|
+
|
157
|
+
if(name.startsWith('=')){
|
158
|
+
name = name.slice(1);
|
159
|
+
let isDecOnly = false, isConstructor = false;
|
160
|
+
if(name.endsWith('*')) {
|
161
|
+
name = name.slice(0, -1);
|
162
|
+
isDecOnly = true;
|
163
|
+
}
|
164
|
+
if(name.endsWith('!')) {
|
165
|
+
name = name.slice(0, -1);
|
166
|
+
isConstructor = true;
|
167
|
+
}
|
168
|
+
aliasValue = (token, tokens, code, hooks, index, setIndex) => {
|
169
|
+
const nextToken = tokens[index+1]
|
170
|
+
let nextToken2 = gnextToken(index, 3, tokens);
|
171
|
+
if (nextToken?.value == '(' || tokens[index+2]?.value == '(') {
|
172
|
+
let params = '';
|
173
|
+
index += 2;
|
174
|
+
let openBrackets = 1;
|
175
|
+
while (index < tokens.length && openBrackets > 0) {
|
176
|
+
const char = tokens[index].value;
|
177
|
+
if (char === '(') openBrackets++;
|
178
|
+
if (char === ')') openBrackets--;
|
179
|
+
if (openBrackets > 0) params += char;
|
180
|
+
index++;
|
181
|
+
}
|
182
|
+
const { token: nextToken2, n: n2, ti } = gnextToken(index, 1, tokens) || {};
|
183
|
+
let offset = 1;
|
184
|
+
if(tokens[ti+1].type == 'WHITESPACE') offset += 2;
|
185
|
+
if(tokens[ti+3].type == 'WHITESPACE') offset += 1;
|
186
|
+
|
187
|
+
let nextToken = gnextToken(index, offset+1, tokens);
|
188
|
+
const args = nextToken.token.value;
|
189
|
+
setIndex(ti + offset);
|
190
|
+
return `${nextToken2.value} = ${isConstructor?'new ':''}${token.value} ${args && args !== '(' && args !== '{' && args !== '[' && args !== '-' && args !== '=' ? `${args},` : ''} ${params.trim() ? params.trim() + ', ' : ''}${args == '(' || args == '[' || args == '{' || args == '=' || args == '-' ? args : ''}`.replace(/,(| )$/, '');
|
191
|
+
} else if(nextToken?.value == ' ' && (isDecOnly || nextToken2?.token.value == '=' || nextToken2?.token.value == ':')){
|
192
|
+
nextToken.value = '';
|
193
|
+
if(isDecOnly){
|
194
|
+
nextToken2 = {
|
195
|
+
token: { value: ':' },
|
196
|
+
ti: index+2
|
197
|
+
}
|
198
|
+
value = '= ' + value + '()';
|
199
|
+
}
|
200
|
+
if(nextToken2.token.value == ':') nextToken2.token.value = '=';
|
201
|
+
hooks.push({
|
202
|
+
index: nextToken2.ti,
|
203
|
+
value: ' ' + (isConstructor?'new ':'') + value
|
204
|
+
})
|
205
|
+
return "";
|
206
|
+
}
|
207
|
+
return token.value;
|
208
|
+
}
|
209
|
+
}
|
210
|
+
|
211
|
+
aliases[type] = aliases[type] || {};
|
212
|
+
aliases[type][name] = aliasValue;
|
213
|
+
|
214
|
+
if(isPublic){
|
215
|
+
straceLog('==>', 'INFO Declaration Globalized');
|
216
|
+
execOptions._syntaxAliases[type] = execOptions._syntaxAliases[type] || {};
|
217
|
+
execOptions._syntaxAliases[type][name] = aliasValue;
|
218
|
+
}
|
219
|
+
}
|
220
|
+
}
|
221
|
+
|
222
|
+
const stdTypes = (isPublic) => {
|
223
|
+
let r = '';
|
224
|
+
const dec = (name, fn, cons = 0) => {
|
225
|
+
r += `#declare${isPublic?'*':''} key "=${name}${cons ? '!' : ''}" = ${fn || name};\n`
|
226
|
+
}
|
227
|
+
dec('int');
|
228
|
+
dec('str');
|
229
|
+
dec('float');
|
230
|
+
dec('num');
|
231
|
+
dec('bool');
|
232
|
+
dec('typedef');
|
233
|
+
dec('typef');
|
234
|
+
dec('struct');
|
235
|
+
dec('Usage', null, 1);
|
236
|
+
return r;
|
237
|
+
};
|
238
|
+
const includeFile = (includeContent, options) => {
|
239
|
+
straceLog('include()', includeContent);
|
240
|
+
const dontInclude = includeContent.startsWith('*');
|
241
|
+
if(dontInclude) {
|
242
|
+
includeContent = includeContent.slice(1);
|
243
|
+
straceLog('==> INFO ingoring output', includeContent);
|
244
|
+
};
|
245
|
+
const fp = path.resolve(path.dirname(options.filename || ''), includeContent);
|
246
|
+
let packageName = options.filename ? (existsSync(fp) ? fp : includeContent) : includeContent;
|
247
|
+
const file = packageName.startsWith('./') || packageName.startsWith('../');
|
248
|
+
if(!(file) && packageName.match('/')) packageName = packageName.split('/').pop();
|
249
|
+
if(file) packageName = path.extname(packageName) ? packageName.replace(path.extname(packageName), '.h.coffee') : packageName;
|
250
|
+
if(file && !packageName.endsWith('.h.coffee')) packageName += '.h.coffee';
|
251
|
+
if(includeContent == 'std') packageName = 'std';
|
252
|
+
|
253
|
+
const _inc = (filename, c) => {
|
254
|
+
options.included = true;
|
255
|
+
options.filename = filename;
|
256
|
+
return '\n'+ compileRewStuff(c ? filename : readFileSync(filename).toString(), options)+'\n'
|
257
|
+
};
|
258
|
+
let r = '';
|
259
|
+
if(packageName == 'std'){
|
260
|
+
r = _inc(stdTypes(dontInclude), true);
|
261
|
+
} else if (existsSync(packageName)) {
|
262
|
+
straceLog('==> includeFile(', '"'+packageName+'"', ')');
|
263
|
+
r = _inc(packageName);
|
264
|
+
} else {
|
265
|
+
const packageName = includeContent.match('/') ? includeContent.split('/')[0] : includeContent;
|
266
|
+
const headerFile = includeContent.match('/') ? includeContent.replace(packageName+'/', '') : 'main.h.coffee';
|
267
|
+
const pathname = path.join(CONFIG_PATH, packageName, 'app', headerFile);
|
268
|
+
straceLog('==> includePackage(', '"'+packageName+'"');
|
269
|
+
if(existsSync(pathname)) r = _inc(pathname);
|
270
|
+
}
|
271
|
+
if(dontInclude){
|
272
|
+
return 'intentional-nothing';
|
273
|
+
}
|
274
|
+
return r;
|
275
|
+
}
|
276
|
+
|
277
|
+
function useImp(token, options){
|
278
|
+
if(token.type == 'STRING' && (
|
279
|
+
token.value.startsWith('"#') ||
|
280
|
+
token.value.startsWith("'#")
|
281
|
+
)){
|
282
|
+
straceLog('==> INFO imp() Uses HEADER');
|
283
|
+
const dem = token.value.slice(0, 1);
|
284
|
+
const value = token.value.slice(1, -1);
|
285
|
+
let packageName = value.slice(1);
|
286
|
+
token.value = dem+packageName+dem;
|
287
|
+
straceLog('imp() with header for', `"${packageName}"`);
|
288
|
+
return includeFile(packageName !== 'std' ? packageName : '*'+packageName, options);
|
289
|
+
}
|
290
|
+
return '';
|
291
|
+
}
|
292
|
+
|
293
|
+
function mapTokensToStrings(tokens) {
|
294
|
+
const result = [];
|
295
|
+
let current = '';
|
296
|
+
let inBrackets = false;
|
297
|
+
|
298
|
+
tokens.forEach(token => {
|
299
|
+
if (token.type === 'OTHER' && token.value === '{') {
|
300
|
+
if (current) {
|
301
|
+
result.push(current.trim());
|
302
|
+
current = '';
|
303
|
+
}
|
304
|
+
inBrackets = true;
|
305
|
+
current += token.value + ' ';
|
306
|
+
} else if (token.type === 'OTHER' && token.value === '}') {
|
307
|
+
current += token.value;
|
308
|
+
result.push(current.trim());
|
309
|
+
current = '';
|
310
|
+
inBrackets = false;
|
311
|
+
} else if(token.type === 'OTHER' && token.value === ',' && !inBrackets){
|
312
|
+
return;
|
313
|
+
} else {
|
314
|
+
current += token.value;
|
315
|
+
if (!inBrackets) {
|
316
|
+
result.push(current.trim());
|
317
|
+
current = '';
|
318
|
+
} else {
|
319
|
+
current += ' ';
|
320
|
+
}
|
321
|
+
}
|
322
|
+
});
|
323
|
+
|
324
|
+
return result;
|
325
|
+
}
|
326
|
+
|
327
|
+
function updateAliases(aliases, o = execOptions._syntaxAliases){
|
328
|
+
for(let h in o){
|
329
|
+
for(let i in o[h]){
|
330
|
+
if(!aliases[h]) aliases[h] = {};
|
331
|
+
aliases[h][i] = o[h][i]
|
332
|
+
}
|
333
|
+
}
|
334
|
+
return aliases;
|
335
|
+
}
|
336
|
+
|
337
|
+
function insertAt(array, index, ...values) {
|
338
|
+
if (index > array.length) {
|
339
|
+
index = array.length;
|
340
|
+
} else if (index < 0) {
|
341
|
+
index = 0;
|
342
|
+
}
|
343
|
+
array.splice(index, 0, ...values);
|
344
|
+
|
345
|
+
return array;
|
346
|
+
}
|
347
|
+
|
348
|
+
function compileRewStuff(content, options) {
|
349
|
+
straceLog('tokeinze(currentFile)');
|
350
|
+
const tokens = tokenizeCoffeeScript(content);
|
351
|
+
let result = '';
|
352
|
+
let multilineDeclareBuffer = [];
|
353
|
+
let multilineDeclare = false;
|
354
|
+
|
355
|
+
let hooks = [];
|
356
|
+
|
357
|
+
|
358
|
+
let aliases = {
|
359
|
+
...execOptions._syntaxAliases
|
360
|
+
}
|
361
|
+
|
362
|
+
for (let i = 0; i < tokens.length; i++) {
|
363
|
+
const token = tokens[i];
|
364
|
+
let { token: nextToken, n } = gnextToken(i, 1, tokens) || {};
|
365
|
+
let { token: prevToken } = gnextToken(i, -2, tokens) || {};
|
366
|
+
|
367
|
+
if(token.type == "COMMENT" && i < 2 && token.value.startsWith('#!')){
|
368
|
+
continue;
|
369
|
+
}
|
370
|
+
|
371
|
+
if(token.type == "IDENTIFIER" && token.value == "fn" && i < 2 && prevToken?.value !== "." && nextToken?.type === "IDENTIFIER"){
|
372
|
+
token.value = "function";
|
373
|
+
}
|
374
|
+
|
375
|
+
if ((token.type === "COMMENT" && multilineDeclare) || (token.type !== "COMMENT" && multilineDeclare)) {
|
376
|
+
if(token.type === "COMMENT"){
|
377
|
+
multilineDeclareBuffer.push(token.value.startsWith('###') ? token.value.slice(3) : token.value.slice(1));
|
378
|
+
if (token.value.trim().endsWith(';')) {
|
379
|
+
multilineDeclare = false;
|
380
|
+
const combinedDeclaration = multilineDeclareBuffer.join('\n');
|
381
|
+
declareAlias(aliases, { ...token, value: combinedDeclaration });
|
382
|
+
multilineDeclareBuffer = [];
|
383
|
+
}
|
384
|
+
} else {
|
385
|
+
multilineDeclare = false;
|
386
|
+
multilineDeclareBuffer = [];
|
387
|
+
}
|
388
|
+
}
|
389
|
+
|
390
|
+
if (token.type === "COMMENT" && token.value.startsWith('#alias')) {
|
391
|
+
let value = '#declare';
|
392
|
+
if(token.value.match(/^#alias\*/)) value += '*';
|
393
|
+
let subs;
|
394
|
+
subs = token.value.replace(/^#alias/, '');
|
395
|
+
if(token.value.endsWith('*')) subs.split('*')[1];
|
396
|
+
|
397
|
+
value += ' key';
|
398
|
+
value += ' ' + subs.replace(/([\S]+)\s*=\s*([\S]+)/, '"$1" = $2').trim();
|
399
|
+
value += ';';
|
400
|
+
declareAlias(aliases, {...token, value});
|
401
|
+
}
|
402
|
+
|
403
|
+
if (token.type === "COMMENT" && token.value.startsWith('#declare')) {
|
404
|
+
if (token.value.includes(';')) {
|
405
|
+
declareAlias(aliases, token);
|
406
|
+
} else {
|
407
|
+
multilineDeclare = true;
|
408
|
+
multilineDeclareBuffer.push(token.value);
|
409
|
+
}
|
410
|
+
}
|
411
|
+
|
412
|
+
if (token.type === 'IDENTIFIER' && token.value === 'opt.set') {
|
413
|
+
const { token: nextNextToken } = gnextToken(i, 2, tokens) || {};
|
414
|
+
if (nextNextToken && nextNextToken.value.slice(1).slice(0, -1) == 'jsxPragma') {
|
415
|
+
const { token: nextLastToken } = gnextToken(i, 5, tokens) || {};
|
416
|
+
execOptions.jsxPragma = nextLastToken.value.slice(1).slice(0, -1);
|
417
|
+
}
|
418
|
+
}
|
419
|
+
|
420
|
+
if (token.type === 'COMMENT' && token.value.slice(1).trim().startsWith('@jsx')) {
|
421
|
+
options.jsx = true;
|
422
|
+
straceLog('jsx().with(comments)');
|
423
|
+
if(token.value.split('@jsx')[1].trim()){
|
424
|
+
options.jsxPragma = token.value.split('@jsx')[1].trim();
|
425
|
+
straceLog('jsx().withPragma(', `"${options.jsxPragma}"`, ')');
|
426
|
+
}
|
427
|
+
}
|
428
|
+
|
429
|
+
if (token.type === 'COMMENT' && token.value.slice(1).trim() === '@cls') {
|
430
|
+
options.cls = true;
|
431
|
+
straceLog('cliSyntax::enable()');
|
432
|
+
straceLog('===> WARN: HIGHLY EXPERIMENTAL FEATURE DETECTED');
|
433
|
+
}
|
434
|
+
|
435
|
+
if (options.cls && token.type === 'OTHER' && token.value === '-' && nextToken.value == '-' && tokens[i-1]?.type == 'WHITESPACE') {
|
436
|
+
// Argument case
|
437
|
+
let offset = 0, writenext = false;
|
438
|
+
const n = gnextToken(i, 2, tokens);
|
439
|
+
let v = gnextToken(i, 3, tokens);
|
440
|
+
if(v.token.type == 'IDENTIFIER' && v.token.value == '$'){
|
441
|
+
writenext = true;
|
442
|
+
}
|
443
|
+
result += n.token.value + ': ' + (writenext ? '' : (v.token.value == ',' ? 'true, ' : v.token.type == "STRING" ? v.token.value : ValueIfy(v.token.value)));
|
444
|
+
|
445
|
+
i = offset + tokens.indexOf(v.token);
|
446
|
+
continue;
|
447
|
+
}
|
448
|
+
|
449
|
+
|
450
|
+
if (tokens[i-1]?.value !== '.' && token.type === 'IDENTIFIER' && token.value === 'export' && !options.keepImports) {
|
451
|
+
token.value = 'pub';
|
452
|
+
straceLog('INFO !TRANSLATE converting export to pub');
|
453
|
+
}
|
454
|
+
|
455
|
+
if (tokens[i-1]?.value !== '.' && token.type === 'IDENTIFIER' && token.value === 'package' && nextToken.type == 'STRING') {
|
456
|
+
token.value = 'appPackage';
|
457
|
+
straceLog('changeAppPackage()');
|
458
|
+
}
|
459
|
+
|
460
|
+
if (
|
461
|
+
token.type === 'OTHER' && token.value === '-' &&
|
462
|
+
nextToken.type == 'IDENTIFIER' && nextToken.value === 'wait'
|
463
|
+
) {
|
464
|
+
const { token: nextNextToken } = gnextToken(i, 2, tokens) || {};
|
465
|
+
if(nextNextToken?.type == 'IDENTIFIER'){
|
466
|
+
token.value = '';
|
467
|
+
hooks.push({
|
468
|
+
index: i + 3,
|
469
|
+
value: ','
|
470
|
+
});
|
471
|
+
}
|
472
|
+
}
|
473
|
+
|
474
|
+
if (tokens[i-1]?.value !== '.' && token.type === 'IDENTIFIER' && token.value === 'using' && !options.disableUse) {
|
475
|
+
straceLog('!+DIRECTIVE using()');
|
476
|
+
const next = nextToken?.value;
|
477
|
+
if(next in USING_DEFAULT) {
|
478
|
+
const { use } = USING_DEFAULT[next];
|
479
|
+
use?.(options);
|
480
|
+
straceLog('==>', nextToken.value);
|
481
|
+
nextToken.value = `"${nextToken.value}"`
|
482
|
+
|
483
|
+
const { token: nextNextToken } = gnextToken(i, 3, tokens) || {};
|
484
|
+
if(nextNextToken.value == "as") nextNextToken.value = ",";
|
485
|
+
} else straceLog('==> !-UNKNOWN');
|
486
|
+
}
|
487
|
+
|
488
|
+
if (token.type === 'IDENTIFIER' && token.value === 'as' && !options.keepImports) {
|
489
|
+
const isFrom = gnextToken(i, 3, tokens);
|
490
|
+
const isInImport = tokens[i-2];
|
491
|
+
if(isFrom?.token.value == 'from' && isInImport?.value !== '*'){
|
492
|
+
insertTokenAt(tokens, i, { type: 'WHITESPACE', value: ' ' });
|
493
|
+
insertTokenAt(tokens, i, { type: 'OTHER', value: '*' });
|
494
|
+
insertTokenAt(tokens, i, { type: 'WHITESPACE', value: ' ' });
|
495
|
+
insertTokenAt(tokens, i, { type: 'IDENTIFIER', value: 'import' });
|
496
|
+
i -= 1;
|
497
|
+
continue;
|
498
|
+
}
|
499
|
+
}
|
500
|
+
|
501
|
+
if (tokens[i-1]?.value !== '.' && token.type === 'IDENTIFIER' && token.value === 'import' && !options.keepImports) {
|
502
|
+
// console.log(nextToken.type);
|
503
|
+
straceLog('import()');
|
504
|
+
straceLog('==> WARN: Slows compilation');
|
505
|
+
let ind = i + n + 2;
|
506
|
+
let isAs = false;
|
507
|
+
let usedDefault = false;
|
508
|
+
|
509
|
+
let defaultName;
|
510
|
+
if (nextToken.type === 'STRING') {
|
511
|
+
straceLog('==> !SIMPLE');
|
512
|
+
if(useImp(nextToken, options)) updateAliases(aliases);
|
513
|
+
result += `inc ${nextToken.value}`;
|
514
|
+
i += n;
|
515
|
+
} else if (nextToken.type === 'OTHER' && nextToken.value == "(") {
|
516
|
+
const closingBraceToken = fnextToken(ind, tokens, 'OTHER', ')');
|
517
|
+
let lastindex = ind;
|
518
|
+
if (closingBraceToken) {
|
519
|
+
const namesTokens = tokens.slice(ind, closingBraceToken.ti);
|
520
|
+
const next = gnextToken(closingBraceToken.ti, 1, tokens);
|
521
|
+
let exports = [];
|
522
|
+
if(next?.token?.value == 'as'){
|
523
|
+
const ebraceOpen = gnextToken(next.token.ti, 1, tokens);
|
524
|
+
const ebraceClose = fnextToken(ebraceOpen.ti, tokens, 'OTHER', ')');
|
525
|
+
const exportsTokens = tokens.slice(ebraceOpen.ti+1, ebraceClose?.ti);
|
526
|
+
exports = mapTokensToStrings(exportsTokens.filter(token => token.type !== 'WHITESPACE'));
|
527
|
+
lastindex = ebraceClose.ti;
|
528
|
+
} else lastindex = closingBraceToken.ti;
|
529
|
+
const statements = namesTokens.filter(token => token.type !== 'WHITESPACE').map((token, index) => {
|
530
|
+
return tokenizeCoffeeScript(`\nimport `+ (exports[index] ? `${exports[index].startsWith('@') ? '* as '+ exports[index].slice(1) : exports[index]} from ${token.value};` : `${token.value}`))
|
531
|
+
});
|
532
|
+
i = lastindex+1;
|
533
|
+
insertAt(tokens, i, ...statements.flat());
|
534
|
+
continue;
|
535
|
+
}
|
536
|
+
} else if (nextToken.value === '{') {
|
537
|
+
const closingBraceToken = fnextToken(ind, tokens, 'OTHER', '}');
|
538
|
+
const nameToken = fnextToken(ind, tokens, 'STRING');
|
539
|
+
if (closingBraceToken) {
|
540
|
+
const exportsTokens = tokens.slice(ind, closingBraceToken.ti);
|
541
|
+
const exports = exportsTokens
|
542
|
+
.filter((t) => t.type === 'IDENTIFIER')
|
543
|
+
.map((t, i, arr) => t.value == 'as' ? [arr[i-1].value +': '+arr[i+1].value] : t.value)
|
544
|
+
.flat(1)
|
545
|
+
.filter((t, i, arr) => !arr[i+1]?.match(':') && !arr[i-1]?.match(':'))
|
546
|
+
.join(', ');
|
547
|
+
|
548
|
+
straceLog('==> !COMPLEX', exports, 'from', nameToken.value);
|
549
|
+
|
550
|
+
if(useImp(nameToken, options)) updateAliases(aliases);
|
551
|
+
result += `{ ${exports} } ${options.type == 'coffee' ? '=' : ':='} inc ${nameToken.value}`;
|
552
|
+
i = nameToken.ti;
|
553
|
+
}
|
554
|
+
} else if (nextToken.value === '*') {
|
555
|
+
const asToken = fnextToken(ind, tokens, 'IDENTIFIER', 'as');
|
556
|
+
if (asToken) {
|
557
|
+
const nameToken = fnextToken(asToken.ti, tokens, 'STRING');
|
558
|
+
const nextToken = fnextToken(asToken.ti + 1, tokens, 'IDENTIFIER');
|
559
|
+
defaultName = nextToken.value;
|
560
|
+
straceLog('==> !COMPLEX', defaultName, 'from', nameToken.value);
|
561
|
+
if(useImp(nameToken, options)) updateAliases(aliases);
|
562
|
+
result += `${defaultName} ${options.type == 'coffee' ? '=' : ':='} inc ${nameToken.value}`;
|
563
|
+
i = ind + 6;
|
564
|
+
isAs = true;
|
565
|
+
}
|
566
|
+
} else if (nextToken) {
|
567
|
+
const nameToken = fnextToken(ind, tokens, 'STRING');
|
568
|
+
defaultName = nextToken.value;
|
569
|
+
let { token: nextNextToken, n: n2 } = gnextToken(i + 2, 1, tokens) || {};
|
570
|
+
if (nextNextToken?.type == 'OTHER' && nextNextToken?.value == ',') {
|
571
|
+
const closingBraceToken = fnextToken(ind, tokens, 'OTHER', '}');
|
572
|
+
if (closingBraceToken) {
|
573
|
+
const exportsTokens = tokens.slice(ind, closingBraceToken.ti);
|
574
|
+
const exports = exportsTokens
|
575
|
+
.filter((t) => t.type === 'IDENTIFIER')
|
576
|
+
.map((t, i, arr) => t.value == 'as' ? [arr[i-1].value +': '+arr[i+1].value] : t.value)
|
577
|
+
.flat(1)
|
578
|
+
.filter((t, i, arr) => !arr[i+1]?.match(':') && !arr[i-1]?.match(':'))
|
579
|
+
.join(', ');
|
580
|
+
straceLog('==> !COMPLEX', defaultName, 'and', exports, 'from', nameToken.value);
|
581
|
+
if(useImp(nameToken, options)) updateAliases(aliases);
|
582
|
+
result += `{ default: ${defaultName}, ${exports} } ${options.type == 'coffee' ? '=' : ':='} inc ${nameToken?.value || ''}`;
|
583
|
+
i = closingBraceToken.ti + 4;
|
584
|
+
usedDefault = true;
|
585
|
+
}
|
586
|
+
} else {
|
587
|
+
if(useImp(nameToken || {}, options)) updateAliases(aliases);
|
588
|
+
result += `{ default: ${defaultName} } ${options.type == 'coffee' ? '=' : ':='} inc ${nameToken?.value || ''}`;
|
589
|
+
usedDefault = true;
|
590
|
+
i = ind + 2;
|
591
|
+
}
|
592
|
+
}
|
593
|
+
|
594
|
+
const nextLastToken = fnextToken(i, tokens, 'IDENTIFIER');
|
595
|
+
|
596
|
+
if (nextLastToken?.value == 'assert') {
|
597
|
+
result += ', ';
|
598
|
+
const assertionToken = gnextToken(nextLastToken.ti, 2, tokens);
|
599
|
+
straceLog('==> !ASSERT', assertionToken);
|
600
|
+
if(assertionToken.token.type == 'OTHER' && assertionToken.token.value == '{'){
|
601
|
+
hooks.push({
|
602
|
+
index: assertionToken.token.ti,
|
603
|
+
value: ' useDefaultForPackages: '+(isAs?'false':usedDefault.toString())+', '
|
604
|
+
})
|
605
|
+
} else {
|
606
|
+
result += 'useDefaultForPackages: '+(isAs?'false':usedDefault.toString())+', '
|
607
|
+
}
|
608
|
+
i += 3;
|
609
|
+
} else {
|
610
|
+
result += ", { useDefaultForPackages: "+(isAs?'false':usedDefault.toString())+" }"
|
611
|
+
}
|
612
|
+
|
613
|
+
continue;
|
614
|
+
}
|
615
|
+
|
616
|
+
if (tokens[i-1]?.value !== '.' && token.type === 'IDENTIFIER' && (token.value === 'imp' || token.value === 'inc')) {
|
617
|
+
straceLog('!+DIRECTIVE imp()');
|
618
|
+
let { token: t1 } = gnextToken(i, 1, tokens) || {};
|
619
|
+
let { token: t2 } = gnextToken(i, 2, tokens) || {};
|
620
|
+
let r = '';
|
621
|
+
|
622
|
+
if(t1?.value == '('){
|
623
|
+
if(t2?.type == 'STRING'){
|
624
|
+
r = useImp(t2, options);
|
625
|
+
}
|
626
|
+
} else if(t1?.type == 'STRING'){
|
627
|
+
r = useImp(t1, options);
|
628
|
+
}
|
629
|
+
|
630
|
+
if(r) {
|
631
|
+
updateAliases(aliases);
|
632
|
+
}
|
633
|
+
}
|
634
|
+
|
635
|
+
if (
|
636
|
+
tokens[i-1]?.value !== '.' &&
|
637
|
+
token.type === 'IDENTIFIER' &&
|
638
|
+
token.value === 'pub' &&
|
639
|
+
nextToken &&
|
640
|
+
nextToken.type === 'IDENTIFIER' &&
|
641
|
+
nextToken.value &&
|
642
|
+
nextToken.value !== 'undefined' && !options.keepImports
|
643
|
+
) {
|
644
|
+
straceLog('!+DIRECTIVE pub()');
|
645
|
+
let next = {...nextToken}, isClass = false;
|
646
|
+
if(next.value == 'default'){
|
647
|
+
i += 2;
|
648
|
+
}
|
649
|
+
if(next.value == 'class'){
|
650
|
+
next.value = gnextToken(i, n + 1, tokens)?.token.value || "default";
|
651
|
+
isClass = true;
|
652
|
+
}
|
653
|
+
if(next.value == 'fn' || next.value == 'function'){
|
654
|
+
next.value = gnextToken(i, n + 1, tokens)?.token.value || "default";
|
655
|
+
isClass = true;
|
656
|
+
if(nextToken && nextToken.value == "fn"){
|
657
|
+
nextToken.value = "function";
|
658
|
+
}
|
659
|
+
}
|
660
|
+
straceLog('==> !PUBLIC', next.value);
|
661
|
+
hooks.push({
|
662
|
+
index: i + 1,
|
663
|
+
value: `"${next.value}", ${isClass ? `${next.value} = ` : ''}`,
|
664
|
+
});
|
665
|
+
}
|
666
|
+
|
667
|
+
const aliasType = aliases[token.type];
|
668
|
+
// if(token.value == 'sidewest') console.log(aliases, token.value, token.type);
|
669
|
+
if (aliasType && Object.keys(aliasType).includes(token.value)) {
|
670
|
+
straceLog('!+DIRECTIVE alias()', token.type);
|
671
|
+
const aliasValue = aliasType[token.value];
|
672
|
+
if (typeof aliasValue === 'function') {
|
673
|
+
straceLog('==> INFO Execute alias:', token.value);
|
674
|
+
result += aliasValue(token, tokens, result, hooks, i, (n) => i = n) || "";
|
675
|
+
} else {
|
676
|
+
straceLog('==> INFO Literal alias:', token.value);
|
677
|
+
result += aliasValue;
|
678
|
+
}
|
679
|
+
continue;
|
680
|
+
}
|
681
|
+
|
682
|
+
// process.stdout.write(token.value);
|
683
|
+
result += token.value;
|
684
|
+
if (hooks.length) {
|
685
|
+
hooks.forEach((hook, ind) => {
|
686
|
+
if (i == hook.index) {
|
687
|
+
result += hook.value;
|
688
|
+
hooks.splice(ind, 1);
|
689
|
+
}
|
690
|
+
});
|
691
|
+
}
|
692
|
+
|
693
|
+
if (token.type === "COMMENT" && token.value.startsWith('#include')) {
|
694
|
+
const includeContent = token.value.split(' ')[1].trim() || '';
|
695
|
+
const _o = {...options};
|
696
|
+
const r = includeFile(includeContent, _o);
|
697
|
+
if(r){
|
698
|
+
result += r == 'intentional-nothing' ? '' : r;
|
699
|
+
updateAliases(aliases, _o.aliases);
|
700
|
+
}
|
701
|
+
}
|
702
|
+
}
|
703
|
+
|
704
|
+
if(options.included){
|
705
|
+
options.aliases = aliases;
|
706
|
+
}
|
707
|
+
// console.log(aliases);
|
708
|
+
// console.log(result);
|
709
|
+
// return "";
|
710
|
+
return result;
|
711
|
+
}
|
712
|
+
|
713
|
+
const compileCivetStuff = (file, options) => {
|
714
|
+
straceLog('compileCivet(currentFile)');
|
715
|
+
const preCompileOptions = {
|
716
|
+
filename: file.path,
|
717
|
+
...options
|
718
|
+
};
|
719
|
+
straceLog('prepareOptions(currentFile).as(', `"${JSON.stringify(preCompileOptions)}"`, ')');
|
720
|
+
|
721
|
+
if(options?.type == 'js' || file?.path?.endsWith('.js')){
|
722
|
+
return {
|
723
|
+
compiled: file?.content || "",
|
724
|
+
options: preCompileOptions
|
725
|
+
}
|
726
|
+
}
|
727
|
+
|
728
|
+
const prepared = compileRewStuff(file.content, preCompileOptions);
|
729
|
+
|
730
|
+
// console.log(prepared);
|
731
|
+
|
732
|
+
const compileOptions = {
|
733
|
+
...preCompileOptions,
|
734
|
+
bare: true,
|
735
|
+
filename: file.path,
|
736
|
+
inlineMap: false,
|
737
|
+
js: true
|
738
|
+
};
|
739
|
+
|
740
|
+
let compiled = options.async ? compileCivet(prepared, compileOptions) : compileCivet(prepared, {
|
741
|
+
...compileOptions,
|
742
|
+
sync: true
|
743
|
+
});
|
744
|
+
straceLog('==> !COMPILER civetCompile(fileContent)');
|
745
|
+
|
746
|
+
return {
|
747
|
+
compiled,
|
748
|
+
options: preCompileOptions
|
749
|
+
};
|
750
|
+
}
|
751
|
+
|
752
|
+
module.exports.compileCivetStuff = compileCivetStuff;
|