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