Skip to content

Instantly share code, notes, and snippets.

@jflemer-ndp
Created July 12, 2021 15:34
Show Gist options
  • Save jflemer-ndp/0242842ffef0517faa683273e03f4273 to your computer and use it in GitHub Desktop.
Save jflemer-ndp/0242842ffef0517faa683273e03f4273 to your computer and use it in GitHub Desktop.
Doxygen filter for Google Protocol Buffers (protobuf, GPB)
#!/usr/bin/env perl
##############################################################################
#
# Copyright 2021, NDP LLC
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
##############################################################################
use strict;
use warnings;
sub format_field {
my ($t, $f, $n, $opts) = $_ =~ m,(optional|required|repeated)\s+(.*?)\s*=\s*(\d+)\s*(?:\[(.*?)\])?\s*;,;
#$f =~ s/\b([us]?int\d+)/${1}_t/;
#$f =~ s/\b(string)/std::${1}/;
my @a = ("index($n)");
my $d = "";
if ($t eq "repeated") {
$f .= "[]";
$d = " = {}";
}
if ($t eq "repeated") {
push @a, $t;
}
if ($t eq "required") {
push @a, $t;
}
if ($opts) {
foreach $_ (split(/\s*,\s*/, $opts)) {
s/^\s+|\s+$//g;
if (s/^default\s*=\s*//) {
$d = " = $_";
} else {
s/\s*=\s*(.*)/($1)/;
s/\s*\(true\s*\)//;
push @a, $_;
}
}
}
$a = join(" ", @a);
"$f$d [$a];"
}
my @ns = ();
my $enum = 0;
while (<>) {
chomp;
my ($space, $line, $comment) = m!^(\s*)(.*?)(\s*//.*)?$!g;
my ($word) = $line =~ m/^\s*(\w+)/g;
$comment = "" unless $comment;
$word = "" unless $word;
if ($word eq "option" || $word eq "syntax") {
$line = "// $line";
} elsif ($word eq "import") {
$line =~ s/import/#include/;
$line =~ s/;\s*$//;
} elsif ($word eq "package") {
my ($package) = $line =~ m/^\s*package\s+([^\s;]*).*$/g;
@ns = split(/\./, $package);
$line = join(" ", map { "namespace $_ {" } @ns);
} elsif ($word eq "message") {
$line =~ s/message/struct/;
} elsif ($word eq "enum") {
$enum = 1;
} elsif ($word eq "optional" || $word eq "required" || $word eq "repeated") {
$line = format_field($line);
}
if ($enum) {
$line =~ s/;/,/g;
$enum = 0 if $line =~ m/}/;
}
$line =~ s/}\s*$/};/g;
print "$space$line$comment\n";
}
if (@ns) {
print(("}") x @ns, "\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment