MCPcopy Index your code
hub / github.com/bufbuild/protocompile

github.com/bufbuild/protocompile @v0.14.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.14.1 ↗ · + Follow
1,669 symbols 5,473 edges 97 files 402 documented · 24% 44 cross-repo links
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

The Buf logo

Protocompile

Build Report Card GoDoc

This repo contains a parsing/linking engine for Protocol Buffers, written in pure Go. It is suitable as an alternative to protoc (Google's official reference compiler for Protocol Buffers). This is the compiler that powers Buf and its bevy of tools.

This repo is also the spiritual successor to the github.com/jhump/protoreflect/desc/protoparse package. If you are looking for a newer version of protoparse that natively works with the newer Protobuf runtime API for Go (google.golang.org/protobuf), you have found it!

Protocol Buffers

If you've come across this repo but don't know what Protocol Buffers are, you might acquaint yourself with the official documentation. Protocol Buffers, or Protobuf for short, is an IDL for describing APIs and data structures and also a binary encoding format for efficiently transmitting and storing that data.

If you want to know more about the language itself, which is what this repo implements, take a look at Buf's Protobuf Guide, which includes a very detailed language specification.

Descriptors

Descriptors are the "lingua franca" for describing Protobuf data schemas. They are the basis of runtime features like reflection and dynamic messages. They are also the output of a Protobuf compiler: a compiler can produce them and write them to a file (whose contents are the binary-encoded form of a FileDescriptorSet) or send them to a plugin to generate code for a particular programming language.

Descriptors are similar to nodes in a syntax tree: the contents of a file descriptor correspond closely to the elements in the source file from which it was generated. Also, the descriptor model's data structures are themselves defined in Protobuf.

Using This Repo

The primary API of this repo is in this root package: github.com/bufbuild/protocompile. This is the suggested entry point and provides a type named Compiler, for compiling Protobuf source files into descriptors. There are also numerous sub-packages, most of which implement various stages of the compiler. Here's an overview (not in alphabetical order):

  • protocompile: This is the entry point, used to configure and initiate a compilation operation.
  • parser: This is the first stage of the compiler. It parses Protobuf source code and produces an AST. This package can also generate a file descriptor proto from an AST.
  • ast: This package models an Abstract Syntax Tree (AST) for the Protobuf language.
  • linker: This is the second stage of the compiler. The descriptor proto (generated from an AST) is linked, producing a more useful data structure than simple descriptor protos. This step also performs numerous validations on the source, like making sure that all type references are correct and that sources don't try to define two elements with the same name.
  • options: This is the next stage of the compiler: interpreting options. The linked data structures that come from the previous stage are used to validate and interpret all options.
  • sourceinfo: This is the last stage of the compiler: generating source code info. Source code info contains metadata that maps elements in the descriptor to the location in the original source file from which it came. This includes access to comments. In order to provide correct source info for options, it must happen last, after options have been interpreted.
  • reporter: This package provides error types generated by the compiler and interfaces used by the compiler to report errors and warnings to the calling code.
  • walk: This package provides functions for walking through all of the elements in a descriptor (or descriptor proto) hierarchy.
  • protoutil: This package contains some other useful functions for interacting with Protobuf descriptors.

Migrating from protoparse

There are a few differences between this repo and its predecessor, github.com/jhump/protoreflect/desc/protoparse.

  • If you want to include "standard imports", for the well-known files that are included with protoc, you have to do so explicitly. To do this, wrap your resolver using protocompile.WithStandardImports.
  • If you used protoparse.FileContentsFromMap, in this new repo you'll use a protocompile.SourceResolver and then use protocompile.SourceAccessorFromMap as its accessor function.
  • If you used Parser.ParseToAST, you won't use the protocompile package but instead directly use parser.Parse in this repo's parser sub-package. This returns an AST for the given file contents.
  • If you used Parser.ParseFilesButDoNotLink, that is still possible in this repo, but not provided directly via a single function. Instead, you need to take a few steps:
  • Parse the source using parser.Parse. Then use parser.ResultFromAST to construct a result that contains a file descriptor proto.
  • Interpret whatever options can be interpreted without linking using options.InterpretUnlinkedOptions. This may leave some options in the descriptor proto uninterpreted (including all custom options).
  • If you want source code info for the file, finally call sourceinfo.GenerateSourceInfo using the index returned from the previous step and store that in the file descriptor proto.

Extension points exported contracts — how you extend this code

DescriptorProtoWrapper (Interface)
DescriptorProtoWrapper is a protoreflect.Descriptor that wraps an underlying descriptor proto. It provides the same inte [9 …
protoutil/protos.go
MessageElement (Interface)
MessageElement is an interface implemented by all AST nodes that can appear in a message body. [11 implementers]
ast/message.go
FileElement (Interface)
FileElement is an interface implemented by all AST nodes that are allowed as top-level declarations in the file. [8 implementers]
ast/file.go
Node (Interface)
Node is the interface implemented by all nodes in the AST. It provides information about the span of this AST node in te [7 …
ast/node.go
NodeWithOptions (Interface)
NodeWithOptions represents a node in the AST that contains option statements. [13 implementers]
ast/options.go
ValueNode (Interface)
ValueNode is an AST node that represents a literal value. It also includes references (e.g. IdentifierValueNode), which [12 …
ast/values.go
SourceSpan (Interface)
SourceSpan represents a range of source positions. [7 implementers]
ast/file_info.go
EnumElement (Interface)
EnumElement is an interface implemented by all AST nodes that can appear in the body of an enum declaration. [4 implementers]
ast/enum.go

Core symbols most depended-on inside this repo

String
called by 186
ast/file_info.go
GetName
called by 149
ast/service.go
NodeInfo
called by 126
ast/file.go
HandleErrorf
called by 117
reporter/reporter.go
Enum
called by 116
linker/descriptors.go
FullName
called by 108
linker/descriptors.go
Name
called by 100
ast/file.go
nodeInfo
called by 88
options/options.go

Shape

Method 957
Function 479
Struct 146
Interface 58
TypeAlias 22
FuncType 7

Languages

Go100%

Modules by API surface

linker/descriptors.go246 symbols
ast/walk.go136 symbols
ast/field.go91 symbols
ast/file_info.go69 symbols
options/options.go68 symbols
parser/result.go61 symbols
sourceinfo/source_code_info.go53 symbols
ast/values.go49 symbols
linker/files.go38 symbols
linker/resolve.go36 symbols
linker/symbols.go31 symbols
parser/lexer.go30 symbols

For agents

$ claude mcp add protocompile \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact