mevento 1.9.6 → 1.9.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,6 +1,6 @@
1
1
  function error(token, expected) {
2
2
  const text = `Invalid token ${token.type}[${token.value}] at ${token.line}, ${token.col} ${expected ? `: expecting ${expected} token` : ''}\n`;
3
- throw (text);
3
+ throw Error(text);
4
4
  }
5
5
  function isNum(value) {
6
6
  return typeof value === 'number';
@@ -12,10 +12,10 @@ function isString(value) {
12
12
  return typeof value === 'string';
13
13
  }
14
14
  function _boolValue(test) {
15
- return test !== null &&
16
- ((isNum(test) && test !== 0) ||
17
- (isString(test) && test.length > 0) ||
18
- (isBoolean(test) && test));
15
+ return (test === null ||
16
+ (isNum(test) && test === 0) ||
17
+ (isString(test) && test.length === 0) ||
18
+ (isBoolean(test) && !test)) ? false : true;
19
19
  }
20
20
  ;
21
21
  function hashCode(str) {
@@ -78,6 +78,8 @@ TokenType.in = 40;
78
78
  TokenType.TILL = 41;
79
79
  TokenType.BREAK = 42;
80
80
  TokenType.CONTINUE = 43;
81
+ TokenType.nullity = 44;
82
+ TokenType.RETURN = 45;
81
83
  class Token {
82
84
  constructor(type, value, line = 1, col = 1) {
83
85
  this.type = type;
@@ -117,6 +119,8 @@ Chars.less = "<".charCodeAt(0);
117
119
  Chars.and = "&".charCodeAt(0);
118
120
  Chars.pipe = "|".charCodeAt(0);
119
121
  Chars.colon = ":".charCodeAt(0);
122
+ Chars.questionMark = "?".charCodeAt(0);
123
+ Chars.shebang = "#".charCodeAt(0);
120
124
  class LexerDictionary {
121
125
  constructor(lang, keywords) {
122
126
  this.keywords = {};
@@ -175,10 +179,10 @@ class Lexer {
175
179
  this._col++;
176
180
  }
177
181
  _pick() {
178
- if (this._position >= this._source.length) {
182
+ if (this._position + 1 >= this._source.length) {
179
183
  return -1;
180
184
  }
181
- return this._source[this._position].charCodeAt(0);
185
+ return this._source[this._position + 1].charCodeAt(0);
182
186
  }
183
187
  _isId(code) {
184
188
  if (code < 48) {
@@ -340,6 +344,22 @@ class Lexer {
340
344
  }
341
345
  return new Token(TokenType.stringConst, ret, lastL, lastC);
342
346
  }
347
+ _skipLineComment() {
348
+ while (!this._isLineEnd(this._currentChar) === true) {
349
+ this._advance();
350
+ }
351
+ }
352
+ _skipComment() {
353
+ while (this._currentChar !== -1) {
354
+ if (this._currentChar === Chars.star && this._pick() === Chars.shebang) {
355
+ // end of block comment
356
+ this._advance();
357
+ this._advance();
358
+ break;
359
+ }
360
+ this._advance();
361
+ }
362
+ }
343
363
  // f=SUBMIT_FORM(xxx);
344
364
  // r = CALL_API(API_ID, f);
345
365
  // DISPLAY("MSG #r")
@@ -358,6 +378,18 @@ class Lexer {
358
378
  this._skipWhiteSpace();
359
379
  continue;
360
380
  }
381
+ if (this._currentChar == Chars.shebang) {
382
+ this._advance();
383
+ if (this._currentChar === Chars.star) {
384
+ // mutli block comment
385
+ this._advance();
386
+ this._skipComment();
387
+ }
388
+ else {
389
+ this._skipLineComment();
390
+ }
391
+ continue;
392
+ }
361
393
  if (this._isDigit(this._currentChar)) {
362
394
  return this._number();
363
395
  }
@@ -462,6 +494,11 @@ class Lexer {
462
494
  this._advance();
463
495
  return new Token(TokenType.or, '||', lastL, lastC);
464
496
  }
497
+ if (this._currentChar === Chars.questionMark && this._pick() === Chars.questionMark) {
498
+ this._advance();
499
+ this._advance();
500
+ return new Token(TokenType.nullity, '??', lastL, lastC);
501
+ }
465
502
  if (this._currentChar === Chars.quote || this._currentChar === Chars.squote) {
466
503
  const startChar = this._currentChar;
467
504
  this._advance();
@@ -489,6 +526,7 @@ Lexer._defaultLanguage = new LexerDictionary("en", {
489
526
  "in": "in",
490
527
  "break": "break",
491
528
  "continue": "continue",
529
+ "return": "return",
492
530
  });
493
531
  Lexer.languages = [
494
532
  Lexer._defaultLanguage,
@@ -507,6 +545,7 @@ Lexer.languages = [
507
545
  "dans": "in",
508
546
  "couper": "break",
509
547
  "continuer": "continue",
548
+ "returner": "return",
510
549
  }),
511
550
  new LexerDictionary("bm", {
512
551
  "nii": "if",
@@ -523,6 +562,7 @@ Lexer.languages = [
523
562
  "kono": "in",
524
563
  "tike": "break",
525
564
  "ipan": "continue",
565
+ "segin": "return",
526
566
  }),
527
567
  ];
528
568
  Lexer.RESERVED = {
@@ -540,6 +580,7 @@ Lexer.RESERVED = {
540
580
  "in": Token.from(TokenType.in, "in"),
541
581
  "break": Token.from(TokenType.BREAK, "break"),
542
582
  "continue": Token.from(TokenType.CONTINUE, "continue"),
583
+ "return": Token.from(TokenType.RETURN, "return"),
543
584
  };
544
585
  /**
545
586
  * Parser
@@ -746,6 +787,12 @@ class BreakAST extends AST {
746
787
  super(line, col);
747
788
  }
748
789
  }
790
+ class ReturnAST extends AST {
791
+ constructor(value, line, col) {
792
+ super(line, col);
793
+ this.value = value;
794
+ }
795
+ }
749
796
  class ContinueAST extends AST {
750
797
  constructor(line, col) {
751
798
  super(line, col);
@@ -785,6 +832,14 @@ class Parser {
785
832
  this._eat(TokenType.id);
786
833
  return node;
787
834
  }
835
+ _return() {
836
+ const token = this.currentToken;
837
+ let value = undefined;
838
+ if (!this._expect(TokenType.eol) && !this._expect(TokenType.semi)) {
839
+ value = this._expression();
840
+ }
841
+ return new ReturnAST(value, token?.line, token?.col);
842
+ }
788
843
  _factor() {
789
844
  const token = this.currentToken;
790
845
  switch (token.type) {
@@ -829,7 +884,7 @@ class Parser {
829
884
  _expression() {
830
885
  let node = this._term();
831
886
  node = this._tryBinaryExpression(0, node);
832
- while ([TokenType.and, TokenType.or].includes(this.currentToken.type)) {
887
+ while ([TokenType.and, TokenType.or, TokenType.nullity].includes(this.currentToken.type)) {
833
888
  const token = this.currentToken;
834
889
  this._eat(token.type);
835
890
  node = new LogicalExpressionAST(node, token, this._expression());
@@ -884,6 +939,7 @@ class Parser {
884
939
  if (this.currentToken?.type != TokenType.rbrace) {
885
940
  this._eatEOL();
886
941
  properties.push(this._property());
942
+ this._eatEOL();
887
943
  }
888
944
  while (this.currentToken?.type === TokenType.comma) {
889
945
  this._eat(TokenType.comma);
@@ -945,6 +1001,7 @@ class Parser {
945
1001
  this._eatEOL();
946
1002
  // if (expect(Lexer.TokenType.RPAREN)) return emptyList()
947
1003
  let expr = this._expression();
1004
+ this._eatEOL();
948
1005
  const result = [expr];
949
1006
  while (this.currentToken?.type === TokenType.comma) {
950
1007
  this._eat(TokenType.comma);
@@ -954,6 +1011,7 @@ class Parser {
954
1011
  }
955
1012
  expr = this._expression();
956
1013
  result.push(expr);
1014
+ this._eatEOL();
957
1015
  }
958
1016
  return result;
959
1017
  }
@@ -1141,6 +1199,9 @@ class Parser {
1141
1199
  case TokenType.CONTINUE:
1142
1200
  this._eat(TokenType.CONTINUE);
1143
1201
  return new ContinueAST(this.currentToken?.line, this.currentToken?.col);
1202
+ case TokenType.RETURN:
1203
+ this._eat(TokenType.RETURN);
1204
+ return this._return();
1144
1205
  case TokenType.semi:
1145
1206
  this._eatSemi();
1146
1207
  return this._statement();
@@ -1215,6 +1276,12 @@ class BreakBranch extends LoopControl {
1215
1276
  }
1216
1277
  class ContinueBranch extends LoopControl {
1217
1278
  }
1279
+ class ReturnBranch {
1280
+ constructor(value) {
1281
+ this.value = value;
1282
+ }
1283
+ }
1284
+ ;
1218
1285
  // AST Wallker
1219
1286
  class ANodeVisitor {
1220
1287
  constructor() {
@@ -1248,6 +1315,7 @@ class NodeVisitor extends ANodeVisitor {
1248
1315
  this.registerVisitor(ForOfStatement, this.visitForOfStatement);
1249
1316
  this.registerVisitor(BreakAST, this.visitBreakAST);
1250
1317
  this.registerVisitor(ContinueAST, this.visitContinueAST);
1318
+ this.registerVisitor(ReturnAST, this.visitReturnAST);
1251
1319
  }
1252
1320
  visit(node) {
1253
1321
  try {
@@ -1332,6 +1400,9 @@ class MEvento extends NodeVisitor {
1332
1400
  let last;
1333
1401
  for (const n of list) {
1334
1402
  last = this.visit(n);
1403
+ if (last instanceof ReturnBranch) {
1404
+ return last.value;
1405
+ }
1335
1406
  }
1336
1407
  return last;
1337
1408
  }
@@ -1344,6 +1415,9 @@ class MEvento extends NodeVisitor {
1344
1415
  if (last instanceof LoopControl) {
1345
1416
  break;
1346
1417
  }
1418
+ if (last instanceof ReturnBranch) {
1419
+ break;
1420
+ }
1347
1421
  }
1348
1422
  this.popScope();
1349
1423
  return last;
@@ -1487,6 +1561,12 @@ class MEvento extends NodeVisitor {
1487
1561
  visitLogicalExpressionAST(node) {
1488
1562
  const leftNode = node.left;
1489
1563
  let test = this.visit(leftNode);
1564
+ if (node.operator.type === TokenType.nullity) {
1565
+ if (test != null) {
1566
+ return test;
1567
+ }
1568
+ return this.visit(node.right);
1569
+ }
1490
1570
  const testBoolValue = _boolValue(test);
1491
1571
  if (node.operator.type === TokenType.and) {
1492
1572
  if (!testBoolValue) {
@@ -1496,12 +1576,11 @@ class MEvento extends NodeVisitor {
1496
1576
  }
1497
1577
  if (node.operator.type === TokenType.or) {
1498
1578
  if (testBoolValue) {
1499
- return test;
1579
+ return true;
1500
1580
  }
1501
- test = this.visit(node.right);
1502
- return test;
1581
+ return _boolValue(this.visit(node.right));
1503
1582
  }
1504
- return false;
1583
+ return null;
1505
1584
  }
1506
1585
  visitIndexAccessorAST(node) {
1507
1586
  const ownerNode = node.owner;
@@ -1551,6 +1630,10 @@ class MEvento extends NodeVisitor {
1551
1630
  if (ret instanceof ContinueBranch) {
1552
1631
  continue;
1553
1632
  }
1633
+ if (ret instanceof ReturnBranch) {
1634
+ this.popScope();
1635
+ return ret;
1636
+ }
1554
1637
  retainer?.push(ret);
1555
1638
  }
1556
1639
  return retainer ?? null;
@@ -1594,6 +1677,10 @@ class MEvento extends NodeVisitor {
1594
1677
  update();
1595
1678
  continue;
1596
1679
  }
1680
+ if (ret instanceof ReturnBranch) {
1681
+ this.popScope();
1682
+ return ret;
1683
+ }
1597
1684
  retainer?.push(ret);
1598
1685
  // update
1599
1686
  update();
@@ -1617,6 +1704,10 @@ class MEvento extends NodeVisitor {
1617
1704
  if (ret instanceof ContinueBranch) {
1618
1705
  continue;
1619
1706
  }
1707
+ if (ret instanceof ReturnBranch) {
1708
+ this.popScope();
1709
+ return ret;
1710
+ }
1620
1711
  retainer?.push(ret);
1621
1712
  }
1622
1713
  this.popScope();
@@ -1625,6 +1716,9 @@ class MEvento extends NodeVisitor {
1625
1716
  visitContinueAST(node) {
1626
1717
  return new ContinueBranch();
1627
1718
  }
1719
+ visitReturnAST(node) {
1720
+ return new ReturnBranch(node.value != null ? this.visit(node.value) : null);
1721
+ }
1628
1722
  _declareForIdentifier(node, value) {
1629
1723
  if (node instanceof TupleExpression) {
1630
1724
  if (!Array.isArray(value)) {
@@ -1711,6 +1805,9 @@ class MEventoAsync extends MEvento {
1711
1805
  let last;
1712
1806
  for (const n of list) {
1713
1807
  last = await this.visit(n);
1808
+ if (last instanceof ReturnBranch) {
1809
+ return last.value;
1810
+ }
1714
1811
  }
1715
1812
  return last;
1716
1813
  }
@@ -1723,6 +1820,9 @@ class MEventoAsync extends MEvento {
1723
1820
  if (last instanceof LoopControl) {
1724
1821
  break;
1725
1822
  }
1823
+ if (last instanceof ReturnBranch) {
1824
+ break;
1825
+ }
1726
1826
  }
1727
1827
  this.popScope();
1728
1828
  return last;
@@ -1866,6 +1966,12 @@ class MEventoAsync extends MEvento {
1866
1966
  async visitLogicalExpressionAST(node) {
1867
1967
  const leftNode = node.left;
1868
1968
  let test = await this.visit(leftNode);
1969
+ if (node.operator.type === TokenType.nullity) {
1970
+ if (test != null) {
1971
+ return test;
1972
+ }
1973
+ return await this.visit(node.right);
1974
+ }
1869
1975
  const testBoolValue = _boolValue(test);
1870
1976
  if (node.operator.type === TokenType.and) {
1871
1977
  if (!testBoolValue) {
@@ -1875,10 +1981,9 @@ class MEventoAsync extends MEvento {
1875
1981
  }
1876
1982
  if (node.operator.type === TokenType.or) {
1877
1983
  if (testBoolValue) {
1878
- return test;
1984
+ return true;
1879
1985
  }
1880
- test = await this.visit(node.right);
1881
- return test;
1986
+ return _boolValue(await this.visit(node.right));
1882
1987
  }
1883
1988
  return false;
1884
1989
  }
@@ -1927,6 +2032,10 @@ class MEventoAsync extends MEvento {
1927
2032
  if (ret instanceof ContinueBranch) {
1928
2033
  continue;
1929
2034
  }
2035
+ if (ret instanceof ReturnBranch) {
2036
+ this.popScope();
2037
+ return ret;
2038
+ }
1930
2039
  retainer?.push(ret);
1931
2040
  }
1932
2041
  return retainer;
@@ -1970,6 +2079,10 @@ class MEventoAsync extends MEvento {
1970
2079
  await update();
1971
2080
  continue;
1972
2081
  }
2082
+ if (ret instanceof ReturnBranch) {
2083
+ this.popScope();
2084
+ return ret;
2085
+ }
1973
2086
  retainer?.push(ret);
1974
2087
  // update
1975
2088
  await update();
@@ -1993,11 +2106,18 @@ class MEventoAsync extends MEvento {
1993
2106
  if (ret instanceof ContinueBranch) {
1994
2107
  continue;
1995
2108
  }
2109
+ if (ret instanceof ReturnBranch) {
2110
+ this.popScope();
2111
+ return ret;
2112
+ }
1996
2113
  retainer?.push(ret);
1997
2114
  }
1998
2115
  this.popScope();
1999
2116
  return retainer;
2000
2117
  }
2118
+ async visitReturnAST(node) {
2119
+ return new ReturnBranch(node.value != null ? await this.visit(node.value) : null);
2120
+ }
2001
2121
  async resolveArgumentsAsync(args) {
2002
2122
  return await Promise.all(args.map(async (it) => {
2003
2123
  return await this.visit(it);
@@ -2037,5 +2157,5 @@ class MEventoAsync extends MEvento {
2037
2157
  * Generated bundle index. Do not edit.
2038
2158
  */
2039
2159
 
2040
- export { AST, ArrayExpression, AssignmentExpressionAST, BinaryExpressionAST, BlockStatementAST, BreakAST, BreakBranch, CallExpressionAST, ContinueAST, ContinueBranch, ExpressionStatementAST, ForLoopStatement, ForOfStatement, IdentifierAST, IfStatementAST, IndexAccessorAST, LexerDictionary, LiteralAST, LogicalExpressionAST, LoopControl, MEventScope, MEvento, MEventoAsync, NodeVisitor, ObjectExpression, ObjectProperty, RootAST, Token, TokenType, TupleExpression, UnaryExpressionAST, WhileLoopStatement };
2160
+ export { AST, ArrayExpression, AssignmentExpressionAST, BinaryExpressionAST, BlockStatementAST, BreakAST, BreakBranch, CallExpressionAST, ContinueAST, ContinueBranch, ExpressionStatementAST, ForLoopStatement, ForOfStatement, IdentifierAST, IfStatementAST, IndexAccessorAST, LexerDictionary, LiteralAST, LogicalExpressionAST, LoopControl, MEventScope, MEvento, MEventoAsync, NodeVisitor, ObjectExpression, ObjectProperty, ReturnAST, ReturnBranch, RootAST, Token, TokenType, TupleExpression, UnaryExpressionAST, WhileLoopStatement };
2041
2161
  //# sourceMappingURL=mevento.mjs.map