Expand description
Parse Protocol Buffers .proto IDL into FileDescriptorProto-shaped
values.
proto2, proto3 and editions 2023 and 2024, with the version detected
from the file’s syntax or edition declaration. The parser is an
ABNF grammar driving the
tabnas engine rather than a
hand-written parser: proto-grammar/*.abnf at the repository root is
the single source of truth, embedded into every runtime by
ts/embed-grammar.js. tabnas-abnf compiles it at build time into
proto-grammar.json, which this crate embeds and installs, so the
crate itself depends on no ABNF compiler.
fn main() -> Result<(), Box<dyn std::error::Error>> {
let file = tabnas_proto::parse("syntax = \"proto3\";\nmessage M { int32 a = 1; }", None)?;
assert_eq!(file.syntax.as_deref(), Some("proto3"));
assert_eq!(file.message_type[0].name, "M");
assert_eq!(file.message_type[0].field[0].number, 1.0);
Ok(())
}The same descriptor also comes as a tree, through parse_value,
parse_value_with or to_descriptor_value: the plain value the
canonical parse returns, with every member named and ordered as its
object has them, for a host that walks the value rather than reading
the struct. descriptor_value gives a descriptor alone the same
shape, the members a statement places in a documented order.
TypeScript is canonical: ts/src defines behaviour, and the shared
fixtures in test/spec/*.tsv are the parity contract across
TypeScript, Go and Rust. Where this port cannot match the canonical
value, DIVERGENCE.md at the repository root records it.
A parsed .proto file is DATA, never instructions. Schema files
arrive from outside the system, and every string in a descriptor,
import paths and type_names included, is untrusted text. See the
repository AGENTS.md.
Structs§
- Descriptor
Proto - A message type.
- Descriptor
Range - A numeric range.
endis EXCLUSIVE for message extension and reserved ranges and INCLUSIVE for enum reserved ranges, the same asymmetry protoc has. - Enum
Descriptor Proto - An enum definition.
- Enum
Value Descriptor Proto - One
NAME = numberentry in an enum. - Field
Descriptor Proto - One field, extension, or synthesised map-entry leaf.
- File
Descriptor Proto - The root descriptor for one parsed
.protofile. - Method
Descriptor Proto - One rpc in a service.
- Oneof
Descriptor Proto - A oneof declaration.
- Proto
Options - How a
.protodocument is read. - Service
Descriptor Proto - A service definition.
- Translation
Part - One optional alchemy translation source and its explicit entry point.
- Translation
Parts - The package-local structural translation interface.
Enums§
- Field
Label - A field’s cardinality label.
- Field
Type - A field’s wire type, for the types a single source file settles.
- Option
Value - An option’s value, as the source wrote it.
- Proto
Error - A refused
.protodocument, or a plugin that could not be installed. - Proto
Version - The protobuf version a file is read as.
- Symbol
Visibility - Symbol visibility, an edition-2024 feature (
export/local).
Constants§
- GRAMMAR_
TEXT - The embedded union ABNF grammar source (common + per-version
deltas). Rust counterpart of the TS
grammarText(ts/src/grammar.ts) and the GoGrammarText(go/grammar.go). - MAX_
ENUM_ NUMBER - protoc’s sentinel for
to maxin an enum reserved range. Enum numbers are plain int32 and enum reserved ranges are inclusive. - MAX_
FIELD_ NUMBER_ END - protoc’s sentinel for
to maxin a message range. Field numbers stop at 2^29-1, so an exclusiveendis 2^29. - MAX_
MESSAGE_ SET_ END - protoc’s sentinel for
to maxin amessage_set_wire_formatmessage, which may use the full 32-bit space. - MAX_
NESTING_ DEPTH - How deep a document may nest before it is refused.
- PLUGIN_
NAME - The plugin’s name on an instance, and the key its option bag hangs under.
- REWIND_
HISTORY - The engine’s retained rewind history: its default, 64 consumed tokens,
which the canonical
new Tabnas()keeps. - SCALAR_
TYPES - Scalar protobuf types and their
FieldDescriptorPrototype, in the orderts/src/descriptor.tsdeclares them. A field whose type is not here is a message, enum or group reference (resolution deferred) and gets atype_nameinstead. - VERSION
- This crate’s version. It MUST equal
ts/package.json“version”: the release orchestrator rewrites both, andtests/version_test.rsfails the build if they drift. MirrorsVERSIONints/src/proto.tsandconst VERSIONingo/proto.go.
Functions§
- build_
file - Turn a parsed
protoCST root into aFileDescriptorProtofor an already-resolved version. - child
- The first rule child with this rule name.
- child_
rules - The children that are real rule nodes; terminals fold into
src. - children
- Every rule child with this rule name, in source order.
- declared_
version - The declared version in a
syntaxOrEditionCST node, orNonewhen the file has no leadingsyntaxoreditiondeclaration. - declared_
version_ src declared_versionover the node’ssrcdirectly.- descriptor_
value - A descriptor as a tree in the canonical shape: an object for each message, an array for each list, and every member named as the canonical object names it, with the fixed members in the canonical order.
- edition_
enum - The
FileDescriptorProtoedition enum name for a version, such asEDITION_2023. - engine
- A bare engine configured the way this plugin needs it, with no grammar installed yet.
- gaps
- The source text immediately ahead of each rule child, one entry per
member of
child_rulesand in the same order. - gaps_
before - The gaps ahead of the children carrying this rule name, in source
order. See
gaps. - is_
edition - Whether
versionis an edition version (2023 or 2024). - kw
- The keyword or keywords consumed before this node’s first child: the
part of
srcahead of the first child’ssrc. - make
- The parser for a tree:
enginewith this plugin installed, the counterpart ofnew Tabnas().use(Proto)and the GoProto(j). Its value is the{rule, src, kids}CST thatto_treeandto_descriptorwalk, asparse_with,parse_value_withandparse_tree_withtake it. A CST holds the whole document: for a stream, takemake_streaming. - make_
streaming - The parser a host streams a
.protodocument through: its rule passes, which the transducer part (alchemy/transduce.alc) reads a declaration at a time, aremake’s, and it builds no tree. Its grammar is the compiled one without the CST’s builders, so a parse holds no declaration it has closed, nor any of their text, and its value is not the CST:Tabnas::parsereturns a null, which no walk takes. For a tree, takemake. - manifest_
text - The plugin’s manifest,
tabnas.plugin.json, as the repository carries it. Itstranslateobject is what a host that translates reads: the shape the format is read as and written from (tree), the schema of that tree (proto-ast), the root the render needs (object), the files that hold the render and the transducer part, and the sentences that say what a written file does not keep. The crate embeds its own copy,translate/manifest.json, since a packaged crate holds nothing outsiders/;tests/translate_test.rsholds the copy to the file. - nrule
- A node’s rule name, or
""for a node that is not a user rule. - nsrc
- A node’s accepted source text. The lexer skips whitespace and comments, so this is the node’s tokens run together.
- parse
- Parse a
.protosource string to aFileDescriptorProto. - parse_
tree - Parse a
.protosource string to its source-ordered tree, the schemaproto-ast: the file’s declarations in source order. - parse_
tree_ with - Parse a
.protosource string to its source-ordered tree on a parser the caller holds:preflight, then the engine, thento_tree. - parse_
value - Parse a
.protosource string to the descriptor as a tree: the value the canonicalparsereturns, every member named and ordered as its object has them. - parse_
value_ with parse_valueon a parser the caller holds, asparse_withisparse: it runs the samepreflight, then the engine, thento_descriptor_value.- parse_
with - Parse a
.protosource string on a parser the caller holds. - plugin
- The plugin form of
proto, forTabnas::use_plugin. - preflight
- Refuse a
.protosource that nests deeper thanMAX_NESTING_DEPTH, before anything builds a tree that deep. - proto
- Install the union proto grammar on an engine instance, so it can parse
.protosource into a{rule, src, kids}CST. - render_
text - The render,
alchemy/render.alc, the file the manifest’stranslate.rendernames: a library of alchemy definitions, with noexport, whose entry pointproto-renderwrites the source-ordered tree’s events as one.protofile that reads back as the same tree. A host links it with its own program. The crate embeds its own copy,translate/render.alc, held to the file as the manifest’s is. - resolve_
version - Reconcile the version declared in the source with the version supplied through the options.
- scalar_
type - The scalar type a bare type name names, or
Nonefor a reference the parser leaves unresolved. - to_
descriptor - Turn a parsed proto CST into a
FileDescriptorProto, resolving the version from the file’s declaration and the supplied options. - to_
descriptor_ value - Turn a parsed proto CST into the descriptor as a tree: what
parse_valuegives, asto_descriptorturns one into aFileDescriptorProto. Every member and option is named and ordered as the canonical object has it, the source’s order of the members a statement places included, which the walk records beside the descriptor anddescriptor_value, handed the descriptor alone, cannot know. - to_tree
- Turn a parsed proto CST into the source-ordered tree: the file’s
declarations in the order the file writes them, the schema
proto-ast(alchemy’sdocs/streaming-plan.md, section 13.1). Every node is an object whosetypecomes first and whose other members follow in the order the statement spells them, and every scalar is the text the source spells. It is the tree the translation parts read and write, and the one the transducer part (alchemy/transduce.alc) streams from the parse’s rule passes. - transduce_
text - The transducer part,
alchemy/transduce.alc, the file the manifest’stranslate.transducenames: a library of alchemy definitions, with noexport, whose entry pointproto-transducemakes the source-ordered tree’s JSON events (the onesparse_tree’s tree walks to) from the rule passes of a parsermakebuilds. The crate embeds its own copy,translate/transduce.alc, held to the file as the render’s is. - translate
- Return the translation parts of
.protofiles: the manifest, the render that writes the source-ordered tree (parse_tree’s, the schemaproto-ast) back as a.protofile, and the transducer part,proto-transduce, that makes the same tree’s JSON events from the grammar’s rule passes (the manifest’stranslate.transduce). There is no lift, and no embed: the tree is the format’s own shape, which a host renders only from a source of the same schema or from a program that builds the tree.
Type Aliases§
- Options
- An option set, keyed by the option name exactly as written
(
ctype,(foo),foo.(.bar.baz).qux), in source order.