Skip to content

Commit fec5c3a

Browse files
Merge pull request grpc#23607 from HannahShiSFB/api-doxygen-step2
PHP: change to awk from gawk, since mac doesn't support it
2 parents 70f0015 + 1e415e2 commit fec5c3a

File tree

4 files changed

+74
-48
lines changed

4 files changed

+74
-48
lines changed

src/php/bin/php_extension_doxygen_filter.awk

Lines changed: 66 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,62 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
# the spaces in the parameter list are necessary to separate out local variables
16+
function sed_gensub(regexp, replacement, how, target, cmd_, ret_) { # arguments and local variables
17+
if (!target) {
18+
target = $0
19+
}
20+
gsub(/'/, "'\"'\"'", target);
21+
gsub(/\\\\/, "\\", regexp);
22+
23+
cmd_ = "printf '" target "' | sed -nE 's/" regexp "/" replacement "/" tolower(how) "p'";
24+
if (cmd_ | getline ret_ != 1) {
25+
close(cmd_);
26+
error = "ERROR: running command: " cmd_ ", ret_: " ret_;
27+
exit;
28+
}
29+
close(cmd_);
30+
return ret_;
31+
}
32+
1533
BEGIN {
1634
namespace = "Grpc";
1735
className = "";
1836
classDocComment = "";
19-
delete methods; # methods[method][doc|args|static]
20-
delete constants; # constants[i][name|doc]
37+
38+
delete methodNames; # i => methodName
39+
delete methodArgs; # methodName => concatenatedArgsStr
40+
delete methodDocs; # methodName => methodDocCommentStr
41+
delete methodStatics; # methodName => 1 if static
42+
methodsCount = 0;
43+
44+
delete constantNames; # i => constantName
45+
delete constantDocs; # constantName => constantDocCommentStr
2146
constantsCount = 0;
2247

2348
# * class className
24-
classLineRegex = "^ \\* class (\\S+)$";
49+
classLineRegex = "^ \\* class ([^ \t]+)$";
2550
# @param type name [= default]
26-
paramLineRegex = "^.*@param\\s+\\S+\\s+(\\$\\S+(\\s+=\\s+\\S+)?)\\s+.*$";
51+
paramLineRegex = "^.*@param[ \t]+[^ \t]+[ \t]+(\\$[^ \t]+([ \t]+=[ \t]+[^ \t]+)?)[ \t]+.*$";
2752
# PHP_METHOD(class, function)
28-
phpMethodLineRegex = "^PHP_METHOD\\((\\S+),\\s*(\\S+)\\).*$";
53+
phpMethodLineRegex = "^PHP_METHOD\\(([^ \t]+),[ \t]*([^ \t]+)\\).*$";
2954

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

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

3661
error = "";
3762

3863
# extension testing methods
3964
hideMethods["Channel::getChannelInfo"] = 1;
4065
hideMethods["Channel::cleanPersistentList"] = 1;
4166
hideMethods["Channel::getPersistentList"] = 1;
42-
4367
}
4468

4569
# '/**' document comment start
46-
/^\s*\/\*\*/ {
70+
/^[ \t]*\/\*\*/ {
4771
inDocComment = 1;
4872
docComment = "";
4973
delete args;
@@ -57,20 +81,19 @@ inDocComment==1 {
5781

5882
# class document, must match ' * class <clasName>'
5983
inDocComment==1 && $0 ~ classLineRegex {
60-
className = gensub(classLineRegex, "\\1", "g");
84+
className = sed_gensub(classLineRegex, "\\1", "g");
6185
}
6286

6387
# end of class document
64-
inDocComment==1 && /\*\// && classDocComment == "" {
88+
inDocComment==1 && /\*\// && className && classDocComment == "" {
6589
classDocComment = docComment;
6690
docComment = "";
6791
}
6892

6993
# param line
7094
inDocComment==1 && $0 ~ paramLineRegex {
71-
arg = gensub(paramLineRegex, "\\1", "g");
72-
args[argsCount]=arg;
73-
argsCount++;
95+
arg = sed_gensub(paramLineRegex, "\\1", "g");
96+
args[argsCount++]=arg;
7497
}
7598

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

81104
# PHP_METHOD
82105
$0 ~ phpMethodLineRegex {
83-
class = gensub(phpMethodLineRegex, "\\1", "g");
106+
class = sed_gensub(phpMethodLineRegex, "\\1", "g");
84107
if (class != className) {
85108
error = "ERROR: Missing or mismatch class names, in class comment block: " \
86109
className ", in PHP_METHOD: " class;
87110
exit;
88111
};
89112

90-
method = gensub(phpMethodLineRegex, "\\2", "g");
91-
methods[method]["doc"] = docComment;
92-
for (i in args) {
93-
methods[method]["args"][i] = args[i];
113+
method = sed_gensub(phpMethodLineRegex, "\\2", "g");
114+
methodNames[methodsCount++] = method;
115+
methodDocs[method] = docComment;
116+
117+
# concat args
118+
if (argsCount > 0) {
119+
methodArgs[method] = args[0];
120+
for (i = 1; i < argsCount; i++) {
121+
methodArgs[method] = methodArgs[method] ", " args[i];
122+
}
94123
}
124+
95125
docComment = "";
96126
}
97127

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

102-
class = gensub(phpMeLineRegex, "\\1", "g");
132+
class = sed_gensub(phpMeLineRegex, "\\1", "g");
103133
if (class != className) {
104134
error = "ERROR: Missing or mismatch class names, in class comment block: " \
105135
className ", in PHP_ME: " class;
106136
exit;
107137
};
108-
method = gensub(phpMeLineRegex, "\\2", "g");
138+
method = sed_gensub(phpMeLineRegex, "\\2", "g");
109139
}
110140

111141
# ZEND_ACC_STATIC
112-
inPhpMe && /ZEND_ACC_STATIC/ {
113-
methods[method]["static"] = 1;
142+
inPhpMe && /ZEND_ACC_STATIC/ {
143+
methodStatics[method] = 1;
114144
}
115145

116146
# closing bracet of PHP_ME(...)
@@ -121,14 +151,13 @@ iinPhpMe && /\)$/ {
121151
# REGISTER_LONG_CONSTANT(constant, ...
122152
$0 ~ phpConstantLineRegs {
123153
inPhpConstant = 1;
124-
constant = gensub(phpConstantLineRegs, "\\1", "g");
125-
constants[constantsCount]["name"] = constant;
126-
constants[constantsCount]["doc"] = docComment;
127-
constantsCount++;
154+
constant = sed_gensub(phpConstantLineRegs, "\\1", "g");
155+
constantNames[constantsCount++] = constant;
156+
constantDocs[constant] = docComment;
128157
}
129158

130-
# closing bracet of PHP_ME(...)
131-
inPhpConstant && /\)\s*;\s*$/ {
159+
# closing bracet of REGISTER_LONG_CONSTANT(...)
160+
inPhpConstant && /\)[ \t]*;[ \t]*$/ {
132161
inPhpConstant = 0;
133162
docComment = "";
134163
}
@@ -145,27 +174,23 @@ END {
145174
if (className != "") {
146175
print classDocComment
147176
print "class " className " {";
148-
for (m in methods) {
177+
for (i = 0; i < methodsCount; i++) {
178+
m = methodNames[i];
149179
if (hideMethods[className"::"m]) continue;
150180

151-
print methods[m]["doc"];
181+
print methodDocs[m];
152182
printf "public"
153-
if (methods[m]["static"]) printf " static"
183+
if (methodStatics[m]) printf " static"
154184
printf " function " m "("
155-
if (isarray(methods[m]["args"])) {
156-
printf methods[m]["args"][0];
157-
for (i = 1; i < length(methods[m]["args"]); i++) {
158-
printf ", " methods[m]["args"][i];
159-
}
160-
}
185+
printf methodArgs[m];
161186
print ") {}";
162187
}
163188
print "\n}";
164189
}
165190

166-
for (i in constants) {
167-
print constants[i]["doc"];
168-
print "const " constants[i]["name"] " = 0;";
191+
for (i = 0; i < constantsCount; i++) {
192+
print constantDocs[constantNames[i]];
193+
print "const " constantNames[i] " = 0;";
169194
}
170195

171196
print "\n}";

src/php/bin/php_extension_to_php_doc.sh

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
set -euo pipefail
1818

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

@@ -28,10 +28,11 @@ COMMAND="${1:-}"
2828
# parse class and methods
2929
for FILENAME in call_credentials.c call.c channel.c channel_credentials.c \
3030
server_credentials.c server.c timeval.c ; do
31-
CLASS_NAME=$(sed -r 's/(^|_)(\w)/\U\2/g' <<< "${FILENAME%.*}")
31+
CLASS_NAME=$(awk -F _ '{for(i=1; i<=NF; i++) printf "%s", toupper(substr($i,1,1)) substr($i, 2);}' \
32+
<<< "${FILENAME%.*}")
3233
if [[ "$COMMAND" == "generate" ]]; then
3334
echo Generating lib/Grpc/$CLASS_NAME.php ...
34-
gawk -f php_extension_doxygen_filter.awk ../ext/grpc/$FILENAME \
35+
awk -f php_extension_doxygen_filter.awk ../ext/grpc/$FILENAME \
3536
> ../lib/Grpc/$CLASS_NAME.php
3637
elif [[ "$COMMAND" == "cleanup" ]]; then
3738
rm ../lib/Grpc/$CLASS_NAME.php
@@ -44,7 +45,7 @@ done
4445
# parse constants
4546
if [[ "$COMMAND" == "generate" ]]; then
4647
echo Generating lib/Grpc/Constants.php ...
47-
gawk -f php_extension_doxygen_filter.awk ../ext/grpc/php_grpc.c \
48+
awk -f php_extension_doxygen_filter.awk ../ext/grpc/php_grpc.c \
4849
> ../lib/Grpc/Constants.php
4950
elif [[ "$COMMAND" == "cleanup" ]]; then
5051
rm ../lib/Grpc/Constants.php

templates/tools/dockerfile/test/sanity/Dockerfile.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

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

2525
# Make Python 3.7 the default Python 3 version

tools/dockerfile/test/sanity/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ RUN apt-get update && apt-get -y install libgflags-dev libgtest-dev libc++-dev c
6767

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

7373
# Make Python 3.7 the default Python 3 version

0 commit comments

Comments
 (0)