Debian Codegraph @

src/bin/psql/gen_tabcomplete.pl

raw
1 #----------------------------------------------------------------------
2 #
3 # gen_tabcomplete.pl
4 # Perl script that transforms tab-complete.in.c to tab-complete.c.
5 #
6 # This script converts a C else-if chain into a switch statement.
7 # The else-if statements to be processed must appear at single-tab-stop
8 # indentation between lines reading
9 # /* BEGIN GEN_TABCOMPLETE */
10 # /* END GEN_TABCOMPLETE */
11 # The first clause in each if-condition must be a call of one of the
12 # functions Matches, HeadMatches, TailMatches, MatchesCS, HeadMatchesCS,
13 # or TailMatchesCS. Its argument(s) must be string literals or macros
14 # that expand to string literals or NULL. These clauses are removed from
15 # the code and replaced by "break; case N:", where N is a unique number
16 # for each such case label.
17 # The BEGIN GEN_TABCOMPLETE and END GEN_TABCOMPLETE lines are replaced
18 # by "switch (pattern_id) {" and "}" wrapping to make a valid switch.
19 # The remainder of the code is copied verbatim.
20 #
21 # An if-condition can also be an OR ("||") of several *Matches function
22 # calls, or it can be an AND ("&&") of a *Matches call with some other
23 # condition. For example,
24 #
25 # else if (HeadMatches("DROP", "DATABASE") && ends_with(prev_wd, '('))
26 #
27 # will be transformed to
28 #
29 # break;
30 # case N:
31 # if (ends_with(prev_wd, '('))
32 #
33 # In addition, there must be one input line that reads
34 # /* Insert tab-completion pattern data here. */
35 # This line is replaced in the output file by macro calls, one for each
36 # replaced match condition. The output for the above example would be
37 # TCPAT(N, HeadMatch, "DROP", "DATABASE"),
38 # where N is the replacement case label, "HeadMatch" is the original
39 # function name minus "es", and the rest are the function arguments.
40 # The tab-completion data line must appear before BEGIN GEN_TABCOMPLETE.
41 #
42 #
43 # Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
44 # Portions Copyright (c) 1994, Regents of the University of California
45 #
46 # src/bin/psql/gen_tabcomplete.pl
47 #
48 #----------------------------------------------------------------------
49
50 use strict;
51 use warnings FATAL => 'all';
52 use Getopt::Long;
53
54 my $outfile = '';
55
56 GetOptions('outfile=s' => \$outfile) or die "$0: wrong arguments";
57
58 my $infile = $ARGV[0];
59 open my $infh, '<', $infile
60 or die "$0: could not open input file '$infile': $!\n";
61
62 my $outfh;
63 if ($outfile)
64 {
65 open $outfh, '>', $outfile
66 or die "$0: could not open output file '$outfile': $!\n";
67 }
68 else
69 {
70 $outfh = *STDOUT;
71 }
72
73 # Opening boilerplate for output file.
74 printf $outfh <<EOM;
75 /*-------------------------------------------------------------------------
76 *
77 * tab-complete.c
78 * Preprocessed tab-completion code.
79 *
80 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
81 * Portions Copyright (c) 1994, Regents of the University of California
82 *
83 * NOTES
84 * ******************************
85 * *** DO NOT EDIT THIS FILE! ***
86 * ******************************
87 *
88 * It has been GENERATED by src/bin/psql/gen_tabcomplete.pl
89 *
90 *-------------------------------------------------------------------------
91 */
92
93 #define SWITCH_CONVERSION_APPLIED
94
95 #line 1 "${infile}"
96 EOM
97
98 # Scan input file until we find the data-replacement label line.
99 # Dump what we scan directly into the output file.
100 while (<$infh>)
101 {
102 chomp;
103 last if m|^\s*/\* Insert tab-completion pattern data here\. \*/\s*$|;
104 print $outfh "$_\n";
105 }
106
107 # $table_data collects what we will substitute for the "pattern data" line.
108 my $table_data = '';
109 # $output_code collects code that we can't emit till after $table_data.
110 my $output_code = '';
111 # last case label assigned
112 my $last_case_label = 0;
113
114 # We emit #line directives to keep the output file's line numbering in sync
115 # with the line numbering of the original, to simplify compiler error message
116 # reading and debugging.
117 my $next_line_no = $. + 1;
118 $output_code .= "#line ${next_line_no} \"${infile}\"\n";
119
120 # Scan until we find the BEGIN GEN_TABCOMPLETE line.
121 # Add the scanned code to $output_code verbatim.
122 while (<$infh>)
123 {
124 chomp;
125 last if m|^\s*/\* BEGIN GEN_TABCOMPLETE \*/\s*$|;
126 $output_code .= $_ . "\n";
127 }
128
129 # Emit the switch-starting lines.
130 $output_code .= "\tswitch (pattern_id)\n";
131 $output_code .= "\t{\n";
132
133 # Keep line numbering in sync.
134 $next_line_no = $. + 1;
135 $output_code .= "#line ${next_line_no} \"${infile}\"\n";
136
137 # Scan input file, collecting outer-level else-if conditions
138 # to pass to process_else_if.
139 # Lines that aren't else-if conditions go to $output_code verbatim.
140 # True if we're handling a multiline else-if condition
141 my $in_else_if = 0;
142 # The accumulated line
143 my $else_if_line;
144 my $else_if_lineno;
145
146 while (<$infh>)
147 {
148 chomp;
149 last if m|^\s*/\* END GEN_TABCOMPLETE \*/\s*$|;
150 if ($in_else_if)
151 {
152 my $rest = $_;
153 # collapse leading whitespace
154 $rest =~ s/^\s+//;
155 $else_if_line .= ' ' . $rest;
156 # Double right paren is currently sufficient to detect completion
157 if ($else_if_line =~ m/\)\)$/)
158 {
159 process_else_if($else_if_line, $else_if_lineno, $.);
160 $in_else_if = 0;
161 }
162 }
163 elsif (m/^\telse if \(/)
164 {
165 $else_if_line = $_;
166 $else_if_lineno = $.;
167 # Double right paren is currently sufficient to detect completion
168 if ($else_if_line =~ m/\)\)$/)
169 {
170 process_else_if($else_if_line, $else_if_lineno, $.);
171 }
172 else
173 {
174 $in_else_if = 1;
175 }
176 }
177 else
178 {
179 $output_code .= $_ . "\n";
180 }
181 }
182
183 die "unfinished else-if" if $in_else_if;
184
185 # Emit the switch-ending lines.
186 $output_code .= "\tbreak;\n";
187 $output_code .= "\tdefault:\n";
188 $output_code .= "\t\tAssert(false);\n";
189 $output_code .= "\t\tbreak;\n";
190 $output_code .= "\t}\n";
191
192 # Keep line numbering in sync.
193 $next_line_no = $. + 1;
194 $output_code .= "#line ${next_line_no} \"${infile}\"\n";
195
196 # Scan the rest, adding it to $output_code verbatim.
197 while (<$infh>)
198 {
199 chomp;
200 $output_code .= $_ . "\n";
201 }
202
203 # Dump out the table data.
204 print $outfh $table_data;
205
206 # Dump out the modified code, and we're done!
207 print $outfh $output_code;
208
209 close($infh);
210 close($outfh);
211
212 # Disassemble an else-if condition.
213 # Add the generated table-contents macro(s) to $table_data,
214 # and add the replacement case label(s) to $output_code.
215 sub process_else_if
216 {
217 my ($else_if_line, $else_if_lineno, $end_lineno) = @_;
218
219 # Strip the initial "else if (", which we know is there
220 $else_if_line =~ s/^\telse if \(//;
221
222 # Handle OR'd conditions
223 my $isfirst = 1;
224 while ($else_if_line =~
225 s/^(Head|Tail|)Matches(CS|)\((("[^"]*"|MatchAnyExcept\("[^"]*"\)|[A-Za-z,\s])+)\)\s*\|\|\s*//
226 )
227 {
228 my $typ = $1;
229 my $cs = $2;
230 my $args = $3;
231 process_match($typ, $cs, $args, $else_if_lineno, $isfirst);
232 $isfirst = 0;
233 }
234
235 # Check for AND'd condition
236 if ($else_if_line =~
237 s/^(Head|Tail|)Matches(CS|)\((("[^"]*"|MatchAnyExcept\("[^"]*"\)|[A-Za-z,\s])+)\)\s*&&\s*//
238 )
239 {
240 my $typ = $1;
241 my $cs = $2;
242 my $args = $3;
243 warn
244 "could not process OR/ANDed if condition at line $else_if_lineno\n"
245 if !$isfirst;
246 process_match($typ, $cs, $args, $else_if_lineno, $isfirst);
247 $isfirst = 0;
248 # approximate line positioning of AND'd condition
249 $output_code .= "#line ${end_lineno} \"${infile}\"\n";
250 $output_code .= "\tif ($else_if_line\n";
251 }
252 elsif ($else_if_line =~
253 s/^(Head|Tail|)Matches(CS|)\((("[^"]*"|MatchAnyExcept\("[^"]*"\)|[A-Za-z,\s])+)\)\)$//
254 )
255 {
256 my $typ = $1;
257 my $cs = $2;
258 my $args = $3;
259 process_match($typ, $cs, $args, $else_if_lineno, $isfirst);
260 $isfirst = 0;
261 }
262 else
263 {
264 warn
265 "could not process if condition at line $else_if_lineno: the rest looks like $else_if_line\n";
266 $output_code .= "\telse if ($else_if_line\n";
267 }
268
269 # Keep line numbering in sync.
270 if ($end_lineno != $else_if_lineno)
271 {
272 my $next_lineno = $end_lineno + 1;
273 $output_code .= "#line ${next_lineno} \"${infile}\"\n";
274 }
275 }
276
277 sub process_match
278 {
279 my ($typ, $cs, $args, $lineno, $isfirst) = @_;
280
281 # Assign a new case label only for the first pattern in an OR group.
282 if ($isfirst)
283 {
284 $last_case_label++;
285
286 # We intentionally keep the "break;" and the "case" on one line, so
287 # that they have the same line number as the original "else if"'s
288 # first line. This avoids misleading displays in, e.g., lcov.
289 $output_code .= "\t";
290 $output_code .= "break; " if $last_case_label > 1;
291 $output_code .= "case $last_case_label:\n";
292 }
293
294 $table_data .=
295 "\tTCPAT(${last_case_label}, ${typ}Match${cs}, ${args}),\n";
296 }
297
298
299 sub usage
300 {
301 die <<EOM;
302 Usage: gen_tabcomplete.pl [--outfile/-o <path>] input_file
303 --outfile Output file (default is stdout)
304
305 gen_tabcomplete.pl transforms tab-complete.in.c to tab-complete.c.
306 EOM
307 }