@tabnas/proto 0.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/src/proto.ts ADDED
@@ -0,0 +1,70 @@
1
+ /* Copyright (c) 2026 Richard Rodger and other contributors, MIT License */
2
+
3
+ // @tabnas/proto — a Tabnas plugin that parses Protocol Buffers `.proto`
4
+ // IDL (proto2, proto3, edition 2023/2024) into FileDescriptorProto-shaped
5
+ // JSON. The version is auto-detected from the file's `syntax`/`edition`
6
+ // declaration and/or supplied via the `version` option.
7
+
8
+ import { Tabnas } from '@tabnas/parser'
9
+ import { abnf } from '@tabnas/abnf'
10
+
11
+ import { grammarText } from './grammar'
12
+ import { buildFile } from './build-descriptor'
13
+ import {
14
+ ProtoVersion, declaredVersion, resolveVersion,
15
+ } from './detect-version'
16
+ import { FileDescriptorProto } from './descriptor'
17
+
18
+ export interface ProtoOptions {
19
+ // Explicit protobuf version. When null, auto-detect from the file's
20
+ // syntax/edition declaration.
21
+ version: null | ProtoVersion
22
+ // When true (default), error if an explicit version disagrees with the
23
+ // file's declaration; when false the declaration wins.
24
+ reconcile: boolean
25
+ }
26
+
27
+ type AnyTabnas = Tabnas & { abnf?: Function }
28
+
29
+ // The Tabnas plugin: installs the union proto grammar so the engine can
30
+ // parse `.proto` source into a `{rule, src, kids}` CST. Use the exported
31
+ // `parse()` / `toDescriptor()` to turn that CST into a FileDescriptorProto.
32
+ const Proto = ((tn: AnyTabnas, _options?: Partial<ProtoOptions>) => {
33
+ // Proto drives parsing through @tabnas/abnf; ensure it is installed.
34
+ if ('function' !== typeof tn.abnf) tn.use(abnf)
35
+ ;(tn.abnf as Function)(grammarText, {
36
+ tag: 'proto',
37
+ start: 'proto',
38
+ wordKeywords: true,
39
+ })
40
+ }) as {
41
+ (tn: AnyTabnas, options?: Partial<ProtoOptions>): void
42
+ defaults: ProtoOptions
43
+ }
44
+
45
+ Proto.defaults = { version: null, reconcile: true }
46
+
47
+ // Turn a parsed proto CST into a FileDescriptorProto, resolving the
48
+ // version from the file's declaration and the supplied options.
49
+ function toDescriptor(cst: any, options?: Partial<ProtoOptions>): FileDescriptorProto {
50
+ const opts: ProtoOptions = { ...Proto.defaults, ...(options || {}) }
51
+ const first = (cst && cst.kids ? cst.kids : []).find((k: any) => k && k.rule)
52
+ const declared =
53
+ first && 'syntaxOrEdition' === first.rule ? declaredVersion(first) : null
54
+ const version = resolveVersion(declared, opts.version, opts.reconcile)
55
+ return buildFile(cst, version)
56
+ }
57
+
58
+ // Convenience: parse a `.proto` source string to a FileDescriptorProto in
59
+ // one call. Builds a fresh engine each time; for repeated parsing reuse an
60
+ // engine via `const j = new Tabnas().use(Proto)` and call
61
+ // `toDescriptor(j.parse(src), opts)`.
62
+ function parse(src: string, options?: Partial<ProtoOptions>): FileDescriptorProto {
63
+ const tn = new Tabnas({ rewind: { history: 8192 } }) as AnyTabnas
64
+ Proto(tn, options)
65
+ return toDescriptor(tn.parse(src), options)
66
+ }
67
+
68
+ export { Proto, parse, toDescriptor }
69
+ export type { ProtoVersion }
70
+ export * from './descriptor'
@@ -0,0 +1,15 @@
1
+ {
2
+ "compilerOptions": {
3
+ "esModuleInterop": true,
4
+ "module": "nodenext",
5
+ "noEmitOnError": true,
6
+ "outDir": "../dist",
7
+ "rootDir": ".",
8
+ "resolveJsonModule": true,
9
+ "sourceMap": true,
10
+ "strict": true,
11
+ "target": "es2021",
12
+ "declaration": true,
13
+ "declarationDir": "../dist"
14
+ }
15
+ }