@arborium/proto 2.17.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 ADDED
@@ -0,0 +1,65 @@
1
+ # @arborium/proto
2
+
3
+ [![Part of Arborium](https://img.shields.io/badge/🌳_Arborium-grammar_collection-228B22)](https://github.com/bearcove/arborium)
4
+ [![npm](https://img.shields.io/npm/v/@arborium/proto)](https://www.npmjs.com/package/@arborium/proto)
5
+ [![license](https://img.shields.io/npm/l/@arborium/proto)](https://github.com/bearcove/arborium)
6
+
7
+ Syntax highlighting for [Protocol Buffers](https://protobuf.dev), powered by WebAssembly and [tree-sitter](https://tree-sitter.github.io/).
8
+
9
+ > Protocol Buffers are a language-neutral, platform-neutral extensible mechanism for serializing structured data.
10
+
11
+
12
+ | | |
13
+ |---|---|
14
+ | **Inventor** | Google |
15
+ | **Year** | 2008 |
16
+
17
+ ## Installation
18
+
19
+ ```bash
20
+ npm install @arborium/proto
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ ```javascript
26
+ import { createHighlighter } from '@arborium/arborium';
27
+ import * as grammar from '@arborium/proto';
28
+
29
+ const highlighter = await createHighlighter();
30
+ await highlighter.loadGrammar(grammar);
31
+
32
+ const html = highlighter.highlight(code, 'proto');
33
+ ```
34
+
35
+ ## What's included
36
+
37
+ This package contains:
38
+
39
+ - `grammar.js` - ES module that loads the WebAssembly grammar
40
+ - `grammar.d.ts` - TypeScript type definitions
41
+ - `grammar_bg.wasm` - The tree-sitter grammar compiled to WebAssembly
42
+
43
+ ## About Arborium
44
+
45
+ This package is part of [**Arborium**](https://github.com/bearcove/arborium), a collection of tree-sitter grammars for syntax highlighting, maintained by [Amos Wenger](https://fasterthanli.me).
46
+
47
+ Arborium provides:
48
+ - **98+ language grammars** compiled to WebAssembly
49
+ - **Browser and Node.js support** via ES modules
50
+ - **TypeScript definitions** for all packages
51
+ - **Consistent API** across all grammars
52
+
53
+ ## Related packages
54
+
55
+ - [`@arborium/arborium`](https://www.npmjs.com/package/@arborium/arborium) - Core highlighter library
56
+ - [All @arborium packages](https://www.npmjs.com/org/arborium)
57
+
58
+ ## License
59
+
60
+ MIT OR Apache-2.0
61
+
62
+ ## Links
63
+
64
+ - [GitHub](https://github.com/bearcove/arborium)
65
+ - [Issues](https://github.com/bearcove/arborium/issues)
package/grammar.js ADDED
@@ -0,0 +1,386 @@
1
+ /* @ts-self-types="./arborium_proto_plugin.d.ts" */
2
+
3
+ /**
4
+ * Cancels an ongoing parse operation.
5
+ * @param {number} session
6
+ */
7
+ export function cancel(session) {
8
+ wasm.cancel(session);
9
+ }
10
+
11
+ /**
12
+ * Creates a new parser session and returns its ID.
13
+ * @returns {number}
14
+ */
15
+ export function create_session() {
16
+ const ret = wasm.create_session();
17
+ return ret >>> 0;
18
+ }
19
+
20
+ /**
21
+ * Frees a parser session.
22
+ * @param {number} session
23
+ */
24
+ export function free_session(session) {
25
+ wasm.free_session(session);
26
+ }
27
+
28
+ /**
29
+ * Returns the list of languages this grammar can inject into (e.g., for embedded languages).
30
+ * Most grammars return an empty array.
31
+ * @returns {string[]}
32
+ */
33
+ export function injection_languages() {
34
+ try {
35
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
36
+ wasm.injection_languages(retptr);
37
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
38
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
39
+ var v1 = getArrayJsValueFromWasm0(r0, r1).slice();
40
+ wasm.__wbindgen_export(r0, r1 * 4, 4);
41
+ return v1;
42
+ } finally {
43
+ wasm.__wbindgen_add_to_stack_pointer(16);
44
+ }
45
+ }
46
+
47
+ /**
48
+ * Returns the language ID for this grammar plugin.
49
+ * @returns {string}
50
+ */
51
+ export function language_id() {
52
+ let deferred1_0;
53
+ let deferred1_1;
54
+ try {
55
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
56
+ wasm.language_id(retptr);
57
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
58
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
59
+ deferred1_0 = r0;
60
+ deferred1_1 = r1;
61
+ return getStringFromWasm0(r0, r1);
62
+ } finally {
63
+ wasm.__wbindgen_add_to_stack_pointer(16);
64
+ wasm.__wbindgen_export(deferred1_0, deferred1_1, 1);
65
+ }
66
+ }
67
+
68
+ /**
69
+ * Parses the text in a session and returns spans with UTF-8 byte offsets.
70
+ *
71
+ * Use this for Rust code that needs to slice strings with `&source[start..end]`.
72
+ * For JavaScript interop, use `parse_utf16` instead.
73
+ * @param {number} session
74
+ * @returns {any}
75
+ */
76
+ export function parse(session) {
77
+ try {
78
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
79
+ wasm.parse(retptr, session);
80
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
81
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
82
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
83
+ if (r2) {
84
+ throw takeObject(r1);
85
+ }
86
+ return takeObject(r0);
87
+ } finally {
88
+ wasm.__wbindgen_add_to_stack_pointer(16);
89
+ }
90
+ }
91
+
92
+ /**
93
+ * Parses the text in a session and returns spans with UTF-16 code unit indices.
94
+ *
95
+ * Use this for JavaScript code that needs to use `String.prototype.slice()`.
96
+ * The offsets are compatible with JavaScript string APIs.
97
+ * @param {number} session
98
+ * @returns {any}
99
+ */
100
+ export function parse_utf16(session) {
101
+ try {
102
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
103
+ wasm.parse_utf16(retptr, session);
104
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
105
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
106
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
107
+ if (r2) {
108
+ throw takeObject(r1);
109
+ }
110
+ return takeObject(r0);
111
+ } finally {
112
+ wasm.__wbindgen_add_to_stack_pointer(16);
113
+ }
114
+ }
115
+
116
+ /**
117
+ * Sets the text for a parser session.
118
+ * @param {number} session
119
+ * @param {string} text
120
+ */
121
+ export function set_text(session, text) {
122
+ const ptr0 = passStringToWasm0(text, wasm.__wbindgen_export2, wasm.__wbindgen_export3);
123
+ const len0 = WASM_VECTOR_LEN;
124
+ wasm.set_text(session, ptr0, len0);
125
+ }
126
+
127
+ function __wbg_get_imports() {
128
+ const import0 = {
129
+ __proto__: null,
130
+ __wbg___wbindgen_throw_6ddd609b62940d55: function(arg0, arg1) {
131
+ throw new Error(getStringFromWasm0(arg0, arg1));
132
+ },
133
+ __wbg_new_a70fbab9066b301f: function() {
134
+ const ret = new Array();
135
+ return addHeapObject(ret);
136
+ },
137
+ __wbg_new_ab79df5bd7c26067: function() {
138
+ const ret = new Object();
139
+ return addHeapObject(ret);
140
+ },
141
+ __wbg_set_282384002438957f: function(arg0, arg1, arg2) {
142
+ getObject(arg0)[arg1 >>> 0] = takeObject(arg2);
143
+ },
144
+ __wbg_set_6be42768c690e380: function(arg0, arg1, arg2) {
145
+ getObject(arg0)[takeObject(arg1)] = takeObject(arg2);
146
+ },
147
+ __wbindgen_cast_0000000000000001: function(arg0) {
148
+ // Cast intrinsic for `F64 -> Externref`.
149
+ const ret = arg0;
150
+ return addHeapObject(ret);
151
+ },
152
+ __wbindgen_cast_0000000000000002: function(arg0, arg1) {
153
+ // Cast intrinsic for `Ref(String) -> Externref`.
154
+ const ret = getStringFromWasm0(arg0, arg1);
155
+ return addHeapObject(ret);
156
+ },
157
+ __wbindgen_object_clone_ref: function(arg0) {
158
+ const ret = getObject(arg0);
159
+ return addHeapObject(ret);
160
+ },
161
+ __wbindgen_object_drop_ref: function(arg0) {
162
+ takeObject(arg0);
163
+ },
164
+ };
165
+ return {
166
+ __proto__: null,
167
+ "./arborium_proto_plugin_bg.js": import0,
168
+ };
169
+ }
170
+
171
+ function addHeapObject(obj) {
172
+ if (heap_next === heap.length) heap.push(heap.length + 1);
173
+ const idx = heap_next;
174
+ heap_next = heap[idx];
175
+
176
+ heap[idx] = obj;
177
+ return idx;
178
+ }
179
+
180
+ function dropObject(idx) {
181
+ if (idx < 1028) return;
182
+ heap[idx] = heap_next;
183
+ heap_next = idx;
184
+ }
185
+
186
+ function getArrayJsValueFromWasm0(ptr, len) {
187
+ ptr = ptr >>> 0;
188
+ const mem = getDataViewMemory0();
189
+ const result = [];
190
+ for (let i = ptr; i < ptr + 4 * len; i += 4) {
191
+ result.push(takeObject(mem.getUint32(i, true)));
192
+ }
193
+ return result;
194
+ }
195
+
196
+ let cachedDataViewMemory0 = null;
197
+ function getDataViewMemory0() {
198
+ if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
199
+ cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
200
+ }
201
+ return cachedDataViewMemory0;
202
+ }
203
+
204
+ function getStringFromWasm0(ptr, len) {
205
+ ptr = ptr >>> 0;
206
+ return decodeText(ptr, len);
207
+ }
208
+
209
+ let cachedUint8ArrayMemory0 = null;
210
+ function getUint8ArrayMemory0() {
211
+ if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
212
+ cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
213
+ }
214
+ return cachedUint8ArrayMemory0;
215
+ }
216
+
217
+ function getObject(idx) { return heap[idx]; }
218
+
219
+ let heap = new Array(1024).fill(undefined);
220
+ heap.push(undefined, null, true, false);
221
+
222
+ let heap_next = heap.length;
223
+
224
+ function passStringToWasm0(arg, malloc, realloc) {
225
+ if (realloc === undefined) {
226
+ const buf = cachedTextEncoder.encode(arg);
227
+ const ptr = malloc(buf.length, 1) >>> 0;
228
+ getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
229
+ WASM_VECTOR_LEN = buf.length;
230
+ return ptr;
231
+ }
232
+
233
+ let len = arg.length;
234
+ let ptr = malloc(len, 1) >>> 0;
235
+
236
+ const mem = getUint8ArrayMemory0();
237
+
238
+ let offset = 0;
239
+
240
+ for (; offset < len; offset++) {
241
+ const code = arg.charCodeAt(offset);
242
+ if (code > 0x7F) break;
243
+ mem[ptr + offset] = code;
244
+ }
245
+ if (offset !== len) {
246
+ if (offset !== 0) {
247
+ arg = arg.slice(offset);
248
+ }
249
+ ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
250
+ const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
251
+ const ret = cachedTextEncoder.encodeInto(arg, view);
252
+
253
+ offset += ret.written;
254
+ ptr = realloc(ptr, len, offset, 1) >>> 0;
255
+ }
256
+
257
+ WASM_VECTOR_LEN = offset;
258
+ return ptr;
259
+ }
260
+
261
+ function takeObject(idx) {
262
+ const ret = getObject(idx);
263
+ dropObject(idx);
264
+ return ret;
265
+ }
266
+
267
+ let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
268
+ cachedTextDecoder.decode();
269
+ const MAX_SAFARI_DECODE_BYTES = 2146435072;
270
+ let numBytesDecoded = 0;
271
+ function decodeText(ptr, len) {
272
+ numBytesDecoded += len;
273
+ if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
274
+ cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
275
+ cachedTextDecoder.decode();
276
+ numBytesDecoded = len;
277
+ }
278
+ return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
279
+ }
280
+
281
+ const cachedTextEncoder = new TextEncoder();
282
+
283
+ if (!('encodeInto' in cachedTextEncoder)) {
284
+ cachedTextEncoder.encodeInto = function (arg, view) {
285
+ const buf = cachedTextEncoder.encode(arg);
286
+ view.set(buf);
287
+ return {
288
+ read: arg.length,
289
+ written: buf.length
290
+ };
291
+ };
292
+ }
293
+
294
+ let WASM_VECTOR_LEN = 0;
295
+
296
+ let wasmModule, wasm;
297
+ function __wbg_finalize_init(instance, module) {
298
+ wasm = instance.exports;
299
+ wasmModule = module;
300
+ cachedDataViewMemory0 = null;
301
+ cachedUint8ArrayMemory0 = null;
302
+ return wasm;
303
+ }
304
+
305
+ async function __wbg_load(module, imports) {
306
+ if (typeof Response === 'function' && module instanceof Response) {
307
+ if (typeof WebAssembly.instantiateStreaming === 'function') {
308
+ try {
309
+ return await WebAssembly.instantiateStreaming(module, imports);
310
+ } catch (e) {
311
+ const validResponse = module.ok && expectedResponseType(module.type);
312
+
313
+ if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
314
+ console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
315
+
316
+ } else { throw e; }
317
+ }
318
+ }
319
+
320
+ const bytes = await module.arrayBuffer();
321
+ return await WebAssembly.instantiate(bytes, imports);
322
+ } else {
323
+ const instance = await WebAssembly.instantiate(module, imports);
324
+
325
+ if (instance instanceof WebAssembly.Instance) {
326
+ return { instance, module };
327
+ } else {
328
+ return instance;
329
+ }
330
+ }
331
+
332
+ function expectedResponseType(type) {
333
+ switch (type) {
334
+ case 'basic': case 'cors': case 'default': return true;
335
+ }
336
+ return false;
337
+ }
338
+ }
339
+
340
+ function initSync(module) {
341
+ if (wasm !== undefined) return wasm;
342
+
343
+
344
+ if (module !== undefined) {
345
+ if (Object.getPrototypeOf(module) === Object.prototype) {
346
+ ({module} = module)
347
+ } else {
348
+ console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
349
+ }
350
+ }
351
+
352
+ const imports = __wbg_get_imports();
353
+ if (!(module instanceof WebAssembly.Module)) {
354
+ module = new WebAssembly.Module(module);
355
+ }
356
+ const instance = new WebAssembly.Instance(module, imports);
357
+ return __wbg_finalize_init(instance, module);
358
+ }
359
+
360
+ async function __wbg_init(module_or_path) {
361
+ if (wasm !== undefined) return wasm;
362
+
363
+
364
+ if (module_or_path !== undefined) {
365
+ if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
366
+ ({module_or_path} = module_or_path)
367
+ } else {
368
+ console.warn('using deprecated parameters for the initialization function; pass a single object instead')
369
+ }
370
+ }
371
+
372
+ if (module_or_path === undefined) {
373
+ module_or_path = new URL('arborium_proto_plugin_bg.wasm', import.meta.url);
374
+ }
375
+ const imports = __wbg_get_imports();
376
+
377
+ if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
378
+ module_or_path = fetch(module_or_path);
379
+ }
380
+
381
+ const { instance, module } = await __wbg_load(await module_or_path, imports);
382
+
383
+ return __wbg_finalize_init(instance, module);
384
+ }
385
+
386
+ export { initSync, __wbg_init as default };
Binary file
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@arborium/proto",
3
+ "version": "2.17.0",
4
+ "description": "Syntax highlighting for Protocol Buffers, powered by WebAssembly",
5
+ "type": "module",
6
+ "main": "./grammar.js",
7
+ "module": "./grammar.js",
8
+ "exports": {
9
+ ".": "./grammar.js",
10
+ "./grammar.js": "./grammar.js",
11
+ "./grammar_bg.wasm": "./grammar_bg.wasm"
12
+ },
13
+ "files": ["grammar.js", "grammar_bg.wasm"],
14
+ "sideEffects": false,
15
+ "keywords": [
16
+ "arborium",
17
+ "syntax-highlighting",
18
+ "tree-sitter",
19
+ "proto",
20
+ "Protocol Buffers",
21
+ "wasm"
22
+ ],
23
+ "author": "Amos Wenger <amos@bearcove.eu>",
24
+ "license": "MIT",
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "https://github.com/bearcove/arborium"
28
+ },
29
+ "homepage": "https://github.com/bearcove/arborium",
30
+ "bugs": {
31
+ "url": "https://github.com/bearcove/arborium/issues"
32
+ },
33
+ "funding": {
34
+ "type": "github",
35
+ "url": "https://github.com/sponsors/fasterthanlime"
36
+ },
37
+ "engines": {
38
+ "node": ">=18"
39
+ },
40
+ "publishConfig": {
41
+ "access": "public"
42
+ }
43
+ }