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/ir.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// Stable, backend-neutral PlainScript intermediate representation.
|
|
2
|
+
//
|
|
3
|
+
// The current JavaScript backend still consumes the rich AST directly. This
|
|
4
|
+
// module establishes a serializable semantic boundary for tooling, diagnostics,
|
|
5
|
+
// contract analysis, and future backends without changing generated programs.
|
|
6
|
+
|
|
7
|
+
const IR_VERSION = 1;
|
|
8
|
+
|
|
9
|
+
function spanOf(node) {
|
|
10
|
+
if (!node || typeof node !== 'object' || node.line == null) return undefined;
|
|
11
|
+
return { line: node.line, column: node.col == null ? 1 : node.col };
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function lowerValue(value) {
|
|
15
|
+
if (Array.isArray(value)) return value.map(lowerValue);
|
|
16
|
+
if (!value || typeof value !== 'object') return value;
|
|
17
|
+
if (value.type === 'FunctionDeclaration' || value.type === 'IntentDeclaration') {
|
|
18
|
+
return lowerDeclaration(value);
|
|
19
|
+
}
|
|
20
|
+
const out = { kind: value.type || 'Object' };
|
|
21
|
+
const span = spanOf(value);
|
|
22
|
+
if (span) out.span = span;
|
|
23
|
+
for (const [key, child] of Object.entries(value)) {
|
|
24
|
+
if (key === 'type' || key === 'line' || key === 'col') continue;
|
|
25
|
+
out[key] = lowerValue(child);
|
|
26
|
+
}
|
|
27
|
+
return out;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function lowerDeclaration(node) {
|
|
31
|
+
const out = {
|
|
32
|
+
kind: node.type === 'IntentDeclaration' ? 'IntentDeclaration' : 'FunctionDeclaration',
|
|
33
|
+
name: node.name,
|
|
34
|
+
parameters: lowerValue(node.params || []),
|
|
35
|
+
body: lowerValue(node.body || []),
|
|
36
|
+
};
|
|
37
|
+
if (node.type === 'IntentDeclaration') {
|
|
38
|
+
out.intent = lowerValue(node.intent || { kind: 'declaration', name: node.name });
|
|
39
|
+
}
|
|
40
|
+
const span = spanOf(node);
|
|
41
|
+
if (span) out.span = span;
|
|
42
|
+
return out;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function lowerToIR(ast) {
|
|
46
|
+
if (!ast || ast.type !== 'Program') {
|
|
47
|
+
throw new Error(`Expected a Program AST but got "${ast && ast.type}".`);
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
irVersion: IR_VERSION,
|
|
51
|
+
language: 'PlainScript',
|
|
52
|
+
kind: 'Program',
|
|
53
|
+
body: ast.body.map(lowerValue),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function serializeIR(ast, space = 2) {
|
|
58
|
+
return JSON.stringify(lowerToIR(ast), null, space) + '\n';
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
module.exports = { IR_VERSION, lowerToIR, serializeIR };
|
package/compiler/lexer.js
CHANGED
|
@@ -86,6 +86,7 @@ const TOKEN = {
|
|
|
86
86
|
CONNECT_DB: 'CONNECT_DB', // alias for database: "connect database "app.db""
|
|
87
87
|
USE_DATABASE: 'USE_DATABASE', // alias for database: "use database "app.db""
|
|
88
88
|
QUERY_KW: 'QUERY_KW',
|
|
89
|
+
JS_BODY: 'JS_BODY',
|
|
89
90
|
INSERT_KW: 'INSERT_KW',
|
|
90
91
|
UPDATE_KW: 'UPDATE_KW',
|
|
91
92
|
DELETE_KW: 'DELETE_KW',
|
|
@@ -144,7 +145,11 @@ const TOKEN = {
|
|
|
144
145
|
LOGICAL_OR_ASSIGN: 'LOGICAL_OR_ASSIGN', // ||=
|
|
145
146
|
LOGICAL_AND_ASSIGN: 'LOGICAL_AND_ASSIGN', // &&=
|
|
146
147
|
NULLISH_ASSIGN: 'NULLISH_ASSIGN', // ??=
|
|
147
|
-
PLUS_ASSIGN: 'PLUS_ASSIGN', //
|
|
148
|
+
PLUS_ASSIGN: 'PLUS_ASSIGN', // += (and its legacy spelling ++=)
|
|
149
|
+
MINUS_ASSIGN: 'MINUS_ASSIGN', // -=
|
|
150
|
+
STAR_ASSIGN: 'STAR_ASSIGN', // *=
|
|
151
|
+
SLASH_ASSIGN: 'SLASH_ASSIGN', // /=
|
|
152
|
+
PERCENT_ASSIGN: 'PERCENT_ASSIGN', // %=
|
|
148
153
|
// Literals & identifiers
|
|
149
154
|
IDENTIFIER: 'IDENTIFIER',
|
|
150
155
|
STRING: 'STRING',
|
|
@@ -180,7 +185,7 @@ const KEYWORDS = {
|
|
|
180
185
|
otherwise: TOKEN.OTHERWISE,
|
|
181
186
|
else: TOKEN.ELSE,
|
|
182
187
|
done: TOKEN.DONE,
|
|
183
|
-
end: TOKEN.
|
|
188
|
+
end: TOKEN.DONE, // synonym for done in every block position (v1.0.363)
|
|
184
189
|
do: TOKEN.DO, // block-bodied lambda opener: (x) do ... done
|
|
185
190
|
greater: TOKEN.GREATER,
|
|
186
191
|
less: TOKEN.LESS,
|
|
@@ -433,10 +438,23 @@ function tokenize(source) {
|
|
|
433
438
|
}
|
|
434
439
|
}
|
|
435
440
|
|
|
436
|
-
// Number literal (may include decimal point
|
|
441
|
+
// Number literal (may include decimal point, scientific notation, or
|
|
442
|
+
// BigInt suffix 'n')
|
|
437
443
|
if (/[0-9]/.test(source[i])) {
|
|
438
444
|
let num = '';
|
|
439
445
|
while (i < source.length && /[0-9.]/.test(source[i])) num += source[i++];
|
|
446
|
+
// Scientific notation: 1e6, 2.5E-3, 1e+2. Only consumed when a real
|
|
447
|
+
// exponent follows (digits after an optional sign), so `2 email` still
|
|
448
|
+
// lexes as number + identifier.
|
|
449
|
+
if (i < source.length && (source[i] === 'e' || source[i] === 'E')) {
|
|
450
|
+
let j = i + 1;
|
|
451
|
+
if (source[j] === '+' || source[j] === '-') j++;
|
|
452
|
+
if (j < source.length && /[0-9]/.test(source[j])) {
|
|
453
|
+
num += source[i++]; // e/E
|
|
454
|
+
if (source[i] === '+' || source[i] === '-') num += source[i++];
|
|
455
|
+
while (i < source.length && /[0-9]/.test(source[i])) num += source[i++];
|
|
456
|
+
}
|
|
457
|
+
}
|
|
440
458
|
// Check for BigInt suffix
|
|
441
459
|
if (i < source.length && source[i] === 'n') {
|
|
442
460
|
i++; // consume 'n'
|
|
@@ -461,12 +479,23 @@ function tokenize(source) {
|
|
|
461
479
|
let j = i;
|
|
462
480
|
while (j < source.length && (source[j] === ' ' || source[j] === '\t')) j++;
|
|
463
481
|
|
|
464
|
-
|
|
482
|
+
// v1.0.363 - the raw-SQL capture must only fire when the word sits in
|
|
483
|
+
// statement position (alone on its line) or completes a declaration
|
|
484
|
+
// ("let users be query" / "remember users as query"). A word like
|
|
485
|
+
// "query" that merely ENDS a line used to capture the following lines
|
|
486
|
+
// as SQL, breaking "show query" or "to search query ... done".
|
|
487
|
+
const beforeWord = source.slice(lineStart, tokenCol - 1).trim();
|
|
488
|
+
const declarationForm = /^(let|remember)\s+[A-Za-z_][A-Za-z0-9_]*\s+(be|as|is)$/.test(beforeWord);
|
|
489
|
+
const atStatementStart = beforeWord === '';
|
|
490
|
+
|
|
491
|
+
if ((atStatementStart || declarationForm) &&
|
|
492
|
+
(j >= source.length || source[j] === '\n' || source[j] === '\r')) {
|
|
465
493
|
// Advance past the newline
|
|
466
494
|
i = j;
|
|
467
495
|
if (i < source.length && source[i] === '\n') { i++; line++; lineStart = i; }
|
|
468
496
|
|
|
469
|
-
// Collect raw lines until a line whose trimmed content is exactly
|
|
497
|
+
// Collect raw lines until a line whose trimmed content is exactly
|
|
498
|
+
// "done" (or its synonym "end").
|
|
470
499
|
let sql = '';
|
|
471
500
|
while (i < source.length) {
|
|
472
501
|
const lineEnd = source.indexOf('\n', i);
|
|
@@ -474,7 +503,7 @@ function tokenize(source) {
|
|
|
474
503
|
const lineText = source.slice(i, realEnd);
|
|
475
504
|
const trimmed = lineText.trim();
|
|
476
505
|
|
|
477
|
-
if (trimmed === 'done') {
|
|
506
|
+
if (trimmed === 'done' || trimmed === 'end') {
|
|
478
507
|
i = realEnd < source.length ? realEnd + 1 : realEnd;
|
|
479
508
|
if (realEnd < source.length) { line++; lineStart = i; }
|
|
480
509
|
break;
|
|
@@ -492,6 +521,7 @@ function tokenize(source) {
|
|
|
492
521
|
continue;
|
|
493
522
|
}
|
|
494
523
|
|
|
524
|
+
|
|
495
525
|
const type = Object.prototype.hasOwnProperty.call(KEYWORDS, word)
|
|
496
526
|
? KEYWORDS[word]
|
|
497
527
|
: TOKEN.IDENTIFIER;
|
|
@@ -515,13 +545,18 @@ function tokenize(source) {
|
|
|
515
545
|
if (source[i] === '.' && source[i + 1] === '.' && source[i + 2] === '.') { tokens.push({ type: TOKEN.SPREAD, value: '...', line: tokenLine, col: tokenCol }); i += 3; continue; }
|
|
516
546
|
if (source[i] === '.') { tokens.push({ type: TOKEN.DOT, value: '.', line: tokenLine, col: tokenCol }); i++; continue; }
|
|
517
547
|
if (source[i] === '+' && source[i + 1] === '+' && source[i + 2] === '=') { tokens.push({ type: TOKEN.PLUS_ASSIGN, value: '++=', line: tokenLine, col: tokenCol }); i += 3; continue; }
|
|
548
|
+
if (source[i] === '+' && source[i + 1] === '=') { tokens.push({ type: TOKEN.PLUS_ASSIGN, value: '+=', line: tokenLine, col: tokenCol }); i += 2; continue; }
|
|
518
549
|
if (source[i] === '+') { tokens.push({ type: TOKEN.PLUS, value: '+', line: tokenLine, col: tokenCol }); i++; continue; }
|
|
519
550
|
// v2.1.1 - arithmetic. "->" is matched first so it never becomes MINUS.
|
|
520
551
|
if (source[i] === '-' && source[i + 1] === '>') { tokens.push({ type: TOKEN.ARROW, value: '->', line: tokenLine, col: tokenCol }); i += 2; continue; }
|
|
552
|
+
if (source[i] === '-' && source[i + 1] === '=') { tokens.push({ type: TOKEN.MINUS_ASSIGN, value: '-=', line: tokenLine, col: tokenCol }); i += 2; continue; }
|
|
521
553
|
if (source[i] === '-') { tokens.push({ type: TOKEN.MINUS, value: '-', line: tokenLine, col: tokenCol }); i++; continue; }
|
|
522
554
|
if (source[i] === '*' && source[i + 1] === '*') { tokens.push({ type: TOKEN.POWER, value: '**', line: tokenLine, col: tokenCol }); i += 2; continue; }
|
|
555
|
+
if (source[i] === '*' && source[i + 1] === '=') { tokens.push({ type: TOKEN.STAR_ASSIGN, value: '*=', line: tokenLine, col: tokenCol }); i += 2; continue; }
|
|
523
556
|
if (source[i] === '*') { tokens.push({ type: TOKEN.STAR, value: '*', line: tokenLine, col: tokenCol }); i++; continue; }
|
|
557
|
+
if (source[i] === '/' && source[i + 1] === '=') { tokens.push({ type: TOKEN.SLASH_ASSIGN, value: '/=', line: tokenLine, col: tokenCol }); i += 2; continue; }
|
|
524
558
|
if (source[i] === '/') { tokens.push({ type: TOKEN.SLASH, value: '/', line: tokenLine, col: tokenCol }); i++; continue; }
|
|
559
|
+
if (source[i] === '%' && source[i + 1] === '=') { tokens.push({ type: TOKEN.PERCENT_ASSIGN, value: '%=', line: tokenLine, col: tokenCol }); i += 2; continue; }
|
|
525
560
|
if (source[i] === '%') { tokens.push({ type: TOKEN.PERCENT, value: '%', line: tokenLine, col: tokenCol }); i++; continue; }
|
|
526
561
|
if (source[i] === '|' && source[i + 1] === '|' && source[i + 2] === '=') { tokens.push({ type: TOKEN.LOGICAL_OR_ASSIGN, value: '||=', line: tokenLine, col: tokenCol }); i += 3; continue; }
|
|
527
562
|
if (source[i] === '&' && source[i + 1] === '&' && source[i + 2] === '=') { tokens.push({ type: TOKEN.LOGICAL_AND_ASSIGN, value: '&&=', line: tokenLine, col: tokenCol }); i += 3; continue; }
|
|
@@ -537,4 +572,4 @@ function tokenize(source) {
|
|
|
537
572
|
return tokens;
|
|
538
573
|
}
|
|
539
574
|
|
|
540
|
-
module.exports = { tokenize, TOKEN };
|
|
575
|
+
module.exports = { tokenize, TOKEN, KEYWORDS };
|
package/compiler/lsp.js
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// PlainScript Language Server Protocol service.
|
|
3
|
+
// Uses the parser -> IR -> static checker pipeline and keeps no editor state
|
|
4
|
+
// outside the in-memory document map.
|
|
5
|
+
|
|
6
|
+
const fs = require('fs');
|
|
7
|
+
const { tokenize } = require('./lexer');
|
|
8
|
+
const { parse } = require('./parser');
|
|
9
|
+
const { lowerToIR } = require('./ir');
|
|
10
|
+
const { checkTypes } = require('./type-checker');
|
|
11
|
+
|
|
12
|
+
function parsePosition(message) {
|
|
13
|
+
const match = /Line (\d+), Column (\d+)/.exec(String(message));
|
|
14
|
+
return match
|
|
15
|
+
? { line: Number(match[1]) - 1, character: Number(match[2]) - 1 }
|
|
16
|
+
: { line: 0, character: 0 };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function wordAt(source, position) {
|
|
20
|
+
const lines = source.split(/\r?\n/);
|
|
21
|
+
const line = lines[position.line] || '';
|
|
22
|
+
let index = Math.max(0, Math.min(position.character, line.length));
|
|
23
|
+
if (index === line.length && index > 0) index--;
|
|
24
|
+
if (!/[A-Za-z0-9_$]/.test(line[index] || '')) return null;
|
|
25
|
+
let start = index;
|
|
26
|
+
let end = index + 1;
|
|
27
|
+
while (start > 0 && /[A-Za-z0-9_$]/.test(line[start - 1])) start--;
|
|
28
|
+
while (end < line.length && /[A-Za-z0-9_$]/.test(line[end])) end++;
|
|
29
|
+
return { word: line.slice(start, end), start, end, line: position.line };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function uriPath(uri) {
|
|
33
|
+
if (!uri) return null;
|
|
34
|
+
if (uri.startsWith('file://')) return decodeURIComponent(uri.slice('file://'.length));
|
|
35
|
+
return uri;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
class LspService {
|
|
39
|
+
constructor() {
|
|
40
|
+
this.documents = new Map();
|
|
41
|
+
this.shutdownRequested = false;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
setDocument(uri, text, version = null) {
|
|
45
|
+
this.documents.set(uri, { uri, text, version });
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
document(uri) {
|
|
49
|
+
const item = this.documents.get(uri);
|
|
50
|
+
if (item) return item;
|
|
51
|
+
const file = uriPath(uri);
|
|
52
|
+
if (file && fs.existsSync(file)) {
|
|
53
|
+
const text = fs.readFileSync(file, 'utf8');
|
|
54
|
+
this.setDocument(uri, text, null);
|
|
55
|
+
return this.documents.get(uri);
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
analyze(uri) {
|
|
61
|
+
const document = this.document(uri);
|
|
62
|
+
if (!document) return { diagnostics: [], ast: null, ir: null, check: null };
|
|
63
|
+
try {
|
|
64
|
+
const ast = parse(tokenize(document.text));
|
|
65
|
+
const ir = lowerToIR(ast);
|
|
66
|
+
const check = checkTypes(ast, { ir });
|
|
67
|
+
return { diagnostics: check.diagnostics, ast, ir, check };
|
|
68
|
+
} catch (error) {
|
|
69
|
+
const position = parsePosition(error.message);
|
|
70
|
+
return {
|
|
71
|
+
diagnostics: [{
|
|
72
|
+
severity: 1,
|
|
73
|
+
source: 'plainscript',
|
|
74
|
+
code: 'PLN-PARSE',
|
|
75
|
+
message: error.message,
|
|
76
|
+
range: { start: position, end: { line: position.line, character: position.character + 1 } },
|
|
77
|
+
}],
|
|
78
|
+
ast: null,
|
|
79
|
+
ir: null,
|
|
80
|
+
check: null,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
publishDiagnostics(uri) {
|
|
86
|
+
return { uri, diagnostics: this.analyze(uri).diagnostics };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
symbols(uri) {
|
|
90
|
+
const result = this.analyze(uri);
|
|
91
|
+
if (!result.ast) return [];
|
|
92
|
+
return result.ast.body.filter(node => ['TypeDeclaration', 'FunctionDeclaration', 'IntentDeclaration', 'RememberStatement'].includes(node.type)).map(node => ({
|
|
93
|
+
name: node.name,
|
|
94
|
+
kind: node.type === 'TypeDeclaration' ? 5 : node.type === 'RememberStatement' ? 13 : 12,
|
|
95
|
+
range: {
|
|
96
|
+
start: { line: (node.line || 1) - 1, character: (node.col || 1) - 1 },
|
|
97
|
+
end: { line: (node.line || 1) - 1, character: (node.col || 1) - 1 + String(node.name || '').length },
|
|
98
|
+
},
|
|
99
|
+
}));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
hover(uri, position) {
|
|
103
|
+
const document = this.document(uri);
|
|
104
|
+
const result = this.analyze(uri);
|
|
105
|
+
if (!document || !result.ast) return null;
|
|
106
|
+
const hit = wordAt(document.text, position);
|
|
107
|
+
if (!hit) return null;
|
|
108
|
+
const type = (result.ast.body || []).find(node => node.type === 'TypeDeclaration' && node.name === hit.word);
|
|
109
|
+
const fn = (result.ast.body || []).find(node => (node.type === 'FunctionDeclaration' || node.type === 'IntentDeclaration') && node.name === hit.word);
|
|
110
|
+
if (type) {
|
|
111
|
+
const fields = (type.fields || []).map(field => `- ${field.key}: ${JSON.stringify(field.type)}`).join('\n');
|
|
112
|
+
return { contents: { kind: 'markdown', value: `**type ${type.name}**\n\n${fields}` } };
|
|
113
|
+
}
|
|
114
|
+
if (fn) {
|
|
115
|
+
const params = (fn.params || []).map(param => `${param.name}${param.typeAnnotation ? ` as ${JSON.stringify(param.typeAnnotation)}` : ''}`).join(', ');
|
|
116
|
+
const prefix = fn.type === 'IntentDeclaration' ? 'intend' : 'make';
|
|
117
|
+
return { contents: { kind: 'markdown', value: `[1m${prefix} ${fn.name}(${params})[0m` } };
|
|
118
|
+
}
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
completion(uri) {
|
|
123
|
+
const result = this.analyze(uri);
|
|
124
|
+
const labels = ['remember', 'show', 'make', 'intend', 'type', 'if', 'otherwise', 'for', 'while', 'done', 'give', 'match', 'test'];
|
|
125
|
+
if (result.ast) {
|
|
126
|
+
for (const node of result.ast.body) {
|
|
127
|
+
if (node.name) labels.push(node.name);
|
|
128
|
+
if (node.type === 'TypeDeclaration') for (const field of node.fields || []) labels.push(field.key);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return { isIncomplete: false, items: [...new Set(labels)].map(label => ({ label, kind: 14 })) };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
definition(uri, position) {
|
|
135
|
+
const document = this.document(uri);
|
|
136
|
+
const result = this.analyze(uri);
|
|
137
|
+
if (!document || !result.ast) return null;
|
|
138
|
+
const hit = wordAt(document.text, position);
|
|
139
|
+
if (!hit) return null;
|
|
140
|
+
const node = result.ast.body.find(item => item.name === hit.word);
|
|
141
|
+
if (!node) return null;
|
|
142
|
+
return [{
|
|
143
|
+
uri,
|
|
144
|
+
range: {
|
|
145
|
+
start: { line: (node.line || 1) - 1, character: (node.col || 1) - 1 },
|
|
146
|
+
end: { line: (node.line || 1) - 1, character: (node.col || 1) - 1 + hit.word.length },
|
|
147
|
+
},
|
|
148
|
+
}];
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
request(method, params = {}) {
|
|
152
|
+
switch (method) {
|
|
153
|
+
case 'initialize':
|
|
154
|
+
return { capabilities: {
|
|
155
|
+
textDocumentSync: { openClose: true, change: 1 },
|
|
156
|
+
hoverProvider: true,
|
|
157
|
+
completionProvider: { triggerCharacters: ['.', ' '] },
|
|
158
|
+
definitionProvider: true,
|
|
159
|
+
}, serverInfo: { name: 'plainscript-lsp', version: '1.0.0' } };
|
|
160
|
+
case 'shutdown': this.shutdownRequested = true; return null;
|
|
161
|
+
case 'textDocument/hover': return this.hover(params.textDocument.uri, params.position);
|
|
162
|
+
case 'textDocument/completion': return this.completion(params.textDocument.uri);
|
|
163
|
+
case 'textDocument/definition': return this.definition(params.textDocument.uri, params.position);
|
|
164
|
+
case 'textDocument/documentSymbol': return this.symbols(params.textDocument.uri);
|
|
165
|
+
case 'workspace/symbol': {
|
|
166
|
+
const all = [];
|
|
167
|
+
for (const uri of this.documents.keys()) all.push(...this.symbols(uri));
|
|
168
|
+
return all;
|
|
169
|
+
}
|
|
170
|
+
default: return null;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
notification(method, params = {}) {
|
|
175
|
+
if (method === 'textDocument/didOpen') {
|
|
176
|
+
this.setDocument(params.textDocument.uri, params.textDocument.text, params.textDocument.version);
|
|
177
|
+
return this.publishDiagnostics(params.textDocument.uri);
|
|
178
|
+
}
|
|
179
|
+
if (method === 'textDocument/didChange') {
|
|
180
|
+
const changes = params.contentChanges || [];
|
|
181
|
+
const current = this.document(params.textDocument.uri);
|
|
182
|
+
const text = changes.length === 1 && !changes[0].range
|
|
183
|
+
? changes[0].text
|
|
184
|
+
: (current ? current.text : '');
|
|
185
|
+
this.setDocument(params.textDocument.uri, text, params.textDocument.version);
|
|
186
|
+
return this.publishDiagnostics(params.textDocument.uri);
|
|
187
|
+
}
|
|
188
|
+
if (method === 'textDocument/didClose') {
|
|
189
|
+
this.documents.delete(params.textDocument.uri);
|
|
190
|
+
return { uri: params.textDocument.uri, diagnostics: [] };
|
|
191
|
+
}
|
|
192
|
+
return null;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function startStdio(input = process.stdin, output = process.stdout) {
|
|
197
|
+
const service = new LspService();
|
|
198
|
+
let buffer = Buffer.alloc(0);
|
|
199
|
+
function send(message) {
|
|
200
|
+
const body = Buffer.from(JSON.stringify(message));
|
|
201
|
+
output.write(`Content-Length: ${body.length}\r\n\r\n`);
|
|
202
|
+
output.write(body);
|
|
203
|
+
}
|
|
204
|
+
function handle(message) {
|
|
205
|
+
const params = message.params || {};
|
|
206
|
+
if (message.method && message.id !== undefined) {
|
|
207
|
+
send({ jsonrpc: '2.0', id: message.id, result: service.request(message.method, params) });
|
|
208
|
+
} else if (message.method) {
|
|
209
|
+
const diagnostics = service.notification(message.method, params);
|
|
210
|
+
if (diagnostics && message.method !== 'exit') send({ jsonrpc: '2.0', method: 'textDocument/publishDiagnostics', params: diagnostics });
|
|
211
|
+
if (message.method === 'exit') process.exit(service.shutdownRequested ? 0 : 1);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
input.on('data', chunk => {
|
|
215
|
+
buffer = Buffer.concat([buffer, chunk]);
|
|
216
|
+
while (true) {
|
|
217
|
+
const separator = buffer.indexOf(Buffer.from('\r\n\r\n'));
|
|
218
|
+
if (separator < 0) break;
|
|
219
|
+
const header = buffer.slice(0, separator).toString('ascii');
|
|
220
|
+
const match = /Content-Length:\s*(\d+)/i.exec(header);
|
|
221
|
+
if (!match) { buffer = buffer.slice(separator + 4); continue; }
|
|
222
|
+
const length = Number(match[1]);
|
|
223
|
+
const start = separator + 4;
|
|
224
|
+
if (buffer.length < start + length) break;
|
|
225
|
+
const body = buffer.slice(start, start + length).toString('utf8');
|
|
226
|
+
buffer = buffer.slice(start + length);
|
|
227
|
+
try { handle(JSON.parse(body)); } catch (error) { send({ jsonrpc: '2.0', id: null, error: { code: -32700, message: error.message } }); }
|
|
228
|
+
}
|
|
229
|
+
});
|
|
230
|
+
return service;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (require.main === module) startStdio();
|
|
234
|
+
module.exports = { LspService, startStdio };
|