#proto-file #json-format #definition #buffer #protocols #convert #syntax

bin+lib proto-file-parser

A Protocol Buffer Definition (.proto) parser that converts proto files to JSON format

2 releases

new 0.1.1 Nov 17, 2024
0.1.0 Nov 10, 2024

#1363 in Parser implementations

Download history 46/week @ 2024-11-04 97/week @ 2024-11-11

143 downloads per month

MIT license

23KB
470 lines

proto-file-parser

A Rust library and CLI tool for parsing Protocol Buffer Definition (.proto) files into JSON format.

https://crates.io/crates/proto-file-parser

Technical Description

The parser processes .proto files through the following stages:

  1. Lexical Analysis

    • Tokenizes the input into Protocol Buffer language elements
    • Handles comments, whitespace, and line continuations
    • Recognizes keywords, identifiers, numbers, and special characters
  2. 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
  3. Semantic Analysis

    • Validates field numbers and types
    • Ensures enum values are unique
    • Verifies package names and imports
  4. 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
proto-file-parser parse input.proto

# Display help information
proto-file-parser help

# Show version and credits
proto-file-parser version

Library

use proto_parser::Parser;

let parser = Parser::new();
let json = parser.parse_file("input.proto")?;
println!("{}", json);

Building and Testing

# Format code
make format

# Run tests
make test

# Run linter
make lint

# Run all checks before committing
make pre-commit

Dependencies

~4–13MB
~182K SLoC