naider 1.17.2 → 1.18.0

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/src/parser.js CHANGED
@@ -468,6 +468,12 @@ export class Parser {
468
468
 
469
469
  parseTypedVariable() {
470
470
  const varType = this.advance().value;
471
+ if (this.at(T.LBRACE)) {
472
+ return this.parseDestructure(varType, false);
473
+ }
474
+ if (this.at(T.LBRACKET)) {
475
+ return this.parseDestructure(varType, false);
476
+ }
471
477
  const name = this.expectIdentLike();
472
478
  this.expect(T.ASSIGN);
473
479
  const value = this.parseExpression();
@@ -481,12 +487,47 @@ export class Parser {
481
487
  node.isMut = true;
482
488
  return node;
483
489
  }
490
+ if (this.at(T.LBRACE) || this.at(T.LBRACKET)) {
491
+ return this.parseDestructure(null, true);
492
+ }
484
493
  const name = this.expectIdentLike();
485
494
  this.expect(T.ASSIGN);
486
495
  const value = this.parseExpression();
487
496
  return new ASTNode('TypedVar', { varType: null, name, value, isMut: true });
488
497
  }
489
498
 
499
+ parseDestructure(varType, isMut) {
500
+ const pattern = this.at(T.LBRACE) ? 'object' : 'array';
501
+ this.advance(); // { or [
502
+ const names = [];
503
+ const endToken = pattern === 'object' ? T.RBRACE : T.RBRACKET;
504
+ while (!this.at(endToken) && !this.at(T.EOF)) {
505
+ this.skipWhitespace();
506
+ if (this.at(endToken)) break;
507
+ if (this.match(T.SPREAD)) {
508
+ const name = this.expectIdentLike();
509
+ names.push({ name, rest: true });
510
+ } else {
511
+ const name = this.expectIdentLike();
512
+ let alias = null;
513
+ if (this.match(T.COLON)) {
514
+ alias = this.expectIdentLike();
515
+ }
516
+ let defaultValue = null;
517
+ if (this.match(T.ASSIGN)) {
518
+ defaultValue = this.parseExpression();
519
+ }
520
+ names.push({ name, alias, defaultValue, rest: false });
521
+ }
522
+ this.match(T.COMMA);
523
+ this.skipWhitespace();
524
+ }
525
+ this.expect(endToken);
526
+ this.expect(T.ASSIGN);
527
+ const value = this.parseExpression();
528
+ return new ASTNode('Destructure', { pattern, names, value, varType, isMut });
529
+ }
530
+
490
531
  parseIf() {
491
532
  this.advance(); // if
492
533
  const condition = this.parseExpression();
@@ -1394,19 +1435,40 @@ export class Parser {
1394
1435
  parseSchema() {
1395
1436
  this.advance(); // schema
1396
1437
  const name = this.expect(T.IDENT).value;
1438
+ let parent = null;
1439
+ if (this.match(T.EXTENDS)) {
1440
+ parent = this.expect(T.IDENT).value;
1441
+ }
1397
1442
  this.expect(T.COLON);
1398
1443
  this.skipNewlines();
1399
1444
  this.expect(T.INDENT);
1400
1445
 
1401
1446
  const fields = [];
1447
+ const methods = [];
1448
+ let init = null;
1402
1449
  this.skipNewlines();
1403
1450
 
1404
1451
  while (!this.at(T.DEDENT) && !this.at(T.EOF)) {
1405
- fields.push(this.parseSchemaField());
1452
+ if (this.at(T.FN) || this.at(T.FN_ASYNC)) {
1453
+ methods.push(this.parseFunction(this.at(T.FN_ASYNC), false));
1454
+ } else if (this.at(T.IDENT) && this.peek().value === 'init' && this.peek(1).type === T.LPAREN) {
1455
+ this.advance(); // init
1456
+ this.expect(T.LPAREN);
1457
+ const params = this.parseFnParams();
1458
+ this.expect(T.RPAREN);
1459
+ this.expect(T.COLON);
1460
+ const body = this.parseBlock();
1461
+ init = { params, body };
1462
+ } else {
1463
+ fields.push(this.parseSchemaField());
1464
+ }
1406
1465
  this.skipNewlines();
1407
1466
  }
1408
1467
  this.match(T.DEDENT);
1409
1468
 
1469
+ if (parent || methods.length > 0 || init) {
1470
+ return new ASTNode('ClassDecl', { name, parent, fields, methods, init });
1471
+ }
1410
1472
  return new ASTNode('SchemaDecl', { name, fields });
1411
1473
  }
1412
1474
 
package/src/tokens.js CHANGED
@@ -114,6 +114,10 @@ export const T = {
114
114
  SWAP: 'SWAP',
115
115
  IS: 'IS',
116
116
  ISNT: 'ISNT',
117
+ EXTENDS: 'EXTENDS',
118
+
119
+ // Types (auto)
120
+ TYPE_AUTO: 'TYPE_AUTO',
117
121
 
118
122
  // Operators
119
123
  ASSIGN: 'ASSIGN',
@@ -256,7 +260,9 @@ export const KEYWORDS = {
256
260
  'swap': T.SWAP,
257
261
  'is': T.IS,
258
262
  'isnt': T.ISNT,
263
+ 'extends': T.EXTENDS,
259
264
  'print': T.LOG,
265
+ 'auto': T.TYPE_AUTO,
260
266
  'true': T.BOOL,
261
267
  'false': T.BOOL,
262
268
  'null': T.NULL,
@@ -274,4 +280,5 @@ export const KEYWORDS = {
274
280
  export const TYPE_TOKENS = new Set([
275
281
  T.TYPE_STR, T.TYPE_INT, T.TYPE_NUM, T.TYPE_BOOL,
276
282
  T.TYPE_LIST, T.TYPE_MAP, T.TYPE_ANY, T.TYPE_JSON, T.TYPE_VOID,
283
+ T.TYPE_AUTO,
277
284
  ]);
@@ -79,7 +79,7 @@
79
79
  "name": "storage.modifier.naide"
80
80
  },
81
81
  "types": {
82
- "match": "\\b(str|int|num|bool|list|map|any|json|void)\\b",
82
+ "match": "\\b(str|int|num|bool|list|map|any|json|void|auto)\\b",
83
83
  "name": "support.type.naide"
84
84
  },
85
85
  "keywords-control": {
@@ -91,7 +91,7 @@
91
91
  "name": "keyword.other.naide"
92
92
  },
93
93
  "keywords-declaration": {
94
- "match": "\\b(fn|fn\\.async|use|from|as|model|extends|schema|env|db|every|watch|test|assert|queue|job|new|enum|swap)\\b",
94
+ "match": "\\b(fn|fn\\.async|use|from|as|model|extends|schema|env|db|every|watch|test|assert|queue|job|new|enum|swap|init)\\b",
95
95
  "name": "keyword.declaration.naide"
96
96
  },
97
97
  "keywords-operator": {
@@ -129,7 +129,7 @@
129
129
  "name": "support.function.naide"
130
130
  },
131
131
  {
132
- "match": "\\b(ask|sleep|exit|read|write|fetch_json|random|sort|reverse|unique|len|upper|lower|trim|split|join|contains|replace|keys|values|entries|range|abs|round|ceil|floor|sqrt|pow|sum|flat|zip|chunk|str|int|float|json_parse|json_str|now|time|print)\\b(?=\\s*[\\(])",
132
+ "match": "\\b(ask|sleep|exit|read|write|fetch_json|random|sort|reverse|unique|len|upper|lower|trim|split|join|contains|replace|keys|values|entries|range|abs|round|ceil|floor|sqrt|pow|sum|flat|zip|chunk|str|int|float|json_parse|json_str|now|time|print|map|filter|reduce|find|every|some|foreach)\\b(?=\\s*[\\(])",
133
133
  "name": "support.function.naide"
134
134
  }
135
135
  ]