plainscript-lang 1.0.3-beta → 1.0.35

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.
Files changed (77) hide show
  1. package/README.md +126 -120
  2. package/compiler/bundler.js +83 -12
  3. package/compiler/cli.js +62 -11
  4. package/compiler/dependency-detector.js +17 -2
  5. package/compiler/generator.js +699 -63
  6. package/compiler/lexer.js +2 -0
  7. package/compiler/parser.js +450 -30
  8. package/compiler/sourcemap.js +142 -0
  9. package/compiler/version.js +1 -1
  10. package/docs/CAPABILITY_GAP_AUDIT.md +24 -16
  11. package/docs/PLAINSCRIPT-SPEC.md +175 -41
  12. package/docs/index.html +62 -34
  13. package/docs/logo-pln.jpg +0 -0
  14. package/docs/visualization-example.svg +32 -0
  15. package/docs/website/script.js +297 -19
  16. package/docs/website/style.css +357 -47
  17. package/examples/ai-providers.pln +1 -1
  18. package/examples/arrays.pln +1 -1
  19. package/examples/async-errors.pln +1 -1
  20. package/examples/auth-api.pln +1 -1
  21. package/examples/auth-sessions.pln +1 -1
  22. package/examples/basics.pln +1 -1
  23. package/examples/bots.pln +1 -1
  24. package/examples/cache-and-jobs.pln +1 -1
  25. package/examples/cache-schedule.pln +1 -1
  26. package/examples/collections.pln +1 -1
  27. package/examples/concurrency.pln +1 -1
  28. package/examples/conditions.pln +1 -1
  29. package/examples/database.pln +1 -1
  30. package/examples/day2.pln +1 -1
  31. package/examples/day3.pln +1 -1
  32. package/examples/deployment.pln +1 -1
  33. package/examples/email-send.pln +1 -1
  34. package/examples/error-handling.pln +1 -1
  35. package/examples/expressions.pln +1 -1
  36. package/examples/file-uploads.pln +1 -1
  37. package/examples/files-and-config.pln +1 -1
  38. package/examples/files.pln +1 -1
  39. package/examples/football-backend/app.pln +1 -1
  40. package/examples/functions.pln +1 -1
  41. package/examples/hello.pln +1 -1
  42. package/examples/http-client.pln +1 -1
  43. package/examples/http.pln +1 -1
  44. package/examples/id-verification/app.pln +1 -1
  45. package/examples/json.pln +1 -1
  46. package/examples/loops.pln +1 -1
  47. package/examples/modules/app.pln +1 -1
  48. package/examples/modules/main.pln +1 -1
  49. package/examples/modules/math.pln +1 -1
  50. package/examples/mongo.pln +33 -0
  51. package/examples/objects.pln +1 -1
  52. package/examples/ocr.pln +1 -1
  53. package/examples/packages.pln +1 -1
  54. package/examples/record-kinds.pln +1 -1
  55. package/examples/records.pln +1 -1
  56. package/examples/rest-api-fetch.pln +1 -1
  57. package/examples/rest-api.pln +1 -1
  58. package/examples/scheduled-tasks.pln +1 -1
  59. package/examples/search-and-filter.pln +1 -1
  60. package/examples/server.pln +1 -1
  61. package/examples/sessions-and-cookies.pln +1 -1
  62. package/examples/sqlite.pln +1 -1
  63. package/examples/start.pln +1 -1
  64. package/examples/stdlib.pln +1 -1
  65. package/examples/string-verbs.pln +16 -0
  66. package/examples/telegram-ai-bot.pln +1 -1
  67. package/examples/telegram-bot.pln +1 -1
  68. package/examples/testing.pln +1 -1
  69. package/examples/visualization.pln +12 -0
  70. package/examples/web-api.pln +1 -1
  71. package/examples/web-app.pln +1 -1
  72. package/examples/websocket-server.pln +1 -1
  73. package/examples/websocket.pln +1 -1
  74. package/examples/whatsapp-bot/pairing.pln +24 -2
  75. package/examples/whatsapp-bot/qr.pln +18 -2
  76. package/package.json +4 -2
  77. package/docs/logo.jpg +0 -0
@@ -347,6 +347,18 @@ function parse(tokens) {
347
347
 
348
348
  function parseStatement() {
349
349
  const token = peek();
350
+ const startLine = token ? token.line : undefined;
351
+ const startCol = token ? token.col : undefined;
352
+ const node = parseStatementCore();
353
+ if (node && typeof node === 'object' && node.line === undefined && startLine !== undefined) {
354
+ node.line = startLine;
355
+ node.col = startCol;
356
+ }
357
+ return node;
358
+ }
359
+
360
+ function parseStatementCore() {
361
+ const token = peek();
350
362
 
351
363
  // "define a kind called ..." - must check before DEFINE token dispatch
352
364
  if ((token.type === TOKEN.DEFINE || (token.type === TOKEN.IDENTIFIER && token.value === 'define')) &&
@@ -357,7 +369,7 @@ function parse(tokens) {
357
369
  // "load env file" - must check before LOAD token dispatch
358
370
  if ((token.type === TOKEN.LOAD || (token.type === TOKEN.IDENTIFIER && token.value === 'load')) &&
359
371
  peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'env' &&
360
- peekAt(2).type === TOKEN.IDENTIFIER && peekAt(2).value === 'file') {
372
+ (peekAt(2).type === TOKEN.IDENTIFIER || peekAt(2).type === TOKEN.FILE_KW) && peekAt(2).value === 'file') {
361
373
  advance(); // load
362
374
  advance(); // env
363
375
  advance(); // file
@@ -391,15 +403,18 @@ function parse(tokens) {
391
403
  }
392
404
  if (token.type === TOKEN.WHILE) return parseWhile();
393
405
  if (token.type === TOKEN.USE) return parseUse();
394
- if (token.type === TOKEN.IMPORT) return parseImport();
395
- if (token.type === TOKEN.INCLUDE) return parseImport(); // alias for import
396
- if (token.type === TOKEN.LOAD) return parseImport(); // alias for import
406
+ if (token.type === TOKEN.IMPORT || token.type === TOKEN.INCLUDE || token.type === TOKEN.LOAD ||
407
+ (token.type === TOKEN.IDENTIFIER && token.value === 'bring')) {
408
+ return parseImport();
409
+ }
397
410
  if (token.type === TOKEN.WHEN) return parseWhen();
398
411
  // v2.3 — English-like function declaration: "to add a and b together ... done"
399
412
  if (token.type === TOKEN.TO && peekAt(1).type === TOKEN.IDENTIFIER) return parseToFunction();
400
413
  // v2.4 — "list with" and "record with" as expressions
401
414
  if (token.type === TOKEN.IDENTIFIER && token.value === 'list' && peekAt(1).type === TOKEN.WITH) return parseListWith();
402
415
  if (token.type === TOKEN.IDENTIFIER && token.value === 'record' && peekAt(1).type === TOKEN.WITH) return parseRecordWith(false);
416
+ if (token.type === TOKEN.IDENTIFIER && token.value === 'put' && peekAt(1).type !== TOKEN.LPAREN) return parsePutStatement();
417
+ if (token.type === TOKEN.IDENTIFIER && token.value === 'unpack') return parseUnpackStatement();
403
418
  if (token.type === TOKEN.LIST_WITH) return parseListWith();
404
419
  if (token.type === TOKEN.RECORD_WITH) return parseRecordWith(false);
405
420
  if (token.type === TOKEN.LISTEN) return parseListen();
@@ -462,6 +477,54 @@ function parse(tokens) {
462
477
 
463
478
  // Statements starting with an identifier: call, becomes, index/member becomes
464
479
  if (token.type === TOKEN.IDENTIFIER) {
480
+ // v2.5 — natural string and collection verbs that transform a variable
481
+ // in place: "lowercase title", "uppercase first letter of each word in title",
482
+ // "split title by \" \"", "join title by \" \"", "trim title".
483
+ const TRANSFORM_VERB = token.value === 'lowercase' || token.value === 'uppercase' ||
484
+ token.value === 'trim' || token.value === 'split' || token.value === 'join';
485
+ if (TRANSFORM_VERB && peekAt(1).type === TOKEN.IDENTIFIER) {
486
+ // "uppercase first letter of each word in <target>"
487
+ if (token.value === 'uppercase' &&
488
+ peekAt(1).value === 'first' && peekAt(2).value === 'letter' &&
489
+ peekAt(3).value === 'of' && peekAt(4).type === TOKEN.EACH && peekAt(4).value === 'each' &&
490
+ peekAt(5).value === 'word') {
491
+ for (let t = 0; t < 6; t++) advance(); // uppercase first letter of each word
492
+ const connector = peek();
493
+ if (connector.type !== TOKEN.IN && !(connector.type === TOKEN.IDENTIFIER && connector.value === 'in')) {
494
+ throw new Error(makeError(
495
+ 'Expected "in <name>" after "uppercase first letter of each word".\n\nExample:\n uppercase first letter of each word in title',
496
+ connector
497
+ ));
498
+ }
499
+ advance(); // in
500
+ const target = peek();
501
+ if (target.type !== TOKEN.IDENTIFIER) {
502
+ throw new Error(makeError('Expected a variable name after "in".', target));
503
+ }
504
+ advance();
505
+ return { type: 'CapitalizeWordsStatement', target: target.value };
506
+ }
507
+
508
+ // "lowercase title" / "uppercase title" / "trim title"
509
+ if (token.value === 'lowercase' || token.value === 'uppercase' || token.value === 'trim') {
510
+ const kind = token.value;
511
+ advance(); // verb
512
+ const target = advance().value;
513
+ return { type: 'StringTransformStatement', kind, target };
514
+ }
515
+
516
+ // "split title by <sep>" / "join list by <sep>"
517
+ if ((token.value === 'split' || token.value === 'join') &&
518
+ peekAt(2).type === TOKEN.IDENTIFIER && peekAt(2).value === 'by') {
519
+ const kind = token.value;
520
+ advance(); // verb
521
+ const target = advance().value;
522
+ advance(); // by
523
+ const separator = parseExpression();
524
+ return { type: kind === 'split' ? 'SplitStatement' : 'JoinStatement', target, separator };
525
+ }
526
+ }
527
+
465
528
  // switch ... against ... done
466
529
  if (token.value === 'switch' && (peekAt(1).type === TOKEN.IDENTIFIER || tokenStartsValue(peekAt(1)))) {
467
530
  return parseSwitchStatement();
@@ -525,6 +588,15 @@ function parse(tokens) {
525
588
  return parsePostgres();
526
589
  }
527
590
 
591
+ // v2.2.0 — mongo "<connection-string>" [db "<name>"]: binds the MongoDB client.
592
+ // Contextual: a variable named "mongo" or "mongodb" keeps its meaning unless
593
+ // a connection string follows on the same line.
594
+ if ((token.value === 'mongo' || token.value === 'mongodb') && (
595
+ peekAt(1).type === TOKEN.STRING || peekAt(1).type === TOKEN.IDENTIFIER ||
596
+ peekAt(1).type === TOKEN.LPAREN || peekAt(1).type === TOKEN.TEMPLATE_STRING)) {
597
+ return parseMongo();
598
+ }
599
+
528
600
  // v2.1.0 — cache "<redis-url>" / cache env("REDIS_URL"): connects the
529
601
  // shared Redis client used by cacheGet / cacheSet / cacheDelete.
530
602
  // Contextual like "postgres": a variable named "cache" keeps its
@@ -614,6 +686,23 @@ function parse(tokens) {
614
686
  return parseWhatsAppBot();
615
687
  }
616
688
 
689
+ // v2.1.1 — pair whatsapp "<phone>": starts an on-demand WhatsApp pairing
690
+ // session for the given phone number. Used by hybrid bots where a
691
+ // Telegram command triggers a WhatsApp pairing flow.
692
+ // Accepts a string literal (validated at compile time) or any expression
693
+ // (e.g. a variable filled by regex matches[1]).
694
+ if (token.value === 'pair' &&
695
+ peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'whatsapp') {
696
+ advance(); // pair
697
+ advance(); // whatsapp
698
+ if (peek().type === TOKEN.STRING) {
699
+ const phone = advance().value;
700
+ return { type: 'WhatsAppPairStatement', phone: { type: 'StringLiteral', value: phone } };
701
+ }
702
+ const phoneExpr = parseExpression();
703
+ return { type: 'WhatsAppPairStatement', phone: phoneExpr };
704
+ }
705
+
617
706
  // v2.1.1 — log message: prints the normalized message record inside an
618
707
  // "on message" handler. Generation rejects it everywhere else with a
619
708
  // teaching error.
@@ -624,6 +713,15 @@ function parse(tokens) {
624
713
  return { type: 'WhatsAppLogStatement' };
625
714
  }
626
715
 
716
+ // v2.14 — download "<path>": saves the current message's media (image,
717
+ // video, audio, document, sticker, …) to a file, inside an "on message"
718
+ // handler. Generation rejects it everywhere else with a teaching error.
719
+ if (token.value === 'download' && peekAt(1).type === TOKEN.STRING) {
720
+ advance(); // download
721
+ const filePath = advance().value;
722
+ return { type: 'WhatsAppDownloadStatement', filePath };
723
+ }
724
+
627
725
  // v2.1.0 — broadcast <expr>: sends to every connected socket.
628
726
  if (token.value === 'broadcast') {
629
727
  advance(); // broadcast
@@ -631,6 +729,16 @@ function parse(tokens) {
631
729
  return { type: 'BroadcastStatement', value };
632
730
  }
633
731
 
732
+ // v2.2.0 — send to <connection> <message>: send to a specific stored connection.
733
+ if (token.value === 'send' &&
734
+ (peekAt(1).type === TOKEN.TO || (peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'to'))) {
735
+ advance(); // send
736
+ advance(); // to
737
+ const connection = parseExpression();
738
+ const value = parseExpression();
739
+ return { type: 'SendToStatement', connection, value };
740
+ }
741
+
634
742
  // v2.1.0 — send socket <expr>: replies to one connected socket.
635
743
  if (token.value === 'send' &&
636
744
  peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'socket') {
@@ -749,7 +857,7 @@ function parse(tokens) {
749
857
  // load env file "<path>" → apply .env KEY=VALUE pairs to process.env
750
858
  if ((token.type === TOKEN.LOAD || (token.type === TOKEN.IDENTIFIER && token.value === 'load')) &&
751
859
  peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'env' &&
752
- peekAt(2).type === TOKEN.IDENTIFIER && peekAt(2).value === 'file') {
860
+ (peekAt(2).type === TOKEN.IDENTIFIER || peekAt(2).type === TOKEN.FILE_KW) && peekAt(2).value === 'file') {
753
861
  advance(); // load
754
862
  advance(); // env
755
863
  advance(); // file
@@ -768,13 +876,11 @@ function parse(tokens) {
768
876
  return parseCheckStatement();
769
877
  }
770
878
 
771
- // export <name> → mark a top-level symbol for module.exports
772
- if (token.value === 'export' &&
773
- peekAt(1).type === TOKEN.IDENTIFIER &&
774
- peekAt(2).type !== TOKEN.LPAREN) {
775
- advance(); // export
776
- const name = advance().value;
777
- return { type: 'ExportStatement', name };
879
+ // export / share / expose → mark top-level symbol(s) or re-export from module
880
+ if ((token.value === 'export' || token.value === 'share' || token.value === 'expose' ||
881
+ token.type === TOKEN.SHARE || token.type === TOKEN.EXPOSE) &&
882
+ peekAt(1).type !== TOKEN.LPAREN && peekAt(1).type !== TOKEN.BECOMES) {
883
+ return parseExport();
778
884
  }
779
885
 
780
886
  // throw <value> → raise an error for the caller's try/recover to catch.
@@ -890,9 +996,10 @@ function parse(tokens) {
890
996
  target = parseInlineObjectLiteral(true); // returns InlineObjectLiteral with properties, destructuring mode
891
997
  target.type = 'ObjectPattern';
892
998
  } else {
893
- // Allow "back" (TOKEN.BACK) as a variable name (it's a keyword for "give back" but valid as identifier)
999
+ // Allow contextual keywords as variable names in remember declarations
894
1000
  let nameToken = peek();
895
- if (nameToken.type === TOKEN.BACK) {
1001
+ const REMEMBER_KEYWORDS = new Set([TOKEN.BACK, TOKEN.TOTAL, TOKEN.REPLY, TOKEN.RESPOND, TOKEN.SEND_BACK, TOKEN.FILE_KW]);
1002
+ if (REMEMBER_KEYWORDS.has(nameToken.type)) {
896
1003
  advance();
897
1004
  target = nameToken.value;
898
1005
  } else {
@@ -903,6 +1010,12 @@ function parse(tokens) {
903
1010
  }
904
1011
  }
905
1012
 
1013
+ // Object literal: next token is IDENTIFIER (except dictionary/map) followed by IS or BE
1014
+ if (peek().type === TOKEN.IDENTIFIER && peek().value !== 'dictionary' && peek().value !== 'map' && (peekAt(1).type === TOKEN.IS || peekAt(1).type === TOKEN.BE)) {
1015
+ const init = parseInlineObjectLiteral(false);
1016
+ return { type: 'VariableDeclaration', name: target, initializer: init, isLet, isConstant: !isLet };
1017
+ }
1018
+
906
1019
  // "let" uses "is" or "be" for simple vars, but also accepts "as" for both simple and destructuring
907
1020
  // "remember" always uses "as"
908
1021
  const isDestructuring = target && typeof target === 'object' && (target.type === 'ArrayPattern' || target.type === 'ObjectPattern');
@@ -1169,6 +1282,122 @@ function parseAsk() {
1169
1282
  return { type: 'InlineObjectLiteral', properties };
1170
1283
  }
1171
1284
 
1285
+ function parseDictionaryWith() {
1286
+ advance(); // dictionary or map
1287
+ consume(TOKEN.WITH, 'Expected "with" after dictionary/map.');
1288
+ const pairs = [];
1289
+ if (peek().type !== TOKEN.DONE && peek().value !== 'done') {
1290
+ while (true) {
1291
+ if (peek().type === TOKEN.DONE || peek().value === 'done') break;
1292
+ const key = parseExpression();
1293
+ consume(TOKEN.IS, 'Expected "is" between key and value in dictionary/map.');
1294
+ const value = parseExpression();
1295
+ pairs.push({ key, value });
1296
+ if (peek().type === TOKEN.AND || (peek().type === TOKEN.IDENTIFIER && peek().value === 'and') || peek().type === TOKEN.COMMA) {
1297
+ advance();
1298
+ continue;
1299
+ }
1300
+ break;
1301
+ }
1302
+ }
1303
+ if (peek().type === TOKEN.DONE || (peek().type === TOKEN.IDENTIFIER && peek().value === 'done')) {
1304
+ advance(); // done
1305
+ }
1306
+ return { type: 'DictionaryLiteral', pairs };
1307
+ }
1308
+
1309
+ function parseSetWith() {
1310
+ advance(); // set
1311
+ consume(TOKEN.WITH, 'Expected "with" after "set".');
1312
+ const elements = [];
1313
+ if (peek().type !== TOKEN.DONE && peek().value !== 'done') {
1314
+ while (true) {
1315
+ if (peek().type === TOKEN.DONE || peek().value === 'done') break;
1316
+ elements.push(parseExpression());
1317
+ if (peek().type === TOKEN.COMMA || peek().type === TOKEN.AND || (peek().type === TOKEN.IDENTIFIER && peek().value === 'and')) {
1318
+ advance();
1319
+ continue;
1320
+ }
1321
+ break;
1322
+ }
1323
+ }
1324
+ if (peek().type === TOKEN.DONE || (peek().type === TOKEN.IDENTIFIER && peek().value === 'done')) {
1325
+ advance(); // done
1326
+ }
1327
+ return { type: 'SetLiteral', elements };
1328
+ }
1329
+
1330
+ function parseTupleWith() {
1331
+ advance(); // tuple
1332
+ consume(TOKEN.WITH, 'Expected "with" after "tuple".');
1333
+ const elements = [];
1334
+ if (peek().type !== TOKEN.DONE && peek().value !== 'done') {
1335
+ while (true) {
1336
+ if (peek().type === TOKEN.DONE || peek().value === 'done') break;
1337
+ elements.push(parseExpression());
1338
+ if (peek().type === TOKEN.COMMA || peek().type === TOKEN.AND || (peek().type === TOKEN.IDENTIFIER && peek().value === 'and')) {
1339
+ advance();
1340
+ continue;
1341
+ }
1342
+ break;
1343
+ }
1344
+ }
1345
+ if (peek().type === TOKEN.DONE || (peek().type === TOKEN.IDENTIFIER && peek().value === 'done')) {
1346
+ advance(); // done
1347
+ }
1348
+ return { type: 'TupleLiteral', elements };
1349
+ }
1350
+
1351
+ function parseTupleCall() {
1352
+ advance(); // tuple
1353
+ consume(TOKEN.LPAREN, 'Expected "(" after tuple.');
1354
+ const elements = [];
1355
+ if (peek().type !== TOKEN.RPAREN) {
1356
+ while (true) {
1357
+ elements.push(parseExpression());
1358
+ if (peek().type === TOKEN.COMMA) { advance(); continue; }
1359
+ break;
1360
+ }
1361
+ }
1362
+ consume(TOKEN.RPAREN, 'Expected ")" to close tuple.');
1363
+ return { type: 'TupleLiteral', elements };
1364
+ }
1365
+
1366
+ function parsePutStatement() {
1367
+ advance(); // put
1368
+ const key = parseNullish();
1369
+ consume(TOKEN.AS, 'Expected "as" between key and value in "put".');
1370
+ const value = parseNullish();
1371
+ if (peek().type === TOKEN.IN || (peek().type === TOKEN.IDENTIFIER && peek().value === 'in')) {
1372
+ advance(); // in
1373
+ } else {
1374
+ consume(TOKEN.IN, 'Expected "in" before map variable in "put".');
1375
+ }
1376
+ const mapVar = parsePrimary();
1377
+ return { type: 'PutStatement', mapVar, key, value };
1378
+ }
1379
+
1380
+ function parseUnpackStatement() {
1381
+ advance(); // unpack
1382
+ if (peek().type === TOKEN.IDENTIFIER && peek().value === 'tuple') {
1383
+ advance(); // tuple
1384
+ }
1385
+ const tupleExpr = parseExpression();
1386
+ if (peek().type === TOKEN.INTO || (peek().type === TOKEN.IDENTIFIER && peek().value === 'into')) {
1387
+ advance(); // into
1388
+ }
1389
+ const variables = [];
1390
+ while (peek().type === TOKEN.IDENTIFIER) {
1391
+ variables.push(advance().value);
1392
+ if (peek().type === TOKEN.COMMA || peek().type === TOKEN.AND || (peek().type === TOKEN.IDENTIFIER && peek().value === 'and')) {
1393
+ advance();
1394
+ continue;
1395
+ }
1396
+ break;
1397
+ }
1398
+ return { type: 'UnpackStatement', tupleExpr, variables };
1399
+ }
1400
+
1172
1401
  function parseParamList() {
1173
1402
  const params = [];
1174
1403
  if (peek().type === TOKEN.RPAREN) return params;
@@ -1202,7 +1431,15 @@ function parseAsk() {
1202
1431
  return pattern;
1203
1432
  }
1204
1433
  // Regular parameter
1205
- const name = consume(TOKEN.IDENTIFIER, 'Expected a parameter name.').value;
1434
+ // Contextual keywords (back/reply/respond/send back/file) are valid
1435
+ // identifier names when used as a parameter, mirroring `remember`.
1436
+ const PARAM_KEYWORDS = new Set([TOKEN.BACK, TOKEN.REPLY, TOKEN.RESPOND, TOKEN.SEND_BACK, TOKEN.FILE_KW]);
1437
+ let name;
1438
+ if (PARAM_KEYWORDS.has(peek().type)) {
1439
+ name = advance().value;
1440
+ } else {
1441
+ name = consume(TOKEN.IDENTIFIER, 'Expected a parameter name.').value;
1442
+ }
1206
1443
  const param = { name };
1207
1444
  if (peek().type === TOKEN.AS) {
1208
1445
  advance(); // as
@@ -1429,13 +1666,19 @@ function parseAsk() {
1429
1666
  return { type: 'WhileStatement', condition, body };
1430
1667
  }
1431
1668
 
1432
- // import "./file.pln"
1433
- // import { name1, name2 } from "./file.pln" (named imports — binds only the
1434
- // listed symbols; the whole file is still bundled as a dependency)
1669
+ // Enterprise & Intent-Oriented Module Importing & Exporting
1670
+ // bring circleArea from "./math.pln"
1671
+ // bring circleArea and squareArea from "./math.pln"
1672
+ // bring all from "./math.pln" as math
1673
+ // bring express from "express"
1674
+ // bring { get, post } from "axios"
1675
+ // import { circleArea, squareArea } from "./math.pln"
1676
+ // import all as math from "./math.pln"
1677
+ // import "./file.pln"
1435
1678
  function parseImport() {
1436
- consume(TOKEN.IMPORT);
1679
+ advance(); // consume keyword (import / bring / include / load)
1437
1680
 
1438
- // Named-import form: import { a, b } from "./file.pln"
1681
+ // Form 1: import { a, b } from "path"
1439
1682
  if (peek().type === TOKEN.LBRACE) {
1440
1683
  advance(); // {
1441
1684
  const names = [];
@@ -1447,12 +1690,8 @@ function parseAsk() {
1447
1690
  if (peek().type === TOKEN.COMMA) { advance(); continue; }
1448
1691
  break;
1449
1692
  }
1450
- consume(TOKEN.RBRACE,
1451
- 'Expected "}" to close the import braces.\n\nExample:\n import { helper } from "./util.pln"');
1452
- // The "from" is a plain identifier; tolerate its optional presence so
1453
- // both "import { x } from "./f.pln"" and "import { x } "./f.pln"" read
1454
- // naturally, but require it for the documented form.
1455
- if (peek().type === TOKEN.IDENTIFIER && peek().value === 'from') {
1693
+ consume(TOKEN.RBRACE, 'Expected "}" to close the import braces.');
1694
+ if (peek().type === TOKEN.FROM || (peek().type === TOKEN.IDENTIFIER && peek().value === 'from')) {
1456
1695
  advance(); // from
1457
1696
  }
1458
1697
  const filePath = consume(
@@ -1462,7 +1701,50 @@ function parseAsk() {
1462
1701
  return { type: 'ImportStatement', path: filePath, names };
1463
1702
  }
1464
1703
 
1465
- // Whole-module form: import "./file.pln"
1704
+ // Form 2: bring all from "path" as math / import all as math from "path" / import * as math from "path"
1705
+ if (peek().type === TOKEN.ALL || (peek().type === TOKEN.IDENTIFIER && peek().value === 'all') || peek().type === TOKEN.TIMES) {
1706
+ advance(); // all or *
1707
+ let namespace = null;
1708
+ if (peek().type === TOKEN.AS) {
1709
+ advance(); // as
1710
+ namespace = consume(TOKEN.IDENTIFIER, 'Expected a namespace identifier after "as".').value;
1711
+ }
1712
+ if (peek().type === TOKEN.FROM || (peek().type === TOKEN.IDENTIFIER && peek().value === 'from')) {
1713
+ advance(); // from
1714
+ }
1715
+ const filePath = consume(TOKEN.STRING, 'Expected a file path or package string after "from".').value;
1716
+ if (peek().type === TOKEN.AS && !namespace) {
1717
+ advance(); // as
1718
+ namespace = consume(TOKEN.IDENTIFIER, 'Expected a namespace identifier after "as".').value;
1719
+ }
1720
+ return { type: 'ImportStatement', path: filePath, namespace };
1721
+ }
1722
+
1723
+ // Form 3: bring symbol from "path" / bring a and b from "path" / bring express from "express"
1724
+ if ((peek().type === TOKEN.IDENTIFIER || peek().type === TOKEN.PACKAGE) && peekAt(1).type !== TOKEN.STRING) {
1725
+ const names = [];
1726
+ const importLine = peek().line;
1727
+ let defaultImport = null;
1728
+ while ((peek().type === TOKEN.IDENTIFIER || peek().type === TOKEN.PACKAGE) && peek().line === importLine) {
1729
+ if (peek().type === TOKEN.FROM || peek().value === 'from') break;
1730
+ names.push(advance().value);
1731
+ if (peek().type === TOKEN.AND || (peek().type === TOKEN.IDENTIFIER && peek().value === 'and') || peek().type === TOKEN.COMMA) {
1732
+ advance();
1733
+ }
1734
+ }
1735
+ if (peek().type === TOKEN.FROM || (peek().type === TOKEN.IDENTIFIER && peek().value === 'from')) {
1736
+ advance(); // from
1737
+ }
1738
+ if (peek().type === TOKEN.STRING) {
1739
+ const filePath = advance().value;
1740
+ if (names.length === 1) {
1741
+ defaultImport = names[0];
1742
+ }
1743
+ return { type: 'ImportStatement', path: filePath, names, defaultImport };
1744
+ }
1745
+ }
1746
+
1747
+ // Form 4: Whole-module form: import "./file.pln"
1466
1748
  const filePath = consume(
1467
1749
  TOKEN.STRING,
1468
1750
  'Expected a file path string after "import".\n\nExample:\n import "./math.pln"'
@@ -1470,6 +1752,65 @@ function parseAsk() {
1470
1752
  return { type: 'ImportStatement', path: filePath };
1471
1753
  }
1472
1754
 
1755
+ // Enterprise & Intent-Oriented Exporting
1756
+ // export circleArea
1757
+ // export circleArea and squareArea
1758
+ // export { circleArea, squareArea }
1759
+ // export all from "./math.pln"
1760
+ // export circleArea and squareArea from "./math.pln"
1761
+ function parseExport() {
1762
+ advance(); // export / share / expose
1763
+
1764
+ // Form 1: export all from "./math.pln" / export * from "./math.pln"
1765
+ if (peek().type === TOKEN.ALL || (peek().type === TOKEN.IDENTIFIER && peek().value === 'all') || peek().type === TOKEN.TIMES) {
1766
+ advance(); // all or *
1767
+ if (peek().type === TOKEN.FROM || (peek().type === TOKEN.IDENTIFIER && peek().value === 'from')) {
1768
+ advance(); // from
1769
+ }
1770
+ const fromPath = consume(TOKEN.STRING, 'Expected a file path string after "from".').value;
1771
+ return { type: 'ExportStatement', exportAll: true, fromPath };
1772
+ }
1773
+
1774
+ // Form 2: export { a, b } [from "./math.pln"]
1775
+ if (peek().type === TOKEN.LBRACE) {
1776
+ advance(); // {
1777
+ const names = [];
1778
+ while (true) {
1779
+ names.push(consume(TOKEN.IDENTIFIER, 'Expected an identifier inside export braces.').value);
1780
+ if (peek().type === TOKEN.COMMA) { advance(); continue; }
1781
+ break;
1782
+ }
1783
+ consume(TOKEN.RBRACE, 'Expected "}" to close export braces.');
1784
+ let fromPath = null;
1785
+ if (peek().type === TOKEN.FROM || (peek().type === TOKEN.IDENTIFIER && peek().value === 'from')) {
1786
+ advance(); // from
1787
+ fromPath = consume(TOKEN.STRING, 'Expected a file path string after "from".').value;
1788
+ }
1789
+ return { type: 'ExportStatement', names, fromPath };
1790
+ }
1791
+
1792
+ // Form 3: export a and b [from "./math.pln"] OR export a
1793
+ const names = [];
1794
+ const exportLine = peek().line;
1795
+ while (peek().type === TOKEN.IDENTIFIER && peek().value !== 'from' && peek().value !== 'export' && peek().value !== 'share' && peek().value !== 'expose' && peek().line === exportLine) {
1796
+ names.push(advance().value);
1797
+ if (peek().type === TOKEN.AND || (peek().type === TOKEN.IDENTIFIER && peek().value === 'and') || peek().type === TOKEN.COMMA) {
1798
+ advance();
1799
+ }
1800
+ }
1801
+ let fromPath = null;
1802
+ if (peek().type === TOKEN.FROM || (peek().type === TOKEN.IDENTIFIER && peek().value === 'from')) {
1803
+ advance(); // from
1804
+ fromPath = consume(TOKEN.STRING, 'Expected a file path string after "from".').value;
1805
+ }
1806
+ return {
1807
+ type: 'ExportStatement',
1808
+ name: names.length === 1 ? names[0] : null,
1809
+ names: names.length > 0 ? names : null,
1810
+ fromPath,
1811
+ };
1812
+ }
1813
+
1473
1814
  // use <module> — side-effect or canonical binding
1474
1815
  // use <module> as <name> — bind the package to a custom variable name
1475
1816
  // <module> may carry a version range: use left-pad@^1.3.0
@@ -1686,6 +2027,7 @@ function parseAsk() {
1686
2027
 
1687
2028
  // reply <expr> / respond <expr> / send back <expr>
1688
2029
  // reply json\n <key> is <val>\n...\ndone
2030
+ // reply file "<path>"
1689
2031
  function parseReply() {
1690
2032
  const isRespond = peek().type === TOKEN.RESPOND;
1691
2033
  const isSendBack = peek().type === TOKEN.SEND_BACK;
@@ -1714,6 +2056,12 @@ function parseAsk() {
1714
2056
  advance(); // consume DONE
1715
2057
  return { type: 'ReplyJsonStatement', properties };
1716
2058
  }
2059
+ if (peek().type === TOKEN.FILE_KW) {
2060
+ advance(); // consume file
2061
+ const filePath = consume(TOKEN.STRING,
2062
+ 'Expected a file path string after "reply file".\n\nExample:\n route get "/"\n reply file "public/index.html"\n done').value;
2063
+ return { type: 'ReplyFileStatement', filePath };
2064
+ }
1717
2065
  const value = parseExpression();
1718
2066
 
1719
2067
  // reply <value> with buttons ... done → Telegram inline keyboard block.
@@ -1914,6 +2262,19 @@ function parseAsk() {
1914
2262
  return { type: 'PostgresStatement', connection };
1915
2263
  }
1916
2264
 
2265
+ // v2.2.0 — mongo "<connection-string>" [db "<name>"]: binds the MongoDB client.
2266
+ // Usage: mongo "mongodb://localhost:27017" db "mydb"
2267
+ function parseMongo() {
2268
+ const token = advance(); // mongo or mongodb
2269
+ const connection = parseExpression();
2270
+ let dbName = null;
2271
+ if (peek().type === TOKEN.IDENTIFIER && peek().value === 'db') {
2272
+ advance(); // db
2273
+ dbName = parseExpression();
2274
+ }
2275
+ return { type: 'MongoStatement', connection, dbName };
2276
+ }
2277
+
1917
2278
  // query/insert/update/delete/execute SQL_BODY DONE
1918
2279
  //
1919
2280
  // v2.1.0 — the raw SQL may reference PlainScript variables with {name}
@@ -2480,6 +2841,51 @@ function parseAsk() {
2480
2841
  peekAt(1).type === TOKEN.WITH) {
2481
2842
  return parseListWith();
2482
2843
  }
2844
+ if (peek().type === TOKEN.IDENTIFIER && (peek().value === 'dictionary' || peek().value === 'map') &&
2845
+ peekAt(1).type === TOKEN.WITH) {
2846
+ return parseDictionaryWith();
2847
+ }
2848
+ if ((peek().type === TOKEN.EMPTY || (peek().type === TOKEN.IDENTIFIER && peek().value === 'empty')) &&
2849
+ (peekAt(1).value === 'dictionary' || peekAt(1).value === 'map' || peekAt(1).value === 'set')) {
2850
+ const typeStr = peekAt(1).value;
2851
+ advance(); advance();
2852
+ if (typeStr === 'set') return { type: 'SetLiteral', elements: [] };
2853
+ return { type: 'DictionaryLiteral', pairs: [] };
2854
+ }
2855
+ if (peek().type === TOKEN.IDENTIFIER && peek().value === 'set') {
2856
+ if (peekAt(1).type === TOKEN.WITH) return parseSetWith();
2857
+ if (peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'from') {
2858
+ advance(); advance();
2859
+ const iterable = parseExpression();
2860
+ return { type: 'SetFromExpression', iterable };
2861
+ }
2862
+ }
2863
+ if (peek().type === TOKEN.IDENTIFIER && peek().value === 'tuple') {
2864
+ if (peekAt(1).type === TOKEN.WITH) return parseTupleWith();
2865
+ if (peekAt(1).type === TOKEN.LPAREN) return parseTupleCall();
2866
+ }
2867
+
2868
+ // Set Algebra: union of a and b / intersection of a and b / difference of a and b
2869
+ if (peek().type === TOKEN.IDENTIFIER &&
2870
+ (peek().value === 'union' || peek().value === 'intersection' || peek().value === 'intersect' || peek().value === 'difference') &&
2871
+ peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'of') {
2872
+ const op = advance().value;
2873
+ advance(); // of
2874
+ const left = parsePrimary();
2875
+ if (peek().type === TOKEN.AND || (peek().type === TOKEN.IDENTIFIER && peek().value === 'and')) advance();
2876
+ const right = parseExpression();
2877
+ return { type: 'SetAlgebraExpression', op, left, right };
2878
+ }
2879
+
2880
+ // Collection reflection: keys of d / values of d / entries of d / size of d
2881
+ if (peek().type === TOKEN.IDENTIFIER &&
2882
+ (peek().value === 'keys' || peek().value === 'values' || peek().value === 'entries' || peek().value === 'size') &&
2883
+ peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'of') {
2884
+ const accessor = advance().value;
2885
+ advance(); // of
2886
+ const target = parsePrimary();
2887
+ return { type: 'CollectionReflectExpression', accessor, target };
2888
+ }
2483
2889
 
2484
2890
  // v1.0.1 — record constructor: `create a Person with name "Ada" and age 17`.
2485
2891
  // `create` then an article ("a"/"an") then a kind name then "with" pairs.
@@ -2716,7 +3122,9 @@ function parseAsk() {
2716
3122
  return { type: 'ImportMetaExpression' };
2717
3123
  }
2718
3124
 
2719
- if (token.type === TOKEN.IDENTIFIER || token.type === TOKEN.BACK) {
3125
+ // Keywords that can also be used as identifiers in expression context
3126
+ const IDENTIFIER_KEYWORDS = new Set([TOKEN.BACK, TOKEN.REPLY, TOKEN.RESPOND, TOKEN.SEND_BACK, TOKEN.FILE_KW]);
3127
+ if (token.type === TOKEN.IDENTIFIER || IDENTIFIER_KEYWORDS.has(token.type)) {
2720
3128
  if (peekAt(1).type === TOKEN.USES || peekAt(1).type === TOKEN.FILLS) {
2721
3129
  const callee = { type: 'Identifier', name: token.value };
2722
3130
  advance();
@@ -3049,6 +3457,7 @@ function parseAsk() {
3049
3457
 
3050
3458
  let authFolder = null;
3051
3459
  let login = null;
3460
+ let baileysModule = null;
3052
3461
  const handlers = [];
3053
3462
 
3054
3463
  while (peek().type !== TOKEN.DONE) {
@@ -3059,6 +3468,16 @@ function parseAsk() {
3059
3468
  ));
3060
3469
  }
3061
3470
 
3471
+ // use baileys "<pkg>" — override the Baileys implementation package.
3472
+ // Default is @qwerty-xcv/baileys. Accepts any require-able package name,
3473
+ // so developers can pin a fork or the upstream @whiskeysockets/baileys.
3474
+ if (peek().type === TOKEN.USE && peekAt(1).value === 'baileys' && peekAt(2).type === TOKEN.STRING) {
3475
+ advance(); // use
3476
+ advance(); // baileys
3477
+ baileysModule = advance().value;
3478
+ continue;
3479
+ }
3480
+
3062
3481
  // auth "<folder>" — where WhatsApp session credentials persist.
3063
3482
  if (peek().type === TOKEN.IDENTIFIER && peek().value === 'auth' &&
3064
3483
  peekAt(1).type === TOKEN.STRING) {
@@ -3113,7 +3532,7 @@ function parseAsk() {
3113
3532
  }
3114
3533
 
3115
3534
  throw new Error(makeError(
3116
- 'A "whatsapp bot" block may only contain an "auth", a "login", and "on message" statements.\n\nExample:\n whatsapp bot\n auth "session"\n login qr\n\n on message\n log message\n done\n done',
3535
+ 'A "whatsapp bot" block may only contain an "auth", a "login", a "use baileys", and "on message" statements.\n\nExample:\n whatsapp bot\n auth "session"\n use baileys "@qwerty-xcv/baileys"\n login qr\n\n on message\n log message\n done\n done',
3117
3536
  peek()
3118
3537
  ));
3119
3538
  }
@@ -3122,8 +3541,9 @@ function parseAsk() {
3122
3541
  return {
3123
3542
  type: 'WhatsAppBotStatement',
3124
3543
  authFolder: authFolder || 'plainscript-whatsapp-auth',
3125
- login: login || { mode: 'qr' },
3544
+ login: login,
3126
3545
  handlers,
3546
+ baileysModule,
3127
3547
  };
3128
3548
  }
3129
3549