mevento 1.9.5 → 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.
@@ -2,7 +2,7 @@ import { __awaiter } from 'tslib';
2
2
 
3
3
  function error(token, expected) {
4
4
  const text = `Invalid token ${token.type}[${token.value}] at ${token.line}, ${token.col} ${expected ? `: expecting ${expected} token` : ''}\n`;
5
- throw (text);
5
+ throw Error(text);
6
6
  }
7
7
  function isNum(value) {
8
8
  return typeof value === 'number';
@@ -14,10 +14,10 @@ function isString(value) {
14
14
  return typeof value === 'string';
15
15
  }
16
16
  function _boolValue(test) {
17
- return test !== null &&
18
- ((isNum(test) && test !== 0) ||
19
- (isString(test) && test.length > 0) ||
20
- (isBoolean(test) && test));
17
+ return (test === null ||
18
+ (isNum(test) && test === 0) ||
19
+ (isString(test) && test.length === 0) ||
20
+ (isBoolean(test) && !test)) ? false : true;
21
21
  }
22
22
  ;
23
23
  function hashCode(str) {
@@ -80,6 +80,8 @@ TokenType.in = 40;
80
80
  TokenType.TILL = 41;
81
81
  TokenType.BREAK = 42;
82
82
  TokenType.CONTINUE = 43;
83
+ TokenType.nullity = 44;
84
+ TokenType.RETURN = 45;
83
85
  class Token {
84
86
  constructor(type, value, line = 1, col = 1) {
85
87
  this.type = type;
@@ -119,6 +121,8 @@ Chars.less = "<".charCodeAt(0);
119
121
  Chars.and = "&".charCodeAt(0);
120
122
  Chars.pipe = "|".charCodeAt(0);
121
123
  Chars.colon = ":".charCodeAt(0);
124
+ Chars.questionMark = "?".charCodeAt(0);
125
+ Chars.shebang = "#".charCodeAt(0);
122
126
  class LexerDictionary {
123
127
  constructor(lang, keywords) {
124
128
  this.keywords = {};
@@ -178,10 +182,10 @@ class Lexer {
178
182
  this._col++;
179
183
  }
180
184
  _pick() {
181
- if (this._position >= this._source.length) {
185
+ if (this._position + 1 >= this._source.length) {
182
186
  return -1;
183
187
  }
184
- return this._source[this._position].charCodeAt(0);
188
+ return this._source[this._position + 1].charCodeAt(0);
185
189
  }
186
190
  _isId(code) {
187
191
  if (code < 48) {
@@ -344,6 +348,22 @@ class Lexer {
344
348
  }
345
349
  return new Token(TokenType.stringConst, ret, lastL, lastC);
346
350
  }
351
+ _skipLineComment() {
352
+ while (!this._isLineEnd(this._currentChar) === true) {
353
+ this._advance();
354
+ }
355
+ }
356
+ _skipComment() {
357
+ while (this._currentChar !== -1) {
358
+ if (this._currentChar === Chars.star && this._pick() === Chars.shebang) {
359
+ // end of block comment
360
+ this._advance();
361
+ this._advance();
362
+ break;
363
+ }
364
+ this._advance();
365
+ }
366
+ }
347
367
  // f=SUBMIT_FORM(xxx);
348
368
  // r = CALL_API(API_ID, f);
349
369
  // DISPLAY("MSG #r")
@@ -362,6 +382,18 @@ class Lexer {
362
382
  this._skipWhiteSpace();
363
383
  continue;
364
384
  }
385
+ if (this._currentChar == Chars.shebang) {
386
+ this._advance();
387
+ if (this._currentChar === Chars.star) {
388
+ // mutli block comment
389
+ this._advance();
390
+ this._skipComment();
391
+ }
392
+ else {
393
+ this._skipLineComment();
394
+ }
395
+ continue;
396
+ }
365
397
  if (this._isDigit(this._currentChar)) {
366
398
  return this._number();
367
399
  }
@@ -466,6 +498,11 @@ class Lexer {
466
498
  this._advance();
467
499
  return new Token(TokenType.or, '||', lastL, lastC);
468
500
  }
501
+ if (this._currentChar === Chars.questionMark && this._pick() === Chars.questionMark) {
502
+ this._advance();
503
+ this._advance();
504
+ return new Token(TokenType.nullity, '??', lastL, lastC);
505
+ }
469
506
  if (this._currentChar === Chars.quote || this._currentChar === Chars.squote) {
470
507
  const startChar = this._currentChar;
471
508
  this._advance();
@@ -493,6 +530,7 @@ Lexer._defaultLanguage = new LexerDictionary("en", {
493
530
  "in": "in",
494
531
  "break": "break",
495
532
  "continue": "continue",
533
+ "return": "return",
496
534
  });
497
535
  Lexer.languages = [
498
536
  Lexer._defaultLanguage,
@@ -511,6 +549,7 @@ Lexer.languages = [
511
549
  "dans": "in",
512
550
  "couper": "break",
513
551
  "continuer": "continue",
552
+ "returner": "return",
514
553
  }),
515
554
  new LexerDictionary("bm", {
516
555
  "nii": "if",
@@ -527,6 +566,7 @@ Lexer.languages = [
527
566
  "kono": "in",
528
567
  "tike": "break",
529
568
  "ipan": "continue",
569
+ "segin": "return",
530
570
  }),
531
571
  ];
532
572
  Lexer.RESERVED = {
@@ -544,6 +584,7 @@ Lexer.RESERVED = {
544
584
  "in": Token.from(TokenType.in, "in"),
545
585
  "break": Token.from(TokenType.BREAK, "break"),
546
586
  "continue": Token.from(TokenType.CONTINUE, "continue"),
587
+ "return": Token.from(TokenType.RETURN, "return"),
547
588
  };
548
589
  /**
549
590
  * Parser
@@ -750,6 +791,12 @@ class BreakAST extends AST {
750
791
  super(line, col);
751
792
  }
752
793
  }
794
+ class ReturnAST extends AST {
795
+ constructor(value, line, col) {
796
+ super(line, col);
797
+ this.value = value;
798
+ }
799
+ }
753
800
  class ContinueAST extends AST {
754
801
  constructor(line, col) {
755
802
  super(line, col);
@@ -793,6 +840,14 @@ class Parser {
793
840
  this._eat(TokenType.id);
794
841
  return node;
795
842
  }
843
+ _return() {
844
+ const token = this.currentToken;
845
+ let value = undefined;
846
+ if (!this._expect(TokenType.eol) && !this._expect(TokenType.semi)) {
847
+ value = this._expression();
848
+ }
849
+ return new ReturnAST(value, token === null || token === void 0 ? void 0 : token.line, token === null || token === void 0 ? void 0 : token.col);
850
+ }
796
851
  _factor() {
797
852
  const token = this.currentToken;
798
853
  switch (token.type) {
@@ -837,7 +892,7 @@ class Parser {
837
892
  _expression() {
838
893
  let node = this._term();
839
894
  node = this._tryBinaryExpression(0, node);
840
- while ([TokenType.and, TokenType.or].includes(this.currentToken.type)) {
895
+ while ([TokenType.and, TokenType.or, TokenType.nullity].includes(this.currentToken.type)) {
841
896
  const token = this.currentToken;
842
897
  this._eat(token.type);
843
898
  node = new LogicalExpressionAST(node, token, this._expression());
@@ -894,6 +949,7 @@ class Parser {
894
949
  if (((_a = this.currentToken) === null || _a === void 0 ? void 0 : _a.type) != TokenType.rbrace) {
895
950
  this._eatEOL();
896
951
  properties.push(this._property());
952
+ this._eatEOL();
897
953
  }
898
954
  while (((_b = this.currentToken) === null || _b === void 0 ? void 0 : _b.type) === TokenType.comma) {
899
955
  this._eat(TokenType.comma);
@@ -956,6 +1012,7 @@ class Parser {
956
1012
  this._eatEOL();
957
1013
  // if (expect(Lexer.TokenType.RPAREN)) return emptyList()
958
1014
  let expr = this._expression();
1015
+ this._eatEOL();
959
1016
  const result = [expr];
960
1017
  while (((_a = this.currentToken) === null || _a === void 0 ? void 0 : _a.type) === TokenType.comma) {
961
1018
  this._eat(TokenType.comma);
@@ -965,6 +1022,7 @@ class Parser {
965
1022
  }
966
1023
  expr = this._expression();
967
1024
  result.push(expr);
1025
+ this._eatEOL();
968
1026
  }
969
1027
  return result;
970
1028
  }
@@ -1154,6 +1212,9 @@ class Parser {
1154
1212
  case TokenType.CONTINUE:
1155
1213
  this._eat(TokenType.CONTINUE);
1156
1214
  return new ContinueAST((_c = this.currentToken) === null || _c === void 0 ? void 0 : _c.line, (_d = this.currentToken) === null || _d === void 0 ? void 0 : _d.col);
1215
+ case TokenType.RETURN:
1216
+ this._eat(TokenType.RETURN);
1217
+ return this._return();
1157
1218
  case TokenType.semi:
1158
1219
  this._eatSemi();
1159
1220
  return this._statement();
@@ -1229,6 +1290,12 @@ class BreakBranch extends LoopControl {
1229
1290
  }
1230
1291
  class ContinueBranch extends LoopControl {
1231
1292
  }
1293
+ class ReturnBranch {
1294
+ constructor(value) {
1295
+ this.value = value;
1296
+ }
1297
+ }
1298
+ ;
1232
1299
  // AST Wallker
1233
1300
  class ANodeVisitor {
1234
1301
  constructor() {
@@ -1262,6 +1329,7 @@ class NodeVisitor extends ANodeVisitor {
1262
1329
  this.registerVisitor(ForOfStatement, this.visitForOfStatement);
1263
1330
  this.registerVisitor(BreakAST, this.visitBreakAST);
1264
1331
  this.registerVisitor(ContinueAST, this.visitContinueAST);
1332
+ this.registerVisitor(ReturnAST, this.visitReturnAST);
1265
1333
  }
1266
1334
  visit(node) {
1267
1335
  var _a;
@@ -1351,6 +1419,9 @@ class MEvento extends NodeVisitor {
1351
1419
  let last;
1352
1420
  for (const n of list) {
1353
1421
  last = this.visit(n);
1422
+ if (last instanceof ReturnBranch) {
1423
+ return last.value;
1424
+ }
1354
1425
  }
1355
1426
  return last;
1356
1427
  }
@@ -1363,6 +1434,9 @@ class MEvento extends NodeVisitor {
1363
1434
  if (last instanceof LoopControl) {
1364
1435
  break;
1365
1436
  }
1437
+ if (last instanceof ReturnBranch) {
1438
+ break;
1439
+ }
1366
1440
  }
1367
1441
  this.popScope();
1368
1442
  return last;
@@ -1400,7 +1474,7 @@ class MEvento extends NodeVisitor {
1400
1474
  const callee = node.callee;
1401
1475
  const args = node.arguments;
1402
1476
  const calleeName = callee.value;
1403
- const fn = this._functionsRegistry[calleeName];
1477
+ const fn = this.resolveFunction(calleeName);
1404
1478
  const argValues = args.map((e) => this.visit(e));
1405
1479
  // print("argValues::", argValues)
1406
1480
  return fn === null || fn === void 0 ? void 0 : fn(argValues, this);
@@ -1507,6 +1581,12 @@ class MEvento extends NodeVisitor {
1507
1581
  visitLogicalExpressionAST(node) {
1508
1582
  const leftNode = node.left;
1509
1583
  let test = this.visit(leftNode);
1584
+ if (node.operator.type === TokenType.nullity) {
1585
+ if (test != null) {
1586
+ return test;
1587
+ }
1588
+ return this.visit(node.right);
1589
+ }
1510
1590
  const testBoolValue = _boolValue(test);
1511
1591
  if (node.operator.type === TokenType.and) {
1512
1592
  if (!testBoolValue) {
@@ -1516,12 +1596,11 @@ class MEvento extends NodeVisitor {
1516
1596
  }
1517
1597
  if (node.operator.type === TokenType.or) {
1518
1598
  if (testBoolValue) {
1519
- return test;
1599
+ return true;
1520
1600
  }
1521
- test = this.visit(node.right);
1522
- return test;
1601
+ return _boolValue(this.visit(node.right));
1523
1602
  }
1524
- return false;
1603
+ return null;
1525
1604
  }
1526
1605
  visitIndexAccessorAST(node) {
1527
1606
  const ownerNode = node.owner;
@@ -1571,6 +1650,10 @@ class MEvento extends NodeVisitor {
1571
1650
  if (ret instanceof ContinueBranch) {
1572
1651
  continue;
1573
1652
  }
1653
+ if (ret instanceof ReturnBranch) {
1654
+ this.popScope();
1655
+ return ret;
1656
+ }
1574
1657
  retainer === null || retainer === void 0 ? void 0 : retainer.push(ret);
1575
1658
  }
1576
1659
  return retainer !== null && retainer !== void 0 ? retainer : null;
@@ -1614,6 +1697,10 @@ class MEvento extends NodeVisitor {
1614
1697
  update();
1615
1698
  continue;
1616
1699
  }
1700
+ if (ret instanceof ReturnBranch) {
1701
+ this.popScope();
1702
+ return ret;
1703
+ }
1617
1704
  retainer === null || retainer === void 0 ? void 0 : retainer.push(ret);
1618
1705
  // update
1619
1706
  update();
@@ -1637,6 +1724,10 @@ class MEvento extends NodeVisitor {
1637
1724
  if (ret instanceof ContinueBranch) {
1638
1725
  continue;
1639
1726
  }
1727
+ if (ret instanceof ReturnBranch) {
1728
+ this.popScope();
1729
+ return ret;
1730
+ }
1640
1731
  retainer === null || retainer === void 0 ? void 0 : retainer.push(ret);
1641
1732
  }
1642
1733
  this.popScope();
@@ -1645,6 +1736,9 @@ class MEvento extends NodeVisitor {
1645
1736
  visitContinueAST(node) {
1646
1737
  return new ContinueBranch();
1647
1738
  }
1739
+ visitReturnAST(node) {
1740
+ return new ReturnBranch(node.value != null ? this.visit(node.value) : null);
1741
+ }
1648
1742
  _declareForIdentifier(node, value) {
1649
1743
  if (node instanceof TupleExpression) {
1650
1744
  if (!Array.isArray(value)) {
@@ -1660,6 +1754,13 @@ class MEvento extends NodeVisitor {
1660
1754
  return this.visit(it);
1661
1755
  });
1662
1756
  }
1757
+ setFunctionResolver(resolver) {
1758
+ this._functionResolver = resolver;
1759
+ }
1760
+ resolveFunction(name) {
1761
+ var _a, _b;
1762
+ return (_a = this._functionsRegistry[name]) !== null && _a !== void 0 ? _a : (_b = this._functionResolver) === null || _b === void 0 ? void 0 : _b.call(this, name);
1763
+ }
1663
1764
  registerFunction(id, fn) {
1664
1765
  this._functionsRegistry[id] = fn;
1665
1766
  }
@@ -1726,6 +1827,9 @@ class MEventoAsync extends MEvento {
1726
1827
  let last;
1727
1828
  for (const n of list) {
1728
1829
  last = yield this.visit(n);
1830
+ if (last instanceof ReturnBranch) {
1831
+ return last.value;
1832
+ }
1729
1833
  }
1730
1834
  return last;
1731
1835
  });
@@ -1740,6 +1844,9 @@ class MEventoAsync extends MEvento {
1740
1844
  if (last instanceof LoopControl) {
1741
1845
  break;
1742
1846
  }
1847
+ if (last instanceof ReturnBranch) {
1848
+ break;
1849
+ }
1743
1850
  }
1744
1851
  this.popScope();
1745
1852
  return last;
@@ -1901,6 +2008,12 @@ class MEventoAsync extends MEvento {
1901
2008
  return __awaiter(this, void 0, void 0, function* () {
1902
2009
  const leftNode = node.left;
1903
2010
  let test = yield this.visit(leftNode);
2011
+ if (node.operator.type === TokenType.nullity) {
2012
+ if (test != null) {
2013
+ return test;
2014
+ }
2015
+ return yield this.visit(node.right);
2016
+ }
1904
2017
  const testBoolValue = _boolValue(test);
1905
2018
  if (node.operator.type === TokenType.and) {
1906
2019
  if (!testBoolValue) {
@@ -1910,10 +2023,9 @@ class MEventoAsync extends MEvento {
1910
2023
  }
1911
2024
  if (node.operator.type === TokenType.or) {
1912
2025
  if (testBoolValue) {
1913
- return test;
2026
+ return true;
1914
2027
  }
1915
- test = yield this.visit(node.right);
1916
- return test;
2028
+ return _boolValue(yield this.visit(node.right));
1917
2029
  }
1918
2030
  return false;
1919
2031
  });
@@ -1972,6 +2084,10 @@ class MEventoAsync extends MEvento {
1972
2084
  if (ret instanceof ContinueBranch) {
1973
2085
  continue;
1974
2086
  }
2087
+ if (ret instanceof ReturnBranch) {
2088
+ this.popScope();
2089
+ return ret;
2090
+ }
1975
2091
  retainer === null || retainer === void 0 ? void 0 : retainer.push(ret);
1976
2092
  }
1977
2093
  return retainer;
@@ -2017,6 +2133,10 @@ class MEventoAsync extends MEvento {
2017
2133
  yield update();
2018
2134
  continue;
2019
2135
  }
2136
+ if (ret instanceof ReturnBranch) {
2137
+ this.popScope();
2138
+ return ret;
2139
+ }
2020
2140
  retainer === null || retainer === void 0 ? void 0 : retainer.push(ret);
2021
2141
  // update
2022
2142
  yield update();
@@ -2042,12 +2162,21 @@ class MEventoAsync extends MEvento {
2042
2162
  if (ret instanceof ContinueBranch) {
2043
2163
  continue;
2044
2164
  }
2165
+ if (ret instanceof ReturnBranch) {
2166
+ this.popScope();
2167
+ return ret;
2168
+ }
2045
2169
  retainer === null || retainer === void 0 ? void 0 : retainer.push(ret);
2046
2170
  }
2047
2171
  this.popScope();
2048
2172
  return retainer;
2049
2173
  });
2050
2174
  }
2175
+ visitReturnAST(node) {
2176
+ return __awaiter(this, void 0, void 0, function* () {
2177
+ return new ReturnBranch(node.value != null ? yield this.visit(node.value) : null);
2178
+ });
2179
+ }
2051
2180
  resolveArgumentsAsync(args) {
2052
2181
  return __awaiter(this, void 0, void 0, function* () {
2053
2182
  return yield Promise.all(args.map((it) => __awaiter(this, void 0, void 0, function* () {
@@ -2093,5 +2222,5 @@ class MEventoAsync extends MEvento {
2093
2222
  * Generated bundle index. Do not edit.
2094
2223
  */
2095
2224
 
2096
- 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 };
2225
+ 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 };
2097
2226
  //# sourceMappingURL=mevento.mjs.map