mhs-lang 2.0.0 → 2.1.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 CHANGED
@@ -2,7 +2,7 @@
2
2
  <img src="./assets/logo.png" alt="Logo mhs-lang" width="180" />
3
3
  </p>
4
4
 
5
- # mhs-lang v2.0 (`.mhs`)
5
+ # mhs-lang v2.1 (`.mhs`)
6
6
 
7
7
  Bahasa pemrograman **Python dalam Bahasa Indonesia** yang bisa diunduh lewat `npm` dan dijalankan di IDE apa pun. Hadir dengan **Dual-Engine**:
8
8
  1. **Engine Node.js + Python Bridge** (`mhs file.mhs`): langsung jalan tanpa Python, tapi otomatis bisa memanggil modul `.py` / `pip` lewat `impor_py` atau `impor "./alat.py"`.
@@ -33,17 +33,38 @@ mhs halo.mhs
33
33
 
34
34
  Untuk memakai Python 3 Native engine, jalankan `mhs --py halo.mhs`.
35
35
 
36
- ### Logo dan ikon file `.mhs` di VS Code
36
+ ### Dukungan IDE dan Language Server
37
37
 
38
- Logo proyek menggunakan gambar di `assets/logo.png`. Tema ikon MHS mempertahankan ikon Material, termasuk ikon `.py`, dan memakai PNG itu untuk file `.mhs`. Untuk memasang tema dari repositori sumber:
38
+ Instalasi npm menyediakan perintah `mhs --lsp`, yaitu language server berbasis LSP untuk autocomplete, diagnostik sintaks, dan semantic highlighting. IDE yang mendukung LSP dapat menjalankannya melalui konfigurasi klien dengan:
39
+
40
+ ```text
41
+ Command: mhs
42
+ Arguments: --lsp
43
+ Transport: stdio
44
+ ```
45
+
46
+ Perintah `mhs --lsp` dijalankan oleh klien editor (bukan sebagai program interaktif di terminal).
47
+
48
+ Instalasi npm **tidak dapat memasang plugin, grammar, atau ikon secara otomatis ke semua IDE**. Setiap editor tetap memerlukan klien LSP, dan ikon file diatur oleh tema/plugin editor masing-masing.
49
+
50
+ - **VS Code dan Antigravity:** pasang ekstensi MHS Tools untuk grammar, ikon, tombol Run, dan koneksi LSP. Di Antigravity gunakan pemasangan VSIX/ekstensi jika tersedia pada versi yang dipakai.
51
+ - **JetBrains:** pasang plugin klien LSP seperti LSP4IJ, lalu daftarkan command `mhs` dengan argumen `--lsp`.
52
+ - **Sublime Text:** pasang paket LSP, lalu daftarkan command server `mhs --lsp`.
53
+ - **Vim/Neovim:** daftarkan filetype `.mhs` dan jalankan klien LSP dengan command `mhs` dan argumen `--lsp`.
54
+ - **Notepad++:** perlu plugin klien LSP pihak ketiga; dukungan warna dan ikon mengikuti plugin/tema yang dipakai.
55
+
56
+ Pada editor selain VS Code, tandai `*.mhs` sebagai file MHS dan aktifkan semantic tokens jika klien LSP menyediakan pengaturan itu. Tidak semua klien LSP menampilkan semantic tokens; warna sintaks dan ikon tetap bergantung pada grammar serta tema editor.
57
+
58
+ Tema ikon VS Code mempertahankan ikon Material, termasuk ikon `.py`, dan memakai PNG proyek untuk file `.mhs`. Untuk membangun dan memasang ekstensi dari repositori:
39
59
 
40
60
  ```powershell
41
- cd vscode-mhs-icons
61
+ cd .\vscode-mhs-icons
42
62
  npx --yes @vscode/vsce package
43
- code --install-extension .\mhs-file-icons-1.2.0.vsix
63
+ code --install-extension .\mhs-file-icons-1.3.0.vsix
44
64
  ```
45
65
 
46
- Setelah ekstensi terpasang, file `.mhs` memakai grammar MHS (jadi kata seperti `fungsi` tidak diperiksa sebagai sintaks Python), dan toolbar editor menampilkan tombol ▶. Tombol menyimpan file lalu menjalankannya dengan `mhs --py`. Tema ikon mempertahankan ikon Material, termasuk ikon `.py`, dan memakai PNG proyek untuk `.mhs`.
66
+ Ekstensi menghubungkan editor ke LSP saat file `.mhs` dibuka. Tombol ▶ menyimpan file lalu menjalankannya dengan `mhs --py`.
67
+ Untuk menampilkan ikon proyek, pilih tema **MHS + Material File Icons** pada perintah **File: Preferences → File Icon Theme**.
47
68
 
48
69
  ### Cara Publish ke npm:
49
70
  ```bash
package/bin/mhs.js CHANGED
@@ -17,6 +17,7 @@ Penggunaan:
17
17
  mhs run <file.mhs> Menjalankan .mhs
18
18
  mhs --emit-py <file.mhs> Menampilkan hasil konversi ke Python 3 (.py)
19
19
  mhs --js <file.mhs> Menampilkan hasil konversi ke JavaScript
20
+ mhs --lsp Menjalankan language server untuk editor berklien LSP
20
21
  mhs init Membuat contoh halo.mhs & konfigurasi VS Code
21
22
  mhs Membuka mode interaktif (REPL)
22
23
  mhs --version, -v Menampilkan versi
@@ -123,6 +124,11 @@ function main() {
123
124
  return;
124
125
  }
125
126
 
127
+ if (cmd === '--lsp') {
128
+ require('../src/language_server').createLanguageServer();
129
+ return;
130
+ }
131
+
126
132
  if (cmd === 'init') {
127
133
  initProject();
128
134
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mhs-lang",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "Bahasa pemrograman Python dalam Bahasa Indonesia (.mhs) dengan dual-engine Node.js + Python 3 native, dukungan impor .mhs, npm, dan modul Python (.py/pip).",
5
5
  "main": "src/index.js",
6
6
  "bin": {
@@ -31,5 +31,9 @@
31
31
  "license": "MIT",
32
32
  "engines": {
33
33
  "node": ">=16.0.0"
34
+ },
35
+ "dependencies": {
36
+ "vscode-languageserver": "^9.0.1",
37
+ "vscode-languageserver-textdocument": "^1.0.12"
34
38
  }
35
39
  }
@@ -0,0 +1,283 @@
1
+ const {
2
+ CompletionItemKind,
3
+ DiagnosticSeverity,
4
+ ProposedFeatures,
5
+ SemanticTokensBuilder,
6
+ TextDocuments,
7
+ TextDocumentSyncKind,
8
+ createConnection,
9
+ } = require('vscode-languageserver/node');
10
+ const { TextDocument } = require('vscode-languageserver-textdocument');
11
+ const { transpile } = require('./transpiler');
12
+
13
+ const KEYWORDS = new Set([
14
+ 'asinkron', 'fungsi', 'kelas', 'jika', 'atau_jika', 'jika_tidak', 'selain_itu',
15
+ 'lainnya', 'selama', 'untuk', 'dalam', 'coba', 'tangkap', 'akhirnya', 'dengan',
16
+ 'kembalikan', 'hasilkan', 'berhenti', 'lanjut', 'lewat', 'lempar', 'pastikan',
17
+ 'umum', 'impor', 'dari', 'sebagai', 'impor_py', 'dari_py',
18
+ 'if', 'elif', 'else', 'while', 'for', 'in', 'def', 'class', 'return', 'yield',
19
+ 'break', 'continue', 'pass', 'raise', 'assert', 'try', 'except', 'finally',
20
+ 'with', 'import', 'from', 'as', 'async', 'await', 'global', 'nonlocal',
21
+ 'lambda', 'and', 'or', 'not', 'is', 'del', 'dan', 'atau', 'bukan',
22
+ 'adalah', 'bukan_adalah',
23
+ ]);
24
+
25
+ const BUILTINS = new Set([
26
+ 'cetak', 'masukan', 'rentang', 'panjang', 'tipe', 'teks', 'angka', 'bulat',
27
+ 'desimal', 'tambah', 'hapus', 'urutkan', 'balik', 'gabung', 'pisah',
28
+ 'huruf_besar', 'huruf_kecil', 'potong', 'ganti', 'mengandung', 'acak',
29
+ 'akar', 'pangkat', 'mutlak', 'bulatkan', 'bawah', 'atas', 'min_nilai',
30
+ 'maks_nilai', 'jumlah', 'waktu_sekarang', 'tunggu', 'daftar', 'kamus',
31
+ 'print', 'input', 'range', 'len', 'str', 'int', 'float', 'list', 'dict',
32
+ 'set', 'tuple', 'enumerate', 'zip', 'sum', 'min', 'max', 'abs', 'round',
33
+ ]);
34
+
35
+ const MODULES = [
36
+ 'matematika', 'modul_acak', 'waktu', 'berkas', 'sistem', 'data_json',
37
+ 'pola', 'koleksi', 'kripto', 'jaringan', 'uji',
38
+ ];
39
+
40
+ const CONSTANTS = ['Benar', 'Salah', 'Kosong', 'True', 'False', 'None'];
41
+
42
+ const COMPLETIONS = [
43
+ ...Array.from(KEYWORDS, (label) => ({
44
+ label,
45
+ kind: CompletionItemKind.Keyword,
46
+ })),
47
+ ...CONSTANTS.map((label) => ({
48
+ label,
49
+ kind: CompletionItemKind.Constant,
50
+ })),
51
+ ...Array.from(BUILTINS, (label) => ({
52
+ label,
53
+ kind: CompletionItemKind.Function,
54
+ })),
55
+ ...MODULES.map((label) => ({
56
+ label,
57
+ kind: CompletionItemKind.Module,
58
+ detail: 'Modul standar MHS',
59
+ })),
60
+ ];
61
+
62
+ const TOKEN_TYPES = ['keyword', 'function', 'type', 'string', 'number', 'comment', 'operator'];
63
+ const TOKEN_TYPE_INDEX = new Map(TOKEN_TYPES.map((name, index) => [name, index]));
64
+
65
+ function collectSemanticTokens(text) {
66
+ const tokens = [];
67
+ const lines = text.split(/\r\n|\r|\n/);
68
+
69
+ for (let lineNumber = 0; lineNumber < lines.length; lineNumber += 1) {
70
+ const line = lines[lineNumber];
71
+ let offset = 0;
72
+ let pendingDeclaration = null;
73
+
74
+ while (offset < line.length) {
75
+ const character = line[offset];
76
+ if (/\s/.test(character)) {
77
+ offset += 1;
78
+ continue;
79
+ }
80
+
81
+ if (character === '#') {
82
+ tokens.push({
83
+ line: lineNumber,
84
+ start: offset,
85
+ length: line.length - offset,
86
+ type: 'comment',
87
+ });
88
+ break;
89
+ }
90
+
91
+ const stringStart = /[fF]/.test(character) && /["']/.test(line[offset + 1])
92
+ ? offset + 1
93
+ : offset;
94
+ if (line[stringStart] === '"' || line[stringStart] === "'") {
95
+ const quote = line[stringStart];
96
+ let end = stringStart + 1;
97
+ while (end < line.length) {
98
+ if (line[end] === '\\') {
99
+ end += 2;
100
+ continue;
101
+ }
102
+ if (line[end] === quote) {
103
+ end += 1;
104
+ break;
105
+ }
106
+ end += 1;
107
+ }
108
+ tokens.push({
109
+ line: lineNumber,
110
+ start: offset,
111
+ length: end - offset,
112
+ type: 'string',
113
+ });
114
+ offset = end;
115
+ pendingDeclaration = null;
116
+ continue;
117
+ }
118
+
119
+ const identifier = line.slice(offset).match(/^[A-Za-z_]\w*/);
120
+ if (identifier) {
121
+ const label = identifier[0];
122
+ let type;
123
+ if (KEYWORDS.has(label)) {
124
+ type = 'keyword';
125
+ if (label === 'fungsi' || label === 'def') {
126
+ pendingDeclaration = 'function';
127
+ } else if (label === 'kelas' || label === 'class') {
128
+ pendingDeclaration = 'type';
129
+ }
130
+ } else if (pendingDeclaration) {
131
+ type = pendingDeclaration;
132
+ pendingDeclaration = null;
133
+ } else if (BUILTINS.has(label)) {
134
+ type = 'function';
135
+ } else if (['Benar', 'Salah', 'Kosong', 'True', 'False', 'None'].includes(label)) {
136
+ type = 'keyword';
137
+ }
138
+
139
+ if (type) {
140
+ tokens.push({
141
+ line: lineNumber,
142
+ start: offset,
143
+ length: label.length,
144
+ type,
145
+ });
146
+ }
147
+ offset += label.length;
148
+ continue;
149
+ }
150
+
151
+ const number = line.slice(offset).match(/^\d+(?:\.\d+)?/);
152
+ if (number) {
153
+ tokens.push({
154
+ line: lineNumber,
155
+ start: offset,
156
+ length: number[0].length,
157
+ type: 'number',
158
+ });
159
+ offset += number[0].length;
160
+ pendingDeclaration = null;
161
+ continue;
162
+ }
163
+
164
+ if (/[+\-*/%=<>!&|^~]/.test(character)) {
165
+ let end = offset + 1;
166
+ while (end < line.length && /[+\-*/%=<>!&|^~]/.test(line[end])) {
167
+ end += 1;
168
+ }
169
+ tokens.push({
170
+ line: lineNumber,
171
+ start: offset,
172
+ length: end - offset,
173
+ type: 'operator',
174
+ });
175
+ offset = end;
176
+ pendingDeclaration = null;
177
+ continue;
178
+ }
179
+
180
+ pendingDeclaration = null;
181
+ offset += 1;
182
+ }
183
+ }
184
+
185
+ return tokens;
186
+ }
187
+
188
+ function getDiagnostics(text) {
189
+ try {
190
+ transpile(text);
191
+ return [];
192
+ } catch (error) {
193
+ if (!(error instanceof SyntaxError)) {
194
+ throw error;
195
+ }
196
+
197
+ const lineMatch = error.message.match(/\bbaris\s+(\d+)/i);
198
+ const lineNumber = lineMatch ? Math.max(0, Number(lineMatch[1]) - 1) : 0;
199
+ const lines = text.split(/\r\n|\r|\n/);
200
+ const lineText = lines[lineNumber] || '';
201
+
202
+ return [{
203
+ range: {
204
+ start: { line: lineNumber, character: 0 },
205
+ end: { line: lineNumber, character: Math.min(1, lineText.length) },
206
+ },
207
+ severity: DiagnosticSeverity.Error,
208
+ source: 'mhs',
209
+ message: error.message,
210
+ }];
211
+ }
212
+ }
213
+
214
+ function createLanguageServer(
215
+ connection = createConnection(ProposedFeatures.all, process.stdin, process.stdout)
216
+ ) {
217
+ const documents = new TextDocuments(TextDocument);
218
+ const legend = {
219
+ tokenTypes: TOKEN_TYPES,
220
+ tokenModifiers: [],
221
+ };
222
+
223
+ connection.onInitialize(() => ({
224
+ capabilities: {
225
+ textDocumentSync: TextDocumentSyncKind.Incremental,
226
+ completionProvider: {
227
+ triggerCharacters: ['.'],
228
+ },
229
+ semanticTokensProvider: {
230
+ legend,
231
+ full: true,
232
+ },
233
+ },
234
+ }));
235
+
236
+ documents.onDidOpen(({ document }) => {
237
+ connection.sendDiagnostics({
238
+ uri: document.uri,
239
+ diagnostics: getDiagnostics(document.getText()),
240
+ });
241
+ });
242
+
243
+ documents.onDidChangeContent(({ document }) => {
244
+ connection.sendDiagnostics({
245
+ uri: document.uri,
246
+ diagnostics: getDiagnostics(document.getText()),
247
+ });
248
+ });
249
+
250
+ documents.onDidClose(({ document }) => {
251
+ connection.sendDiagnostics({ uri: document.uri, diagnostics: [] });
252
+ });
253
+
254
+ connection.onCompletion(() => COMPLETIONS);
255
+
256
+ connection.languages.semanticTokens.on(({ textDocument }) => {
257
+ const document = documents.get(textDocument.uri);
258
+ if (!document) {
259
+ return { data: [] };
260
+ }
261
+
262
+ const builder = new SemanticTokensBuilder(legend);
263
+ for (const token of collectSemanticTokens(document.getText())) {
264
+ builder.push(
265
+ token.line,
266
+ token.start,
267
+ token.length,
268
+ TOKEN_TYPE_INDEX.get(token.type),
269
+ 0
270
+ );
271
+ }
272
+ return builder.build();
273
+ });
274
+
275
+ documents.listen(connection);
276
+ connection.listen();
277
+ }
278
+
279
+ module.exports = {
280
+ collectSemanticTokens,
281
+ createLanguageServer,
282
+ getDiagnostics,
283
+ };