plainscript-lang 1.0.362 → 1.0.363
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/README.md +382 -54
- package/compiler/bundler.js +78 -3
- package/compiler/cli.js +224 -30
- package/compiler/formatter.js +21 -1
- package/compiler/generator.js +453 -107
- package/compiler/ir.js +61 -0
- package/compiler/lexer.js +42 -7
- package/compiler/lsp.js +234 -0
- package/compiler/parser.js +780 -110
- package/compiler/type-checker.js +246 -0
- package/compiler/version.js +1 -1
- package/docs/CAPABILITY_GAP_AUDIT.md +19 -3
- package/docs/GAME-PROMPT.md +3 -3
- package/docs/PLAINSCRIPT-GRAMMAR.md +308 -0
- package/docs/PLAINSCRIPT-SPEC.md +231 -5
- package/docs/index.html +5 -5
- package/docs/website/script.js +1 -1
- package/examples/ai-memory-cli.pln +221 -0
- package/examples/ai-memory.json +1 -0
- package/examples/ai-providers.pln +1 -1
- package/examples/arrays.pln +1 -1
- package/examples/async-errors.pln +1 -1
- package/examples/auth-api.pln +1 -1
- package/examples/auth-sessions.pln +1 -1
- package/examples/basics.pln +1 -1
- package/examples/bots.pln +1 -1
- package/examples/browser-interactive/README.md +1 -1
- package/examples/browser-interactive/src/interactive.pln +1 -1
- package/examples/cache-and-jobs.pln +1 -1
- package/examples/cache-schedule.pln +1 -1
- package/examples/canvas-game/README.md +1 -1
- package/examples/canvas-game/src/game.pln +1 -1
- package/examples/collections.pln +1 -1
- package/examples/compound-assignments.pln +24 -0
- package/examples/concurrency.pln +2 -2
- package/examples/conditions.pln +1 -1
- package/examples/database.pln +1 -1
- package/examples/day2.pln +1 -1
- package/examples/day3.pln +1 -1
- package/examples/deployment.pln +1 -1
- package/examples/email-send.pln +1 -1
- package/examples/error-handling.pln +1 -1
- package/examples/expressions.pln +1 -1
- package/examples/file-manager-cli.pln +51 -0
- package/examples/file-uploads.pln +1 -1
- package/examples/files-and-config.pln +1 -1
- package/examples/files.pln +1 -1
- package/examples/football-backend/app.pln +1 -1
- package/examples/functions.pln +1 -1
- package/examples/hello.pln +1 -1
- package/examples/http-client.pln +1 -1
- package/examples/http.pln +1 -1
- package/examples/id-verification/app.pln +1 -1
- package/examples/interactive-cli.pln +34 -0
- package/examples/javascript-library/README.md +1 -1
- package/examples/javascript-library/src/interop.pln +1 -1
- package/examples/json.pln +1 -1
- package/examples/loops.pln +1 -1
- package/examples/memoize-demo.pln +31 -0
- package/examples/modules/app.pln +1 -1
- package/examples/modules/main.pln +1 -1
- package/examples/modules/math.pln +1 -1
- package/examples/mongo.pln +1 -1
- package/examples/normalization-forms.pln +22 -0
- package/examples/objects.pln +1 -1
- package/examples/ocr.pln +1 -1
- package/examples/packages.pln +1 -1
- package/examples/random-cli.pln +32 -0
- package/examples/record-kinds.pln +1 -1
- package/examples/records.pln +1 -1
- package/examples/rest-api-fetch.pln +1 -1
- package/examples/rest-api.pln +1 -1
- package/examples/run-in-parallel.pln +25 -0
- package/examples/scheduled-tasks.pln +1 -1
- package/examples/search-and-filter.pln +1 -1
- package/examples/server.pln +1 -1
- package/examples/sessions-and-cookies.pln +1 -1
- package/examples/sqlite.pln +1 -1
- package/examples/start.pln +1 -1
- package/examples/stats-cli.pln +29 -0
- package/examples/stdlib.pln +1 -1
- package/examples/string-helpers.pln +23 -0
- package/examples/string-verbs.pln +1 -1
- package/examples/telegram-ai-bot.pln +1 -1
- package/examples/telegram-bot.pln +1 -1
- package/examples/terminal-cli.pln +11 -0
- package/examples/testing.pln +1 -1
- package/examples/three-dimensional/README.md +1 -1
- package/examples/three-dimensional/src/scene.pln +1 -1
- package/examples/todo-cli.pln +98 -0
- package/examples/web-api.pln +1 -1
- package/examples/web-app.pln +1 -1
- package/examples/websocket-server.pln +1 -1
- package/examples/websocket.pln +1 -1
- package/examples/whatsapp-bot/pairing.pln +1 -1
- package/examples/whatsapp-bot/qr.pln +1 -1
- package/package.json +13 -4
package/compiler/parser.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
// Parser: converts a token stream into an AST (Abstract Syntax Tree).
|
|
2
2
|
|
|
3
|
-
const { TOKEN } = require('./lexer');
|
|
3
|
+
const { tokenize, TOKEN, KEYWORDS } = require('./lexer');
|
|
4
4
|
|
|
5
5
|
// Statement-starting PlainScript keywords, used for "did you mean?" suggestions.
|
|
6
6
|
const STATEMENT_KEYWORDS = [
|
|
7
|
-
'remember', 'show', 'display', 'log', 'if', 'make', 'give',
|
|
7
|
+
'remember', 'show', 'display', 'log', 'if', 'make', 'intend', 'give',
|
|
8
8
|
'for', 'while', 'repeat', 'until', 'use', 'import', 'when', 'listen', 'reply', 'serve',
|
|
9
9
|
'web', 'route', 'start', 'database', 'query', 'insert', 'update', 'delete', 'execute',
|
|
10
|
-
'ask', '
|
|
10
|
+
'ask', 'bot', 'ocr', 'try', 'recover', 'retry',
|
|
11
11
|
'gather', 'filter', 'total', 'match', 'emit', 'stream', 'run',
|
|
12
12
|
'switch', 'case', 'default', 'break', 'continue', 'return',
|
|
13
13
|
'throw', 'catch', 'finally', 'new', 'class', 'extends', 'super',
|
|
@@ -98,6 +98,29 @@ function closestKeyword(word) {
|
|
|
98
98
|
}
|
|
99
99
|
|
|
100
100
|
// Format an error message with position info from a token when available.
|
|
101
|
+
// Words that can never be used as declaration names (variable, function,
|
|
102
|
+
// parameter). Documentation copy for docs/PLAINSCRIPT-GRAMMAR.md lives here so
|
|
103
|
+
// the error hints and the docs cannot drift apart.
|
|
104
|
+
const RESERVED_DECLARATION_WORDS = {
|
|
105
|
+
remember: true, let: true, show: true, print: true, display: true,
|
|
106
|
+
as: true, is: true, if: true, otherwise: true, else: true, done: true,
|
|
107
|
+
end: true, do: true, greater: true, less: true, than: true, make: true,
|
|
108
|
+
define: true, function: true, give: true, return: true, becomes: true,
|
|
109
|
+
for: true, each: true, every: true, in: true, while: true, use: true,
|
|
110
|
+
import: true, include: true, load: true, when: true, listens: true,
|
|
111
|
+
listen: true, on: true, json: true, file: true, serve: true, folder: true,
|
|
112
|
+
above: true, below: true, at: true, least: true, most: true, not: true,
|
|
113
|
+
empty: true, contains: true, starts: true, ends: true, with: true,
|
|
114
|
+
between: true, and: true, or: true, instanceof: true, now: true,
|
|
115
|
+
web: true, route: true, start: true, database: true, ask: true,
|
|
116
|
+
prompt: true, ocr: true, gather: true, filter: true, match: true,
|
|
117
|
+
against: true, pattern: true, parallel: true, stream: true, emit: true,
|
|
118
|
+
happens: true, catches: true, yield: true, symbol: true, debugger: true,
|
|
119
|
+
to: true, together: true, be: true, then: true, plus: true, minus: true,
|
|
120
|
+
times: true, raise: true, raises: true, choosing: true, uses: true,
|
|
121
|
+
fills: true, repeat: true, until: true, log: true,
|
|
122
|
+
};
|
|
123
|
+
|
|
101
124
|
function makeError(message, token) {
|
|
102
125
|
if (token && token.line) {
|
|
103
126
|
return `Line ${token.line}, Column ${token.col}: ${message}`;
|
|
@@ -105,13 +128,91 @@ function makeError(message, token) {
|
|
|
105
128
|
return message;
|
|
106
129
|
}
|
|
107
130
|
|
|
131
|
+
// When a declaration name is a reserved word, say so explicitly instead of a
|
|
132
|
+
// generic "Expected a variable/function name" (torture-test discoverability fix).
|
|
133
|
+
// Note: `back`, `total`, `reply`, `respond`, `send` and SQL words are
|
|
134
|
+
// deliberately usable as declaration names, so they are not listed here.
|
|
135
|
+
// (`file` is accepted as a variable name by `remember` but not as a
|
|
136
|
+
// function name, so it stays listed.)
|
|
137
|
+
function reservedWordHint(token) {
|
|
138
|
+
if (token && token.value && Object.prototype.hasOwnProperty.call(RESERVED_DECLARATION_WORDS, token.value)) {
|
|
139
|
+
return `\n\n"${token.value}" is a reserved PlainScript word and cannot be used as a name here. Pick a different name (see docs/PLAINSCRIPT-GRAMMAR.md).`;
|
|
140
|
+
}
|
|
141
|
+
return '';
|
|
142
|
+
}
|
|
143
|
+
|
|
108
144
|
function parse(tokens) {
|
|
109
145
|
let pos = 0;
|
|
110
146
|
let _inRepeatCount = false;
|
|
111
|
-
|
|
147
|
+
const declaredTypeNames = new Set(['any', 'number', 'text', 'boolean', 'null', 'object']);
|
|
112
148
|
function peek() { return tokens[pos]; }
|
|
113
149
|
function peekAt(offset) { return tokens[pos + offset] || { type: TOKEN.EOF }; }
|
|
114
150
|
function advance() { return tokens[pos++]; }
|
|
151
|
+
// v1.0.363 - at a block terminator? A done/together followed by "of" is a
|
|
152
|
+
// FIELD read ("done of item" - the README todo example), so it never closes a
|
|
153
|
+
// block. Every statement-bearing body loop must ask this instead of comparing
|
|
154
|
+
// the token type directly, or "done of t becomes true" inside an if/loop gets
|
|
155
|
+
// swallowed as the block's terminator.
|
|
156
|
+
function atBlockEnd() {
|
|
157
|
+
const t = peek();
|
|
158
|
+
if (t.type !== TOKEN.DONE && t.type !== TOKEN.TOGETHER) return false;
|
|
159
|
+
return !(peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'of');
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// v1.0.364 - recursion depth guards. Nesting beyond ~700 levels recurses
|
|
163
|
+
// past V8's stack and crashes with a raw "Maximum call stack size exceeded"
|
|
164
|
+
// RangeError — no position, no PlainScript message. Both recursive funnels
|
|
165
|
+
// (expressions via parsePrimary, statements via parseBody) count depth and
|
|
166
|
+
// fail with a clean positional error long before the stack runs out. The
|
|
167
|
+
// limits are deliberately far below the measured crash threshold (~700
|
|
168
|
+
// expression levels / ~850 block levels) because the guards themselves
|
|
169
|
+
// consume stack; real code never nests anywhere near these numbers.
|
|
170
|
+
// ParseDepthError is a sentinel: speculative-parse probe sites (which catch
|
|
171
|
+
// and replay) must rethrow it, or deep inputs get re-parsed exponentially.
|
|
172
|
+
class ParseDepthError extends Error {}
|
|
173
|
+
const MAX_EXPR_DEPTH = 300;
|
|
174
|
+
const MAX_STMT_DEPTH = 200;
|
|
175
|
+
let exprDepth = 0;
|
|
176
|
+
let stmtDepth = 0;
|
|
177
|
+
// The typo-probe below speculatively parses a full statement to produce a
|
|
178
|
+
// helpful "Expected a comparison" error. Nested conditions make that probe
|
|
179
|
+
// re-parse the whole remainder at every level — exponential behaviour that
|
|
180
|
+
// hangs on legitimately deep (but valid) nesting. Two bounds keep it cheap:
|
|
181
|
+
// 1. block-structured statement starters are never probed (a typo'd
|
|
182
|
+
// comparison cannot look like "if ..."; probing would re-parse their
|
|
183
|
+
// entire bodies), and
|
|
184
|
+
// 2. probes may only nest a bounded number of levels; deeper levels take the
|
|
185
|
+
// plain bare-value condition path, which is what the probe concludes.
|
|
186
|
+
const MAX_CONDITION_PROBE_DEPTH = 3;
|
|
187
|
+
let conditionProbeDepth = 0;
|
|
188
|
+
const PROBE_BLOCKER_SET = new Set(
|
|
189
|
+
['IF', 'WHILE', 'FOR', 'REPEAT', 'EACH', 'EVERY', 'MAKE', 'DEFINE', 'FUNCTION',
|
|
190
|
+
'TO', 'TRY', 'WHEN', 'USE', 'IMPORT', 'INCLUDE', 'LOAD', 'GATHER', 'FILTER_KW',
|
|
191
|
+
'TOTAL', 'MATCH', 'EMIT', 'STREAM', 'LISTEN', 'START_KW', 'START_ON', 'SERVE',
|
|
192
|
+
'SERVE_ON', 'SERVE_STATIC', 'SERVE_PUBLIC', 'DATABASE_KW', 'CONNECT_DB',
|
|
193
|
+
'USE_DATABASE', 'ROUTE_KW', 'WEB', 'ASK', 'PROMPT', 'OCR_KW', 'REPLY',
|
|
194
|
+
'RESPOND', 'SEND_BACK', 'DEBUGGER_KW', 'TEST']
|
|
195
|
+
.filter(k => TOKEN[k] !== undefined)
|
|
196
|
+
.map(k => TOKEN[k])
|
|
197
|
+
);
|
|
198
|
+
const PROBE_BLOCKER_WORDS = new Set(
|
|
199
|
+
['bring', 'repeat', 'define', 'when', 'start', 'serve', 'database', 'every',
|
|
200
|
+
'try', 'catch', 'class', 'test', 'respond', 'listen']
|
|
201
|
+
);
|
|
202
|
+
|
|
203
|
+
function parsePrimary() {
|
|
204
|
+
if (++exprDepth > MAX_EXPR_DEPTH) {
|
|
205
|
+
throw new ParseDepthError(makeError(
|
|
206
|
+
`Expression is nested more than ${MAX_EXPR_DEPTH} levels deep. Split it into intermediate variables.`,
|
|
207
|
+
peek()
|
|
208
|
+
));
|
|
209
|
+
}
|
|
210
|
+
try {
|
|
211
|
+
return parsePrimaryInner();
|
|
212
|
+
} finally {
|
|
213
|
+
exprDepth--;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
115
216
|
|
|
116
217
|
function consume(expectedType, hint) {
|
|
117
218
|
const token = tokens[pos];
|
|
@@ -210,7 +311,41 @@ function parse(tokens) {
|
|
|
210
311
|
// Comparisons are expressions too, so parseExpression may already have
|
|
211
312
|
// folded this into a comparison node - that IS the comparison level.
|
|
212
313
|
if (isComparisonNode(left)) return left;
|
|
213
|
-
|
|
314
|
+
const result = tryParseComparisonOperator(left, false);
|
|
315
|
+
if (result !== left) return result;
|
|
316
|
+
// No comparison operator: the value itself is the condition (`if ok`,
|
|
317
|
+
// `if not fileExists("x")`, `while connected`) - a truthy check, like
|
|
318
|
+
// JavaScript's `if (value)`. But a bare identifier followed by a word that
|
|
319
|
+
// cannot start a statement is almost certainly a typo'd comparison
|
|
320
|
+
// ("if x bigger 1" for "if x is greater than 1"), so probe the parser to
|
|
321
|
+
// keep the helpful comparison error in that case and only treat clear
|
|
322
|
+
// statement/block boundaries as a bare-value condition.
|
|
323
|
+
const next = peek();
|
|
324
|
+
const isBoundary = next.type === TOKEN.DONE || next.type === TOKEN.OTHERWISE ||
|
|
325
|
+
next.type === TOKEN.ELSE ||
|
|
326
|
+
next.type === TOKEN.TOGETHER || next.type === TOKEN.THEN || next.type === TOKEN.EOF;
|
|
327
|
+
const probeBlockedByToken = PROBE_BLOCKER_SET.has(next.type) ||
|
|
328
|
+
(next.type === TOKEN.IDENTIFIER && PROBE_BLOCKER_WORDS.has(next.value));
|
|
329
|
+
if (!isBoundary && !probeBlockedByToken && conditionProbeDepth < MAX_CONDITION_PROBE_DEPTH) {
|
|
330
|
+
const savedPos = pos;
|
|
331
|
+
conditionProbeDepth++;
|
|
332
|
+
try {
|
|
333
|
+
const stmt = parseStatement();
|
|
334
|
+
pos = savedPos;
|
|
335
|
+
if (!stmt) return { type: 'ConditionExpression', value: left };
|
|
336
|
+
} catch (e) {
|
|
337
|
+
// Depth-limit sentinels must never be swallowed by speculative probes:
|
|
338
|
+
// the alternative is exponential re-parsing of deep inputs.
|
|
339
|
+
if (e instanceof ParseDepthError) throw e;
|
|
340
|
+
throw new Error(makeError(
|
|
341
|
+
'Expected a comparison after the value. Use "is", "is above", "is below", "contains", "starts with", etc.',
|
|
342
|
+
peek()
|
|
343
|
+
));
|
|
344
|
+
} finally {
|
|
345
|
+
conditionProbeDepth--;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
return { type: 'ConditionExpression', value: left };
|
|
214
349
|
}
|
|
215
350
|
|
|
216
351
|
// Shared comparison-operator continuation. Given an already-parsed left
|
|
@@ -489,6 +624,31 @@ function parse(tokens) {
|
|
|
489
624
|
function parseStatementCore() {
|
|
490
625
|
const token = peek();
|
|
491
626
|
|
|
627
|
+
// Intent declarations share function semantics but remain distinct in the
|
|
628
|
+
// AST so future tooling can retain the author's stated intent.
|
|
629
|
+
if (token.type === TOKEN.IDENTIFIER && token.value === 'intend') {
|
|
630
|
+
return parseIntentDeclaration();
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
// `type User ... done` is contextual so existing identifier expressions
|
|
634
|
+
// remain compatible outside statement position.
|
|
635
|
+
if (token.type === TOKEN.IDENTIFIER && token.value === 'type' &&
|
|
636
|
+
peekAt(1).type === TOKEN.IDENTIFIER) {
|
|
637
|
+
return parseTypeDeclaration();
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
// v1.0.363 - `done` can name a record field ("record with text "milk" and
|
|
641
|
+
// done true"), so the statement-position target "done of t becomes ..."
|
|
642
|
+
// must parse; anything else starting with the done keyword keeps its
|
|
643
|
+
// terminator meaning.
|
|
644
|
+
if ((token.type === TOKEN.DONE || token.type === TOKEN.TOGETHER) &&
|
|
645
|
+
peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'of') {
|
|
646
|
+
const expr = parsePrimary();
|
|
647
|
+
consume(TOKEN.BECOMES, 'Expected "becomes" after "done of <record>".\n\nExample:\n done of item becomes true');
|
|
648
|
+
const value = parseExpression();
|
|
649
|
+
return { type: 'BecomeStatement', target: expr, value, op: '=' };
|
|
650
|
+
}
|
|
651
|
+
|
|
492
652
|
// "define a kind called ..." - must check before DEFINE token dispatch
|
|
493
653
|
if ((token.type === TOKEN.DEFINE || (token.type === TOKEN.IDENTIFIER && token.value === 'define')) &&
|
|
494
654
|
peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'a') {
|
|
@@ -545,7 +705,7 @@ function parse(tokens) {
|
|
|
545
705
|
peekAt(2).type === TOKEN.IDENTIFIER && TIME_UNITS[peekAt(2).value]) {
|
|
546
706
|
return parseEvery();
|
|
547
707
|
}
|
|
548
|
-
// v1.0.
|
|
708
|
+
// v1.0.363 - every frame … done: requestAnimationFrame loop. "every" lexes
|
|
549
709
|
// as TOKEN.EACH; guard on the following "frame" identifier ("for every
|
|
550
710
|
// item in list" is intercepted by TOKEN.FOR above).
|
|
551
711
|
if (token.type === TOKEN.EACH && peekAt(1).type === TOKEN.IDENTIFIER &&
|
|
@@ -599,16 +759,20 @@ function parse(tokens) {
|
|
|
599
759
|
if (token.type === TOKEN.QUERY_KW && peekAt(1).type === TOKEN.LPAREN) {
|
|
600
760
|
return { type: 'ExpressionStatement', expression: parseCallExpression() };
|
|
601
761
|
}
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
762
|
+
// v1.0.363 - when the lexer did NOT capture a raw SQL body (the word is
|
|
763
|
+
// not alone on its line), query/insert/update/execute are ordinary names
|
|
764
|
+
// and flow through the identifier branch below.
|
|
765
|
+
if (token.type === TOKEN.QUERY_KW && peekAt(1).type === TOKEN.SQL_BODY) return parseSqlBlock('query', 'QueryStatement');
|
|
766
|
+
if (token.type === TOKEN.INSERT_KW && peekAt(1).type === TOKEN.SQL_BODY) return parseSqlBlock('insert', 'InsertStatement');
|
|
767
|
+
if (token.type === TOKEN.UPDATE_KW && peekAt(1).type === TOKEN.SQL_BODY) return parseSqlBlock('update', 'UpdateStatement');
|
|
605
768
|
// v2.1.1 - delete "<url>" is an HTTP DELETE request; a bare "delete"
|
|
606
769
|
// starting a raw block keeps its SQL meaning.
|
|
607
770
|
if (token.type === TOKEN.DELETE_KW && tokenStartsValue(peekAt(1))) {
|
|
608
771
|
return { type: 'ExpressionStatement', expression: parseHttpCall('delete') };
|
|
609
772
|
}
|
|
610
773
|
if (token.type === TOKEN.DELETE_KW) return parseSqlBlock('delete', 'DeleteStatement');
|
|
611
|
-
if (token.type === TOKEN.EXECUTE_KW)
|
|
774
|
+
if (token.type === TOKEN.EXECUTE_KW && peekAt(1).type === TOKEN.SQL_BODY) return parseSqlBlock('execute', 'ExecuteStatement');
|
|
775
|
+
|
|
612
776
|
|
|
613
777
|
// IOPL-native features
|
|
614
778
|
if (token.type === TOKEN.GATHER) return parseGatherStatement();
|
|
@@ -627,7 +791,7 @@ function parse(tokens) {
|
|
|
627
791
|
}
|
|
628
792
|
|
|
629
793
|
// Statements starting with an identifier: call, becomes, index/member becomes
|
|
630
|
-
if (token.type === TOKEN.IDENTIFIER) {
|
|
794
|
+
if (token.type === TOKEN.IDENTIFIER || isSqlWordIdentifier(token)) {
|
|
631
795
|
// v2.5 - natural string and collection verbs that transform a variable
|
|
632
796
|
// in place: "lowercase title", "uppercase first letter of each word in title",
|
|
633
797
|
// "split title by \" \"", "join title by \" \"", "trim title".
|
|
@@ -689,7 +853,7 @@ function parse(tokens) {
|
|
|
689
853
|
// return statement
|
|
690
854
|
if (token.value === 'return' && peekAt(1).type !== TOKEN.BECOMES && peekAt(1).type !== TOKEN.LPAREN) {
|
|
691
855
|
advance();
|
|
692
|
-
const value = peek().type !== TOKEN.DONE && peek().type !== TOKEN.EOF && peek().type !== TOKEN.OTHERWISE && peek().type !== TOKEN.RBRACE
|
|
856
|
+
const value = peek().type !== TOKEN.DONE && peek().type !== TOKEN.EOF && peek().type !== TOKEN.OTHERWISE && peek().type !== TOKEN.ELSE && peek().type !== TOKEN.RBRACE
|
|
693
857
|
? parseExpression()
|
|
694
858
|
: null;
|
|
695
859
|
return { type: 'ReturnStatement', value };
|
|
@@ -797,14 +961,14 @@ function parse(tokens) {
|
|
|
797
961
|
return parseEvery();
|
|
798
962
|
}
|
|
799
963
|
|
|
800
|
-
// v1.0.
|
|
964
|
+
// v1.0.363 - every frame … done: requestAnimationFrame loop. Identifier
|
|
801
965
|
// mirror of the EACH dispatch above.
|
|
802
966
|
if (token.value === 'every' && peekAt(1).type === TOKEN.IDENTIFIER &&
|
|
803
967
|
peekAt(1).value === 'frame') {
|
|
804
968
|
return parseEveryFrame();
|
|
805
969
|
}
|
|
806
970
|
|
|
807
|
-
// v1.0.
|
|
971
|
+
// v1.0.363 - after <n> <unit>s … done: one-shot delayed execution.
|
|
808
972
|
// Contextual like "retry": "after becomes 5" and after(...) keep their
|
|
809
973
|
// ordinary meanings.
|
|
810
974
|
if (token.value === 'after' &&
|
|
@@ -827,7 +991,8 @@ function parse(tokens) {
|
|
|
827
991
|
advance(); // run
|
|
828
992
|
advance(); // background
|
|
829
993
|
const call = parsePrimary();
|
|
830
|
-
if (call.type !== 'CallExpression'
|
|
994
|
+
if (call.type !== 'CallExpression' && call.type !== 'AddCall' &&
|
|
995
|
+
call.type !== 'RemoveCall' && call.type !== 'WriteCall') {
|
|
831
996
|
throw new Error(makeError(
|
|
832
997
|
'Expected a function call after "run background".\n\nExample:\n run background resizeImage("photo.png")',
|
|
833
998
|
peek()
|
|
@@ -993,6 +1158,28 @@ function parse(tokens) {
|
|
|
993
1158
|
peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'cookie') {
|
|
994
1159
|
return parseSetCookie();
|
|
995
1160
|
}
|
|
1161
|
+
|
|
1162
|
+
// v1.0.363 - prefix assignment forms: "set <target> to <value>" and
|
|
1163
|
+
// "change <target> to <value>" are aliases for "<target> becomes <value>".
|
|
1164
|
+
// Contextual: a variable named "set" or "change" keeps its usual meaning
|
|
1165
|
+
// unless a value and "to" follow. "set cookie" and "set with" keep their
|
|
1166
|
+
// own meanings and are intercepted before this point/expression level.
|
|
1167
|
+
if ((token.value === 'set' || token.value === 'change') &&
|
|
1168
|
+
!(peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'with')) {
|
|
1169
|
+
const verb = token.value;
|
|
1170
|
+
advance(); // set / change
|
|
1171
|
+
const target = parseExpression();
|
|
1172
|
+
if (peek().type !== TOKEN.TO &&
|
|
1173
|
+
!(peek().type === TOKEN.IDENTIFIER && peek().value === 'to')) {
|
|
1174
|
+
throw new Error(makeError(
|
|
1175
|
+
`Expected "to" after the target of "${verb}".\n\nExample:\n ${verb} age to 17`,
|
|
1176
|
+
peek()
|
|
1177
|
+
));
|
|
1178
|
+
}
|
|
1179
|
+
advance(); // to
|
|
1180
|
+
const value = parseExpression();
|
|
1181
|
+
return { type: 'BecomeStatement', target, value, op: '=' };
|
|
1182
|
+
}
|
|
996
1183
|
if (token.value === 'clear' &&
|
|
997
1184
|
peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'cookie') {
|
|
998
1185
|
advance(); // clear
|
|
@@ -1088,8 +1275,20 @@ function parse(tokens) {
|
|
|
1088
1275
|
// Assignment operators: becomes, is now, set to, change to, or becomes (||=), and becomes (&&=), nullish becomes (??=)
|
|
1089
1276
|
let becomeOp = null;
|
|
1090
1277
|
if (peek().type === TOKEN.PLUS_ASSIGN) {
|
|
1091
|
-
advance(); // ++=
|
|
1278
|
+
advance(); // += (or the legacy ++=)
|
|
1092
1279
|
becomeOp = '+=';
|
|
1280
|
+
} else if (peek().type === TOKEN.MINUS_ASSIGN) {
|
|
1281
|
+
advance(); // -=
|
|
1282
|
+
becomeOp = '-=';
|
|
1283
|
+
} else if (peek().type === TOKEN.STAR_ASSIGN) {
|
|
1284
|
+
advance(); // *=
|
|
1285
|
+
becomeOp = '*=';
|
|
1286
|
+
} else if (peek().type === TOKEN.SLASH_ASSIGN) {
|
|
1287
|
+
advance(); // /=
|
|
1288
|
+
becomeOp = '/=';
|
|
1289
|
+
} else if (peek().type === TOKEN.PERCENT_ASSIGN) {
|
|
1290
|
+
advance(); // %=
|
|
1291
|
+
becomeOp = '%=';
|
|
1093
1292
|
} else if (peek().type === TOKEN.LOGICAL_OR_ASSIGN) {
|
|
1094
1293
|
advance(); // ||=
|
|
1095
1294
|
becomeOp = '||=';
|
|
@@ -1124,6 +1323,16 @@ function parse(tokens) {
|
|
|
1124
1323
|
} else if (peek().type === TOKEN.CHANGE_TO) {
|
|
1125
1324
|
advance(); // change to
|
|
1126
1325
|
becomeOp = '=';
|
|
1326
|
+
} else if (peek().type === TOKEN.IDENTIFIER && peek().value === 'set' &&
|
|
1327
|
+
peekAt(1).type === TOKEN.TO) {
|
|
1328
|
+
advance(); // set
|
|
1329
|
+
advance(); // to
|
|
1330
|
+
becomeOp = '=';
|
|
1331
|
+
} else if (peek().type === TOKEN.IDENTIFIER && peek().value === 'change' &&
|
|
1332
|
+
peekAt(1).type === TOKEN.TO) {
|
|
1333
|
+
advance(); // change
|
|
1334
|
+
advance(); // to
|
|
1335
|
+
becomeOp = '=';
|
|
1127
1336
|
}
|
|
1128
1337
|
|
|
1129
1338
|
if (becomeOp !== null) {
|
|
@@ -1171,6 +1380,7 @@ function parse(tokens) {
|
|
|
1171
1380
|
|
|
1172
1381
|
// Check for destructuring patterns: [ ... ] or { ... }
|
|
1173
1382
|
let target;
|
|
1383
|
+
let nameLine = peek().line;
|
|
1174
1384
|
if (peek().type === TOKEN.LBRACKET) {
|
|
1175
1385
|
target = parseArrayLiteral(); // returns ArrayLiteral with elements
|
|
1176
1386
|
target.type = 'ArrayPattern';
|
|
@@ -1181,26 +1391,34 @@ function parse(tokens) {
|
|
|
1181
1391
|
// Allow contextual keywords as variable names in remember declarations
|
|
1182
1392
|
let nameToken = peek();
|
|
1183
1393
|
const REMEMBER_KEYWORDS = new Set([TOKEN.BACK, TOKEN.TOTAL, TOKEN.REPLY, TOKEN.RESPOND, TOKEN.SEND_BACK, TOKEN.FILE_KW]);
|
|
1184
|
-
if (REMEMBER_KEYWORDS.has(nameToken.type)) {
|
|
1394
|
+
if (REMEMBER_KEYWORDS.has(nameToken.type) || isSqlWordIdentifier(nameToken)) {
|
|
1185
1395
|
advance();
|
|
1186
1396
|
target = nameToken.value;
|
|
1187
1397
|
} else {
|
|
1188
1398
|
target = consume(
|
|
1189
1399
|
TOKEN.IDENTIFIER,
|
|
1190
|
-
`Expected a variable name after "${isLet ? 'let' : 'remember'}"
|
|
1400
|
+
`Expected a variable name after "${isLet ? 'let' : 'remember'}".${reservedWordHint(nameToken)}\n\nExample:\n ${isLet ? 'let age is 16' : 'remember age as 16'}`
|
|
1191
1401
|
).value;
|
|
1192
1402
|
}
|
|
1403
|
+
nameLine = nameToken.line;
|
|
1193
1404
|
}
|
|
1194
1405
|
|
|
1195
1406
|
// Object literal: next token is IDENTIFIER (except dictionary/map) followed by IS or BE
|
|
1196
1407
|
if (peek().type === TOKEN.IDENTIFIER && peek().value !== 'dictionary' && peek().value !== 'map' && (peekAt(1).type === TOKEN.IS || peekAt(1).type === TOKEN.BE) && !isObjectShorthandAmbiguousWithComparison()) {
|
|
1197
1408
|
const init = parseInlineObjectLiteral(false);
|
|
1198
|
-
return { type: '
|
|
1409
|
+
return { type: 'RememberStatement', name: target, value: init, isLet, isConstant: !isLet };
|
|
1199
1410
|
}
|
|
1200
1411
|
|
|
1201
1412
|
// "let" uses "is" or "be" for simple vars, but also accepts "as" for both simple and destructuring
|
|
1202
1413
|
// "remember" always uses "as"
|
|
1414
|
+
// v1.0.363 - an uninitialized declaration ("let undef") is accepted and
|
|
1415
|
+
// binds undefined; typical before "undef nullish becomes "default"".
|
|
1203
1416
|
const isDestructuring = target && typeof target === 'object' && (target.type === 'ArrayPattern' || target.type === 'ObjectPattern');
|
|
1417
|
+
// The statement's name token is on the declaration line; a next token on a
|
|
1418
|
+
// LATER line means no initializer follows on that line.
|
|
1419
|
+
if (!isDestructuring && typeof target === 'string' && peek().line > nameLine) {
|
|
1420
|
+
return { type: 'RememberStatement', name: target, value: null, isLet, isConstant: !isLet };
|
|
1421
|
+
}
|
|
1204
1422
|
const assignToken = isLet ? ((peek().type === TOKEN.IS || peek().type === TOKEN.BE) ? peek().type : TOKEN.AS) : TOKEN.AS;
|
|
1205
1423
|
const expectedKeyword = (isLet && (peek().type === TOKEN.IS || peek().type === TOKEN.BE)) ? (peek().type === TOKEN.BE ? 'be' : 'is') : 'as';
|
|
1206
1424
|
consume(
|
|
@@ -1352,7 +1570,7 @@ function parseAsk() {
|
|
|
1352
1570
|
const nameToken = peek();
|
|
1353
1571
|
if (nameToken.type !== TOKEN.IDENTIFIER && nameToken.type !== TOKEN.LOAD) {
|
|
1354
1572
|
throw new Error(makeError(
|
|
1355
|
-
`Expected a function name after "${keyword}"
|
|
1573
|
+
`Expected a function name after "${keyword}".${reservedWordHint(nameToken)}\n\nExample:\n ${keyword} greet()\n show "Hello"\n done`,
|
|
1356
1574
|
nameToken
|
|
1357
1575
|
));
|
|
1358
1576
|
}
|
|
@@ -1364,6 +1582,75 @@ function parseAsk() {
|
|
|
1364
1582
|
return { type: 'FunctionDeclaration', name, params, body };
|
|
1365
1583
|
}
|
|
1366
1584
|
|
|
1585
|
+
function parseIntentDeclaration() {
|
|
1586
|
+
advance(); // intend
|
|
1587
|
+
const nameToken = consume(TOKEN.IDENTIFIER, 'Expected an intent name after "intend".');
|
|
1588
|
+
const name = nameToken.value;
|
|
1589
|
+
consume(TOKEN.LPAREN, `Expected "(" after intent name "${name}".`);
|
|
1590
|
+
const params = parseParamList();
|
|
1591
|
+
consume(TOKEN.RPAREN, 'Expected ")" to close the intent parameter list.');
|
|
1592
|
+
const body = parseBody(`intent "${name}"`);
|
|
1593
|
+
return {
|
|
1594
|
+
type: 'IntentDeclaration',
|
|
1595
|
+
name,
|
|
1596
|
+
params,
|
|
1597
|
+
body,
|
|
1598
|
+
intent: { kind: 'declaration', name, parameterCount: params.length },
|
|
1599
|
+
};
|
|
1600
|
+
}
|
|
1601
|
+
|
|
1602
|
+
const primitiveTypeNames = new Set(['any', 'number', 'text', 'boolean', 'null', 'object']);
|
|
1603
|
+
function isTypeNameStart(token) {
|
|
1604
|
+
return token && token.type === TOKEN.IDENTIFIER &&
|
|
1605
|
+
(declaredTypeNames.has(token.value) || primitiveTypeNames.has(token.value) || /^[A-Z]/.test(token.value));
|
|
1606
|
+
}
|
|
1607
|
+
function parseTypeAtom() {
|
|
1608
|
+
const token = peek();
|
|
1609
|
+
if (token.type === TOKEN.IDENTIFIER && token.value === 'optional') {
|
|
1610
|
+
advance();
|
|
1611
|
+
return { kind: 'optional', value: parseTypeAtom() };
|
|
1612
|
+
}
|
|
1613
|
+
if (token.type === TOKEN.IDENTIFIER && (token.value === 'list' || token.value === 'dictionary')) {
|
|
1614
|
+
const kind = advance().value;
|
|
1615
|
+
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'of') advance();
|
|
1616
|
+
return { kind, value: parseTypeAtom() };
|
|
1617
|
+
}
|
|
1618
|
+
if (!isTypeNameStart(token) && token.type !== TOKEN.NULL_KW) {
|
|
1619
|
+
throw new Error(makeError('Expected a type name (number, text, boolean, object, or a declared type).', token));
|
|
1620
|
+
}
|
|
1621
|
+
advance();
|
|
1622
|
+
return { kind: 'name', name: token.value };
|
|
1623
|
+
}
|
|
1624
|
+
function parseTypeSpec() {
|
|
1625
|
+
const types = [parseTypeAtom()];
|
|
1626
|
+
while ((peek().type === TOKEN.OR || (peek().type === TOKEN.IDENTIFIER && peek().value === 'or')) &&
|
|
1627
|
+
peekAt(1).line === peek().line) {
|
|
1628
|
+
advance();
|
|
1629
|
+
types.push(parseTypeAtom());
|
|
1630
|
+
}
|
|
1631
|
+
return types.length === 1 ? types[0] : { kind: 'union', values: types };
|
|
1632
|
+
}
|
|
1633
|
+
function parseTypeDeclaration() {
|
|
1634
|
+
advance(); // type
|
|
1635
|
+
const nameToken = consume(TOKEN.IDENTIFIER, 'Expected a type name after "type".');
|
|
1636
|
+
const name = nameToken.value;
|
|
1637
|
+
if (declaredTypeNames.has(name)) {
|
|
1638
|
+
throw new Error(makeError(`Type "${name}" is already declared.`, nameToken));
|
|
1639
|
+
}
|
|
1640
|
+
declaredTypeNames.add(name);
|
|
1641
|
+
const fields = [];
|
|
1642
|
+
while (!atBlockEnd()) {
|
|
1643
|
+
if (peek().type === TOKEN.EOF) {
|
|
1644
|
+
throw new Error(makeError(`Expected keyword "done" to close type "${name}".`, peek()));
|
|
1645
|
+
}
|
|
1646
|
+
const field = consume(TOKEN.IDENTIFIER, `Expected a field name in type "${name}".`);
|
|
1647
|
+
consume(TOKEN.IS, `Expected "is" after field name "${field.value}".`);
|
|
1648
|
+
fields.push({ key: field.value, type: parseTypeSpec(), optional: false });
|
|
1649
|
+
}
|
|
1650
|
+
advance();
|
|
1651
|
+
return { type: 'TypeDeclaration', name, fields };
|
|
1652
|
+
}
|
|
1653
|
+
|
|
1367
1654
|
// v2.3 - English-like function declaration: "to <name> <a> and <b> together ... done"
|
|
1368
1655
|
// Parameters are identifiers joined by "and". Body ends with "together" or "done".
|
|
1369
1656
|
function parseToFunction() {
|
|
@@ -1376,21 +1663,34 @@ function parseAsk() {
|
|
|
1376
1663
|
));
|
|
1377
1664
|
}
|
|
1378
1665
|
const name = advance().value;
|
|
1666
|
+
const nameLine = nameToken.line;
|
|
1379
1667
|
const params = [];
|
|
1380
|
-
// Parameters are identifiers joined by "and"
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1668
|
+
// Parameters are identifiers joined by "and". The header ends at
|
|
1669
|
+
// "together"/"done" on the SAME line, or when the body begins on a later
|
|
1670
|
+
// line. The line rule is what makes the zero-parameter form work:
|
|
1671
|
+
// to loadTodos
|
|
1672
|
+
// show "reading"
|
|
1673
|
+
// done
|
|
1674
|
+
// Previously the header loop read the first body token as an attempted
|
|
1675
|
+
// parameter and failed with "Expected a parameter name".
|
|
1676
|
+
while (true) {
|
|
1386
1677
|
if (peek().type === TOKEN.EOF) {
|
|
1387
1678
|
throw new Error(makeError(
|
|
1388
1679
|
`Expected "together" or "done" to close function "${name}" before end of file.`,
|
|
1389
1680
|
peek()
|
|
1390
1681
|
));
|
|
1391
1682
|
}
|
|
1683
|
+
if (peek().line > nameLine) break; // body starts on a later line
|
|
1684
|
+
if (peek().type === TOKEN.TOGETHER || peek().type === TOKEN.DONE) {
|
|
1685
|
+
advance(); // header terminator on the header line
|
|
1686
|
+
break;
|
|
1687
|
+
}
|
|
1688
|
+
if (peek().type === TOKEN.AND) {
|
|
1689
|
+
advance(); // skip "and" between parameters
|
|
1690
|
+
continue;
|
|
1691
|
+
}
|
|
1392
1692
|
const paramToken = peek();
|
|
1393
|
-
if (paramToken.type !== TOKEN.IDENTIFIER) {
|
|
1693
|
+
if (paramToken.type !== TOKEN.IDENTIFIER && !isSqlWordIdentifier(paramToken)) {
|
|
1394
1694
|
throw new Error(makeError(
|
|
1395
1695
|
`Expected a parameter name or "together"/"done" in function "${name}".`,
|
|
1396
1696
|
paramToken
|
|
@@ -1398,9 +1698,6 @@ function parseAsk() {
|
|
|
1398
1698
|
}
|
|
1399
1699
|
params.push({ name: advance().value });
|
|
1400
1700
|
}
|
|
1401
|
-
// The params loop stops at "together"/"done" - consume the terminator here
|
|
1402
|
-
// so parseBody parses the real body instead of treating this as an empty one.
|
|
1403
|
-
advance(); // consume "together" or "done"
|
|
1404
1701
|
const body = parseBody(`function "${name}"`);
|
|
1405
1702
|
return { type: 'FunctionDeclaration', name, params, body };
|
|
1406
1703
|
}
|
|
@@ -1423,13 +1720,31 @@ function parseAsk() {
|
|
|
1423
1720
|
|
|
1424
1721
|
function parseListWith() {
|
|
1425
1722
|
// Accept both "list_with" keyword and contextual "list" + "with"
|
|
1723
|
+
const openerLine = peek().line;
|
|
1426
1724
|
if (peek().type === TOKEN.LIST_WITH) {
|
|
1427
1725
|
consume(TOKEN.LIST_WITH);
|
|
1428
1726
|
} else {
|
|
1429
1727
|
advance(); // "list"
|
|
1430
1728
|
consume(TOKEN.WITH, 'Expected "with" after "list".');
|
|
1431
1729
|
}
|
|
1432
|
-
|
|
1730
|
+
// v1.0.363 - block form: first element on a later line, one element per
|
|
1731
|
+
// line, closed by its own "done"/"end". Previously the terminator was
|
|
1732
|
+
// never consumed here, so "list with\n record ...\nend" leaked "end".
|
|
1733
|
+
const blockForm = peek().line > openerLine;
|
|
1734
|
+
const elements = [];
|
|
1735
|
+
if (blockForm) {
|
|
1736
|
+
while (canContinueElement(peek())) {
|
|
1737
|
+
elements.push(parseBooleanAtom());
|
|
1738
|
+
while (peek().type === TOKEN.COMMA ||
|
|
1739
|
+
peek().type === TOKEN.AND ||
|
|
1740
|
+
(peek().type === TOKEN.IDENTIFIER && peek().value === 'and')) {
|
|
1741
|
+
advance();
|
|
1742
|
+
}
|
|
1743
|
+
}
|
|
1744
|
+
} else {
|
|
1745
|
+
elements.push(...parseListWithElements());
|
|
1746
|
+
}
|
|
1747
|
+
consumeCollectionTerminator(openerLine, blockForm, elements.length > 0);
|
|
1433
1748
|
return { type: 'ArrayLiteral', elements };
|
|
1434
1749
|
}
|
|
1435
1750
|
|
|
@@ -1437,6 +1752,7 @@ function parseAsk() {
|
|
|
1437
1752
|
// consumeTerminator: true when used as expression (done closes record), false when standalone statement
|
|
1438
1753
|
function parseRecordWith(consumeTerminator) {
|
|
1439
1754
|
// Accept both "record_with" keyword and contextual "record" + "with"
|
|
1755
|
+
const openerLine = peek().line;
|
|
1440
1756
|
if (peek().type === TOKEN.RECORD_WITH) {
|
|
1441
1757
|
consume(TOKEN.RECORD_WITH);
|
|
1442
1758
|
} else {
|
|
@@ -1444,15 +1760,50 @@ function parseAsk() {
|
|
|
1444
1760
|
consume(TOKEN.WITH, 'Expected "with" after "record".');
|
|
1445
1761
|
}
|
|
1446
1762
|
const properties = [];
|
|
1447
|
-
|
|
1763
|
+
const recordStartLine = peek().line;
|
|
1764
|
+
// v1.0.363 - block form: first field on a later line. In that shape a
|
|
1765
|
+
// trailing done/together is the record's own terminator; in inline shape a
|
|
1766
|
+
// done on a LATER line belongs to the enclosing block and must be left
|
|
1767
|
+
// alone (it used to be swallowed, breaking "if ... record with ... done").
|
|
1768
|
+
const blockForm = recordStartLine > openerLine;
|
|
1769
|
+
while (true) {
|
|
1448
1770
|
const keyToken = peek();
|
|
1449
|
-
|
|
1771
|
+
// `done`/`together` may name a FREQUENT boolean field, as in a todo item:
|
|
1772
|
+
// record with text "milk" and done true (a value follows on the same line).
|
|
1773
|
+
// A following "of" is the field-READ shape ("done of item") instead, so
|
|
1774
|
+
// the record must end there rather than eat "done of t ..." as fields.
|
|
1775
|
+
if ((keyToken.type === TOKEN.DONE || keyToken.type === TOKEN.TOGETHER) &&
|
|
1776
|
+
isRecordFieldValueStart(peekAt(1)) && peekAt(1).line === keyToken.line &&
|
|
1777
|
+
peekAt(1).value !== 'of') {
|
|
1778
|
+
advance(); // consume `done` (or `together`) as a field name
|
|
1779
|
+
} else if (keyToken.type === TOKEN.DONE || keyToken.type === TOKEN.TOGETHER ||
|
|
1780
|
+
keyToken.type === TOKEN.EOF ||
|
|
1781
|
+
keyToken.type === TOKEN.TO || keyToken.type === TOKEN.FROM ||
|
|
1782
|
+
(keyToken.type === TOKEN.IDENTIFIER && keyToken.value === 'from') ||
|
|
1783
|
+
keyToken.type === TOKEN.RPAREN || keyToken.type === TOKEN.COMMA ||
|
|
1784
|
+
keyToken.type === TOKEN.RBRACKET) {
|
|
1785
|
+
break; // block terminator, add/remove special forms, or value boundaries
|
|
1786
|
+
} else if (keyToken.type === TOKEN.IDENTIFIER || keyToken.type === TOKEN.STRING ||
|
|
1787
|
+
keyToken.type === TOKEN.NUMBER) {
|
|
1788
|
+
advance(); // consume key
|
|
1789
|
+
} else if (isRecordFieldKeyword(keyToken)) {
|
|
1790
|
+
// Keyword words (`total`, `back`) work as fields when a value follows
|
|
1791
|
+
// on the same line; on a later line a keyword starts a new statement.
|
|
1792
|
+
if (isRecordFieldValueStart(peekAt(1)) &&
|
|
1793
|
+
peekAt(1).line === keyToken.line &&
|
|
1794
|
+
(keyToken.line === recordStartLine || !isRecordStatementOpener(keyToken))) {
|
|
1795
|
+
advance(); // consume keyword as a field name
|
|
1796
|
+
} else {
|
|
1797
|
+
break;
|
|
1798
|
+
}
|
|
1799
|
+
} else if (isRecordStatementOpener(keyToken)) {
|
|
1800
|
+
break; // next statement begins: the record is finished
|
|
1801
|
+
} else {
|
|
1450
1802
|
throw new Error(makeError(
|
|
1451
1803
|
'Expected a property name after "record with".\n\nExample:\n record with name "Alice" and age 30',
|
|
1452
1804
|
keyToken
|
|
1453
1805
|
));
|
|
1454
1806
|
}
|
|
1455
|
-
advance(); // consume key
|
|
1456
1807
|
const key = keyToken.value;
|
|
1457
1808
|
if (peek().type === TOKEN.DONE || peek().type === TOKEN.TOGETHER || peek().type === TOKEN.EOF || peek().type === TOKEN.AND) {
|
|
1458
1809
|
properties.push({ key, value: { type: 'UndefinedLiteral' } });
|
|
@@ -1464,19 +1815,142 @@ function parseAsk() {
|
|
|
1464
1815
|
advance(); // consume "and"
|
|
1465
1816
|
}
|
|
1466
1817
|
}
|
|
1467
|
-
if (consumeTerminator
|
|
1468
|
-
|
|
1818
|
+
if (consumeTerminator) {
|
|
1819
|
+
consumeCollectionTerminator(openerLine, blockForm, properties.length > 0);
|
|
1469
1820
|
}
|
|
1470
1821
|
return { type: 'InlineObjectLiteral', properties };
|
|
1471
1822
|
}
|
|
1472
1823
|
|
|
1824
|
+
// True for keyword tokens that can serve as record field names and never
|
|
1825
|
+
// begin a PlainScript statement alone (`total`, `back`). Also covers both
|
|
1826
|
+
// structural words (now, folder, pattern, ...) and words that double as
|
|
1827
|
+
// statement openers (status, reply, send, ...) - those only act as fields
|
|
1828
|
+
// when parsed on the record's own line (see the loop above). Exotic keys
|
|
1829
|
+
// (fields named `show`, `give`, ...) stay expressible via { ... } literals.
|
|
1830
|
+
const RECORD_FIELD_KEYWORDS = new Set([
|
|
1831
|
+
'total', 'back', 'yield', 'match', 'retry', 'cache', 'route', 'group',
|
|
1832
|
+
'status', 'reply', 'send', 'log', 'bot', 'mail', 'catch', 'recover',
|
|
1833
|
+
'folder', 'now', 'gather', 'stream', 'parallel', 'against', 'pattern',
|
|
1834
|
+
'start', 'serve', 'listen', 'run', 'wait', 'exit', 'put', 'unpack',
|
|
1835
|
+
'set', 'change', 'add', 'remove', 'write', 'read', 'load', 'schedule',
|
|
1836
|
+
'every', 'accept', 'limit', 'require', 'enable', 'disable', 'allow',
|
|
1837
|
+
'google', 'websocket', 'postgres', 'database', 'query', 'insert',
|
|
1838
|
+
'update', 'delete', 'execute', 'transaction', 'destroy', 'redirect',
|
|
1839
|
+
]);
|
|
1840
|
+
function isRecordFieldKeyword(tok) {
|
|
1841
|
+
if (!tok || typeof tok.value !== 'string') return false;
|
|
1842
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(tok.value)) return false;
|
|
1843
|
+
return RECORD_FIELD_KEYWORDS.has(tok.value);
|
|
1844
|
+
}
|
|
1845
|
+
|
|
1846
|
+
// True for keyword tokens that always begin a PlainScript statement. When
|
|
1847
|
+
// one follows a record's fields, the record is finished (the keyword starts
|
|
1848
|
+
// the next statement); without this, `record with a 1` followed by `show
|
|
1849
|
+
// x` would try to read `show` as a field name.
|
|
1850
|
+
function isRecordStatementOpener(tok) {
|
|
1851
|
+
if (!tok || typeof tok.value !== 'string') return false;
|
|
1852
|
+
return [
|
|
1853
|
+
'show', 'print', 'display', 'log', 'let', 'remember', 'give', 'return',
|
|
1854
|
+
'make', 'intend', 'define', 'function', 'use', 'import', 'bring', 'export', 'ask',
|
|
1855
|
+
'prompt', 'if', 'when', 'while', 'for', 'each', 'repeat', 'test', 'check',
|
|
1856
|
+
'try', 'raise', 'yield', 'listen', 'start', 'serve', 'run', 'reply',
|
|
1857
|
+
'status', 'send', 'wait', 'exit', 'put', 'unpack', 'set', 'change',
|
|
1858
|
+
'add', 'remove', 'write', 'read', 'load', 'schedule', 'every', 'cache',
|
|
1859
|
+
'route', 'group', 'redirect', 'destroy', 'accept', 'limit', 'require',
|
|
1860
|
+
'enable', 'disable', 'allow', 'google', 'websocket', 'mail', 'whatsapp',
|
|
1861
|
+
'telegram', 'bot', 'postgres', 'database', 'query', 'insert', 'update',
|
|
1862
|
+
'delete', 'execute', 'transaction', 'retry', 'match', 'recover',
|
|
1863
|
+
'finally', 'not', 'in', 'and', 'or', 'is', 'to', 'with', 'than', 'as',
|
|
1864
|
+
'be', 'then', 'otherwise', 'else', 'above', 'below', 'at', 'least',
|
|
1865
|
+
'most', 'between', 'contains', 'starts', 'ends', 'empty', 'now', 'back',
|
|
1866
|
+
'folder', 'catch',
|
|
1867
|
+
].includes(tok.value);
|
|
1868
|
+
}
|
|
1869
|
+
|
|
1870
|
+
// Tokens that can begin a value in "record with <name> <value>". Only these
|
|
1871
|
+
// make `done` look like a field name instead of the block terminator.
|
|
1872
|
+
function isRecordFieldValueStart(tok) {
|
|
1873
|
+
return !!tok && [
|
|
1874
|
+
TOKEN.NUMBER, TOKEN.STRING, TOKEN.IDENTIFIER, TOKEN.TRUE_KW, TOKEN.FALSE_KW,
|
|
1875
|
+
TOKEN.NULL_KW, TOKEN.LPAREN, TOKEN.LBRACKET, TOKEN.LBRACE, TOKEN.MINUS,
|
|
1876
|
+
].includes(tok.type);
|
|
1877
|
+
}
|
|
1878
|
+
|
|
1879
|
+
// v1.0.363 - can this token begin a collection element on a LATER line?
|
|
1880
|
+
// Used to give "list with" / "dictionary with" / "set with" / "tuple with"
|
|
1881
|
+
// newline-separated element forms. Deliberately conservative: only literal
|
|
1882
|
+
// and literal-introducing starters qualify, so a following statement that
|
|
1883
|
+
// begins with a plain identifier ("score becomes 10") or a statement keyword
|
|
1884
|
+
// is never swallowed as an element.
|
|
1885
|
+
function canContinueElement(tok) {
|
|
1886
|
+
if (!tok || tok.type === TOKEN.EOF) return false;
|
|
1887
|
+
if (tok.type === TOKEN.NUMBER || tok.type === TOKEN.STRING ||
|
|
1888
|
+
tok.type === TOKEN.TRUE_KW || tok.type === TOKEN.FALSE_KW ||
|
|
1889
|
+
tok.type === TOKEN.NULL_KW || tok.type === TOKEN.LPAREN ||
|
|
1890
|
+
tok.type === TOKEN.LBRACKET || tok.type === TOKEN.LBRACE ||
|
|
1891
|
+
tok.type === TOKEN.MINUS) return true;
|
|
1892
|
+
if (tok.type === TOKEN.IDENTIFIER) {
|
|
1893
|
+
return [
|
|
1894
|
+
'list', 'record', 'dictionary', 'map', 'set', 'tuple', 'create',
|
|
1895
|
+
'count', 'all', 'any', 'settled', 'spread', 'keys', 'values',
|
|
1896
|
+
'entries', 'size', 'union', 'intersection', 'intersect', 'difference',
|
|
1897
|
+
'new', 'not',
|
|
1898
|
+
].includes(tok.value);
|
|
1899
|
+
}
|
|
1900
|
+
return false;
|
|
1901
|
+
}
|
|
1902
|
+
|
|
1903
|
+
// Terminator handling shared by the "X with ..." collection literals.
|
|
1904
|
+
// `openerLine` is the line the "list/record/dictionary/set/tuple" token sits
|
|
1905
|
+
// on; `firstContentLine` the line of the first element token. A trailing
|
|
1906
|
+
// done/together is the collection's OWN terminator only when it is written
|
|
1907
|
+
// on the opener line ("record with a 1 done") or the collection is in block
|
|
1908
|
+
// form (first element on a later line, closed by its own done). Otherwise it
|
|
1909
|
+
// belongs to the enclosing block and must be left alone, so constructs like
|
|
1910
|
+
// if ok
|
|
1911
|
+
// let r be record with name "Ada"
|
|
1912
|
+
// done
|
|
1913
|
+
// stop eating the if-block's terminator.
|
|
1914
|
+
function consumeCollectionTerminator(openerLine, blockForm, hasContent) {
|
|
1915
|
+
if (peek().type !== TOKEN.DONE && peek().type !== TOKEN.TOGETHER) return;
|
|
1916
|
+
if (!hasContent) {
|
|
1917
|
+
if (peek().line === openerLine) advance(); // "list with done" on one line
|
|
1918
|
+
return;
|
|
1919
|
+
}
|
|
1920
|
+
if (blockForm || peek().line === openerLine) advance();
|
|
1921
|
+
}
|
|
1922
|
+
|
|
1923
|
+
// Acceptable record field names beyond plain identifiers: quoted and
|
|
1924
|
+
// numeric keys ({ "first name": ... } style) plus the non-structural
|
|
1925
|
+
// keyword words in RECORD_FIELD_KEYWORDS handled in the loop above.
|
|
1926
|
+
function isRecordFieldName(tok) {
|
|
1927
|
+
return !!tok && (tok.type === TOKEN.IDENTIFIER || tok.type === TOKEN.STRING ||
|
|
1928
|
+
tok.type === TOKEN.NUMBER);
|
|
1929
|
+
}
|
|
1930
|
+
|
|
1931
|
+
// v1.0.363 - SQL words as ordinary identifiers. "query", "insert",
|
|
1932
|
+
// "update" and "execute" keep their SQL-statement meaning only when the
|
|
1933
|
+
// lexer produced a raw SQL body for them (word alone on its line); anywhere
|
|
1934
|
+
// else they are ordinary names, so common spellings like "let query be ..."
|
|
1935
|
+
// or "to search query ... done" work. ("delete" is a JavaScript reserved
|
|
1936
|
+
// word and stays reserved.)
|
|
1937
|
+
function isSqlWordIdentifier(tok) {
|
|
1938
|
+
return !!tok && [
|
|
1939
|
+
[TOKEN.QUERY_KW, 'query'], [TOKEN.INSERT_KW, 'insert'],
|
|
1940
|
+
[TOKEN.UPDATE_KW, 'update'], [TOKEN.EXECUTE_KW, 'execute'],
|
|
1941
|
+
].some(([type, word]) => tok.type === type && tok.value === word);
|
|
1942
|
+
}
|
|
1943
|
+
|
|
1473
1944
|
function parseDictionaryWith() {
|
|
1945
|
+
const openerLine = peek().line;
|
|
1474
1946
|
advance(); // dictionary or map
|
|
1475
1947
|
consume(TOKEN.WITH, 'Expected "with" after dictionary/map.');
|
|
1948
|
+
// v1.0.363 - newline-separated pairs in block form (one "key is value"
|
|
1949
|
+
// per line, closed by its own done/end); see consumeCollectionTerminator.
|
|
1950
|
+
const blockForm = peek().line > openerLine;
|
|
1476
1951
|
const pairs = [];
|
|
1477
|
-
if (
|
|
1478
|
-
while (
|
|
1479
|
-
if (peek().type === TOKEN.DONE || peek().value === 'done') break;
|
|
1952
|
+
if (blockForm) {
|
|
1953
|
+
while (canContinueElement(peek())) {
|
|
1480
1954
|
// Key parsed below the comparison level so `"a" is 1 and "b" is 2`
|
|
1481
1955
|
// keeps the `is` as the dictionary separator (parseExpression would
|
|
1482
1956
|
// fold `"a" is 1` into a comparison).
|
|
@@ -1486,6 +1960,17 @@ function parseAsk() {
|
|
|
1486
1960
|
// keeps `and` as the pair separator. Comparisons still fold.
|
|
1487
1961
|
const value = parseBooleanAtom();
|
|
1488
1962
|
pairs.push({ key, value });
|
|
1963
|
+
while (peek().type === TOKEN.AND || (peek().type === TOKEN.IDENTIFIER && peek().value === 'and') || peek().type === TOKEN.COMMA) {
|
|
1964
|
+
advance();
|
|
1965
|
+
}
|
|
1966
|
+
}
|
|
1967
|
+
} else if (peek().type !== TOKEN.DONE && peek().value !== 'done') {
|
|
1968
|
+
while (true) {
|
|
1969
|
+
if (peek().type === TOKEN.DONE || peek().value === 'done') break;
|
|
1970
|
+
const key = parseNullish();
|
|
1971
|
+
consume(TOKEN.IS, 'Expected "is" between key and value in dictionary/map.');
|
|
1972
|
+
const value = parseBooleanAtom();
|
|
1973
|
+
pairs.push({ key, value });
|
|
1489
1974
|
if (peek().type === TOKEN.AND || (peek().type === TOKEN.IDENTIFIER && peek().value === 'and') || peek().type === TOKEN.COMMA) {
|
|
1490
1975
|
advance();
|
|
1491
1976
|
continue;
|
|
@@ -1493,17 +1978,26 @@ function parseAsk() {
|
|
|
1493
1978
|
break;
|
|
1494
1979
|
}
|
|
1495
1980
|
}
|
|
1496
|
-
|
|
1497
|
-
advance(); // done
|
|
1498
|
-
}
|
|
1981
|
+
consumeCollectionTerminator(openerLine, blockForm, pairs.length > 0);
|
|
1499
1982
|
return { type: 'DictionaryLiteral', pairs };
|
|
1500
1983
|
}
|
|
1501
1984
|
|
|
1502
1985
|
function parseSetWith() {
|
|
1986
|
+
const openerLine = peek().line;
|
|
1503
1987
|
advance(); // set
|
|
1504
1988
|
consume(TOKEN.WITH, 'Expected "with" after "set".');
|
|
1989
|
+
// v1.0.363 - newline-separated elements in block form; see
|
|
1990
|
+
// consumeCollectionTerminator for the terminator rule.
|
|
1991
|
+
const blockForm = peek().line > openerLine;
|
|
1505
1992
|
const elements = [];
|
|
1506
|
-
if (
|
|
1993
|
+
if (blockForm) {
|
|
1994
|
+
while (canContinueElement(peek())) {
|
|
1995
|
+
elements.push(parseBooleanAtom());
|
|
1996
|
+
while (peek().type === TOKEN.COMMA || peek().type === TOKEN.AND || (peek().type === TOKEN.IDENTIFIER && peek().value === 'and')) {
|
|
1997
|
+
advance();
|
|
1998
|
+
}
|
|
1999
|
+
}
|
|
2000
|
+
} else if (peek().type !== TOKEN.DONE && peek().value !== 'done') {
|
|
1507
2001
|
while (true) {
|
|
1508
2002
|
if (peek().type === TOKEN.DONE || peek().value === 'done') break;
|
|
1509
2003
|
elements.push(parseBooleanAtom());
|
|
@@ -1514,17 +2008,26 @@ function parseAsk() {
|
|
|
1514
2008
|
break;
|
|
1515
2009
|
}
|
|
1516
2010
|
}
|
|
1517
|
-
|
|
1518
|
-
advance(); // done
|
|
1519
|
-
}
|
|
2011
|
+
consumeCollectionTerminator(openerLine, blockForm, elements.length > 0);
|
|
1520
2012
|
return { type: 'SetLiteral', elements };
|
|
1521
2013
|
}
|
|
1522
2014
|
|
|
1523
2015
|
function parseTupleWith() {
|
|
2016
|
+
const openerLine = peek().line;
|
|
1524
2017
|
advance(); // tuple
|
|
1525
2018
|
consume(TOKEN.WITH, 'Expected "with" after "tuple".');
|
|
2019
|
+
// v1.0.363 - newline-separated elements in block form; see
|
|
2020
|
+
// consumeCollectionTerminator for the terminator rule.
|
|
2021
|
+
const blockForm = peek().line > openerLine;
|
|
1526
2022
|
const elements = [];
|
|
1527
|
-
if (
|
|
2023
|
+
if (blockForm) {
|
|
2024
|
+
while (canContinueElement(peek())) {
|
|
2025
|
+
elements.push(parseBooleanAtom());
|
|
2026
|
+
while (peek().type === TOKEN.COMMA || peek().type === TOKEN.AND || (peek().type === TOKEN.IDENTIFIER && peek().value === 'and')) {
|
|
2027
|
+
advance();
|
|
2028
|
+
}
|
|
2029
|
+
}
|
|
2030
|
+
} else if (peek().type !== TOKEN.DONE && peek().value !== 'done') {
|
|
1528
2031
|
while (true) {
|
|
1529
2032
|
if (peek().type === TOKEN.DONE || peek().value === 'done') break;
|
|
1530
2033
|
elements.push(parseBooleanAtom());
|
|
@@ -1535,9 +2038,7 @@ function parseAsk() {
|
|
|
1535
2038
|
break;
|
|
1536
2039
|
}
|
|
1537
2040
|
}
|
|
1538
|
-
|
|
1539
|
-
advance(); // done
|
|
1540
|
-
}
|
|
2041
|
+
consumeCollectionTerminator(openerLine, blockForm, elements.length > 0);
|
|
1541
2042
|
return { type: 'TupleLiteral', elements };
|
|
1542
2043
|
}
|
|
1543
2044
|
|
|
@@ -1636,8 +2137,12 @@ function parseAsk() {
|
|
|
1636
2137
|
const param = { name };
|
|
1637
2138
|
if (peek().type === TOKEN.AS) {
|
|
1638
2139
|
advance(); // as
|
|
1639
|
-
|
|
1640
|
-
|
|
2140
|
+
if (isTypeNameStart(peek())) {
|
|
2141
|
+
param.typeAnnotation = parseTypeSpec();
|
|
2142
|
+
} else {
|
|
2143
|
+
const dv = parseDefaultValue();
|
|
2144
|
+
if (dv) param.defaultValue = dv;
|
|
2145
|
+
}
|
|
1641
2146
|
}
|
|
1642
2147
|
return param;
|
|
1643
2148
|
}
|
|
@@ -2106,7 +2611,7 @@ function parseAsk() {
|
|
|
2106
2611
|
(nextToken.type === TOKEN.IDENTIFIER && (nextToken.value === 'nothing' || nextToken.value === 'socket'));
|
|
2107
2612
|
|
|
2108
2613
|
if (!isEventWhen) {
|
|
2109
|
-
// v1.0.
|
|
2614
|
+
// v1.0.363 - when <target> "<event>" happens [as <name>] … done binds a
|
|
2110
2615
|
// DOM event listener: when button "click" happens => addEventListener.
|
|
2111
2616
|
// Detection probes the target expression and commits only when a string
|
|
2112
2617
|
// followed by "happens" is next; otherwise the tokens are replayed and
|
|
@@ -2126,8 +2631,10 @@ function parseAsk() {
|
|
|
2126
2631
|
const body = parseBody('"when happens" block');
|
|
2127
2632
|
return { type: 'WhenTargetedStatement', target, event: eventStr, paramName, body };
|
|
2128
2633
|
}
|
|
2129
|
-
} catch (
|
|
2634
|
+
} catch (e) {
|
|
2130
2635
|
// Not a targeted "when" - replay and fall through to the condition form.
|
|
2636
|
+
// (Depth-limit sentinels propagate — see parseComparisonCondition.)
|
|
2637
|
+
if (e instanceof ParseDepthError) throw e;
|
|
2131
2638
|
}
|
|
2132
2639
|
pos = savedPos;
|
|
2133
2640
|
|
|
@@ -2136,19 +2643,19 @@ function parseAsk() {
|
|
|
2136
2643
|
if (peek().type === TOKEN.THEN) {
|
|
2137
2644
|
advance(); // consume "then"
|
|
2138
2645
|
}
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
|
|
2146
|
-
}
|
|
2147
|
-
const stmt = parseStatement();
|
|
2148
|
-
if (stmt) consequent.push(stmt);
|
|
2646
|
+
const consequent = [];
|
|
2647
|
+
while (!atBlockEnd()) {
|
|
2648
|
+
if (peek().type === TOKEN.EOF) {
|
|
2649
|
+
throw new Error(makeError(
|
|
2650
|
+
'Expected keyword "done" to close the "when" block before end of file.',
|
|
2651
|
+
peek()
|
|
2652
|
+
));
|
|
2149
2653
|
}
|
|
2150
|
-
|
|
2151
|
-
|
|
2654
|
+
const stmt = parseStatement();
|
|
2655
|
+
if (stmt) consequent.push(stmt);
|
|
2656
|
+
}
|
|
2657
|
+
advance(); // consume DONE or TOGETHER
|
|
2658
|
+
return { type: 'IfStatement', condition, consequent, alternate: null };
|
|
2152
2659
|
}
|
|
2153
2660
|
|
|
2154
2661
|
// v2.1.1 - "when nothing matches … done" registers the 404 catch-all.
|
|
@@ -2586,7 +3093,7 @@ function parseAsk() {
|
|
|
2586
3093
|
advance(); // try
|
|
2587
3094
|
const tryBody = [];
|
|
2588
3095
|
// Parse try body - stop at "done", "recover", "finally", or "handled by"
|
|
2589
|
-
while (!(
|
|
3096
|
+
while (!(atBlockEnd() ||
|
|
2590
3097
|
(peek().type === TOKEN.IDENTIFIER && (peek().value === 'recover' || peek().value === 'finally')) ||
|
|
2591
3098
|
isHandledBy())) {
|
|
2592
3099
|
if (peek().type === TOKEN.EOF) {
|
|
@@ -2629,7 +3136,7 @@ function parseAsk() {
|
|
|
2629
3136
|
}
|
|
2630
3137
|
}
|
|
2631
3138
|
const recoverBody = [];
|
|
2632
|
-
while (
|
|
3139
|
+
while (!atBlockEnd() &&
|
|
2633
3140
|
!(peek().type === TOKEN.IDENTIFIER && (peek().value === 'recover' || peek().value === 'finally')) &&
|
|
2634
3141
|
!isHandledBy()) {
|
|
2635
3142
|
if (peek().type === TOKEN.EOF) {
|
|
@@ -2651,7 +3158,7 @@ function parseAsk() {
|
|
|
2651
3158
|
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'finally') {
|
|
2652
3159
|
advance(); // finally
|
|
2653
3160
|
finallyBody = [];
|
|
2654
|
-
while (
|
|
3161
|
+
while (!atBlockEnd()) {
|
|
2655
3162
|
if (peek().type === TOKEN.EOF) {
|
|
2656
3163
|
throw new Error(makeError(
|
|
2657
3164
|
'Expected keyword "done" to close the "finally" block before end of file.',
|
|
@@ -2885,11 +3392,18 @@ function parseAsk() {
|
|
|
2885
3392
|
// ── Conditions ─────────────────────────────────────────────────────────────
|
|
2886
3393
|
|
|
2887
3394
|
function parseIf() {
|
|
3395
|
+
if (++stmtDepth > MAX_STMT_DEPTH) {
|
|
3396
|
+
throw new ParseDepthError(makeError(
|
|
3397
|
+
`Blocks are nested more than ${MAX_STMT_DEPTH} levels deep. Split the program into functions or reduce nesting.`,
|
|
3398
|
+
peek()
|
|
3399
|
+
));
|
|
3400
|
+
}
|
|
3401
|
+
try {
|
|
2888
3402
|
consume(TOKEN.IF);
|
|
2889
3403
|
const condition = parseCondition();
|
|
2890
3404
|
|
|
2891
3405
|
const consequent = [];
|
|
2892
|
-
while (peek().type !== TOKEN.OTHERWISE && peek().type !== TOKEN.
|
|
3406
|
+
while (peek().type !== TOKEN.OTHERWISE && peek().type !== TOKEN.ELSE && !atBlockEnd()) {
|
|
2893
3407
|
if (peek().type === TOKEN.EOF) {
|
|
2894
3408
|
throw new Error(makeError(
|
|
2895
3409
|
'Expected keyword "done" before end of file to close the "if" block.',
|
|
@@ -2901,9 +3415,9 @@ function parseAsk() {
|
|
|
2901
3415
|
}
|
|
2902
3416
|
|
|
2903
3417
|
let alternate = null;
|
|
2904
|
-
if (peek().type === TOKEN.OTHERWISE) {
|
|
2905
|
-
// `otherwise if` is an else-if chain (sharing one `done`)
|
|
2906
|
-
// sits on the same line
|
|
3418
|
+
if (peek().type === TOKEN.OTHERWISE || peek().type === TOKEN.ELSE) {
|
|
3419
|
+
// `otherwise if` / `else if` is an else-if chain (sharing one `done`)
|
|
3420
|
+
// ONLY when `if` sits on the same line; a newline-separated `if` is a
|
|
2907
3421
|
// nested if statement with its own `done`.
|
|
2908
3422
|
const otherwiseLine = peek().line;
|
|
2909
3423
|
advance();
|
|
@@ -2911,7 +3425,7 @@ function parseAsk() {
|
|
|
2911
3425
|
alternate = [parseElseIfChain()];
|
|
2912
3426
|
} else {
|
|
2913
3427
|
alternate = [];
|
|
2914
|
-
while (
|
|
3428
|
+
while (!atBlockEnd()) {
|
|
2915
3429
|
if (peek().type === TOKEN.EOF) {
|
|
2916
3430
|
throw new Error(makeError(
|
|
2917
3431
|
'Expected keyword "done" before end of file to close the "otherwise" block.',
|
|
@@ -2926,6 +3440,9 @@ function parseAsk() {
|
|
|
2926
3440
|
|
|
2927
3441
|
advance(); // consume DONE or TOGETHER
|
|
2928
3442
|
return { type: 'IfStatement', condition, consequent, alternate };
|
|
3443
|
+
} finally {
|
|
3444
|
+
stmtDepth--;
|
|
3445
|
+
}
|
|
2929
3446
|
}
|
|
2930
3447
|
|
|
2931
3448
|
// `otherwise if <condition> ... [otherwise if ...] [otherwise ...] done`
|
|
@@ -2933,11 +3450,18 @@ function parseAsk() {
|
|
|
2933
3450
|
// trailing `otherwise` supplies the final else. The trailing `done` belongs to
|
|
2934
3451
|
// the outermost `if` and is consumed there, not here.
|
|
2935
3452
|
function parseElseIfChain() {
|
|
3453
|
+
if (++stmtDepth > MAX_STMT_DEPTH) {
|
|
3454
|
+
throw new ParseDepthError(makeError(
|
|
3455
|
+
`Blocks are nested more than ${MAX_STMT_DEPTH} levels deep. Split the program into functions or reduce nesting.`,
|
|
3456
|
+
peek()
|
|
3457
|
+
));
|
|
3458
|
+
}
|
|
3459
|
+
try {
|
|
2936
3460
|
advance(); // consume IF
|
|
2937
3461
|
const condition = parseCondition();
|
|
2938
3462
|
|
|
2939
3463
|
const consequent = [];
|
|
2940
|
-
while (peek().type !== TOKEN.OTHERWISE && peek().type !== TOKEN.
|
|
3464
|
+
while (peek().type !== TOKEN.OTHERWISE && peek().type !== TOKEN.ELSE && !atBlockEnd()) {
|
|
2941
3465
|
if (peek().type === TOKEN.EOF) {
|
|
2942
3466
|
throw new Error(makeError(
|
|
2943
3467
|
'Expected keyword "done" before end of file to close the "otherwise if" block.',
|
|
@@ -2949,14 +3473,14 @@ function parseAsk() {
|
|
|
2949
3473
|
}
|
|
2950
3474
|
|
|
2951
3475
|
let alternate = null;
|
|
2952
|
-
if (peek().type === TOKEN.OTHERWISE) {
|
|
3476
|
+
if (peek().type === TOKEN.OTHERWISE || peek().type === TOKEN.ELSE) {
|
|
2953
3477
|
const otherwiseLine = peek().line;
|
|
2954
3478
|
advance();
|
|
2955
3479
|
if (peek().type === TOKEN.IF && peek().line === otherwiseLine) {
|
|
2956
3480
|
alternate = [parseElseIfChain()];
|
|
2957
3481
|
} else {
|
|
2958
3482
|
alternate = [];
|
|
2959
|
-
while (
|
|
3483
|
+
while (!atBlockEnd()) {
|
|
2960
3484
|
if (peek().type === TOKEN.EOF) {
|
|
2961
3485
|
throw new Error(makeError(
|
|
2962
3486
|
'Expected keyword "done" before end of file to close the "otherwise" block.',
|
|
@@ -2970,13 +3494,33 @@ function parseAsk() {
|
|
|
2970
3494
|
}
|
|
2971
3495
|
|
|
2972
3496
|
return { type: 'IfStatement', condition, consequent, alternate };
|
|
3497
|
+
} finally {
|
|
3498
|
+
stmtDepth--;
|
|
3499
|
+
}
|
|
2973
3500
|
}
|
|
2974
3501
|
|
|
2975
3502
|
// ── Shared helpers ──────────────────────────────────────────────────────────
|
|
2976
3503
|
|
|
2977
3504
|
function parseBody(context) {
|
|
3505
|
+
if (++stmtDepth > MAX_STMT_DEPTH) {
|
|
3506
|
+
throw new ParseDepthError(makeError(
|
|
3507
|
+
`Blocks are nested more than ${MAX_STMT_DEPTH} levels deep. Split the program into functions or reduce nesting.`,
|
|
3508
|
+
peek()
|
|
3509
|
+
));
|
|
3510
|
+
}
|
|
3511
|
+
try {
|
|
3512
|
+
return parseBodyInner(context);
|
|
3513
|
+
} finally {
|
|
3514
|
+
stmtDepth--;
|
|
3515
|
+
}
|
|
3516
|
+
}
|
|
3517
|
+
|
|
3518
|
+
function parseBodyInner(context) {
|
|
2978
3519
|
const body = [];
|
|
2979
|
-
|
|
3520
|
+
// v1.0.363 - "done of <record>" reads the `done` FIELD (the README todo
|
|
3521
|
+
// example), so a done/together followed by "of" is never a block
|
|
3522
|
+
// terminator; it starts the next statement instead.
|
|
3523
|
+
while (!atBlockEnd()) {
|
|
2980
3524
|
if (peek().type === TOKEN.EOF) {
|
|
2981
3525
|
throw new Error(makeError(
|
|
2982
3526
|
`Expected keyword "done" to close the ${context} before end of file.`,
|
|
@@ -3005,13 +3549,88 @@ function parseAsk() {
|
|
|
3005
3549
|
const condition = parseCondition();
|
|
3006
3550
|
if (peek().type === TOKEN.THEN) advance();
|
|
3007
3551
|
const consequent = parseExpression();
|
|
3008
|
-
if (peek().type === TOKEN.OTHERWISE) advance();
|
|
3552
|
+
if (peek().type === TOKEN.OTHERWISE || peek().type === TOKEN.ELSE) advance();
|
|
3009
3553
|
const alternate = parseExpression();
|
|
3010
3554
|
return { type: 'ConditionalExpression', condition, consequent, alternate };
|
|
3011
3555
|
}
|
|
3012
3556
|
return parseBooleanOrExpression();
|
|
3013
3557
|
}
|
|
3014
3558
|
|
|
3559
|
+
// v1.0.364 - template interpolation is compiled, not spliced. The lexer
|
|
3560
|
+
// delivers backtick content verbatim; the ${...} parts used to be emitted as
|
|
3561
|
+
// raw source text, so PlainScript-only expressions inside them ("message of
|
|
3562
|
+
// e", "x is at least 80", "a contains b") generated invalid or wrong
|
|
3563
|
+
// JavaScript. Split the content here, tokenize each expression with the same
|
|
3564
|
+
// lexer, and parse it with the ordinary expression grammar. A part that fails
|
|
3565
|
+
// to parse falls back to raw text so legacy output is unchanged.
|
|
3566
|
+
function parseTemplateLiteral(token) {
|
|
3567
|
+
const content = token.value;
|
|
3568
|
+
if (!content.includes('${')) return { type: 'TemplateLiteral', value: content };
|
|
3569
|
+
const parts = [];
|
|
3570
|
+
let plain = '';
|
|
3571
|
+
let i = 0;
|
|
3572
|
+
while (i < content.length) {
|
|
3573
|
+
if (content[i] === '\\' && content[i + 1] === '$' && content[i + 2] === '{') {
|
|
3574
|
+
plain += '\\${'; // keep the backslash: JS renders \${ as literal "${"
|
|
3575
|
+
i += 3;
|
|
3576
|
+
continue;
|
|
3577
|
+
}
|
|
3578
|
+
if (content[i] === '$' && content[i + 1] === '{') {
|
|
3579
|
+
// Find the matching close brace, honouring nested braces and strings.
|
|
3580
|
+
let depth = 1;
|
|
3581
|
+
let j = i + 2;
|
|
3582
|
+
let exprText = '';
|
|
3583
|
+
while (j < content.length && depth > 0) {
|
|
3584
|
+
const ch = content[j];
|
|
3585
|
+
if (ch === '{') depth++;
|
|
3586
|
+
else if (ch === '}') {
|
|
3587
|
+
depth--;
|
|
3588
|
+
if (depth === 0) break;
|
|
3589
|
+
} else if (ch === '"' || ch === "'" || ch === '`') {
|
|
3590
|
+
const quote = ch;
|
|
3591
|
+
exprText += ch;
|
|
3592
|
+
j++;
|
|
3593
|
+
while (j < content.length) {
|
|
3594
|
+
if (content[j] === '\\') { exprText += content[j] + (content[j + 1] || ''); j += 2; continue; }
|
|
3595
|
+
exprText += content[j];
|
|
3596
|
+
if (content[j] === quote) { j++; break; }
|
|
3597
|
+
j++;
|
|
3598
|
+
}
|
|
3599
|
+
continue;
|
|
3600
|
+
}
|
|
3601
|
+
exprText += ch;
|
|
3602
|
+
j++;
|
|
3603
|
+
}
|
|
3604
|
+
if (depth !== 0) {
|
|
3605
|
+
throw new Error(makeError('Unterminated "${" in template string: the closing "}" is missing.', token));
|
|
3606
|
+
}
|
|
3607
|
+
if (plain) { parts.push({ kind: 'text', text: plain }); plain = ''; }
|
|
3608
|
+
let parsed = null;
|
|
3609
|
+
try {
|
|
3610
|
+
const inner = tokenize('show ' + exprText);
|
|
3611
|
+
const sub = parse(inner);
|
|
3612
|
+
if (sub.body.length === 1 && sub.body[0].type === 'ShowStatement') {
|
|
3613
|
+
parsed = sub.body[0].value;
|
|
3614
|
+
}
|
|
3615
|
+
} catch (e) {
|
|
3616
|
+
if (/must contain one expression/.test(e.message) || e instanceof ParseDepthError) throw e;
|
|
3617
|
+
parsed = null; // not valid PlainScript: keep legacy raw-text behaviour
|
|
3618
|
+
}
|
|
3619
|
+
if (parsed) parts.push({ kind: 'expr', expr: parsed, raw: exprText });
|
|
3620
|
+
else parts.push({ kind: 'raw', text: '${' + exprText + '}' });
|
|
3621
|
+
i = j + 1;
|
|
3622
|
+
continue;
|
|
3623
|
+
}
|
|
3624
|
+
plain += content[i];
|
|
3625
|
+
i++;
|
|
3626
|
+
}
|
|
3627
|
+
if (plain) parts.push({ kind: 'text', text: plain });
|
|
3628
|
+
if (parts.every(p => p.kind === 'text')) {
|
|
3629
|
+
return { type: 'TemplateLiteral', value: parts.map(p => p.text).join('') };
|
|
3630
|
+
}
|
|
3631
|
+
return { type: 'TemplateLiteral', value: content, parts };
|
|
3632
|
+
}
|
|
3633
|
+
|
|
3015
3634
|
// Expression-level boolean algebra: boolean operators usable in ordinary
|
|
3016
3635
|
// value positions (e.g. `(x is above 3) and (x is below 10)` as a value).
|
|
3017
3636
|
// Produces the SAME LogicalCondition nodes the condition level builds, so no
|
|
@@ -3219,7 +3838,7 @@ function parseAsk() {
|
|
|
3219
3838
|
}
|
|
3220
3839
|
|
|
3221
3840
|
// primary → itemExpr | atom (postfix)*
|
|
3222
|
-
function
|
|
3841
|
+
function parsePrimaryInner() {
|
|
3223
3842
|
// Arrow function: (params) -> body - detected before grouped expression
|
|
3224
3843
|
if (peek().type === TOKEN.LPAREN && isArrowFunctionPattern()) {
|
|
3225
3844
|
return parseArrowFunction();
|
|
@@ -3325,7 +3944,7 @@ function parseAsk() {
|
|
|
3325
3944
|
|
|
3326
3945
|
const item = tryParseItemExpression();
|
|
3327
3946
|
if (item) return item;
|
|
3328
|
-
// v1.0.
|
|
3947
|
+
// v1.0.363 - `new` works anywhere an expression is parsed (assignment
|
|
3329
3948
|
// targets, call arguments, member chains). "new" is not a keyword, so this
|
|
3330
3949
|
// identifier must be intercepted here; parseAtom would read it as a plain
|
|
3331
3950
|
// Identifier and turn "new THREE.Scene()" into "new.THREE.Scene()".
|
|
@@ -3409,8 +4028,14 @@ function parseAsk() {
|
|
|
3409
4028
|
} else if (peek().type === TOKEN.AT && peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'position') {
|
|
3410
4029
|
advance(); // at
|
|
3411
4030
|
advance(); // position
|
|
3412
|
-
|
|
3413
|
-
|
|
4031
|
+
// Accept a literal number (the common form) or any expression, so
|
|
4032
|
+
// loop indices and computed indexes work: xs at position i, y at
|
|
4033
|
+
// position (count of xs - 1). parseExpression stops at "to"/")"/","
|
|
4034
|
+
// so "set data at position 0 to 99" keeps its meaning.
|
|
4035
|
+
const index = peek().type === TOKEN.NUMBER
|
|
4036
|
+
? { type: 'NumberLiteral', value: advance().value }
|
|
4037
|
+
: parseExpression();
|
|
4038
|
+
node = { type: 'IndexExpression', object: node, index };
|
|
3414
4039
|
} else if (peek().type === TOKEN.LPAREN) {
|
|
3415
4040
|
// Postfix call: invoke any expression - f()(), arr[0](1), mul(6)(7),
|
|
3416
4041
|
// obj.method()(x), or an immediately-invoked lambda ((x) -> x + 1)(2).
|
|
@@ -3492,10 +4117,18 @@ function parseAsk() {
|
|
|
3492
4117
|
return { type: 'NumberedItem', index, collection };
|
|
3493
4118
|
}
|
|
3494
4119
|
|
|
3495
|
-
throw
|
|
3496
|
-
|
|
3497
|
-
|
|
3498
|
-
|
|
4120
|
+
// v1.0.363 - never throw here for the "X Y of ..." shape: that is the
|
|
4121
|
+
// ordinary property form ("completed of todo"), so the fall-through parses
|
|
4122
|
+
// it normally. Only a plain "X Y from ..." keeps the number-word hint,
|
|
4123
|
+
// since "from" cannot start a property access.
|
|
4124
|
+
if (third.value === 'from') {
|
|
4125
|
+
throw new Error(makeError(
|
|
4126
|
+
`Expected a number word after "${first.value}" before "from".\n\nUse number words like "one", "two", "three".\n\nExample:\n ${first.value} one from players`,
|
|
4127
|
+
second
|
|
4128
|
+
));
|
|
4129
|
+
}
|
|
4130
|
+
|
|
4131
|
+
return null;
|
|
3499
4132
|
}
|
|
3500
4133
|
|
|
3501
4134
|
// atom → STRING | NUMBER | true | false | null | undefined | BigInt | '(' expr ')' |
|
|
@@ -3504,7 +4137,10 @@ function parseAsk() {
|
|
|
3504
4137
|
const token = peek();
|
|
3505
4138
|
|
|
3506
4139
|
if (token.type === TOKEN.STRING) { advance(); return { type: 'StringLiteral', value: token.value }; }
|
|
3507
|
-
if (token.type === TOKEN.TEMPLATE_STRING) {
|
|
4140
|
+
if (token.type === TOKEN.TEMPLATE_STRING) {
|
|
4141
|
+
advance();
|
|
4142
|
+
return parseTemplateLiteral(token);
|
|
4143
|
+
}
|
|
3508
4144
|
if (token.type === TOKEN.NUMBER) { advance(); return { type: 'NumberLiteral', value: token.value }; }
|
|
3509
4145
|
if (token.type === TOKEN.BIGINT) { advance(); return { type: 'BigIntLiteral', value: token.value }; }
|
|
3510
4146
|
// v2.1.1 - boolean and null literals
|
|
@@ -3559,7 +4195,17 @@ function parseAsk() {
|
|
|
3559
4195
|
|
|
3560
4196
|
// Keywords that can also be used as identifiers in expression context
|
|
3561
4197
|
const IDENTIFIER_KEYWORDS = new Set([TOKEN.BACK, TOKEN.REPLY, TOKEN.RESPOND, TOKEN.SEND_BACK, TOKEN.FILE_KW]);
|
|
3562
|
-
|
|
4198
|
+
// v1.0.363 - `done` can be a record field NAME ("record with text "milk"
|
|
4199
|
+
// and done true", the README todo example), so it must also be readable:
|
|
4200
|
+
// `done of item`, `done of item becomes true`. Accept it as a word only in
|
|
4201
|
+
// the `done of <expr>` shape; a bare `done` elsewhere stays the block
|
|
4202
|
+
// terminator so empty bodies and missing values still fail clearly.
|
|
4203
|
+
if ((token.type === TOKEN.DONE || token.type === TOKEN.TOGETHER) &&
|
|
4204
|
+
peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'of') {
|
|
4205
|
+
advance();
|
|
4206
|
+
return { type: 'Identifier', name: token.value };
|
|
4207
|
+
}
|
|
4208
|
+
if (token.type === TOKEN.IDENTIFIER || IDENTIFIER_KEYWORDS.has(token.type) || isSqlWordIdentifier(token)) {
|
|
3563
4209
|
if (peekAt(1).type === TOKEN.USES || peekAt(1).type === TOKEN.FILLS) {
|
|
3564
4210
|
const callee = { type: 'Identifier', name: token.value };
|
|
3565
4211
|
advance();
|
|
@@ -3642,7 +4288,7 @@ function parseAsk() {
|
|
|
3642
4288
|
urlToken
|
|
3643
4289
|
));
|
|
3644
4290
|
}
|
|
3645
|
-
const url =
|
|
4291
|
+
const url = parseExpression();
|
|
3646
4292
|
let body = null;
|
|
3647
4293
|
let headers = null;
|
|
3648
4294
|
let timeout = null;
|
|
@@ -3708,8 +4354,16 @@ function parseAsk() {
|
|
|
3708
4354
|
continue;
|
|
3709
4355
|
}
|
|
3710
4356
|
const keyToken = peek();
|
|
3711
|
-
// Allow keywords that are valid JS identifiers as property names (e.g., "back" from "give back"
|
|
3712
|
-
|
|
4357
|
+
// Allow keywords that are valid JS identifiers as property names (e.g., "back" from "give back",
|
|
4358
|
+
// "done" from the block terminator), plus strings ("with space": 1) and numbers
|
|
4359
|
+
// ({ 3: "three" }) for data-shaped objects. A keyword is only accepted as a key when an
|
|
4360
|
+
// explicit colon follows, so block-terminator usage of "done" can never be misread here.
|
|
4361
|
+
const keyIsWord = keyToken.type === TOKEN.IDENTIFIER || keyToken.type === TOKEN.STRING ||
|
|
4362
|
+
keyToken.type === TOKEN.NUMBER || keyToken.type === TOKEN.BACK;
|
|
4363
|
+
const keyIsColonKeyword = keyToken.type !== TOKEN.IDENTIFIER &&
|
|
4364
|
+
typeof keyToken.value === 'string' && /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(keyToken.value) &&
|
|
4365
|
+
peekAt(1).type === TOKEN.COLON;
|
|
4366
|
+
if (!keyIsWord && !keyIsColonKeyword) {
|
|
3713
4367
|
throw new Error(makeError(
|
|
3714
4368
|
'Expected a property name inside the inline object.\n\nExample:\n { text: "hi" }',
|
|
3715
4369
|
keyToken
|
|
@@ -3781,7 +4435,7 @@ function parseAsk() {
|
|
|
3781
4435
|
keyToken.type === TOKEN.OR || keyToken.type === TOKEN.NOT ||
|
|
3782
4436
|
keyToken.type === TOKEN.IN || keyToken.type === TOKEN.WITH ||
|
|
3783
4437
|
keyToken.type === TOKEN.AS || keyToken.type === TOKEN.IF ||
|
|
3784
|
-
keyToken.type === TOKEN.OTHERWISE) {
|
|
4438
|
+
keyToken.type === TOKEN.OTHERWISE || keyToken.type === TOKEN.ELSE) {
|
|
3785
4439
|
advance();
|
|
3786
4440
|
} else {
|
|
3787
4441
|
throw new Error(makeError('Expected a property name.', keyToken));
|
|
@@ -3816,7 +4470,7 @@ function parseAsk() {
|
|
|
3816
4470
|
return { type: 'EveryStatement', count, unit, body };
|
|
3817
4471
|
}
|
|
3818
4472
|
|
|
3819
|
-
// v1.0.
|
|
4473
|
+
// v1.0.363 - every frame … done: a requestAnimationFrame loop whose body runs
|
|
3820
4474
|
// once per animation frame. The next frame is scheduled after the body so it
|
|
3821
4475
|
// always runs (t is the frame timestamp, like the DOM's rAF callback).
|
|
3822
4476
|
function parseEveryFrame() {
|
|
@@ -3826,7 +4480,7 @@ function parseAsk() {
|
|
|
3826
4480
|
return { type: 'EveryFrameStatement', body };
|
|
3827
4481
|
}
|
|
3828
4482
|
|
|
3829
|
-
// v1.0.
|
|
4483
|
+
// v1.0.363 - after <n> <unit>s … done: run work once after a delay.
|
|
3830
4484
|
// after 5 seconds
|
|
3831
4485
|
// show "butter!" ... done
|
|
3832
4486
|
// The delay is a number expression ("after count seconds"); the unit is
|
|
@@ -3873,7 +4527,7 @@ function parseAsk() {
|
|
|
3873
4527
|
let messageBody = null;
|
|
3874
4528
|
let disconnectBody = null;
|
|
3875
4529
|
|
|
3876
|
-
while (
|
|
4530
|
+
while (!atBlockEnd()) {
|
|
3877
4531
|
if (peek().type === TOKEN.EOF) {
|
|
3878
4532
|
throw new Error(makeError(
|
|
3879
4533
|
'Expected keyword "done" to close the websocket block before end of file.',
|
|
@@ -3925,7 +4579,7 @@ function parseAsk() {
|
|
|
3925
4579
|
let baileysModule = null;
|
|
3926
4580
|
const handlers = [];
|
|
3927
4581
|
|
|
3928
|
-
while (
|
|
4582
|
+
while (!atBlockEnd()) {
|
|
3929
4583
|
if (peek().type === TOKEN.EOF) {
|
|
3930
4584
|
throw new Error(makeError(
|
|
3931
4585
|
'Expected keyword "done" to close the "whatsapp bot" block before end of file.',
|
|
@@ -4151,12 +4805,12 @@ function parseAsk() {
|
|
|
4151
4805
|
));
|
|
4152
4806
|
}
|
|
4153
4807
|
// otherwise → body
|
|
4154
|
-
if (peek().type === TOKEN.OTHERWISE) {
|
|
4155
|
-
advance(); // otherwise
|
|
4808
|
+
if (peek().type === TOKEN.OTHERWISE || peek().type === TOKEN.ELSE) {
|
|
4809
|
+
advance(); // otherwise/else
|
|
4156
4810
|
consume(TOKEN.ARROW,
|
|
4157
4811
|
'Expected "->" after "otherwise".\n\nExample:\n otherwise → show "other"');
|
|
4158
4812
|
defaultCase = [];
|
|
4159
|
-
while (
|
|
4813
|
+
while (!atBlockEnd()) {
|
|
4160
4814
|
if (peek().type === TOKEN.EOF) {
|
|
4161
4815
|
throw new Error(makeError(
|
|
4162
4816
|
'Expected keyword "done" to close the "match" block before end of file.',
|
|
@@ -4172,7 +4826,8 @@ function parseAsk() {
|
|
|
4172
4826
|
consume(TOKEN.ARROW,
|
|
4173
4827
|
'Expected "->" after the case expression.\n\nExample:\n "red" → show "stop"');
|
|
4174
4828
|
const caseBody = [];
|
|
4175
|
-
while (
|
|
4829
|
+
while (!atBlockEnd() && peek().type !== TOKEN.OTHERWISE &&
|
|
4830
|
+
peek().type !== TOKEN.ELSE &&
|
|
4176
4831
|
!(peek().type === TOKEN.STRING || peek().type === TOKEN.NUMBER ||
|
|
4177
4832
|
peek().type === TOKEN.TRUE_KW || peek().type === TOKEN.FALSE_KW ||
|
|
4178
4833
|
peek().type === TOKEN.NULL_KW)) {
|
|
@@ -4322,11 +4977,11 @@ function parseAsk() {
|
|
|
4322
4977
|
));
|
|
4323
4978
|
}
|
|
4324
4979
|
// otherwise → body
|
|
4325
|
-
if (peek().type === TOKEN.OTHERWISE) {
|
|
4326
|
-
advance(); // otherwise
|
|
4980
|
+
if (peek().type === TOKEN.OTHERWISE || peek().type === TOKEN.ELSE) {
|
|
4981
|
+
advance(); // otherwise/else
|
|
4327
4982
|
consume(TOKEN.ARROW, 'Expected "->" after "otherwise".\n\nExample:\n otherwise → show "other"');
|
|
4328
4983
|
defaultCase = [];
|
|
4329
|
-
while (
|
|
4984
|
+
while (!atBlockEnd()) {
|
|
4330
4985
|
if (peek().type === TOKEN.EOF) {
|
|
4331
4986
|
throw new Error(makeError(
|
|
4332
4987
|
'Expected keyword "done" to close the "switch" block before end of file.',
|
|
@@ -4341,7 +4996,8 @@ function parseAsk() {
|
|
|
4341
4996
|
const test = parseExpression();
|
|
4342
4997
|
consume(TOKEN.ARROW, 'Expected "->" after the case expression.\n\nExample:\n "red" → show "stop"');
|
|
4343
4998
|
const caseBody = [];
|
|
4344
|
-
while (
|
|
4999
|
+
while (!atBlockEnd() && peek().type !== TOKEN.OTHERWISE &&
|
|
5000
|
+
peek().type !== TOKEN.ELSE &&
|
|
4345
5001
|
!(peek().type === TOKEN.STRING || peek().type === TOKEN.NUMBER ||
|
|
4346
5002
|
peek().type === TOKEN.TRUE_KW || peek().type === TOKEN.FALSE_KW ||
|
|
4347
5003
|
peek().type === TOKEN.NULL_KW)) {
|
|
@@ -4435,12 +5091,26 @@ function parseAsk() {
|
|
|
4435
5091
|
|
|
4436
5092
|
// ── Program ────────────────────────────────────────────────────────────────
|
|
4437
5093
|
|
|
4438
|
-
|
|
4439
|
-
|
|
4440
|
-
|
|
4441
|
-
|
|
5094
|
+
// v1.0.364 - last-resort backstop: any stack overflow that escapes the
|
|
5095
|
+
// recursive descent (the depth guards fire first, but unguarded paths like
|
|
5096
|
+
// class-in-class nesting must never leak a raw RangeError either) becomes
|
|
5097
|
+
// a clean PlainScript error. By the time this catch runs the stack has
|
|
5098
|
+
// unwound, so building the replacement error is safe.
|
|
5099
|
+
try {
|
|
5100
|
+
const body = [];
|
|
5101
|
+
while (peek().type !== TOKEN.EOF) {
|
|
5102
|
+
const stmt = parseStatement();
|
|
5103
|
+
if (stmt) body.push(stmt);
|
|
5104
|
+
}
|
|
5105
|
+
return { type: 'Program', body };
|
|
5106
|
+
} catch (e) {
|
|
5107
|
+
if (e instanceof RangeError) {
|
|
5108
|
+
throw new Error(
|
|
5109
|
+
'Program is too deeply nested to compile. Split it into functions or intermediate variables.'
|
|
5110
|
+
);
|
|
5111
|
+
}
|
|
5112
|
+
throw e;
|
|
4442
5113
|
}
|
|
4443
|
-
return { type: 'Program', body };
|
|
4444
5114
|
}
|
|
4445
5115
|
|
|
4446
5116
|
module.exports = { parse };
|