@makano/rew 1.5.5 → 1.5.8

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