functionalscript 0.6.8 → 0.6.10

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,10 +1,12 @@
1
1
  import * as parser from "./module.f.js";
2
2
  import * as tokenizer from "../tokenizer/module.f.js";
3
- import { toArray } from "../../types/list/module.f.js";
3
+ import * as list from "../../types/list/module.f.js";
4
+ const { toArray } = list;
4
5
  import { sort } from "../../types/object/module.f.js";
5
6
  import * as encoding from "../../text/utf16/module.f.js";
6
7
  import { stringifyAsTree } from "../serializer/module.f.js";
7
- const tokenizeString = s => toArray(tokenizer.tokenize(encoding.stringToList(s)));
8
+ import { stringify } from "../../json/module.f.js";
9
+ const tokenizeString = s => toArray(tokenizer.tokenize(encoding.stringToList(s))(''));
8
10
  const stringifyDjsModule = stringifyAsTree(sort);
9
11
  export default {
10
12
  valid: [
@@ -173,6 +175,17 @@ export default {
173
175
  throw result;
174
176
  }
175
177
  },
178
+ () => {
179
+ const tokenList = tokenizeString('export default {a: 1}');
180
+ const obj = parser.parseFromTokens(tokenList);
181
+ if (obj[0] !== 'ok') {
182
+ throw obj;
183
+ }
184
+ const result = stringifyDjsModule(obj[1]);
185
+ if (result !== '[[],[{"a":1}]]') {
186
+ throw result;
187
+ }
188
+ },
176
189
  () => {
177
190
  const tokenList = tokenizeString('export default 1234567890n');
178
191
  const obj = parser.parseFromTokens(tokenList);
@@ -194,6 +207,28 @@ export default {
194
207
  if (result !== '[[],[["array",[1234567890n]]]]') {
195
208
  throw result;
196
209
  }
210
+ },
211
+ () => {
212
+ const tokenList = tokenizeString('export default [1,]');
213
+ const obj = parser.parseFromTokens(tokenList);
214
+ if (obj[0] !== 'ok') {
215
+ throw obj;
216
+ }
217
+ const result = stringifyDjsModule(obj[1]);
218
+ if (result !== '[[],[["array",[1]]]]') {
219
+ throw result;
220
+ }
221
+ },
222
+ () => {
223
+ const tokenList = tokenizeString('export default {"a":1,}');
224
+ const obj = parser.parseFromTokens(tokenList);
225
+ if (obj[0] !== 'ok') {
226
+ throw obj;
227
+ }
228
+ const result = stringifyDjsModule(obj[1]);
229
+ if (result !== '[[],[{"a":1}]]') {
230
+ throw result;
231
+ }
197
232
  }
198
233
  ],
199
234
  invalid: [
@@ -203,8 +238,8 @@ export default {
203
238
  if (obj[0] !== 'error') {
204
239
  throw obj;
205
240
  }
206
- if (obj[1] !== 'unexpected end') {
207
- throw obj;
241
+ if (obj[1].message !== 'unexpected end') {
242
+ throw obj[1].message;
208
243
  }
209
244
  },
210
245
  () => {
@@ -213,8 +248,8 @@ export default {
213
248
  if (obj[0] !== 'error') {
214
249
  throw obj;
215
250
  }
216
- if (obj[1] !== 'unexpected token') {
217
- throw obj;
251
+ if (obj[1].message !== 'unexpected token') {
252
+ throw obj[1].message;
218
253
  }
219
254
  },
220
255
  () => {
@@ -223,8 +258,8 @@ export default {
223
258
  if (obj[0] !== 'error') {
224
259
  throw obj;
225
260
  }
226
- if (obj[1] !== 'unexpected token') {
227
- throw obj;
261
+ if (obj[1].message !== 'unexpected token') {
262
+ throw obj[1].message;
228
263
  }
229
264
  },
230
265
  () => {
@@ -233,8 +268,8 @@ export default {
233
268
  if (obj[0] !== 'error') {
234
269
  throw obj;
235
270
  }
236
- if (obj[1] !== 'unexpected token') {
237
- throw obj;
271
+ if (obj[1].message !== 'unexpected token') {
272
+ throw obj[1].message;
238
273
  }
239
274
  },
240
275
  () => {
@@ -243,8 +278,8 @@ export default {
243
278
  if (obj[0] !== 'error') {
244
279
  throw obj;
245
280
  }
246
- if (obj[1] !== 'unexpected token') {
247
- throw obj;
281
+ if (obj[1].message !== 'unexpected token') {
282
+ throw obj[1].message;
248
283
  }
249
284
  },
250
285
  () => {
@@ -253,8 +288,8 @@ export default {
253
288
  if (obj[0] !== 'error') {
254
289
  throw obj;
255
290
  }
256
- if (obj[1] !== 'unexpected token') {
257
- throw obj;
291
+ if (obj[1].message !== 'unexpected token') {
292
+ throw obj[1].message;
258
293
  }
259
294
  },
260
295
  () => {
@@ -263,18 +298,8 @@ export default {
263
298
  if (obj[0] !== 'error') {
264
299
  throw obj;
265
300
  }
266
- if (obj[1] !== 'unexpected end') {
267
- throw obj;
268
- }
269
- },
270
- () => {
271
- const tokenList = tokenizeString('export default [1,]');
272
- const obj = parser.parseFromTokens(tokenList);
273
- if (obj[0] !== 'error') {
274
- throw obj;
275
- }
276
- if (obj[1] !== 'unexpected token') {
277
- throw obj;
301
+ if (obj[1].message !== 'unexpected end') {
302
+ throw obj[1].message;
278
303
  }
279
304
  },
280
305
  () => {
@@ -283,8 +308,8 @@ export default {
283
308
  if (obj[0] !== 'error') {
284
309
  throw obj;
285
310
  }
286
- if (obj[1] !== 'unexpected token') {
287
- throw obj;
311
+ if (obj[1].message !== 'unexpected token') {
312
+ throw obj[1].message;
288
313
  }
289
314
  },
290
315
  () => {
@@ -293,8 +318,8 @@ export default {
293
318
  if (obj[0] !== 'error') {
294
319
  throw obj;
295
320
  }
296
- if (obj[1] !== 'unexpected token') {
297
- throw obj;
321
+ if (obj[1].message !== 'unexpected token') {
322
+ throw obj[1].message;
298
323
  }
299
324
  },
300
325
  () => {
@@ -303,8 +328,8 @@ export default {
303
328
  if (obj[0] !== 'error') {
304
329
  throw obj;
305
330
  }
306
- if (obj[1] !== 'unexpected token') {
307
- throw obj;
331
+ if (obj[1].message !== 'unexpected token') {
332
+ throw obj[1].message;
308
333
  }
309
334
  },
310
335
  () => {
@@ -313,8 +338,8 @@ export default {
313
338
  if (obj[0] !== 'error') {
314
339
  throw obj;
315
340
  }
316
- if (obj[1] !== 'unexpected token') {
317
- throw obj;
341
+ if (obj[1].message !== 'unexpected token') {
342
+ throw obj[1].message;
318
343
  }
319
344
  },
320
345
  () => {
@@ -323,8 +348,8 @@ export default {
323
348
  if (obj[0] !== 'error') {
324
349
  throw obj;
325
350
  }
326
- if (obj[1] !== 'unexpected token') {
327
- throw obj;
351
+ if (obj[1].message !== 'unexpected token') {
352
+ throw obj[1].message;
328
353
  }
329
354
  },
330
355
  () => {
@@ -333,8 +358,8 @@ export default {
333
358
  if (obj[0] !== 'error') {
334
359
  throw obj;
335
360
  }
336
- if (obj[1] !== 'unexpected token') {
337
- throw obj;
361
+ if (obj[1].message !== 'unexpected token') {
362
+ throw obj[1].message;
338
363
  }
339
364
  },
340
365
  () => {
@@ -343,8 +368,8 @@ export default {
343
368
  if (obj[0] !== 'error') {
344
369
  throw obj;
345
370
  }
346
- if (obj[1] !== 'unexpected token') {
347
- throw obj;
371
+ if (obj[1].message !== 'unexpected token') {
372
+ throw obj[1].message;
348
373
  }
349
374
  },
350
375
  () => {
@@ -353,8 +378,8 @@ export default {
353
378
  if (obj[0] !== 'error') {
354
379
  throw obj;
355
380
  }
356
- if (obj[1] !== 'unexpected token') {
357
- throw obj;
381
+ if (obj[1].message !== 'unexpected token') {
382
+ throw obj[1].message;
358
383
  }
359
384
  },
360
385
  () => {
@@ -363,8 +388,8 @@ export default {
363
388
  if (obj[0] !== 'error') {
364
389
  throw obj;
365
390
  }
366
- if (obj[1] !== 'unexpected token') {
367
- throw obj;
391
+ if (obj[1].message !== 'unexpected token') {
392
+ throw obj[1].message;
368
393
  }
369
394
  },
370
395
  () => {
@@ -373,68 +398,71 @@ export default {
373
398
  if (obj[0] !== 'error') {
374
399
  throw obj;
375
400
  }
376
- if (obj[1] !== 'unexpected end') {
377
- throw obj;
401
+ if (obj[1].message !== 'unexpected end') {
402
+ throw obj[1].message;
378
403
  }
379
404
  },
380
405
  () => {
381
- const tokenList = tokenizeString('export default {"1":2,}');
406
+ const tokenList = tokenizeString('export default {,"1":2}');
382
407
  const obj = parser.parseFromTokens(tokenList);
383
408
  if (obj[0] !== 'error') {
384
409
  throw obj;
385
410
  }
386
- if (obj[1] !== 'unexpected token') {
387
- throw obj;
411
+ if (obj[1].message !== 'unexpected token') {
412
+ throw obj[1].message;
388
413
  }
389
414
  },
390
415
  () => {
391
- const tokenList = tokenizeString('export default {,"1":2}');
416
+ const tokenList = tokenizeString('export default }');
392
417
  const obj = parser.parseFromTokens(tokenList);
393
418
  if (obj[0] !== 'error') {
394
419
  throw obj;
395
420
  }
396
- if (obj[1] !== 'unexpected token') {
397
- throw obj;
421
+ if (obj[1].message !== 'unexpected token') {
422
+ throw obj[1].message;
398
423
  }
399
424
  },
400
425
  () => {
401
- const tokenList = tokenizeString('export default }');
426
+ const tokenList = tokenizeString('export default [{]}');
402
427
  const obj = parser.parseFromTokens(tokenList);
403
428
  if (obj[0] !== 'error') {
404
429
  throw obj;
405
430
  }
406
- if (obj[1] !== 'unexpected token') {
407
- throw obj;
431
+ if (obj[1].message !== 'unexpected token') {
432
+ throw obj[1].message;
408
433
  }
409
434
  },
410
435
  () => {
411
- const tokenList = tokenizeString('export default [{]}');
436
+ const tokenList = tokenizeString('export default {[}]');
412
437
  const obj = parser.parseFromTokens(tokenList);
413
438
  if (obj[0] !== 'error') {
414
439
  throw obj;
415
440
  }
416
- if (obj[1] !== 'unexpected token') {
417
- throw obj;
441
+ if (obj[1].message !== 'unexpected token') {
442
+ throw obj[1].message;
418
443
  }
419
444
  },
420
445
  () => {
421
- const tokenList = tokenizeString('export default {[}]');
446
+ const tokenList = tokenizeString('export default 10-5');
422
447
  const obj = parser.parseFromTokens(tokenList);
423
448
  if (obj[0] !== 'error') {
424
449
  throw obj;
425
450
  }
426
- if (obj[1] !== 'unexpected token') {
427
- throw obj;
451
+ if (obj[1].message !== 'unexpected token') {
452
+ throw obj[1].message;
428
453
  }
429
454
  },
455
+ ],
456
+ errorMetadata: [
430
457
  () => {
431
- const tokenList = tokenizeString('export default 10-5');
458
+ const tokenList = tokenizeString('export default [,]');
432
459
  const obj = parser.parseFromTokens(tokenList);
433
460
  if (obj[0] !== 'error') {
434
461
  throw obj;
435
462
  }
436
- if (obj[1] !== 'unexpected token') {
437
- throw obj;
463
+ const errorString = stringify(sort)(obj[1]);
464
+ if (errorString !== '{"message":"unexpected token","metadata":{"column":18,"line":1,"path":""}}') {
465
+ throw errorString;
438
466
  }
439
467
  },
440
468
  ],
@@ -537,7 +565,7 @@ export default {
537
565
  if (obj[0] !== 'error') {
538
566
  throw obj;
539
567
  }
540
- if (obj[1] !== 'const not found') {
568
+ if (obj[1].message !== 'const not found') {
541
569
  throw obj;
542
570
  }
543
571
  },
@@ -547,7 +575,7 @@ export default {
547
575
  if (obj[0] !== 'error') {
548
576
  throw obj;
549
577
  }
550
- if (obj[1] !== 'unexpected token') {
578
+ if (obj[1].message !== 'unexpected token') {
551
579
  throw obj;
552
580
  }
553
581
  },
@@ -557,7 +585,7 @@ export default {
557
585
  if (obj[0] !== 'error') {
558
586
  throw obj;
559
587
  }
560
- if (obj[1] !== 'unexpected token') {
588
+ if (obj[1].message !== 'unexpected token') {
561
589
  throw obj;
562
590
  }
563
591
  },
@@ -615,7 +643,7 @@ export default {
615
643
  if (obj[0] !== 'error') {
616
644
  throw obj;
617
645
  }
618
- if (obj[1] !== 'unexpected token') {
646
+ if (obj[1].message !== 'unexpected token') {
619
647
  throw obj;
620
648
  }
621
649
  },
@@ -625,7 +653,7 @@ export default {
625
653
  if (obj[0] !== 'error') {
626
654
  throw obj;
627
655
  }
628
- if (obj[1] !== 'unexpected token') {
656
+ if (obj[1].message !== 'unexpected token') {
629
657
  throw obj;
630
658
  }
631
659
  },
@@ -635,7 +663,7 @@ export default {
635
663
  if (obj[0] !== 'error') {
636
664
  throw obj;
637
665
  }
638
- if (obj[1] !== 'duplicate id') {
666
+ if (obj[1].message !== 'duplicate id') {
639
667
  throw obj;
640
668
  }
641
669
  },
@@ -682,7 +710,7 @@ export default {
682
710
  if (obj[0] !== 'error') {
683
711
  throw obj;
684
712
  }
685
- if (obj[1] !== 'unexpected token') {
713
+ if (obj[1].message !== 'unexpected token') {
686
714
  throw obj;
687
715
  }
688
716
  },
@@ -692,7 +720,7 @@ export default {
692
720
  if (obj[0] !== 'error') {
693
721
  throw obj;
694
722
  }
695
- if (obj[1] !== 'unexpected token') {
723
+ if (obj[1].message !== 'unexpected token') {
696
724
  throw obj;
697
725
  }
698
726
  },
@@ -702,7 +730,7 @@ export default {
702
730
  if (obj[0] !== 'error') {
703
731
  throw obj;
704
732
  }
705
- if (obj[1] !== 'unexpected token') {
733
+ if (obj[1].message !== 'unexpected token') {
706
734
  throw obj;
707
735
  }
708
736
  },
@@ -712,7 +740,7 @@ export default {
712
740
  if (obj[0] !== 'error') {
713
741
  throw obj;
714
742
  }
715
- if (obj[1] !== 'unexpected token') {
743
+ if (obj[1].message !== 'unexpected token') {
716
744
  throw obj;
717
745
  }
718
746
  },
@@ -722,7 +750,7 @@ export default {
722
750
  if (obj[0] !== 'error') {
723
751
  throw obj;
724
752
  }
725
- if (obj[1] !== 'duplicate id') {
753
+ if (obj[1].message !== 'duplicate id') {
726
754
  throw obj;
727
755
  }
728
756
  },
@@ -732,7 +760,7 @@ export default {
732
760
  if (obj[0] !== 'error') {
733
761
  throw obj;
734
762
  }
735
- if (obj[1] !== 'duplicate id') {
763
+ if (obj[1].message !== 'duplicate id') {
736
764
  throw obj;
737
765
  }
738
766
  },
@@ -4,5 +4,9 @@ export type DjsToken = {
4
4
  readonly kind: 'true' | 'false' | 'null' | 'undefined';
5
5
  } | {
6
6
  readonly kind: '{' | '}' | ':' | ',' | '[' | ']' | '.' | '=';
7
- } | jsTokenizer.StringToken | jsTokenizer.NumberToken | jsTokenizer.ErrorToken | jsTokenizer.IdToken | jsTokenizer.BigIntToken | jsTokenizer.WhitespaceToken | jsTokenizer.NewLineToken | jsTokenizer.CommentToken;
8
- export declare const tokenize: (input: list.List<number>) => list.List<DjsToken>;
7
+ } | jsTokenizer.StringToken | jsTokenizer.NumberToken | jsTokenizer.ErrorToken | jsTokenizer.IdToken | jsTokenizer.BigIntToken | jsTokenizer.WhitespaceToken | jsTokenizer.NewLineToken | jsTokenizer.CommentToken | jsTokenizer.EofToken;
8
+ export type DjsTokenWithMetadata = {
9
+ readonly token: DjsToken;
10
+ readonly metadata: jsTokenizer.TokenMetadata;
11
+ };
12
+ export declare const tokenize: (input: list.List<number>) => (path: string) => list.List<DjsTokenWithMetadata>;
@@ -25,22 +25,21 @@ const mapToken = input => {
25
25
  case 'undefined':
26
26
  case '//':
27
27
  case '/*':
28
+ case 'eof':
28
29
  case 'error': return [input];
29
30
  default: return jsTokenizer.isKeywordToken(input) ? [{ kind: 'id', value: input.kind }] : [{ kind: 'error', message: 'invalid token' }];
30
31
  }
31
32
  };
32
33
  const parseDefaultState = input => {
33
- if (input === null)
34
- return [empty, { kind: 'def' }];
35
34
  switch (input.kind) {
35
+ case 'eof': return [[{ kind: 'eof' }], { kind: 'def' }];
36
36
  case '-': return [empty, { kind: '-' }];
37
37
  default: return [mapToken(input), { kind: 'def' }];
38
38
  }
39
39
  };
40
40
  const parseMinusState = input => {
41
- if (input === null)
42
- return [[{ kind: 'error', message: 'invalid token' }], { kind: 'def' }];
43
41
  switch (input.kind) {
42
+ case 'eof': return [[{ kind: 'error', message: 'invalid token' }, { kind: 'eof' }], { kind: 'def' }];
44
43
  case '-': return [[{ kind: 'error', message: 'invalid token' }], { kind: '-' }];
45
44
  case 'bigint': return [[{ kind: 'bigint', value: -1n * input.value }], { kind: 'def' }];
46
45
  case 'number': return [[{ kind: 'number', bf: multiply(input.bf)(-1n), value: `-${input.value}` }], { kind: 'def' }];
@@ -53,7 +52,13 @@ const scanToken = state => input => {
53
52
  default: return parseDefaultState(input);
54
53
  }
55
54
  };
56
- export const tokenize = input => {
57
- const jsTokens = jsTokenizer.tokenize(input);
58
- return flat(stateScan(scanToken)({ kind: 'def' })(list.concat(jsTokens)([null])));
55
+ const mapTokenWithMetadata = metadata => token => { return { token, metadata }; };
56
+ const scanTokenWithMetadata = state => (input) => {
57
+ const [djsTokens, newState] = scanToken(state)(input.token);
58
+ const djsTokensWithMetadata = list.map(mapTokenWithMetadata(input.metadata))(djsTokens);
59
+ return [djsTokensWithMetadata, newState];
60
+ };
61
+ export const tokenize = input => path => {
62
+ const jsTokens = jsTokenizer.tokenize(input)(path);
63
+ return flat(stateScan(scanTokenWithMetadata)({ kind: 'def' })(jsTokens));
59
64
  };
@@ -3,5 +3,6 @@ declare const _default: {
3
3
  id: (() => void)[];
4
4
  keywords: (() => void)[];
5
5
  comments: (() => void)[];
6
+ metadata: (() => void)[];
6
7
  };
7
8
  export default _default;