mhs-lang 2.3.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +47 -134
- package/bin/mhs.js +89 -81
- package/examples/demo_lengkap.mhs +12 -9
- package/examples/halo.mhs +7 -7
- package/examples/python_native.mhs +5 -4
- package/examples/rumus_elektronika.mhs +6 -6
- package/examples/tes_semua_modul.mhs +7 -7
- package/examples/utama_impor.mhs +8 -7
- package/package.json +6 -7
- package/src/index.js +84 -28
- package/src/language_server.js +58 -28
- package/src/lexer.js +444 -0
- package/src/modules/data_json.js +16 -4
- package/src/modules/index.js +0 -18
- package/src/parser.js +1523 -0
- package/src/py_modules/data_json.py +13 -2
- package/src/py_transpiler.js +1279 -202
- package/src/rust_runtime.js +443 -0
- package/src/safety_analyzer.js +2139 -0
- package/src/stdlib.js +123 -77
- package/src/transpiler.js +813 -337
- package/LICENSE +0 -21
- package/assets/mhs-file-icons.vsix +0 -0
- package/assets/mhs.ico +0 -0
- package/assets/mhs.svg +0 -3
- package/examples/__pycache__/alat_py.cpython-312.pyc +0 -0
- package/examples/tes_modul_baru.mhs +0 -44
- package/scripts/postinstall.js +0 -276
- package/src/modules/antrian.js +0 -37
- package/src/modules/arsip.js +0 -31
- package/src/modules/basis_data.js +0 -47
- package/src/modules/iterasi.js +0 -136
- package/src/modules/jalur.js +0 -77
- package/src/modules/proses.js +0 -43
- package/src/modules/statistika.js +0 -79
- package/src/modules/tabel.js +0 -83
- package/src/py_modules/__pycache__/antrian.cpython-312.pyc +0 -0
- package/src/py_modules/__pycache__/basis_data.cpython-312.pyc +0 -0
- package/src/py_modules/__pycache__/berkas.cpython-312.pyc +0 -0
- package/src/py_modules/__pycache__/data_json.cpython-312.pyc +0 -0
- package/src/py_modules/__pycache__/iterasi.cpython-312.pyc +0 -0
- package/src/py_modules/__pycache__/jalur.cpython-312.pyc +0 -0
- package/src/py_modules/__pycache__/jaringan.cpython-312.pyc +0 -0
- package/src/py_modules/__pycache__/koleksi.cpython-312.pyc +0 -0
- package/src/py_modules/__pycache__/kripto.cpython-312.pyc +0 -0
- package/src/py_modules/__pycache__/matematika.cpython-312.pyc +0 -0
- package/src/py_modules/__pycache__/modul_acak.cpython-312.pyc +0 -0
- package/src/py_modules/__pycache__/pola.cpython-312.pyc +0 -0
- package/src/py_modules/__pycache__/sistem.cpython-312.pyc +0 -0
- package/src/py_modules/__pycache__/statistika.cpython-312.pyc +0 -0
- package/src/py_modules/__pycache__/uji.cpython-312.pyc +0 -0
- package/src/py_modules/__pycache__/waktu.cpython-312.pyc +0 -0
- package/src/py_modules/antrian.py +0 -20
- package/src/py_modules/arsip.py +0 -37
- package/src/py_modules/basis_data.py +0 -29
- package/src/py_modules/iterasi.py +0 -37
- package/src/py_modules/jalur.py +0 -47
- package/src/py_modules/proses.py +0 -35
- package/src/py_modules/statistika.py +0 -29
- package/src/py_modules/tabel.py +0 -29
package/src/index.js
CHANGED
|
@@ -9,6 +9,7 @@ const { transpileToPython } = require('./py_transpiler');
|
|
|
9
9
|
const { importPythonModule } = require('./py_bridge');
|
|
10
10
|
const { stdlib } = require('./stdlib');
|
|
11
11
|
const { STD_MODULES } = require('./modules/index');
|
|
12
|
+
const { analyzeSafety, assertSafe, MhsSafetyError, SANDBOX_FORBIDDEN_MODULES } = require('./safety_analyzer');
|
|
12
13
|
|
|
13
14
|
let _hasPythonCache = null;
|
|
14
15
|
function getPythonCommand() {
|
|
@@ -52,10 +53,15 @@ function resolvePyFile(target, baseDir) {
|
|
|
52
53
|
return null;
|
|
53
54
|
}
|
|
54
55
|
|
|
55
|
-
function createImporter(baseDir, moduleCache) {
|
|
56
|
+
function createImporter(baseDir, moduleCache, { sandbox = false } = {}) {
|
|
56
57
|
const localRequire = Module.createRequire(path.join(baseDir, 'index.js'));
|
|
57
58
|
|
|
58
59
|
return function __mhs_impor(target) {
|
|
60
|
+
const baseMod = target.replace(/\.(mhs|py)$/i, '').split('.')[0];
|
|
61
|
+
if (sandbox && (SANDBOX_FORBIDDEN_MODULES.has(baseMod) || target.endsWith('.py'))) {
|
|
62
|
+
throw new Error(`[Kotak Pasir] Impor modul "${target}" dilarang dalam mode --sandbox.`);
|
|
63
|
+
}
|
|
64
|
+
|
|
59
65
|
const mhsPath = resolveMhsModule(target, baseDir);
|
|
60
66
|
if (mhsPath) {
|
|
61
67
|
if (moduleCache.has(mhsPath)) {
|
|
@@ -64,7 +70,7 @@ function createImporter(baseDir, moduleCache) {
|
|
|
64
70
|
const exportsPlaceholder = {};
|
|
65
71
|
moduleCache.set(mhsPath, exportsPlaceholder);
|
|
66
72
|
const source = fs.readFileSync(mhsPath, 'utf8');
|
|
67
|
-
const { exports } = executeMhs(source, mhsPath, moduleCache);
|
|
73
|
+
const { exports } = executeMhs(source, mhsPath, moduleCache, { sandbox });
|
|
68
74
|
Object.assign(exportsPlaceholder, exports);
|
|
69
75
|
return exportsPlaceholder;
|
|
70
76
|
}
|
|
@@ -73,6 +79,10 @@ function createImporter(baseDir, moduleCache) {
|
|
|
73
79
|
return STD_MODULES[target];
|
|
74
80
|
}
|
|
75
81
|
|
|
82
|
+
if (sandbox) {
|
|
83
|
+
throw new Error(`[Kotak Pasir] Impor eksternal "${target}" dilarang dalam mode --sandbox.`);
|
|
84
|
+
}
|
|
85
|
+
|
|
76
86
|
const pyPath = resolvePyFile(target, baseDir);
|
|
77
87
|
if (pyPath) {
|
|
78
88
|
return importPythonModule(pyPath, baseDir);
|
|
@@ -90,16 +100,32 @@ function createImporter(baseDir, moduleCache) {
|
|
|
90
100
|
};
|
|
91
101
|
}
|
|
92
102
|
|
|
93
|
-
function executeMhs(source, filename = '<mhs>', moduleCache = new Map()) {
|
|
103
|
+
function executeMhs(source, filename = '<mhs>', moduleCache = new Map(), options = {}) {
|
|
94
104
|
source = source.replace(/^\uFEFF/, '');
|
|
105
|
+
const { sandbox = false, isRepl = false } = options;
|
|
95
106
|
const absFilename = filename.startsWith('<') ? filename : path.resolve(filename);
|
|
96
107
|
const baseDir = filename.startsWith('<') ? process.cwd() : path.dirname(absFilename);
|
|
97
108
|
|
|
98
|
-
const jsCode = transpile(source);
|
|
109
|
+
const jsCode = transpile(source, { filename: absFilename, sandbox, isRepl });
|
|
110
|
+
|
|
111
|
+
const safeStdlib = sandbox
|
|
112
|
+
? {
|
|
113
|
+
...stdlib,
|
|
114
|
+
masukan: () => {
|
|
115
|
+
throw new Error('[Kotak Pasir] masukan() stdin dilarang dalam mode --sandbox');
|
|
116
|
+
},
|
|
117
|
+
}
|
|
118
|
+
: stdlib;
|
|
119
|
+
|
|
99
120
|
const context = {
|
|
100
|
-
...
|
|
101
|
-
__mhs_impor: createImporter(baseDir, moduleCache),
|
|
102
|
-
__mhs_impor_py: (modName) =>
|
|
121
|
+
...safeStdlib,
|
|
122
|
+
__mhs_impor: createImporter(baseDir, moduleCache, { sandbox }),
|
|
123
|
+
__mhs_impor_py: (modName) => {
|
|
124
|
+
if (sandbox) {
|
|
125
|
+
throw new Error(`[Kotak Pasir] impor_py("${modName}") dilarang dalam mode --sandbox`);
|
|
126
|
+
}
|
|
127
|
+
return importPythonModule(modName, baseDir);
|
|
128
|
+
},
|
|
103
129
|
__mhs_exports__: {},
|
|
104
130
|
console,
|
|
105
131
|
Math,
|
|
@@ -119,56 +145,73 @@ function executeMhs(source, filename = '<mhs>', moduleCache = new Map()) {
|
|
|
119
145
|
return { result, exports: context.__mhs_exports__ };
|
|
120
146
|
}
|
|
121
147
|
|
|
122
|
-
function runCode(source, filename = '<mhs>') {
|
|
123
|
-
return executeMhs(source, filename).result;
|
|
148
|
+
function runCode(source, filename = '<mhs>', options = {}) {
|
|
149
|
+
return executeMhs(source, filename, new Map(), options).result;
|
|
124
150
|
}
|
|
125
151
|
|
|
126
|
-
function runFile(filePath, { engine = 'auto', args = [] } = {}) {
|
|
152
|
+
function runFile(filePath, { engine = 'auto', sandbox = false, args = [] } = {}) {
|
|
127
153
|
const absPath = path.resolve(filePath);
|
|
128
154
|
if (!fs.existsSync(absPath)) {
|
|
129
155
|
throw new Error(`File tidak ditemukan: ${absPath}`);
|
|
130
156
|
}
|
|
131
|
-
const source = fs.readFileSync(absPath, 'utf8').replace(
|
|
157
|
+
const source = fs.readFileSync(absPath, 'utf8').replace(/^\uFEFF/, '');
|
|
132
158
|
|
|
133
159
|
if (engine === 'py' || (engine === 'auto' && hasPython3())) {
|
|
134
|
-
return runPyCode(source, absPath, args);
|
|
160
|
+
return runPyCode(source, absPath, { sandbox, args });
|
|
135
161
|
}
|
|
136
|
-
return runCode(source, absPath);
|
|
162
|
+
return runCode(source, absPath, { sandbox, args });
|
|
137
163
|
}
|
|
138
164
|
|
|
139
|
-
function
|
|
140
|
-
|
|
165
|
+
function collectImportedMhsFiles(source, baseDir, visited = new Set(), options = {}) {
|
|
166
|
+
const { program } = analyzeSafety(source, options);
|
|
167
|
+
const files = [];
|
|
168
|
+
for (const st of program.body) {
|
|
169
|
+
if (st.kind === 'ImportStmt' || st.kind === 'FromImportStmt') {
|
|
170
|
+
const mhsPath = resolveMhsModule(st.target, baseDir);
|
|
171
|
+
if (mhsPath && !visited.has(mhsPath)) {
|
|
172
|
+
visited.add(mhsPath);
|
|
173
|
+
const subSrc = fs.readFileSync(mhsPath, 'utf8').replace(/^\uFEFF/, '');
|
|
174
|
+
files.push({ mhsPath, source: subSrc });
|
|
175
|
+
files.push(...collectImportedMhsFiles(subSrc, path.dirname(mhsPath), visited, { ...options, filename: mhsPath }));
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return files;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function runPyCode(source, filename = '<mhs>', options = {}) {
|
|
183
|
+
source = source.replace(/^\uFEFF/, '');
|
|
184
|
+
const { sandbox = false, args = [] } = options;
|
|
141
185
|
const absFilename = filename.startsWith('<') ? filename : path.resolve(filename);
|
|
142
186
|
const baseDir = filename.startsWith('<') ? process.cwd() : path.dirname(absFilename);
|
|
143
187
|
const pyModulesDir = path.resolve(__dirname, 'py_modules');
|
|
144
188
|
|
|
145
189
|
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'mhs-py-'));
|
|
146
190
|
try {
|
|
191
|
+
const pyCode = transpileToPython(source, { filename: absFilename, sandbox });
|
|
192
|
+
|
|
147
193
|
if (fs.existsSync(baseDir)) {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
}
|
|
194
|
+
const visited = new Set([absFilename]);
|
|
195
|
+
const importedMods = collectImportedMhsFiles(source, baseDir, visited, { filename: absFilename, sandbox });
|
|
196
|
+
for (const mod of importedMods) {
|
|
197
|
+
const pyOut = transpileToPython(mod.source, { filename: mod.mhsPath, sandbox });
|
|
198
|
+
const relName = path.basename(mod.mhsPath).replace(/\.mhs$/i, '.py');
|
|
199
|
+
fs.writeFileSync(path.join(tmpDir, relName), pyOut, 'utf8');
|
|
155
200
|
}
|
|
156
201
|
}
|
|
157
202
|
|
|
158
|
-
const pyCode = transpileToPython(source);
|
|
159
203
|
const pythonCmd = getPythonCommand();
|
|
160
204
|
const env = {
|
|
161
205
|
...process.env,
|
|
162
206
|
PYTHONPATH: [tmpDir, pyModulesDir, baseDir, process.env.PYTHONPATH || ''].filter(Boolean).join(path.delimiter),
|
|
163
207
|
};
|
|
164
208
|
|
|
165
|
-
// Eksekusi berkas temporer
|
|
209
|
+
// Eksekusi via berkas temporer agar bebas dari batas panjang baris perintah (-c) di Windows
|
|
166
210
|
const entryPyPath = path.join(tmpDir, '__mhs_entry__.py');
|
|
167
211
|
const entryCode = `import sys as _mhs_sys\n_mhs_sys.argv[0] = ${JSON.stringify(absFilename)}\n` + pyCode;
|
|
168
212
|
fs.writeFileSync(entryPyPath, entryCode, 'utf8');
|
|
169
|
-
const execArgs = [entryPyPath, ...args];
|
|
170
213
|
|
|
171
|
-
return execFileSync(pythonCmd,
|
|
214
|
+
return execFileSync(pythonCmd, [entryPyPath, ...args], {
|
|
172
215
|
cwd: baseDir,
|
|
173
216
|
env,
|
|
174
217
|
stdio: 'inherit',
|
|
@@ -178,8 +221,17 @@ function runPyCode(source, filename = '<mhs>', args = []) {
|
|
|
178
221
|
}
|
|
179
222
|
}
|
|
180
223
|
|
|
181
|
-
function runPyFile(filePath) {
|
|
182
|
-
return runFile(filePath, { engine: 'py' });
|
|
224
|
+
function runPyFile(filePath, options = {}) {
|
|
225
|
+
return runFile(filePath, { engine: 'py', ...options });
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function checkFile(filePath, { sandbox = false } = {}) {
|
|
229
|
+
const absPath = path.resolve(filePath);
|
|
230
|
+
if (!fs.existsSync(absPath)) {
|
|
231
|
+
throw new Error(`File tidak ditemukan: ${absPath}`);
|
|
232
|
+
}
|
|
233
|
+
const source = fs.readFileSync(absPath, 'utf8').replace(/^\uFEFF/, '');
|
|
234
|
+
return assertSafe(source, { filename: absPath, sandbox });
|
|
183
235
|
}
|
|
184
236
|
|
|
185
237
|
module.exports = {
|
|
@@ -189,6 +241,10 @@ module.exports = {
|
|
|
189
241
|
runFile,
|
|
190
242
|
runPyCode,
|
|
191
243
|
runPyFile,
|
|
244
|
+
checkFile,
|
|
245
|
+
analyzeSafety,
|
|
246
|
+
assertSafe,
|
|
247
|
+
MhsSafetyError,
|
|
192
248
|
hasPython3,
|
|
193
249
|
stdlib,
|
|
194
250
|
STD_MODULES,
|
package/src/language_server.js
CHANGED
|
@@ -8,13 +8,15 @@ const {
|
|
|
8
8
|
createConnection,
|
|
9
9
|
} = require('vscode-languageserver/node');
|
|
10
10
|
const { TextDocument } = require('vscode-languageserver-textdocument');
|
|
11
|
-
const {
|
|
11
|
+
const { analyzeSafety } = require('./safety_analyzer');
|
|
12
12
|
|
|
13
13
|
const KEYWORDS = new Set([
|
|
14
|
+
'tetap', 'ubah', 'konst', 'pindah', 'pinjam', 'pinjam_ubah', 'tidak_aman',
|
|
15
|
+
'cocok', 'struktur', 'pilihan_tipe', 'implementasi',
|
|
14
16
|
'asinkron', 'fungsi', 'kelas', 'jika', 'atau_jika', 'jika_tidak', 'selain_itu',
|
|
15
17
|
'lainnya', 'selama', 'untuk', 'dalam', 'coba', 'tangkap', 'akhirnya', 'dengan',
|
|
16
18
|
'kembalikan', 'hasilkan', 'berhenti', 'lanjut', 'lewat', 'lempar', 'pastikan',
|
|
17
|
-
'umum', 'impor', 'dari', 'sebagai', 'impor_py', 'dari_py',
|
|
19
|
+
'umum', 'impor', 'dari', 'sebagai', 'impor_py', 'dari_py', 'anonim',
|
|
18
20
|
'if', 'elif', 'else', 'while', 'for', 'in', 'def', 'class', 'return', 'yield',
|
|
19
21
|
'break', 'continue', 'pass', 'raise', 'assert', 'try', 'except', 'finally',
|
|
20
22
|
'with', 'import', 'from', 'as', 'async', 'await', 'global', 'nonlocal',
|
|
@@ -22,38 +24,40 @@ const KEYWORDS = new Set([
|
|
|
22
24
|
'adalah', 'bukan_adalah',
|
|
23
25
|
]);
|
|
24
26
|
|
|
27
|
+
const TYPES = new Set([
|
|
28
|
+
'bulat', 'desimal', 'angka', 'teks', 'bool', 'logika', 'bytes',
|
|
29
|
+
'Daftar', 'Kamus', 'Himpunan', 'Tupel', 'Opsi', 'Hasil', 'Fungsi', 'Unit', 'ApaSaja',
|
|
30
|
+
'int', 'float', 'str', 'list', 'dict', 'set', 'tuple', 'List', 'Dict', 'Optional',
|
|
31
|
+
]);
|
|
32
|
+
|
|
25
33
|
const BUILTINS = new Set([
|
|
26
34
|
'cetak', 'masukan', 'rentang', 'panjang', 'tipe', 'teks', 'angka', 'bulat',
|
|
27
35
|
'desimal', 'tambah', 'hapus', 'urutkan', 'balik', 'gabung', 'pisah',
|
|
28
36
|
'huruf_besar', 'huruf_kecil', 'potong', 'ganti', 'mengandung', 'acak',
|
|
29
37
|
'akar', 'pangkat', 'mutlak', 'bulatkan', 'bawah', 'atas', 'min_nilai',
|
|
30
38
|
'maks_nilai', 'jumlah', 'waktu_sekarang', 'tunggu', 'daftar', 'kamus',
|
|
31
|
-
'
|
|
32
|
-
'
|
|
33
|
-
'atur_atribut', 'hapus_atribut', 'adalah_turunan', 'turunan_dari',
|
|
34
|
-
'format_teks', 'teks_biner', 'teks_heksa', 'teks_oktal', 'nilai_karakter',
|
|
35
|
-
'karakter', 'evaluasi', 'eksekusi', 'daftar_anggota', 'penghitung',
|
|
36
|
-
'berikutnya', 'keluar', 'salin', 'salin_dalam', 'bekukan',
|
|
39
|
+
'panik', 'salin', 'bekukan', 'dari_kosong', 'ambil_indeks', 'ambil_kunci',
|
|
40
|
+
'urai_bulat', 'urai_desimal', 'tambah_aman', 'kurang_aman', 'kali_aman', 'bagi_aman',
|
|
37
41
|
'print', 'input', 'range', 'len', 'str', 'int', 'float', 'list', 'dict',
|
|
38
42
|
'set', 'tuple', 'enumerate', 'zip', 'sum', 'min', 'max', 'abs', 'round',
|
|
39
|
-
'all', 'any', 'filter', 'map', 'callable', 'hasattr', 'getattr', 'setattr',
|
|
40
|
-
'isinstance', 'format', 'ord', 'chr', 'eval', 'exec', 'dir', 'iter', 'next',
|
|
41
43
|
]);
|
|
42
44
|
|
|
43
45
|
const MODULES = [
|
|
44
|
-
'matematika', 'modul_acak', '
|
|
45
|
-
'pola', 'koleksi', 'kripto', 'jaringan', 'uji',
|
|
46
|
-
'tabel', 'csv_id', 'proses', 'subproses', 'jalur', 'arsip', 'basis_data',
|
|
47
|
-
'antrian',
|
|
46
|
+
'matematika', 'modul_acak', 'waktu', 'berkas', 'sistem', 'data_json',
|
|
47
|
+
'pola', 'koleksi', 'kripto', 'jaringan', 'uji',
|
|
48
48
|
];
|
|
49
49
|
|
|
50
|
-
const CONSTANTS = ['Benar', 'Salah', 'Kosong', 'True', 'False', 'None'];
|
|
50
|
+
const CONSTANTS = ['Benar', 'Salah', 'Kosong', 'Ada', 'TidakAda', 'Ok', 'Galat', 'True', 'False', 'None'];
|
|
51
51
|
|
|
52
52
|
const COMPLETIONS = [
|
|
53
53
|
...Array.from(KEYWORDS, (label) => ({
|
|
54
54
|
label,
|
|
55
55
|
kind: CompletionItemKind.Keyword,
|
|
56
56
|
})),
|
|
57
|
+
...Array.from(TYPES, (label) => ({
|
|
58
|
+
label,
|
|
59
|
+
kind: CompletionItemKind.TypeParameter,
|
|
60
|
+
})),
|
|
57
61
|
...CONSTANTS.map((label) => ({
|
|
58
62
|
label,
|
|
59
63
|
kind: CompletionItemKind.Constant,
|
|
@@ -98,7 +102,7 @@ function collectSemanticTokens(text) {
|
|
|
98
102
|
break;
|
|
99
103
|
}
|
|
100
104
|
|
|
101
|
-
const stringStart = /[
|
|
105
|
+
const stringStart = /[fFrRbB]/.test(character) && /["']/.test(line[offset + 1])
|
|
102
106
|
? offset + 1
|
|
103
107
|
: offset;
|
|
104
108
|
if (line[stringStart] === '"' || line[stringStart] === "'") {
|
|
@@ -134,15 +138,17 @@ function collectSemanticTokens(text) {
|
|
|
134
138
|
type = 'keyword';
|
|
135
139
|
if (label === 'fungsi' || label === 'def') {
|
|
136
140
|
pendingDeclaration = 'function';
|
|
137
|
-
} else if (label === 'kelas' || label === 'class') {
|
|
141
|
+
} else if (label === 'kelas' || label === 'class' || label === 'struktur' || label === 'pilihan_tipe' || label === 'implementasi') {
|
|
138
142
|
pendingDeclaration = 'type';
|
|
139
143
|
}
|
|
140
144
|
} else if (pendingDeclaration) {
|
|
141
145
|
type = pendingDeclaration;
|
|
142
146
|
pendingDeclaration = null;
|
|
147
|
+
} else if (TYPES.has(label)) {
|
|
148
|
+
type = 'type';
|
|
143
149
|
} else if (BUILTINS.has(label)) {
|
|
144
150
|
type = 'function';
|
|
145
|
-
} else if (
|
|
151
|
+
} else if (CONSTANTS.includes(label)) {
|
|
146
152
|
type = 'keyword';
|
|
147
153
|
}
|
|
148
154
|
|
|
@@ -158,7 +164,7 @@ function collectSemanticTokens(text) {
|
|
|
158
164
|
continue;
|
|
159
165
|
}
|
|
160
166
|
|
|
161
|
-
const number = line.slice(offset).match(/^\d+(?:\.\d+)?/);
|
|
167
|
+
const number = line.slice(offset).match(/^\d+(?:_\d+)*(?:\.\d+)?/);
|
|
162
168
|
if (number) {
|
|
163
169
|
tokens.push({
|
|
164
170
|
line: lineNumber,
|
|
@@ -171,9 +177,9 @@ function collectSemanticTokens(text) {
|
|
|
171
177
|
continue;
|
|
172
178
|
}
|
|
173
179
|
|
|
174
|
-
if (/[
|
|
180
|
+
if (/[+\-*/%=<>!&|^~?.]/.test(character)) {
|
|
175
181
|
let end = offset + 1;
|
|
176
|
-
while (end < line.length && /[
|
|
182
|
+
while (end < line.length && /[+\-*/%=<>!&|^~?.]/.test(line[end])) {
|
|
177
183
|
end += 1;
|
|
178
184
|
}
|
|
179
185
|
tokens.push({
|
|
@@ -195,11 +201,35 @@ function collectSemanticTokens(text) {
|
|
|
195
201
|
return tokens;
|
|
196
202
|
}
|
|
197
203
|
|
|
198
|
-
function getDiagnostics(text) {
|
|
199
|
-
text = text.replace(/^\uFEFF/, '');
|
|
204
|
+
function getDiagnostics(text, filename = '<mhs>') {
|
|
200
205
|
try {
|
|
201
|
-
|
|
202
|
-
|
|
206
|
+
const result = analyzeSafety(text, { filename });
|
|
207
|
+
const lines = result.program.rawLines || text.split(/\r\n|\r|\n/);
|
|
208
|
+
|
|
209
|
+
return result.diagnostics.map((diag) => {
|
|
210
|
+
const lineIdx = Math.max(0, (diag.line || 1) - 1);
|
|
211
|
+
const lineText = lines[lineIdx] || '';
|
|
212
|
+
const startChar = Math.max(0, (diag.col || 1) - 1);
|
|
213
|
+
const endChar = Math.max(
|
|
214
|
+
startChar + 1,
|
|
215
|
+
Math.min(lineText.length || startChar + 1, (diag.endCol ? diag.endCol - 1 : startChar + 1))
|
|
216
|
+
);
|
|
217
|
+
|
|
218
|
+
let fullMessage = `[${diag.code}] ${diag.message}`;
|
|
219
|
+
if (diag.note) fullMessage += `\nCatatan: ${diag.note}`;
|
|
220
|
+
if (diag.hint) fullMessage += `\nBantuan: ${diag.hint}`;
|
|
221
|
+
|
|
222
|
+
return {
|
|
223
|
+
range: {
|
|
224
|
+
start: { line: lineIdx, character: startChar },
|
|
225
|
+
end: { line: lineIdx, character: endChar },
|
|
226
|
+
},
|
|
227
|
+
severity: diag.severity === 'warning' ? DiagnosticSeverity.Warning : DiagnosticSeverity.Error,
|
|
228
|
+
code: diag.code,
|
|
229
|
+
source: 'mhs-rust-safety',
|
|
230
|
+
message: fullMessage,
|
|
231
|
+
};
|
|
232
|
+
});
|
|
203
233
|
} catch (error) {
|
|
204
234
|
if (!(error instanceof SyntaxError)) {
|
|
205
235
|
throw error;
|
|
@@ -235,7 +265,7 @@ function createLanguageServer(
|
|
|
235
265
|
capabilities: {
|
|
236
266
|
textDocumentSync: TextDocumentSyncKind.Incremental,
|
|
237
267
|
completionProvider: {
|
|
238
|
-
triggerCharacters: ['.'],
|
|
268
|
+
triggerCharacters: ['.', '&'],
|
|
239
269
|
},
|
|
240
270
|
semanticTokensProvider: {
|
|
241
271
|
legend,
|
|
@@ -247,14 +277,14 @@ function createLanguageServer(
|
|
|
247
277
|
documents.onDidOpen(({ document }) => {
|
|
248
278
|
connection.sendDiagnostics({
|
|
249
279
|
uri: document.uri,
|
|
250
|
-
diagnostics: getDiagnostics(document.getText()),
|
|
280
|
+
diagnostics: getDiagnostics(document.getText(), document.uri),
|
|
251
281
|
});
|
|
252
282
|
});
|
|
253
283
|
|
|
254
284
|
documents.onDidChangeContent(({ document }) => {
|
|
255
285
|
connection.sendDiagnostics({
|
|
256
286
|
uri: document.uri,
|
|
257
|
-
diagnostics: getDiagnostics(document.getText()),
|
|
287
|
+
diagnostics: getDiagnostics(document.getText(), document.uri),
|
|
258
288
|
});
|
|
259
289
|
});
|
|
260
290
|
|