Skip to content
This repository has been archived by the owner on Nov 6, 2023. It is now read-only.

Commit

Permalink
replace gawk with awk, so as not to introduce an extra dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
HannahShiSFB committed Jul 24, 2020
1 parent c69c06b commit d3c1cbf
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 47 deletions.
105 changes: 65 additions & 40 deletions src/php/bin/php_extension_doxygen_filter.awk
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,62 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# the spaces in the parameter list are necessary to separate out local variables
function sed_gensub(regexp, replacement, how, target, cmd_, ret_) { # arguments and local variables
if (!target) {
target = $0
}
gsub(/'/, "'\"'\"'", target);
gsub(/\\\\/, "\\", regexp);

cmd_ = "printf '" target "' | sed -E 's/" regexp "/" replacement "/" tolower(how) "'";
if (cmd_ | getline ret_ != 1) {
close(cmd_);
error = "ERROR: running command: " cmd_ ", ret_: " ret_;
exit;
}
close(cmd_);
return ret_;
}

BEGIN {
namespace = "Grpc";
className = "";
classDocComment = "";
delete methods; # methods[method][doc|args|static]
delete constants; # constants[i][name|doc]

delete methodNames; # i => methodName
delete methodArgs; # methodName => concatenatedArgsStr
delete methodDocs; # methodName => methodDocCommentStr
delete methodStatics; # methodName => 1 if static
methodsCount = 0;

delete constantNames; # i => constantName
delete constantDocs; # constantName => constantDocCommentStr
constantsCount = 0;

# * class className
classLineRegex = "^ \\* class (\\S+)$";
classLineRegex = "^ \\* class ([^ \t]+)$";
# @param type name [= default]
paramLineRegex = "^.*@param\\s+\\S+\\s+(\\$\\S+(\\s+=\\s+\\S+)?)\\s+.*$";
paramLineRegex = "^.*@param[ \t]+[^ \t]+[ \t]+(\\$[^ \t]+([ \t]+=[ \t]+[^ \t]+)?)[ \t]+.*$";
# PHP_METHOD(class, function)
phpMethodLineRegex = "^PHP_METHOD\\((\\S+),\\s*(\\S+)\\).*$";
phpMethodLineRegex = "^PHP_METHOD\\(([^ \t]+),[ \t]*([^ \t]+)\\).*$";

# PHP_ME(class, function, arginfo, flags)
phpMeLineRegex = "^\\s*PHP_ME\\((\\S+),\\s*(\\S+),.*$";
phpMeLineRegex = "^[ \t]*PHP_ME\\(([^ \t]+),[ \t]*([^ \t]+),.*$";

# REGISTER_LONG_CONSTANT("namespace\\constant", grpcConstant, ..)
phpConstantLineRegs = "^\\s*REGISTER_LONG_CONSTANT\\(\"Grpc\\\\\\\\(\\S+)\",.*$";
phpConstantLineRegs = "^[ \t]*REGISTER_LONG_CONSTANT\\(\"Grpc\\\\\\\\([^ \t]+)\",.*$";

error = "";

# extension testing methods
hideMethods["Channel::getChannelInfo"] = 1;
hideMethods["Channel::cleanPersistentList"] = 1;
hideMethods["Channel::getPersistentList"] = 1;

}

# '/**' document comment start
/^\s*\/\*\*/ {
/^[ \t]*\/\*\*/ {
inDocComment = 1;
docComment = "";
delete args;
Expand All @@ -57,7 +81,7 @@ inDocComment==1 {

# class document, must match ' * class <clasName>'
inDocComment==1 && $0 ~ classLineRegex {
className = gensub(classLineRegex, "\\1", "g");
className = sed_gensub(classLineRegex, "\\1", "g");
}

# end of class document
Expand All @@ -68,9 +92,8 @@ inDocComment==1 && /\*\// && classDocComment == "" {

# param line
inDocComment==1 && $0 ~ paramLineRegex {
arg = gensub(paramLineRegex, "\\1", "g");
args[argsCount]=arg;
argsCount++;
arg = sed_gensub(paramLineRegex, "\\1", "g");
args[argsCount++]=arg;
}

# '*/' document comment end
Expand All @@ -80,37 +103,44 @@ inDocComment==1 && /\*\// {

# PHP_METHOD
$0 ~ phpMethodLineRegex {
class = gensub(phpMethodLineRegex, "\\1", "g");
class = sed_gensub(phpMethodLineRegex, "\\1", "g");
if (class != className) {
error = "ERROR: Missing or mismatch class names, in class comment block: " \
className ", in PHP_METHOD: " class;
exit;
};

method = gensub(phpMethodLineRegex, "\\2", "g");
methods[method]["doc"] = docComment;
for (i in args) {
methods[method]["args"][i] = args[i];
method = sed_gensub(phpMethodLineRegex, "\\2", "g");
methodNames[methodsCount++] = method;
methodDocs[method] = docComment;

# concat args
if (argsCount > 0) {
methodArgs[method] = args[0];
for (i = 1; i < argsCount; i++) {
methodArgs[method] = methodArgs[method] ", " args[i];
}
}

docComment = "";
}

# PHP_ME(class, function,...
$0 ~ phpMeLineRegex {
inPhpMe = 1;

class = gensub(phpMeLineRegex, "\\1", "g");
class = sed_gensub(phpMeLineRegex, "\\1", "g");
if (class != className) {
error = "ERROR: Missing or mismatch class names, in class comment block: " \
className ", in PHP_ME: " class;
exit;
};
method = gensub(phpMeLineRegex, "\\2", "g");
method = sed_gensub(phpMeLineRegex, "\\2", "g");
}

# ZEND_ACC_STATIC
inPhpMe && /ZEND_ACC_STATIC/ {
methods[method]["static"] = 1;
inPhpMe && /ZEND_ACC_STATIC/ {
methodStatics[method] = 1;
}

# closing bracet of PHP_ME(...)
Expand All @@ -121,14 +151,13 @@ iinPhpMe && /\)$/ {
# REGISTER_LONG_CONSTANT(constant, ...
$0 ~ phpConstantLineRegs {
inPhpConstant = 1;
constant = gensub(phpConstantLineRegs, "\\1", "g");
constants[constantsCount]["name"] = constant;
constants[constantsCount]["doc"] = docComment;
constantsCount++;
constant = sed_gensub(phpConstantLineRegs, "\\1", "g");
constantNames[constantsCount++] = constant;
constantDocs[constant] = docComment;
}

# closing bracet of PHP_ME(...)
inPhpConstant && /\)\s*;\s*$/ {
# closing bracet of REGISTER_LONG_CONSTANT(...)
inPhpConstant && /\)[ \t]*;[ \t]*$/ {
inPhpConstant = 0;
docComment = "";
}
Expand All @@ -145,27 +174,23 @@ END {
if (className != "") {
print classDocComment
print "class " className " {";
for (m in methods) {
for (i = 0; i < methodsCount; i++) {
m = methodNames[i];
if (hideMethods[className"::"m]) continue;

print methods[m]["doc"];
print methodDocs[m];
printf "public"
if (methods[m]["static"]) printf " static"
if (methodStatics[m]) printf " static"
printf " function " m "("
if (isarray(methods[m]["args"])) {
printf methods[m]["args"][0];
for (i = 1; i < length(methods[m]["args"]); i++) {
printf ", " methods[m]["args"][i];
}
}
printf methodArgs[m];
print ") {}";
}
print "\n}";
}

for (i in constants) {
print constants[i]["doc"];
print "const " constants[i]["name"] " = 0;";
for (i = 0; i < constantsCount; i++) {
print constantDocs[constantNames[i]];
print "const " constantNames[i] " = 0;";
}

print "\n}";
Expand Down
10 changes: 5 additions & 5 deletions src/php/bin/php_extension_to_php_doc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

set -euo pipefail

if ! command -v gawk > /dev/null; then
>&2 echo "ERROR: 'gawk' not installed"
if ! command -v awk > /dev/null; then
>&2 echo "ERROR: 'awk' not installed"
exit 1
fi

Expand All @@ -28,10 +28,10 @@ COMMAND="${1:-}"
# parse class and methods
for FILENAME in call_credentials.c call.c channel.c channel_credentials.c \
server_credentials.c server.c timeval.c ; do
CLASS_NAME=$(sed -r 's/(^|_)(\w)/\U\2/g' <<< "${FILENAME%.*}")
CLASS_NAME=$(sed -E 's/(^|_)(\w)/\U\2/g' <<< "${FILENAME%.*}")
if [[ "$COMMAND" == "generate" ]]; then
echo Generating lib/Grpc/$CLASS_NAME.php ...
gawk -f php_extension_doxygen_filter.awk ../ext/grpc/$FILENAME \
awk -f php_extension_doxygen_filter.awk ../ext/grpc/$FILENAME \
> ../lib/Grpc/$CLASS_NAME.php
elif [[ "$COMMAND" == "cleanup" ]]; then
rm ../lib/Grpc/$CLASS_NAME.php
Expand All @@ -44,7 +44,7 @@ done
# parse constants
if [[ "$COMMAND" == "generate" ]]; then
echo Generating lib/Grpc/Constants.php ...
gawk -f php_extension_doxygen_filter.awk ../ext/grpc/php_grpc.c \
awk -f php_extension_doxygen_filter.awk ../ext/grpc/php_grpc.c \
> ../lib/Grpc/Constants.php
elif [[ "$COMMAND" == "cleanup" ]]; then
rm ../lib/Grpc/Constants.php
Expand Down
2 changes: 1 addition & 1 deletion templates/tools/dockerfile/test/sanity/Dockerfile.template
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#========================
# Sanity test dependencies
RUN apt-get update && apt-get -t buster install -y python3.7 python3-all-dev gawk
RUN apt-get update && apt-get -t buster install -y python3.7 python3-all-dev
RUN curl https://bootstrap.pypa.io/get-pip.py | python3.7

# Make Python 3.7 the default Python 3 version
Expand Down
2 changes: 1 addition & 1 deletion tools/dockerfile/test/sanity/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ RUN apt-get update && apt-get -y install libgflags-dev libgtest-dev libc++-dev c

#========================
# Sanity test dependencies
RUN apt-get update && apt-get -t buster install -y python3.7 python3-all-dev gawk
RUN apt-get update && apt-get -t buster install -y python3.7 python3-all-dev
RUN curl https://bootstrap.pypa.io/get-pip.py | python3.7

# Make Python 3.7 the default Python 3 version
Expand Down

0 comments on commit d3c1cbf

Please sign in to comment.