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.
package/dist/bobe.cjs.js CHANGED
@@ -226,7 +226,7 @@ class Tokenizer {
226
226
  if (!this.token) return false;
227
227
  return this.token.type & TokenType.Identifier && this.token.value === Tokenizer.EofId;
228
228
  }
229
- setToken(type, value) {
229
+ setToken(type, value, dt = 1) {
230
230
  this.token = {
231
231
  type,
232
232
  typeName: TokenType[type],
@@ -325,28 +325,23 @@ class Tokenizer {
325
325
  }
326
326
  comment() {
327
327
  this.getComment();
328
- this.next();
329
328
  }
330
329
  condExp() {
331
330
  let value = '';
332
331
  this.token = null;
333
- try {
334
- if (this.code[this.i] === '\n') {
335
- this.setToken(TokenType.Identifier, true);
336
- return this.token;
337
- }
338
- while (this.code[this.i + 1] !== '\n') {
339
- value += this.code[this.i];
340
- this.next();
332
+ let char = this.code[this.i];
333
+ while (char !== '\n') {
334
+ if (char === '"' || char === "'") {
335
+ value += char + this.getStr(char);
341
336
  }
342
337
  value += this.code[this.i];
343
- const trimmed = value.replace(/\/\/[\s\S]+/, '').trim();
344
- this.setToken(TokenType.Identifier, trimmed ? trimmed : true);
345
- return this.token;
346
- } finally {
347
338
  this.next();
348
- this.handledTokens.push(this.token);
339
+ char = this.code[this.i];
349
340
  }
341
+ const trimmed = value.replace(/\/\/[\s\S]+/, '').trim();
342
+ this.setToken(TokenType.Identifier, trimmed ? trimmed : true, 0);
343
+ this.handledTokens.push(this.token);
344
+ return this.token;
350
345
  }
351
346
  isEol(i) {
352
347
  return this.code[i] === '\n' || this.code[i] === '/';
@@ -354,25 +349,18 @@ class Tokenizer {
354
349
  jsExp() {
355
350
  this.token = null;
356
351
  let value = '';
357
- try {
358
- const char = this.code[this.i];
359
- if (char === ';' || char === '\n') {
360
- this.setToken(TokenType.Identifier, value);
361
- return this.token;
362
- }
363
- let nextC = this.code[this.i + 1];
364
- while (nextC !== ';' && nextC !== '\n') {
365
- value += this.code[this.i];
366
- this.next();
367
- nextC = this.code[this.i + 1];
352
+ let char = this.code[this.i];
353
+ while (char !== ';' && char !== '\n') {
354
+ if (char === '"' || char === "'") {
355
+ value += char + this.getStr(char);
368
356
  }
369
357
  value += this.code[this.i];
370
- this.setToken(TokenType.Identifier, value);
371
- return this.token;
372
- } finally {
373
358
  this.next();
374
- this.handledTokens.push(this.token);
359
+ char = this.code[this.i];
375
360
  }
361
+ this.setToken(TokenType.Identifier, value, 0);
362
+ this.handledTokens.push(this.token);
363
+ return this.token;
376
364
  }
377
365
  peekChar() {
378
366
  let i = this.i;
@@ -430,10 +418,8 @@ class Tokenizer {
430
418
  startLine = this.line,
431
419
  startCol = this.preCol;
432
420
  let inComment,
433
- inString,
434
421
  count = 0,
435
- value = '',
436
- backslashCount = 0;
422
+ value = '';
437
423
  while (1) {
438
424
  const char = this.code[this.i];
439
425
  if (char === undefined) {
@@ -446,11 +432,6 @@ class Tokenizer {
446
432
  inComment = null;
447
433
  value += this.code[this.i];
448
434
  this.next();
449
- } else if (inString) {
450
- if (char === inString && backslashCount % 2 === 0) {
451
- inString = null;
452
- }
453
- backslashCount = char === '\\' ? backslashCount + 1 : 0;
454
435
  } else {
455
436
  if (char === '/' && nextChar === '/') {
456
437
  inComment = 'single';
@@ -460,15 +441,15 @@ class Tokenizer {
460
441
  inComment = 'multi';
461
442
  value += this.code[this.i];
462
443
  this.next();
463
- } else if (char === "'" || char === '"' || char === '`') {
464
- inString = char;
444
+ } else if (char === "'" || char === '"') {
445
+ value += char + this.getStr(char);
465
446
  } else if (char === '{') {
466
447
  count++;
467
448
  } else if (char === '}') {
468
449
  count--;
469
450
  }
470
451
  }
471
- if (count === 0 && inString == null && inComment == null) {
452
+ if (count === 0 && inComment == null) {
472
453
  return value.slice(1);
473
454
  }
474
455
  value += this.code[this.i];
@@ -664,7 +645,7 @@ class Tokenizer {
664
645
  }
665
646
  this.setToken(tokenType, realValue);
666
647
  }
667
- str(char) {
648
+ getStr(head, parseEscape = true) {
668
649
  const startOffset = this.preI,
669
650
  startLine = this.line,
670
651
  startCol = this.preCol;
@@ -683,11 +664,20 @@ class Tokenizer {
683
664
  continuousBackslashCount = 0;
684
665
  }
685
666
  this.next();
686
- if (nextC === char && memoCount % 2 === 0) {
667
+ if (nextC === head && memoCount % 2 === 0) {
687
668
  break;
688
669
  }
689
- value += nextC;
670
+ const mapped = bobeShared.escapeMap[nextC];
671
+ if (parseEscape && mapped) {
672
+ value += mapped;
673
+ } else {
674
+ value += nextC;
675
+ }
690
676
  }
677
+ return value;
678
+ }
679
+ str(char) {
680
+ const value = this.getStr(char, false);
691
681
  this.setToken(TokenType.String, value);
692
682
  }
693
683
  number(char) {
@@ -1127,7 +1117,7 @@ class Compiler {
1127
1117
  }
1128
1118
  parseLoopNode(node) {
1129
1119
  const forLoc = this.tokenizer.token.loc ?? this.tokenizer.emptyLoc();
1130
- this.tokenizer.nextToken();
1120
+ this.tokenizer.jsExp();
1131
1121
  const collection = this.parseJsExp();
1132
1122
  if (!collection.value && collection.value !== 0) {
1133
1123
  this.addError(ParseErrorCode.MISSING_FOR_COLLECTION, '"for" 缺少集合表达式', forLoc, node);
@@ -1588,7 +1578,7 @@ class Interpreter {
1588
1578
  return _node;
1589
1579
  }
1590
1580
  forDeclaration() {
1591
- const arrExp = this.tokenizer.nextToken().value;
1581
+ const arrExp = this.tokenizer.jsExp().value;
1592
1582
  this.tokenizer.nextToken();
1593
1583
  const itemToken = this.tokenizer.nextToken();
1594
1584
  const isDestruct = itemToken.type === TokenType.InsertionExp;