12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
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
+
15
33
BEGIN {
16
34
namespace = " Grpc" ;
17
35
className = " " ;
18
36
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
21
46
constantsCount = 0 ;
22
47
23
48
# * class className
24
- classLineRegex = " ^ \\ * class (\\ S +)$" ;
49
+ classLineRegex = " ^ \\ * class ([^ \t ] +)$" ;
25
50
# @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 ] +.*$" ;
27
52
# PHP_METHOD(class, function)
28
- phpMethodLineRegex = " ^PHP_METHOD\\ ((\\ S +),\\ s*( \\ S +)\\ ).*$" ;
53
+ phpMethodLineRegex = " ^PHP_METHOD\\ (([^ \t ] +),[ \t ]*([^ \t ] +)\\ ).*$" ;
29
54
30
55
# PHP_ME(class, function, arginfo, flags)
31
- phpMeLineRegex = " ^\\ s *PHP_ME\\ ((\\ S +),\\ s*( \\ S +),.*$" ;
56
+ phpMeLineRegex = " ^[ \t ] *PHP_ME\\ (([^ \t ] +),[ \t ]*([^ \t ] +),.*$" ;
32
57
33
58
# REGISTER_LONG_CONSTANT("namespace\\constant", grpcConstant, ..)
34
- phpConstantLineRegs = " ^\\ s *REGISTER_LONG_CONSTANT\\ (\" Grpc\\\\\\\\ (\\ S +)\" ,.*$" ;
59
+ phpConstantLineRegs = " ^[ \t ] *REGISTER_LONG_CONSTANT\\ (\" Grpc\\\\\\\\ ([^ \t ] +)\" ,.*$" ;
35
60
36
61
error = " " ;
37
62
38
63
# extension testing methods
39
64
hideMethods[ " Channel::getChannelInfo" ] = 1 ;
40
65
hideMethods[ " Channel::cleanPersistentList" ] = 1 ;
41
66
hideMethods[ " Channel::getPersistentList" ] = 1 ;
42
-
43
67
}
44
68
45
69
# '/**' document comment start
46
- /^ \s * \/\*\* / {
70
+ /^ [ \t ] * \/\*\* / {
47
71
inDocComment = 1 ;
48
72
docComment = " " ;
49
73
delete args;
@@ -57,20 +81,19 @@ inDocComment==1 {
57
81
58
82
# class document, must match ' * class <clasName>'
59
83
inDocComment== 1 && $0 ~ classLineRegex {
60
- className = gensub (classLineRegex, " \\ 1" , " g" );
84
+ className = sed_gensub (classLineRegex, " \\ 1" , " g" );
61
85
}
62
86
63
87
# end of class document
64
- inDocComment== 1 && /\*\/ / && classDocComment == " " {
88
+ inDocComment== 1 && /\*\/ / && className && classDocComment == " " {
65
89
classDocComment = docComment;
66
90
docComment = " " ;
67
91
}
68
92
69
93
# param line
70
94
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;
74
97
}
75
98
76
99
# '*/' document comment end
@@ -80,37 +103,44 @@ inDocComment==1 && /\*\// {
80
103
81
104
# PHP_METHOD
82
105
$0 ~ phpMethodLineRegex {
83
- class = gensub (phpMethodLineRegex, " \\ 1" , " g" );
106
+ class = sed_gensub (phpMethodLineRegex, " \\ 1" , " g" );
84
107
if (class != className) {
85
108
error = " ERROR: Missing or mismatch class names, in class comment block: " \
86
109
className " , in PHP_METHOD: " class;
87
110
exit ;
88
111
};
89
112
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
+ }
94
123
}
124
+
95
125
docComment = " " ;
96
126
}
97
127
98
128
# PHP_ME(class, function,...
99
129
$0 ~ phpMeLineRegex {
100
130
inPhpMe = 1 ;
101
131
102
- class = gensub (phpMeLineRegex, " \\ 1" , " g" );
132
+ class = sed_gensub (phpMeLineRegex, " \\ 1" , " g" );
103
133
if (class != className) {
104
134
error = " ERROR: Missing or mismatch class names, in class comment block: " \
105
135
className " , in PHP_ME: " class;
106
136
exit ;
107
137
};
108
- method = gensub (phpMeLineRegex, " \\ 2" , " g" );
138
+ method = sed_gensub (phpMeLineRegex, " \\ 2" , " g" );
109
139
}
110
140
111
141
# ZEND_ACC_STATIC
112
- inPhpMe && /ZEND_ACC_STATIC / {
113
- methods [ method][ " static " ] = 1 ;
142
+ inPhpMe && /ZEND_ACC_STATIC / {
143
+ methodStatics [ method] = 1 ;
114
144
}
115
145
116
146
# closing bracet of PHP_ME(...)
@@ -121,14 +151,13 @@ iinPhpMe && /\)$/ {
121
151
# REGISTER_LONG_CONSTANT(constant, ...
122
152
$0 ~ phpConstantLineRegs {
123
153
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;
128
157
}
129
158
130
- # closing bracet of PHP_ME (...)
131
- inPhpConstant && /\) \s * ; \s * $ / {
159
+ # closing bracet of REGISTER_LONG_CONSTANT (...)
160
+ inPhpConstant && /\) [ \t ] * ;[ \t ] * $ / {
132
161
inPhpConstant = 0 ;
133
162
docComment = " " ;
134
163
}
@@ -145,27 +174,23 @@ END {
145
174
if (className != " " ) {
146
175
print classDocComment
147
176
print " class " className " {" ;
148
- for (m in methods) {
177
+ for (i = 0 ; i < methodsCount; i++ ) {
178
+ m = methodNames[ i] ;
149
179
if (hideMethods[ className" ::" m] ) continue ;
150
180
151
- print methods [ m ][ " doc " ] ;
181
+ print methodDocs [ m ] ;
152
182
printf " public"
153
- if (methods [ m ][ " static " ] ) printf " static"
183
+ if (methodStatics [ m ] ) printf " static"
154
184
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] ;
161
186
print " ) {}" ;
162
187
}
163
188
print " \n }" ;
164
189
}
165
190
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;" ;
169
194
}
170
195
171
196
print " \n }" ;
0 commit comments