plainscript-lang 0.1.7-latest

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 (42) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +815 -0
  3. package/compiler/bundler.js +86 -0
  4. package/compiler/cli.js +843 -0
  5. package/compiler/dependency-detector.js +173 -0
  6. package/compiler/formatter.js +170 -0
  7. package/compiler/generator.js +2032 -0
  8. package/compiler/lexer.js +413 -0
  9. package/compiler/parser.js +2077 -0
  10. package/compiler/version.js +6 -0
  11. package/docs/PLINJS_SPEC.md +1051 -0
  12. package/docs/index.html +2287 -0
  13. package/docs/logo.jpg +0 -0
  14. package/docs/website/index.html +1500 -0
  15. package/docs/website/script.js +197 -0
  16. package/docs/website/style.css +1117 -0
  17. package/examples/arrays.pln +13 -0
  18. package/examples/cache-and-jobs.pln +29 -0
  19. package/examples/database.pln +41 -0
  20. package/examples/day2.pln +7 -0
  21. package/examples/day3.pln +11 -0
  22. package/examples/deployment.pln +12 -0
  23. package/examples/email-send.pln +22 -0
  24. package/examples/expressions.pln +40 -0
  25. package/examples/football-backend/app.pln +90 -0
  26. package/examples/hello.pln +3 -0
  27. package/examples/id-verification/app.pln +29 -0
  28. package/examples/id-verification/make-sample-id.js +123 -0
  29. package/examples/loops.pln +17 -0
  30. package/examples/objects.pln +12 -0
  31. package/examples/rest-api-fetch.pln +94 -0
  32. package/examples/rest-api.pln +68 -0
  33. package/examples/scheduled-tasks.pln +23 -0
  34. package/examples/server.pln +25 -0
  35. package/examples/start.pln +2 -0
  36. package/examples/stdlib.pln +41 -0
  37. package/examples/telegram-bot.pln +42 -0
  38. package/examples/web-app.pln +20 -0
  39. package/examples/websocket-server.pln +27 -0
  40. package/examples/whatsapp-bot/pairing.pln +17 -0
  41. package/examples/whatsapp-bot/qr.pln +8 -0
  42. package/package.json +47 -0
@@ -0,0 +1,413 @@
1
+ // Lexer: converts PlainScript source text into a stream of tokens.
2
+ // Each token includes { type, value, line, col } for diagnostic reporting.
3
+
4
+ const TOKEN = {
5
+ // Core keywords (v0.1–v0.4)
6
+ REMEMBER: 'REMEMBER',
7
+ SHOW: 'SHOW',
8
+ AS: 'AS',
9
+ IF: 'IF',
10
+ OTHERWISE: 'OTHERWISE',
11
+ DONE: 'DONE',
12
+ IS: 'IS',
13
+ GREATER: 'GREATER',
14
+ LESS: 'LESS',
15
+ THAN: 'THAN',
16
+ MAKE: 'MAKE',
17
+ GIVE: 'GIVE',
18
+ BECOMES: 'BECOMES',
19
+ FOR: 'FOR',
20
+ EACH: 'EACH', // also used for "every" alias
21
+ IN: 'IN',
22
+ WHILE: 'WHILE',
23
+ USE: 'USE',
24
+ IMPORT: 'IMPORT',
25
+ // v0.3 — Express runtime
26
+ WHEN: 'WHEN',
27
+ SOMEONE: 'SOMEONE',
28
+ VISITS: 'VISITS',
29
+ LISTEN: 'LISTEN',
30
+ ON: 'ON',
31
+ REPLY: 'REPLY',
32
+ JSON_KW: 'JSON_KW',
33
+ SERVE: 'SERVE',
34
+ FOLDER: 'FOLDER',
35
+ // v0.6 — Extended comparisons
36
+ ABOVE: 'ABOVE',
37
+ BELOW: 'BELOW',
38
+ AT: 'AT',
39
+ LEAST: 'LEAST',
40
+ MOST: 'MOST',
41
+ NOT: 'NOT',
42
+ EMPTY: 'EMPTY',
43
+ CONTAINS: 'CONTAINS',
44
+ STARTS: 'STARTS',
45
+ ENDS: 'ENDS',
46
+ WITH: 'WITH',
47
+ BETWEEN: 'BETWEEN',
48
+ AND: 'AND',
49
+ OR: 'OR', // v2.1.1 — logical or in conditions
50
+ // v0.6 — Express DX
51
+ WEB: 'WEB',
52
+ ROUTE_KW: 'ROUTE_KW',
53
+ START_KW: 'START_KW',
54
+ // v0.6 — SQLite DX
55
+ DATABASE_KW: 'DATABASE_KW',
56
+ QUERY_KW: 'QUERY_KW',
57
+ INSERT_KW: 'INSERT_KW',
58
+ UPDATE_KW: 'UPDATE_KW',
59
+ DELETE_KW: 'DELETE_KW',
60
+ EXECUTE_KW: 'EXECUTE_KW',
61
+ SQL_BODY: 'SQL_BODY', // raw SQL collected between a block keyword and "done"
62
+ // v1.1.1 — JavaScript Gateway (RFC-0011)
63
+ JAVASCRIPT_KW: 'JAVASCRIPT_KW', // the "javascript" keyword introducing a raw JS block
64
+ JS_BODY: 'JS_BODY', // raw JavaScript collected between "javascript" and "done"
65
+ ASK: 'ASK', // interactive input: ask name / ask "prompt" as name
66
+ OCR_KW: 'OCR_KW', // v2.0.1 — ocr "<image>" as <name> [using "<lang>"]
67
+ PACKAGE: 'PACKAGE', // bare npm package name after "use" (may contain -, _, ., /, @)
68
+ // Punctuation
69
+ LBRACE: 'LBRACE', // { — inline object literal (v1.2)
70
+ RBRACE: 'RBRACE', // }
71
+ COLON: 'COLON', // : — inline object property separator (v1.2)
72
+ ARROW: 'ARROW', // -> — Telegram inline keyboard button (v1.2)
73
+ LPAREN: 'LPAREN',
74
+ RPAREN: 'RPAREN',
75
+ LBRACKET: 'LBRACKET',
76
+ RBRACKET: 'RBRACKET',
77
+ COMMA: 'COMMA',
78
+ DOT: 'DOT',
79
+ PLUS: 'PLUS',
80
+ MINUS: 'MINUS', // v2.1.1 — subtraction / unary minus
81
+ STAR: 'STAR', // v2.1.1 — multiplication
82
+ SLASH: 'SLASH', // v2.1.1 — division
83
+ PERCENT: 'PERCENT', // v2.1.1 — remainder (modulo)
84
+ // Literals & identifiers
85
+ IDENTIFIER: 'IDENTIFIER',
86
+ STRING: 'STRING',
87
+ NUMBER: 'NUMBER',
88
+ TRUE_KW: 'TRUE_KW', // v2.1.1 — boolean literal true
89
+ FALSE_KW: 'FALSE_KW', // v2.1.1 — boolean literal false
90
+ NULL_KW: 'NULL_KW', // v2.1.1 — null literal
91
+ TEMPLATE_STRING: 'TEMPLATE_STRING', // backtick-delimited string with interpolation
92
+ // End of input
93
+ EOF: 'EOF',
94
+ };
95
+
96
+ const KEYWORDS = {
97
+ // Core
98
+ remember: TOKEN.REMEMBER,
99
+ show: TOKEN.SHOW,
100
+ as: TOKEN.AS,
101
+ if: TOKEN.IF,
102
+ otherwise: TOKEN.OTHERWISE,
103
+ done: TOKEN.DONE,
104
+ is: TOKEN.IS,
105
+ greater: TOKEN.GREATER,
106
+ less: TOKEN.LESS,
107
+ than: TOKEN.THAN,
108
+ make: TOKEN.MAKE,
109
+ give: TOKEN.GIVE,
110
+ becomes: TOKEN.BECOMES,
111
+ for: TOKEN.FOR,
112
+ each: TOKEN.EACH,
113
+ every: TOKEN.EACH, // alias: "for every" = "for each"
114
+ in: TOKEN.IN,
115
+ while: TOKEN.WHILE,
116
+ use: TOKEN.USE,
117
+ import: TOKEN.IMPORT,
118
+ // v0.3
119
+ when: TOKEN.WHEN,
120
+ someone: TOKEN.SOMEONE,
121
+ visits: TOKEN.VISITS,
122
+ listen: TOKEN.LISTEN,
123
+ on: TOKEN.ON,
124
+ reply: TOKEN.REPLY,
125
+ json: TOKEN.JSON_KW,
126
+ serve: TOKEN.SERVE,
127
+ folder: TOKEN.FOLDER,
128
+ // v0.6 — comparisons
129
+ above: TOKEN.ABOVE,
130
+ below: TOKEN.BELOW,
131
+ at: TOKEN.AT,
132
+ least: TOKEN.LEAST,
133
+ most: TOKEN.MOST,
134
+ not: TOKEN.NOT,
135
+ empty: TOKEN.EMPTY,
136
+ contains: TOKEN.CONTAINS,
137
+ starts: TOKEN.STARTS,
138
+ ends: TOKEN.ENDS,
139
+ with: TOKEN.WITH,
140
+ between: TOKEN.BETWEEN,
141
+ and: TOKEN.AND,
142
+ or: TOKEN.OR,
143
+ // v2.1.1 — literal keywords. These were previously plain identifiers that
144
+ // passed through to generated JavaScript; making them explicit tokens gives
145
+ // them first-class AST nodes and deterministic diagnostics.
146
+ true: TOKEN.TRUE_KW,
147
+ false: TOKEN.FALSE_KW,
148
+ null: TOKEN.NULL_KW,
149
+ // v0.6 — Express DX
150
+ web: TOKEN.WEB,
151
+ route: TOKEN.ROUTE_KW,
152
+ start: TOKEN.START_KW,
153
+ // v0.6 — SQLite DX
154
+ database: TOKEN.DATABASE_KW,
155
+ // v1.1.1 — JavaScript Gateway (RFC-0011)
156
+ javascript: TOKEN.JAVASCRIPT_KW,
157
+ ask: TOKEN.ASK,
158
+ // v2.0.1 — OCR capability
159
+ ocr: TOKEN.OCR_KW,
160
+ };
161
+
162
+ // Keywords that introduce raw SQL blocks (content up to "done" is collected verbatim).
163
+ const SQL_BLOCK_WORDS = {
164
+ query: TOKEN.QUERY_KW,
165
+ insert: TOKEN.INSERT_KW,
166
+ update: TOKEN.UPDATE_KW,
167
+ delete: TOKEN.DELETE_KW,
168
+ execute: TOKEN.EXECUTE_KW,
169
+ };
170
+
171
+ // Decode one escape sequence inside a double-quoted string, starting at
172
+ // source[index] (the backslash). Returns [decodedText, charsConsumed].
173
+ // Supported: \n \t \r \0 \\ \" \' — any other escaped character is kept as
174
+ // itself (JavaScript-style leniency), so "\q" means "q".
175
+ function decodeEscape(source, index) {
176
+ const next = source[index + 1];
177
+ if (next === undefined) return ['', 1]; // trailing backslash at EOF: drop it
178
+ const simple = { n: '\n', t: '\t', r: '\r', 0: '\0', '\\': '\\', '"': '"', "'": "'" };
179
+ if (next in simple) return [simple[next], 2];
180
+ return [next, 2];
181
+ }
182
+
183
+ function tokenize(source) {
184
+ const tokens = [];
185
+ let i = 0;
186
+ let line = 1;
187
+ let lineStart = 0;
188
+ let pendingUse = false; // true between a "use" keyword and its package name
189
+
190
+ function col() { return i - lineStart + 1; }
191
+
192
+ while (i < source.length) {
193
+ // Skip whitespace (track newlines for line counting)
194
+ if (/\s/.test(source[i])) {
195
+ if (source[i] === '\n') { line++; lineStart = i + 1; }
196
+ i++;
197
+ continue;
198
+ }
199
+
200
+ // Single-line comment: skip to end of line
201
+ if (source[i] === '/' && source[i + 1] === '/') {
202
+ while (i < source.length && source[i] !== '\n') i++;
203
+ continue;
204
+ }
205
+
206
+ const tokenLine = line;
207
+ const tokenCol = col();
208
+
209
+ // Package name after "use": a bare npm specifier may contain hyphens,
210
+ // underscores, dots, slashes, a scope ("@scope/package-name"), and a
211
+ // version range ("pkg@^1.2.0", "pkg@~2", "pkg@>=3 <4", "pkg@*").
212
+ if (pendingUse) {
213
+ if (/[A-Za-z0-9@]/.test(source[i])) {
214
+ let pkg = '';
215
+ while (i < source.length && /[A-Za-z0-9_.@/^~><=*-]/.test(source[i])) pkg += source[i++];
216
+ tokens.push({ type: TOKEN.PACKAGE, value: pkg, line: tokenLine, col: tokenCol });
217
+ pendingUse = false;
218
+ continue;
219
+ }
220
+ pendingUse = false; // not a package start — tokenize normally
221
+ }
222
+
223
+ // String literal. Normal escapes are decoded here (\n, \t, \r, \\, \",
224
+ // \', \0) so generated JavaScript receives the intended characters.
225
+ // An unknown escape keeps the escaped character itself, matching the
226
+ // lenient behaviour of JavaScript string literals.
227
+ if (source[i] === '"') {
228
+ let str = '';
229
+ i++; // skip opening quote
230
+ while (i < source.length && source[i] !== '"') {
231
+ if (source[i] === '\n') { line++; lineStart = i + 1; }
232
+ if (source[i] === '\\') {
233
+ const [decoded, consumed] = decodeEscape(source, i, tokenLine, tokenCol);
234
+ str += decoded;
235
+ // Track newlines inside multi-character escapes like "\n".
236
+ for (let k = 0; k < consumed; k++) {
237
+ if (source[i + k] === '\n') { line++; lineStart = i + k + 1; }
238
+ }
239
+ i += consumed;
240
+ continue;
241
+ }
242
+ str += source[i++];
243
+ }
244
+ if (i >= source.length) {
245
+ throw new Error(
246
+ `Line ${tokenLine}, Column ${tokenCol}: Unterminated string: the closing " is missing.`
247
+ );
248
+ }
249
+ i++; // skip closing quote
250
+ tokens.push({ type: TOKEN.STRING, value: str, line: tokenLine, col: tokenCol });
251
+ continue;
252
+ }
253
+
254
+ // Backtick string (template literal): preserves whitespace and supports
255
+ // interpolation. Scanning is escape-aware — a backslash escapes the next
256
+ // character verbatim (\` does not close the string, \\ stays a backslash,
257
+ // \$ guards ${ from interpolating) while ordinary characters, real
258
+ // newlines and ${expr} pass through untouched for the generator to emit.
259
+ if (source[i] === '`') {
260
+ let content = '';
261
+ i++; // skip opening backtick
262
+ while (i < source.length && source[i] !== '`') {
263
+ if (source[i] === '\n') { line++; lineStart = i + 1; }
264
+ if (source[i] === '\\' && i + 1 >= source.length) {
265
+ throw new Error(
266
+ `Line ${tokenLine}, Column ${tokenCol}: Unterminated backtick string: the closing \` is missing.`
267
+ );
268
+ }
269
+ if (source[i] === '\\') {
270
+ content += source[i] + source[i + 1]; // keep escape pair verbatim
271
+ i += 2;
272
+ continue;
273
+ }
274
+ content += source[i++];
275
+ }
276
+ if (i >= source.length) {
277
+ throw new Error(
278
+ `Line ${tokenLine}, Column ${tokenCol}: Unterminated backtick string: the closing \` is missing.`
279
+ );
280
+ }
281
+ i++; // skip closing backtick
282
+ tokens.push({ type: TOKEN.TEMPLATE_STRING, value: content, line: tokenLine, col: tokenCol });
283
+ continue;
284
+ }
285
+
286
+ // Number literal (may include decimal point)
287
+ if (/[0-9]/.test(source[i])) {
288
+ let num = '';
289
+ while (i < source.length && /[0-9.]/.test(source[i])) num += source[i++];
290
+ tokens.push({ type: TOKEN.NUMBER, value: Number(num), line: tokenLine, col: tokenCol });
291
+ continue;
292
+ }
293
+
294
+ // Word: keyword, SQL-block keyword, or identifier
295
+ if (/[a-zA-Z_]/.test(source[i])) {
296
+ let word = '';
297
+ while (i < source.length && /[a-zA-Z0-9_]/.test(source[i])) word += source[i++];
298
+
299
+ // SQL block keywords: collect raw content up to "done"
300
+ if (SQL_BLOCK_WORDS[word]) {
301
+ const kwType = SQL_BLOCK_WORDS[word];
302
+ tokens.push({ type: kwType, value: word, line: tokenLine, col: tokenCol });
303
+
304
+ // Check if the rest of this line is blank (raw block mode)
305
+ let j = i;
306
+ while (j < source.length && (source[j] === ' ' || source[j] === '\t')) j++;
307
+
308
+ if (j >= source.length || source[j] === '\n' || source[j] === '\r') {
309
+ // Advance past the newline
310
+ i = j;
311
+ if (i < source.length && source[i] === '\n') { i++; line++; lineStart = i; }
312
+
313
+ // Collect raw lines until a line whose trimmed content is exactly "done"
314
+ let sql = '';
315
+ while (i < source.length) {
316
+ const lineEnd = source.indexOf('\n', i);
317
+ const realEnd = lineEnd === -1 ? source.length : lineEnd;
318
+ const lineText = source.slice(i, realEnd);
319
+ const trimmed = lineText.trim();
320
+
321
+ if (trimmed === 'done') {
322
+ i = realEnd < source.length ? realEnd + 1 : realEnd;
323
+ if (realEnd < source.length) { line++; lineStart = i; }
324
+ break;
325
+ }
326
+
327
+ sql += lineText + '\n';
328
+ i = realEnd < source.length ? realEnd + 1 : realEnd;
329
+ if (realEnd < source.length) { line++; lineStart = i; }
330
+ }
331
+
332
+ tokens.push({ type: TOKEN.SQL_BODY, value: sql.trimEnd(), line: tokenLine, col: tokenCol });
333
+ tokens.push({ type: TOKEN.DONE, value: 'done', line, col: col() });
334
+ }
335
+ // else: stays on same line — parsed normally by the parser as kwType + next tokens
336
+ continue;
337
+ }
338
+
339
+ // "javascript" introduces a raw JavaScript block: collect every line
340
+ // verbatim until a line whose trimmed content is exactly "done".
341
+ // This mirrors the SQL block behaviour so the lexer never has to
342
+ // understand the JavaScript grammar inside the block.
343
+ if (word === 'javascript') {
344
+ tokens.push({ type: TOKEN.JAVASCRIPT_KW, value: word, line: tokenLine, col: tokenCol });
345
+
346
+ let j = i;
347
+ while (j < source.length && (source[j] === ' ' || source[j] === '\t')) j++;
348
+
349
+ if (j >= source.length || source[j] === '\n' || source[j] === '\r') {
350
+ // Raw block mode — advance past the newline and collect until "done"
351
+ i = j;
352
+ if (i < source.length && source[i] === '\n') { i++; line++; lineStart = i; }
353
+
354
+ let js = '';
355
+ while (i < source.length) {
356
+ const lineEnd = source.indexOf('\n', i);
357
+ const realEnd = lineEnd === -1 ? source.length : lineEnd;
358
+ const lineText = source.slice(i, realEnd);
359
+ const trimmed = lineText.trim();
360
+
361
+ if (trimmed === 'done') {
362
+ i = realEnd < source.length ? realEnd + 1 : realEnd;
363
+ if (realEnd < source.length) { line++; lineStart = i; }
364
+ break;
365
+ }
366
+
367
+ js += lineText + '\n';
368
+ i = realEnd < source.length ? realEnd + 1 : realEnd;
369
+ if (realEnd < source.length) { line++; lineStart = i; }
370
+ }
371
+
372
+ tokens.push({ type: TOKEN.JS_BODY, value: js.trimEnd(), line: tokenLine, col: tokenCol });
373
+ tokens.push({ type: TOKEN.DONE, value: 'done', line, col: col() });
374
+ }
375
+ // else: "javascript" on its own line is followed by a value —
376
+ // parsed normally by the parser (used as a remembered value).
377
+ continue;
378
+ }
379
+
380
+ const type = KEYWORDS[word] || TOKEN.IDENTIFIER;
381
+ if (type === TOKEN.USE) pendingUse = true;
382
+ tokens.push({ type, value: word, line: tokenLine, col: tokenCol });
383
+ continue;
384
+ }
385
+
386
+ // Single-character punctuation
387
+ if (source[i] === '{') { tokens.push({ type: TOKEN.LBRACE, value: '{', line: tokenLine, col: tokenCol }); i++; continue; }
388
+ if (source[i] === '}') { tokens.push({ type: TOKEN.RBRACE, value: '}', line: tokenLine, col: tokenCol }); i++; continue; }
389
+ if (source[i] === ':') { tokens.push({ type: TOKEN.COLON, value: ':', line: tokenLine, col: tokenCol }); i++; continue; }
390
+ if (source[i] === '(') { tokens.push({ type: TOKEN.LPAREN, value: '(', line: tokenLine, col: tokenCol }); i++; continue; }
391
+ if (source[i] === ')') { tokens.push({ type: TOKEN.RPAREN, value: ')', line: tokenLine, col: tokenCol }); i++; continue; }
392
+ if (source[i] === '[') { tokens.push({ type: TOKEN.LBRACKET, value: '[', line: tokenLine, col: tokenCol }); i++; continue; }
393
+ if (source[i] === ']') { tokens.push({ type: TOKEN.RBRACKET, value: ']', line: tokenLine, col: tokenCol }); i++; continue; }
394
+ if (source[i] === ',') { tokens.push({ type: TOKEN.COMMA, value: ',', line: tokenLine, col: tokenCol }); i++; continue; }
395
+ if (source[i] === '.') { tokens.push({ type: TOKEN.DOT, value: '.', line: tokenLine, col: tokenCol }); i++; continue; }
396
+ if (source[i] === '+') { tokens.push({ type: TOKEN.PLUS, value: '+', line: tokenLine, col: tokenCol }); i++; continue; }
397
+ // v2.1.1 — arithmetic. "->" is matched first so it never becomes MINUS.
398
+ if (source[i] === '-' && source[i + 1] === '>') { tokens.push({ type: TOKEN.ARROW, value: '->', line: tokenLine, col: tokenCol }); i += 2; continue; }
399
+ if (source[i] === '-') { tokens.push({ type: TOKEN.MINUS, value: '-', line: tokenLine, col: tokenCol }); i++; continue; }
400
+ if (source[i] === '*') { tokens.push({ type: TOKEN.STAR, value: '*', line: tokenLine, col: tokenCol }); i++; continue; }
401
+ if (source[i] === '/') { tokens.push({ type: TOKEN.SLASH, value: '/', line: tokenLine, col: tokenCol }); i++; continue; }
402
+ if (source[i] === '%') { tokens.push({ type: TOKEN.PERCENT, value: '%', line: tokenLine, col: tokenCol }); i++; continue; }
403
+
404
+ throw new Error(
405
+ `Line ${line}, Column ${col()}: Unexpected character "${source[i]}". PlainScript only uses letters, numbers, strings, and known symbols.`
406
+ );
407
+ }
408
+
409
+ tokens.push({ type: TOKEN.EOF, line, col: col() });
410
+ return tokens;
411
+ }
412
+
413
+ module.exports = { tokenize, TOKEN };