mevento 1.9.0 → 1.9.4

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,5 +1,5 @@
1
- function error(token) {
2
- const text = `Invalid token ${token.type}[${token.value}] at ${token.line}, ${token.col}\n`;
1
+ function error(token, expected) {
2
+ const text = `Invalid token ${token.type}[${token.value}] at ${token.line}, ${token.col} ${expected ? `: expecting ${expected} token` : ''}\n`;
3
3
  throw (text);
4
4
  }
5
5
  function isNum(value) {
@@ -236,8 +236,7 @@ class Lexer {
236
236
  ['\n', '\r', '\u2028', '\u2029'].includes(String.fromCharCode(c));
237
237
  }
238
238
  _isWhiteSpace(c) {
239
- return String.fromCharCode(c).trim() === "" &&
240
- String.fromCharCode(c).length > 0;
239
+ return [' ', '\t'].includes(String.fromCharCode(c));
241
240
  }
242
241
  _isDigit(c) {
243
242
  // tslint:disable-next-line:no-bitwise
@@ -762,7 +761,7 @@ class Parser {
762
761
  this.currentToken = this.lexer.nextToken();
763
762
  }
764
763
  else {
765
- error(this.currentToken);
764
+ error(this.currentToken, type);
766
765
  }
767
766
  }
768
767
  _eatEOL() {
@@ -883,11 +882,14 @@ class Parser {
883
882
  _objectProperties() {
884
883
  const properties = [];
885
884
  if (this.currentToken?.type != TokenType.rbrace) {
885
+ this._eatEOL();
886
886
  properties.push(this._property());
887
887
  }
888
888
  while (this.currentToken?.type === TokenType.comma) {
889
889
  this._eat(TokenType.comma);
890
+ this._eatEOL();
890
891
  properties.push(this._property());
892
+ this._eatEOL();
891
893
  }
892
894
  return properties;
893
895
  }
@@ -972,6 +974,7 @@ class Parser {
972
974
  const node = this._expression();
973
975
  if (![TokenType.semi, TokenType.eol, TokenType.eof]
974
976
  .includes(this.currentToken.type)) {
977
+ console.log('DDDMMMS', this.currentToken);
975
978
  error(this.currentToken);
976
979
  }
977
980
  return node;
@@ -1242,10 +1245,15 @@ class NodeVisitor extends ANodeVisitor {
1242
1245
  this.registerVisitor(ContinueAST, this.visitContinueAST);
1243
1246
  }
1244
1247
  visit(node) {
1245
- const methodName = `visit${node.constructor.name}`;
1246
- // const fn = Object.getPrototypeOf(this)[methodName];
1247
- const fn = this._nodesVisitors[methodName];
1248
- return fn?.call(this, node);
1248
+ try {
1249
+ const methodName = `visit${node.constructor.name}`;
1250
+ // const fn = Object.getPrototypeOf(this)[methodName];
1251
+ const fn = this._nodesVisitors[methodName];
1252
+ return fn?.call(this, node);
1253
+ }
1254
+ catch (error) {
1255
+ return null;
1256
+ }
1249
1257
  }
1250
1258
  assignProperty(owner, property, value) {
1251
1259
  if (Array.isArray(owner)) {
@@ -1265,7 +1273,7 @@ class MEventScope {
1265
1273
  this.parent = parent;
1266
1274
  }
1267
1275
  resolve(key) {
1268
- return Object.keys(this.memory).includes(key) ? this.memory[key] : (this.parent ? this.parent.resolve(key) : null);
1276
+ return Object.keys(this.memory).includes(key) ? this.memory[key] : (this.parent?.resolve(key) ?? null);
1269
1277
  }
1270
1278
  change(key, value, declare = true) {
1271
1279
  if (Object.keys(this.memory).includes(key)) {
@@ -1287,27 +1295,27 @@ class MEvento extends NodeVisitor {
1287
1295
  constructor() {
1288
1296
  super();
1289
1297
  this.rootScope = new MEventScope("Program", {});
1290
- this.currentScrope = this.rootScope;
1298
+ this.currentScope = this.rootScope;
1291
1299
  this.debug = false;
1292
1300
  this._functionsRegistry = {};
1293
1301
  this._functionsRegistry = { ...MEvento._globalFunctionsRegistry };
1294
1302
  }
1295
1303
  resolve(name) {
1296
- return this.currentScrope?.resolve(name) ?? null;
1304
+ return this.currentScope?.resolve(name) ?? null;
1297
1305
  }
1298
1306
  changeVariable(name, value) {
1299
- if (this.currentScrope?.change(name, value)) {
1307
+ if (this.currentScope?.change(name, value)) {
1300
1308
  return value;
1301
1309
  }
1302
1310
  else
1303
1311
  return null;
1304
1312
  }
1305
1313
  pushScope(name) {
1306
- const scope = new MEventScope(name, {}, this.currentScrope);
1307
- this.currentScrope = scope;
1314
+ const scope = new MEventScope(name, {}, this.currentScope);
1315
+ this.currentScope = scope;
1308
1316
  }
1309
1317
  popScope() {
1310
- this.currentScrope = this.currentScrope?.parent;
1318
+ this.currentScope = this.currentScope?.parent;
1311
1319
  }
1312
1320
  log(message) {
1313
1321
  if (this.debug) {
@@ -1337,7 +1345,7 @@ class MEvento extends NodeVisitor {
1337
1345
  }
1338
1346
  visitIdentifierAST(node) {
1339
1347
  const id = node.value;
1340
- return this.currentScrope?.resolve(id) ?? null;
1348
+ return this?.resolve(id) ?? null;
1341
1349
  }
1342
1350
  visitLiteralAST(node) {
1343
1351
  const value = node.value;
@@ -1486,6 +1494,9 @@ class MEvento extends NodeVisitor {
1486
1494
  visitIndexAccessorAST(node) {
1487
1495
  const ownerNode = node.owner;
1488
1496
  const owner = this.visit(ownerNode);
1497
+ if (owner === null || owner === undefined) {
1498
+ return null;
1499
+ }
1489
1500
  if (typeof owner === 'object') {
1490
1501
  const key = this.visit(node.key);
1491
1502
  return owner[key];
@@ -1850,6 +1861,9 @@ class MEventoAsync extends MEvento {
1850
1861
  async visitIndexAccessorAST(node) {
1851
1862
  const ownerNode = node.owner;
1852
1863
  const owner = await this.visit(ownerNode);
1864
+ if (owner === null || owner === undefined) {
1865
+ return null;
1866
+ }
1853
1867
  if (typeof owner === 'object') {
1854
1868
  const key = await this.visit(node.key);
1855
1869
  return owner[key];