bobe 0.0.40 → 0.0.42

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,4 @@
1
- import { Queue, isNum, matchIdStart2, matchId, jsVarRegexp } from 'bobe-shared';
1
+ import { Queue, isNum, matchIdStart2, matchId, escapeMap, jsVarRegexp } from 'bobe-shared';
2
2
  import { getPulling, setPulling, Keys, Computed, Effect, toRaw, runWithPulling, Scope, deepSignal, Store, shareSignal, effect } from 'aoye';
3
3
  export * from 'aoye';
4
4
 
@@ -234,7 +234,7 @@ class Tokenizer {
234
234
  if (!this.token) return false;
235
235
  return this.token.type & TokenType.Identifier && this.token.value === Tokenizer.EofId;
236
236
  }
237
- setToken(type, value) {
237
+ setToken(type, value, dt = 1) {
238
238
  this.token = {
239
239
  type,
240
240
  typeName: TokenType[type],
@@ -246,11 +246,11 @@ class Tokenizer {
246
246
  column: this.preCol
247
247
  },
248
248
  end: {
249
- offset: this.i + 1,
249
+ offset: this.i + dt,
250
250
  line: this.line,
251
- column: this.column + 1
251
+ column: this.column + dt
252
252
  },
253
- source: this.code.slice(this.preI, this.i + 1)
253
+ source: this.code.slice(this.preI, this.i + dt)
254
254
  } : null
255
255
  };
256
256
  this.isFirstToken = false;
@@ -351,7 +351,6 @@ class Tokenizer {
351
351
  }
352
352
  comment() {
353
353
  this.getComment();
354
- this.next();
355
354
  }
356
355
  condExp() {
357
356
  {
@@ -361,26 +360,22 @@ class Tokenizer {
361
360
  }
362
361
  let value = '';
363
362
  this.token = null;
364
- try {
365
- if (this.code[this.i] === '\n') {
366
- this.setToken(TokenType.Identifier, true);
367
- return this.token;
368
- }
369
- while (this.code[this.i + 1] !== '\n') {
370
- value += this.code[this.i];
371
- this.next();
363
+ let char = this.code[this.i];
364
+ while (char !== '\n') {
365
+ if (char === '"' || char === "'") {
366
+ value += char + this.getStr(char);
372
367
  }
373
368
  value += this.code[this.i];
374
- const trimmed = value.replace(/\/\/[\s\S]+/, '').trim();
375
- this.setToken(TokenType.Identifier, trimmed ? trimmed : true);
376
- return this.token;
377
- } finally {
378
369
  this.next();
379
- this.handledTokens.push(this.token);
380
- {
381
- this.needLoc = false;
382
- }
370
+ char = this.code[this.i];
383
371
  }
372
+ const trimmed = value.replace(/\/\/[\s\S]+/, '').trim();
373
+ this.setToken(TokenType.Identifier, trimmed ? trimmed : true, 0);
374
+ this.handledTokens.push(this.token);
375
+ {
376
+ this.needLoc = false;
377
+ }
378
+ return this.token;
384
379
  }
385
380
  isEol(i) {
386
381
  return this.code[i] === '\n' || this.code[i] === '/';
@@ -393,28 +388,21 @@ class Tokenizer {
393
388
  }
394
389
  this.token = null;
395
390
  let value = '';
396
- try {
397
- const char = this.code[this.i];
398
- if (char === ';' || char === '\n') {
399
- this.setToken(TokenType.Identifier, value);
400
- return this.token;
401
- }
402
- let nextC = this.code[this.i + 1];
403
- while (nextC !== ';' && nextC !== '\n') {
404
- value += this.code[this.i];
405
- this.next();
406
- nextC = this.code[this.i + 1];
391
+ let char = this.code[this.i];
392
+ while (char !== ';' && char !== '\n') {
393
+ if (char === '"' || char === "'") {
394
+ value += char + this.getStr(char);
407
395
  }
408
396
  value += this.code[this.i];
409
- this.setToken(TokenType.Identifier, value);
410
- return this.token;
411
- } finally {
412
397
  this.next();
413
- this.handledTokens.push(this.token);
414
- {
415
- this.needLoc = false;
416
- }
398
+ char = this.code[this.i];
399
+ }
400
+ this.setToken(TokenType.Identifier, value, 0);
401
+ this.handledTokens.push(this.token);
402
+ {
403
+ this.needLoc = false;
417
404
  }
405
+ return this.token;
418
406
  }
419
407
  peekChar() {
420
408
  let i = this.i;
@@ -472,10 +460,8 @@ class Tokenizer {
472
460
  startLine = this.line,
473
461
  startCol = this.preCol;
474
462
  let inComment,
475
- inString,
476
463
  count = 0,
477
- value = '',
478
- backslashCount = 0;
464
+ value = '';
479
465
  while (1) {
480
466
  const char = this.code[this.i];
481
467
  if (char === undefined) {
@@ -488,11 +474,6 @@ class Tokenizer {
488
474
  inComment = null;
489
475
  value += this.code[this.i];
490
476
  this.next();
491
- } else if (inString) {
492
- if (char === inString && backslashCount % 2 === 0) {
493
- inString = null;
494
- }
495
- backslashCount = char === '\\' ? backslashCount + 1 : 0;
496
477
  } else {
497
478
  if (char === '/' && nextChar === '/') {
498
479
  inComment = 'single';
@@ -502,15 +483,15 @@ class Tokenizer {
502
483
  inComment = 'multi';
503
484
  value += this.code[this.i];
504
485
  this.next();
505
- } else if (char === "'" || char === '"' || char === '`') {
506
- inString = char;
486
+ } else if (char === "'" || char === '"') {
487
+ value += char + this.getStr(char);
507
488
  } else if (char === '{') {
508
489
  count++;
509
490
  } else if (char === '}') {
510
491
  count--;
511
492
  }
512
493
  }
513
- if (count === 0 && inString == null && inComment == null) {
494
+ if (count === 0 && inComment == null) {
514
495
  return value.slice(1);
515
496
  }
516
497
  value += this.code[this.i];
@@ -706,7 +687,7 @@ class Tokenizer {
706
687
  }
707
688
  this.setToken(tokenType, realValue);
708
689
  }
709
- str(char) {
690
+ getStr(head, parseEscape = true) {
710
691
  const startOffset = this.preI,
711
692
  startLine = this.line,
712
693
  startCol = this.preCol;
@@ -725,11 +706,18 @@ class Tokenizer {
725
706
  continuousBackslashCount = 0;
726
707
  }
727
708
  this.next();
728
- if (nextC === char && memoCount % 2 === 0) {
709
+ if (nextC === head && memoCount % 2 === 0) {
729
710
  break;
730
711
  }
731
- value += nextC;
712
+ escapeMap[nextC];
713
+ {
714
+ value += nextC;
715
+ }
732
716
  }
717
+ return value;
718
+ }
719
+ str(char) {
720
+ const value = this.getStr(char, false);
733
721
  this.setToken(TokenType.String, value);
734
722
  }
735
723
  number(char) {
@@ -1169,7 +1157,7 @@ class Compiler {
1169
1157
  }
1170
1158
  parseLoopNode(node) {
1171
1159
  const forLoc = this.tokenizer.token.loc ?? this.tokenizer.emptyLoc();
1172
- this.tokenizer.nextToken();
1160
+ this.tokenizer.jsExp();
1173
1161
  const collection = this.parseJsExp();
1174
1162
  if (!collection.value && collection.value !== 0) {
1175
1163
  this.addError(ParseErrorCode.MISSING_FOR_COLLECTION, '"for" 缺少集合表达式', forLoc, node);
@@ -1630,7 +1618,7 @@ class Interpreter {
1630
1618
  return _node;
1631
1619
  }
1632
1620
  forDeclaration() {
1633
- const arrExp = this.tokenizer.nextToken().value;
1621
+ const arrExp = this.tokenizer.jsExp().value;
1634
1622
  this.tokenizer.nextToken();
1635
1623
  const itemToken = this.tokenizer.nextToken();
1636
1624
  const isDestruct = itemToken.type === TokenType.InsertionExp;