proto-file-parser
A Rust library and CLI tool for parsing Protocol Buffer Definition (.proto) files into JSON format.
Technical Description
The parser processes .proto files through the following stages:
-
Lexical Analysis
- Tokenizes the input into Protocol Buffer language elements
- Handles comments, whitespace, and line continuations
- Recognizes keywords, identifiers, numbers, and special characters
-
Syntax Parsing
- Parses the token stream into an Abstract Syntax Tree (AST)
- Validates syntax according to Protocol Buffer Language Specification
- Handles nested message definitions and imports
-
Semantic Analysis
- Validates field numbers and types
- Ensures enum values are unique
- Verifies package names and imports
-
JSON Generation
- Converts the AST into a structured JSON format
- Preserves all relevant information from the .proto file
- Maintains nested structure relationships
Grammar Rules
proto = syntax_spec package_spec? import_spec*
(message_def | enum_def | service_def)*
syntax_spec = "syntax" "=" quote ("proto2" | "proto3") quote ";"
package_spec = "package" full_ident ";"
import_spec = "import" quote import_path quote ";"
message_def = "message" ident "{" field_def* "}"
field_def = type_name ident "=" number ";"
enum_def = "enum" ident "{" enum_field* "}"
service_def = "service" ident "{" rpc_def* "}"
rpc_def = "rpc" ident "(" message_type ")"
"returns" "(" message_type ")" ";"
Usage
CLI
# Parse a .proto file and output JSON
# Display help information
# Show version and credits
Library
use Parser;
let parser = new;
let json = parser.parse_file?;
println!;
Building and Testing
# Format code
# Run tests
# Run linter
# Run all checks before committing