| 1 |
#!/usr/bin/perl |
| 2 |
# src/interfaces/ecpg/preproc/parse.pl |
| 3 |
# parser generator for ecpg |
| 4 |
# |
| 5 |
# See README.parser for some explanation of what this does. |
| 6 |
# |
| 7 |
# Command-line options: |
| 8 |
# --srcdir: where to find ecpg-provided input files (default ".") |
| 9 |
# --parser: the backend gram.y file to read (required, no default) |
| 10 |
# --output: where to write preproc.y (required, no default) |
| 11 |
# |
| 12 |
# Copyright (c) 2007-2025, PostgreSQL Global Development Group |
| 13 |
# |
| 14 |
# Written by Mike Aubury <[email protected]> |
| 15 |
# Michael Meskes <[email protected]> |
| 16 |
# Andy Colson <[email protected]> |
| 17 |
# |
| 18 |
# Placed under the same license as PostgreSQL. |
| 19 |
# |
| 20 |
|
| 21 |
use strict; |
| 22 |
use warnings FATAL => 'all'; |
| 23 |
use Getopt::Long; |
| 24 |
|
| 25 |
my $srcdir = '.'; |
| 26 |
my $outfile = ''; |
| 27 |
my $parser = ''; |
| 28 |
|
| 29 |
GetOptions( |
| 30 |
'srcdir=s' => \$srcdir, |
| 31 |
'output=s' => \$outfile, |
| 32 |
'parser=s' => \$parser,) or die "wrong arguments"; |
| 33 |
|
| 34 |
|
| 35 |
# These hash tables define additional transformations to apply to |
| 36 |
# grammar rules. For bug-detection purposes, we count usages of |
| 37 |
# each hash table entry in a second hash table, and verify that |
| 38 |
# all the entries get used. |
| 39 |
|
| 40 |
# Substitutions to apply to tokens whenever they are seen in a rule. |
| 41 |
my %replace_token = ( |
| 42 |
'BCONST' => 'ecpg_bconst', |
| 43 |
'FCONST' => 'ecpg_fconst', |
| 44 |
'Sconst' => 'ecpg_sconst', |
| 45 |
'XCONST' => 'ecpg_xconst', |
| 46 |
'IDENT' => 'ecpg_ident', |
| 47 |
'PARAM' => 'ecpg_param',); |
| 48 |
|
| 49 |
my %replace_token_used; |
| 50 |
|
| 51 |
# This hash can provide a result type to override "void" for nonterminals |
| 52 |
# that need that, or it can specify 'ignore' to cause us to skip the rule |
| 53 |
# for that nonterminal. (In either case, ecpg.trailer had better provide |
| 54 |
# a substitute rule, since the default won't do.) |
| 55 |
my %replace_types = ( |
| 56 |
'PrepareStmt' => '<prep>', |
| 57 |
'ExecuteStmt' => '<exec>', |
| 58 |
'opt_array_bounds' => '<index>', |
| 59 |
|
| 60 |
# "ignore" means: do not create type and rules for this nonterminal |
| 61 |
'parse_toplevel' => 'ignore', |
| 62 |
'stmtmulti' => 'ignore', |
| 63 |
'CreateAsStmt' => 'ignore', |
| 64 |
'DeallocateStmt' => 'ignore', |
| 65 |
'ColId' => 'ignore', |
| 66 |
'type_function_name' => 'ignore', |
| 67 |
'ColLabel' => 'ignore', |
| 68 |
'Sconst' => 'ignore', |
| 69 |
'opt_distinct_clause' => 'ignore', |
| 70 |
'PLpgSQL_Expr' => 'ignore', |
| 71 |
'PLAssignStmt' => 'ignore', |
| 72 |
'plassign_target' => 'ignore', |
| 73 |
'plassign_equals' => 'ignore',); |
| 74 |
|
| 75 |
my %replace_types_used; |
| 76 |
|
| 77 |
# This hash provides an "ignore" option or substitute expansion for any |
| 78 |
# rule or rule alternative. The hash key is the same "tokenlist" tag |
| 79 |
# used for lookup in ecpg.addons. |
| 80 |
my %replace_line = ( |
| 81 |
# These entries excise certain keywords from the core keyword lists. |
| 82 |
# Be sure to account for these in ColLabel and related productions. |
| 83 |
'unreserved_keyword CONNECTION' => 'ignore', |
| 84 |
'unreserved_keyword CURRENT_P' => 'ignore', |
| 85 |
'unreserved_keyword DAY_P' => 'ignore', |
| 86 |
'unreserved_keyword HOUR_P' => 'ignore', |
| 87 |
'unreserved_keyword INPUT_P' => 'ignore', |
| 88 |
'unreserved_keyword MINUTE_P' => 'ignore', |
| 89 |
'unreserved_keyword MONTH_P' => 'ignore', |
| 90 |
'unreserved_keyword SECOND_P' => 'ignore', |
| 91 |
'unreserved_keyword YEAR_P' => 'ignore', |
| 92 |
'col_name_keyword CHAR_P' => 'ignore', |
| 93 |
'col_name_keyword INT_P' => 'ignore', |
| 94 |
'col_name_keyword VALUES' => 'ignore', |
| 95 |
'reserved_keyword TO' => 'ignore', |
| 96 |
'reserved_keyword UNION' => 'ignore', |
| 97 |
|
| 98 |
# some other production rules have to be ignored or replaced |
| 99 |
'fetch_args FORWARD opt_from_in cursor_name' => 'ignore', |
| 100 |
'fetch_args BACKWARD opt_from_in cursor_name' => 'ignore', |
| 101 |
"opt_array_bounds opt_array_bounds '[' Iconst ']'" => 'ignore', |
| 102 |
'VariableShowStmt SHOW var_name' => 'SHOW var_name ecpg_into', |
| 103 |
'VariableShowStmt SHOW TIME ZONE' => 'SHOW TIME ZONE ecpg_into', |
| 104 |
'VariableShowStmt SHOW TRANSACTION ISOLATION LEVEL' => |
| 105 |
'SHOW TRANSACTION ISOLATION LEVEL ecpg_into', |
| 106 |
'VariableShowStmt SHOW SESSION AUTHORIZATION' => |
| 107 |
'SHOW SESSION AUTHORIZATION ecpg_into', |
| 108 |
'returning_clause RETURNING returning_with_clause target_list' => |
| 109 |
'RETURNING returning_with_clause target_list opt_ecpg_into', |
| 110 |
'ExecuteStmt EXECUTE name execute_param_clause' => |
| 111 |
'EXECUTE prepared_name execute_param_clause execute_rest', |
| 112 |
'ExecuteStmt CREATE OptTemp TABLE create_as_target AS EXECUTE name execute_param_clause opt_with_data' |
| 113 |
=> 'CREATE OptTemp TABLE create_as_target AS EXECUTE prepared_name execute_param_clause opt_with_data execute_rest', |
| 114 |
'ExecuteStmt CREATE OptTemp TABLE IF_P NOT EXISTS create_as_target AS EXECUTE name execute_param_clause opt_with_data' |
| 115 |
=> 'CREATE OptTemp TABLE IF_P NOT EXISTS create_as_target AS EXECUTE prepared_name execute_param_clause opt_with_data execute_rest', |
| 116 |
'PrepareStmt PREPARE name prep_type_clause AS PreparableStmt' => |
| 117 |
'PREPARE prepared_name prep_type_clause AS PreparableStmt', |
| 118 |
'var_name ColId' => 'ECPGColId'); |
| 119 |
|
| 120 |
my %replace_line_used; |
| 121 |
|
| 122 |
|
| 123 |
# Declare assorted state variables. |
| 124 |
|
| 125 |
# yaccmode counts the '%%' separator lines we have seen, so that we can |
| 126 |
# distinguish prologue, rules, and epilogue sections of gram.y. |
| 127 |
my $yaccmode = 0; |
| 128 |
# in /* ... */ comment? |
| 129 |
my $comment = 0; |
| 130 |
# in { ... } braced text? |
| 131 |
my $brace_indent = 0; |
| 132 |
# within a rule (production)? |
| 133 |
my $in_rule = 0; |
| 134 |
# count of alternatives processed within the current rule. |
| 135 |
my $alt_count = 0; |
| 136 |
# copymode = 1 when we want to emit the current rule to preproc.y. |
| 137 |
# If it's 0, we have decided to ignore the current rule, and should |
| 138 |
# skip all output until we get to the ending semicolon. |
| 139 |
my $copymode = 0; |
| 140 |
# tokenmode = 1 indicates we are processing %token and following declarations. |
| 141 |
my $tokenmode = 0; |
| 142 |
# stmt_mode = 1 indicates that we are processing the 'stmt:' rule. |
| 143 |
my $stmt_mode = 0; |
| 144 |
# Hacky state for emitting feature-not-supported warnings. |
| 145 |
my $has_feature_not_supported = 0; |
| 146 |
my $has_if_command = 0; |
| 147 |
|
| 148 |
# %addons holds the rules loaded from ecpg.addons. |
| 149 |
my %addons; |
| 150 |
|
| 151 |
# %buff holds various named "buffers", which are just strings that accumulate |
| 152 |
# the output destined for different sections of the preproc.y file. This |
| 153 |
# allows us to process the input in one pass even though the resulting output |
| 154 |
# needs to appear in various places. See dump_buffer calls below for the |
| 155 |
# set of buffer names and the order in which they'll be dumped. |
| 156 |
my %buff; |
| 157 |
|
| 158 |
# %tokens contains an entry for every name we have discovered to be a token. |
| 159 |
my %tokens; |
| 160 |
|
| 161 |
# $non_term_id is the name of the nonterminal that is the target of the |
| 162 |
# current rule. |
| 163 |
my $non_term_id; |
| 164 |
|
| 165 |
# $line holds the reconstructed rule text (that is, RHS token list) that |
| 166 |
# we plan to emit for the current rule. |
| 167 |
my $line = ''; |
| 168 |
|
| 169 |
# count of tokens included in $line. |
| 170 |
my $line_count = 0; |
| 171 |
|
| 172 |
|
| 173 |
# Open parser / output file early, to raise errors early. |
| 174 |
open(my $parserfh, '<', $parser) or die "could not open parser file $parser"; |
| 175 |
open(my $outfh, '>', $outfile) or die "could not open output file $outfile"; |
| 176 |
|
| 177 |
# Read the various ecpg-supplied input files. |
| 178 |
# ecpg.addons is loaded into the %addons hash, while the other files |
| 179 |
# are just copied into buffers for verbatim output later. |
| 180 |
preload_addons(); |
| 181 |
include_file('header', 'ecpg.header'); |
| 182 |
include_file('tokens', 'ecpg.tokens'); |
| 183 |
include_file('ecpgtype', 'ecpg.type'); |
| 184 |
include_file('trailer', 'ecpg.trailer'); |
| 185 |
|
| 186 |
# Read gram.y, and do the bulk of the processing. |
| 187 |
main(); |
| 188 |
|
| 189 |
# Emit data from the various buffers we filled. |
| 190 |
dump_buffer('header'); |
| 191 |
dump_buffer('tokens'); |
| 192 |
dump_buffer('types'); |
| 193 |
dump_buffer('ecpgtype'); |
| 194 |
dump_buffer('orig_tokens'); |
| 195 |
print $outfh '%%', "\n"; |
| 196 |
print $outfh 'prog: statements;', "\n"; |
| 197 |
dump_buffer('rules'); |
| 198 |
dump_buffer('trailer'); |
| 199 |
|
| 200 |
close($parserfh); |
| 201 |
|
| 202 |
# Cross-check that we don't have dead or ambiguous addon rules. |
| 203 |
foreach (keys %addons) |
| 204 |
{ |
| 205 |
die "addon rule $_ was never used\n" if $addons{$_}{used} == 0; |
| 206 |
die "addon rule $_ was matched multiple times\n" if $addons{$_}{used} > 1; |
| 207 |
} |
| 208 |
|
| 209 |
# Likewise cross-check that entries in our internal hash tables match something. |
| 210 |
foreach (keys %replace_token) |
| 211 |
{ |
| 212 |
die "replace_token entry $_ was never used\n" |
| 213 |
if !defined($replace_token_used{$_}); |
| 214 |
# multiple use of a replace_token entry is fine |
| 215 |
} |
| 216 |
|
| 217 |
foreach (keys %replace_types) |
| 218 |
{ |
| 219 |
die "replace_types entry $_ was never used\n" |
| 220 |
if !defined($replace_types_used{$_}); |
| 221 |
die "replace_types entry $_ was matched multiple times\n" |
| 222 |
if $replace_types_used{$_} > 1; |
| 223 |
} |
| 224 |
|
| 225 |
foreach (keys %replace_line) |
| 226 |
{ |
| 227 |
die "replace_line entry $_ was never used\n" |
| 228 |
if !defined($replace_line_used{$_}); |
| 229 |
die "replace_line entry $_ was matched multiple times\n" |
| 230 |
if $replace_line_used{$_} > 1; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
# Read the backend grammar. |
| 235 |
sub main |
| 236 |
{ |
| 237 |
line: while (<$parserfh>) |
| 238 |
{ |
| 239 |
chomp; |
| 240 |
|
| 241 |
if (/^%%/) |
| 242 |
{ |
| 243 |
# New file section, so advance yaccmode. |
| 244 |
$yaccmode++; |
| 245 |
# We are no longer examining %token and related commands. |
| 246 |
$tokenmode = 0; |
| 247 |
# Shouldn't be anything else on the line. |
| 248 |
next line; |
| 249 |
} |
| 250 |
|
| 251 |
# Hacky check for rules that throw FEATURE_NOT_SUPPORTED |
| 252 |
# (do this before $_ has a chance to get clobbered) |
| 253 |
if ($yaccmode == 1) |
| 254 |
{ |
| 255 |
$has_feature_not_supported = 1 if /ERRCODE_FEATURE_NOT_SUPPORTED/; |
| 256 |
$has_if_command = 1 if /^\s*if/; |
| 257 |
} |
| 258 |
|
| 259 |
# Make sure any braces are split into separate fields |
| 260 |
s/{/ { /g; |
| 261 |
s/}/ } /g; |
| 262 |
|
| 263 |
# Likewise for comment start/end markers |
| 264 |
s|\/\*| /* |g; |
| 265 |
s|\*\/| */ |g; |
| 266 |
|
| 267 |
# Now split the line into individual fields |
| 268 |
my @arr = split(' '); |
| 269 |
|
| 270 |
# Ignore empty lines |
| 271 |
if (!@arr) |
| 272 |
{ |
| 273 |
next line; |
| 274 |
} |
| 275 |
|
| 276 |
# Once we have seen %token in the prologue, we assume all that follows |
| 277 |
# up to the '%%' separator is %token and associativity declarations. |
| 278 |
# Collect and process that as necessary. |
| 279 |
if ($arr[0] eq '%token' && $yaccmode == 0) |
| 280 |
{ |
| 281 |
$tokenmode = 1; |
| 282 |
} |
| 283 |
|
| 284 |
if ($tokenmode == 1) |
| 285 |
{ |
| 286 |
# Collect everything of interest on this line into $str. |
| 287 |
my $str = ''; |
| 288 |
for my $a (@arr) |
| 289 |
{ |
| 290 |
# Skip comments. |
| 291 |
if ($a eq '/*') |
| 292 |
{ |
| 293 |
$comment++; |
| 294 |
next; |
| 295 |
} |
| 296 |
if ($a eq '*/') |
| 297 |
{ |
| 298 |
$comment--; |
| 299 |
next; |
| 300 |
} |
| 301 |
if ($comment) |
| 302 |
{ |
| 303 |
next; |
| 304 |
} |
| 305 |
|
| 306 |
# If it's "<something>", it's a type in a %token declaration, |
| 307 |
# which we should just drop so that the tokens have void type. |
| 308 |
if (substr($a, 0, 1) eq '<') |
| 309 |
{ |
| 310 |
next; |
| 311 |
} |
| 312 |
|
| 313 |
# Remember that this is a token. This will also make entries |
| 314 |
# for "%token" and the associativity keywords such as "%left", |
| 315 |
# which should be harmless so it's not worth the trouble to |
| 316 |
# avoid it. If a token appears both in %token and in an |
| 317 |
# associativity declaration, we'll redundantly re-set its |
| 318 |
# entry, which is also OK. |
| 319 |
$tokens{$a} = 1; |
| 320 |
|
| 321 |
# Accumulate the line in $str. |
| 322 |
$str = $str . ' ' . $a; |
| 323 |
|
| 324 |
# Give our token CSTRING the same precedence as IDENT. |
| 325 |
if ($a eq 'IDENT' && $arr[0] eq '%nonassoc') |
| 326 |
{ |
| 327 |
$str = $str . " CSTRING"; |
| 328 |
} |
| 329 |
} |
| 330 |
# Save the lightly-processed line in orig_tokens. |
| 331 |
add_to_buffer('orig_tokens', $str); |
| 332 |
next line; |
| 333 |
} |
| 334 |
|
| 335 |
# The rest is only appropriate if we're in the rules section of gram.y |
| 336 |
if ($yaccmode != 1) |
| 337 |
{ |
| 338 |
next line; |
| 339 |
} |
| 340 |
|
| 341 |
# Go through each word of the rule in turn |
| 342 |
for ( |
| 343 |
my $fieldIndexer = 0; |
| 344 |
$fieldIndexer < scalar(@arr); |
| 345 |
$fieldIndexer++) |
| 346 |
{ |
| 347 |
# Detect and ignore comments and braced action text |
| 348 |
if ($arr[$fieldIndexer] eq '*/' && $comment) |
| 349 |
{ |
| 350 |
$comment = 0; |
| 351 |
next; |
| 352 |
} |
| 353 |
elsif ($comment) |
| 354 |
{ |
| 355 |
next; |
| 356 |
} |
| 357 |
elsif ($arr[$fieldIndexer] eq '/*') |
| 358 |
{ |
| 359 |
# start of a possibly-multiline comment |
| 360 |
$comment = 1; |
| 361 |
next; |
| 362 |
} |
| 363 |
elsif ($arr[$fieldIndexer] eq '}') |
| 364 |
{ |
| 365 |
$brace_indent--; |
| 366 |
next; |
| 367 |
} |
| 368 |
elsif ($arr[$fieldIndexer] eq '{') |
| 369 |
{ |
| 370 |
$brace_indent++; |
| 371 |
next; |
| 372 |
} |
| 373 |
if ($brace_indent > 0) |
| 374 |
{ |
| 375 |
next; |
| 376 |
} |
| 377 |
|
| 378 |
# OK, it's not a comment or part of an action. |
| 379 |
# Check for ';' ending the current rule, or '|' ending the |
| 380 |
# current alternative. |
| 381 |
if ($arr[$fieldIndexer] eq ';') |
| 382 |
{ |
| 383 |
if ($copymode) |
| 384 |
{ |
| 385 |
# Print the accumulated rule. |
| 386 |
emit_rule(); |
| 387 |
add_to_buffer('rules', ";\n\n"); |
| 388 |
} |
| 389 |
else |
| 390 |
{ |
| 391 |
# End of an ignored rule; revert to copymode = 1. |
| 392 |
$copymode = 1; |
| 393 |
} |
| 394 |
|
| 395 |
# Reset for the next rule. |
| 396 |
$line = ''; |
| 397 |
$line_count = 0; |
| 398 |
$in_rule = 0; |
| 399 |
$alt_count = 0; |
| 400 |
$has_feature_not_supported = 0; |
| 401 |
$has_if_command = 0; |
| 402 |
next; |
| 403 |
} |
| 404 |
|
| 405 |
if ($arr[$fieldIndexer] eq '|') |
| 406 |
{ |
| 407 |
if ($copymode) |
| 408 |
{ |
| 409 |
# Print the accumulated alternative. |
| 410 |
# Increment $alt_count for each non-ignored alternative. |
| 411 |
$alt_count += emit_rule(); |
| 412 |
} |
| 413 |
|
| 414 |
# Reset for the next alternative. |
| 415 |
# Start the next line with '|' if we've printed at least one |
| 416 |
# alternative. |
| 417 |
if ($alt_count > 1) |
| 418 |
{ |
| 419 |
$line = '| '; |
| 420 |
} |
| 421 |
else |
| 422 |
{ |
| 423 |
$line = ''; |
| 424 |
} |
| 425 |
$line_count = 0; |
| 426 |
$has_feature_not_supported = 0; |
| 427 |
$has_if_command = 0; |
| 428 |
next; |
| 429 |
} |
| 430 |
|
| 431 |
# Apply replace_token substitution if we have one. |
| 432 |
if (exists $replace_token{ $arr[$fieldIndexer] }) |
| 433 |
{ |
| 434 |
$replace_token_used{ $arr[$fieldIndexer] }++; |
| 435 |
$arr[$fieldIndexer] = $replace_token{ $arr[$fieldIndexer] }; |
| 436 |
} |
| 437 |
|
| 438 |
# Are we looking at a declaration of a non-terminal? |
| 439 |
# We detect that by seeing ':' on the end of the token or |
| 440 |
# as the next token. |
| 441 |
if (($arr[$fieldIndexer] =~ /[A-Za-z0-9]+:$/) |
| 442 |
|| ( $fieldIndexer + 1 < scalar(@arr) |
| 443 |
&& $arr[ $fieldIndexer + 1 ] eq ':')) |
| 444 |
{ |
| 445 |
# Extract the non-terminal, sans : if any |
| 446 |
$non_term_id = $arr[$fieldIndexer]; |
| 447 |
$non_term_id =~ tr/://d; |
| 448 |
|
| 449 |
# Consume the ':' if it's separate |
| 450 |
if (!($arr[$fieldIndexer] =~ /[A-Za-z0-9]+:$/)) |
| 451 |
{ |
| 452 |
$fieldIndexer++; |
| 453 |
} |
| 454 |
|
| 455 |
# Check for %replace_types entry indicating to ignore it. |
| 456 |
if (defined $replace_types{$non_term_id} |
| 457 |
&& $replace_types{$non_term_id} eq 'ignore') |
| 458 |
{ |
| 459 |
# We'll ignore this nonterminal and rule altogether. |
| 460 |
$replace_types_used{$non_term_id}++; |
| 461 |
$copymode = 0; |
| 462 |
next line; |
| 463 |
} |
| 464 |
|
| 465 |
# OK, we want this rule. |
| 466 |
$copymode = 1; |
| 467 |
|
| 468 |
# Set special mode for the "stmt:" rule. |
| 469 |
if ($non_term_id eq 'stmt') |
| 470 |
{ |
| 471 |
$stmt_mode = 1; |
| 472 |
} |
| 473 |
else |
| 474 |
{ |
| 475 |
$stmt_mode = 0; |
| 476 |
} |
| 477 |
|
| 478 |
# Emit appropriate %type declaration for this nonterminal, |
| 479 |
# if it has a type; otherwise omit that. |
| 480 |
if (defined $replace_types{$non_term_id}) |
| 481 |
{ |
| 482 |
my $tstr = |
| 483 |
'%type ' |
| 484 |
. $replace_types{$non_term_id} . ' ' |
| 485 |
. $non_term_id; |
| 486 |
add_to_buffer('types', $tstr); |
| 487 |
$replace_types_used{$non_term_id}++; |
| 488 |
} |
| 489 |
|
| 490 |
# Emit the target part of the rule. |
| 491 |
# Note: the leading space is just to match |
| 492 |
# the rather weird pre-v18 output logic. |
| 493 |
my $tstr = ' ' . $non_term_id . ':'; |
| 494 |
add_to_buffer('rules', $tstr); |
| 495 |
|
| 496 |
# Prepare for reading the tokens of the rule. |
| 497 |
$line = ''; |
| 498 |
$line_count = 0; |
| 499 |
die "unterminated rule at grammar line $.\n" |
| 500 |
if $in_rule; |
| 501 |
$in_rule = 1; |
| 502 |
$alt_count = 1; |
| 503 |
next; |
| 504 |
} |
| 505 |
elsif ($copymode) |
| 506 |
{ |
| 507 |
# Not a nonterminal declaration, so just add it to $line. |
| 508 |
$line = $line . ' ' . $arr[$fieldIndexer]; |
| 509 |
$line_count++; |
| 510 |
} |
| 511 |
} |
| 512 |
} |
| 513 |
die "unterminated rule at end of grammar\n" |
| 514 |
if $in_rule; |
| 515 |
return; |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
# append a file onto a buffer. |
| 520 |
# Arguments: buffer_name, filename (without path) |
| 521 |
sub include_file |
| 522 |
{ |
| 523 |
my ($buffer, $filename) = @_; |
| 524 |
my $full = "$srcdir/$filename"; |
| 525 |
open(my $fh, '<', $full) or die; |
| 526 |
while (<$fh>) |
| 527 |
{ |
| 528 |
chomp; |
| 529 |
add_to_buffer($buffer, $_); |
| 530 |
} |
| 531 |
close($fh); |
| 532 |
return; |
| 533 |
} |
| 534 |
|
| 535 |
# Emit the semantic action for the current rule. |
| 536 |
# This function mainly accounts for any modifications specified |
| 537 |
# by an ecpg.addons entry. |
| 538 |
sub emit_rule_action |
| 539 |
{ |
| 540 |
my ($tag) = @_; |
| 541 |
|
| 542 |
# See if we have an addons entry; if not, just emit default action |
| 543 |
my $rec = $addons{$tag}; |
| 544 |
if (!$rec) |
| 545 |
{ |
| 546 |
emit_default_action(0); |
| 547 |
return; |
| 548 |
} |
| 549 |
|
| 550 |
# Track addons entry usage for later cross-check |
| 551 |
$rec->{used}++; |
| 552 |
|
| 553 |
my $rectype = $rec->{type}; |
| 554 |
if ($rectype eq 'rule') |
| 555 |
{ |
| 556 |
# Emit default action and then the code block. |
| 557 |
emit_default_action(0); |
| 558 |
} |
| 559 |
elsif ($rectype eq 'addon') |
| 560 |
{ |
| 561 |
# Emit the code block wrapped in the same braces as the default action. |
| 562 |
add_to_buffer('rules', ' { '); |
| 563 |
} |
| 564 |
|
| 565 |
# Emit the addons entry's code block. |
| 566 |
# We have an array to add to the buffer, we'll add it directly instead of |
| 567 |
# calling add_to_buffer, which does not know about arrays. |
| 568 |
push(@{ $buff{'rules'} }, @{ $rec->{lines} }); |
| 569 |
|
| 570 |
if ($rectype eq 'addon') |
| 571 |
{ |
| 572 |
emit_default_action(1); |
| 573 |
} |
| 574 |
return; |
| 575 |
} |
| 576 |
|
| 577 |
# Add the given line to the specified buffer. |
| 578 |
# Pass: buffer_name, string_to_append |
| 579 |
# Note we add a newline automatically. |
| 580 |
sub add_to_buffer |
| 581 |
{ |
| 582 |
push(@{ $buff{ $_[0] } }, "$_[1]\n"); |
| 583 |
return; |
| 584 |
} |
| 585 |
|
| 586 |
# Dump the specified buffer to the output file. |
| 587 |
sub dump_buffer |
| 588 |
{ |
| 589 |
my ($buffer) = @_; |
| 590 |
# Label the output for debugging purposes. |
| 591 |
print $outfh '/* ', $buffer, ' */', "\n"; |
| 592 |
my $ref = $buff{$buffer}; |
| 593 |
print $outfh @$ref; |
| 594 |
return; |
| 595 |
} |
| 596 |
|
| 597 |
# Emit the default action (usually token concatenation) for the current rule. |
| 598 |
# Pass: brace_printed boolean |
| 599 |
# brace_printed should be true if caller already printed action's open brace. |
| 600 |
sub emit_default_action |
| 601 |
{ |
| 602 |
my ($brace_printed) = @_; |
| 603 |
|
| 604 |
if ($stmt_mode == 0) |
| 605 |
{ |
| 606 |
# Normal rule |
| 607 |
if ($has_feature_not_supported and not $has_if_command) |
| 608 |
{ |
| 609 |
# The backend unconditionally reports |
| 610 |
# FEATURE_NOT_SUPPORTED in this rule, so let's emit |
| 611 |
# a warning on the ecpg side. |
| 612 |
if (!$brace_printed) |
| 613 |
{ |
| 614 |
add_to_buffer('rules', ' { '); |
| 615 |
$brace_printed = 1; |
| 616 |
} |
| 617 |
add_to_buffer('rules', |
| 618 |
'mmerror(PARSE_ERROR, ET_WARNING, "unsupported feature will be passed to server");' |
| 619 |
); |
| 620 |
} |
| 621 |
|
| 622 |
add_to_buffer('rules', '}') if ($brace_printed); |
| 623 |
} |
| 624 |
else |
| 625 |
{ |
| 626 |
# We're in the "stmt:" rule, where we need to output special actions. |
| 627 |
# This code assumes that no ecpg.addons entry applies. |
| 628 |
if ($line_count) |
| 629 |
{ |
| 630 |
# Any regular kind of statement calls output_statement |
| 631 |
add_to_buffer('rules', |
| 632 |
' { output_statement(@1, 0, ECPGst_normal); }'); |
| 633 |
} |
| 634 |
else |
| 635 |
{ |
| 636 |
# The empty production for stmt: do nothing |
| 637 |
} |
| 638 |
} |
| 639 |
return; |
| 640 |
} |
| 641 |
|
| 642 |
# Print the accumulated rule text (in $line) and the appropriate action. |
| 643 |
# Ordinarily return 1. However, if the rule matches an "ignore" |
| 644 |
# entry in %replace_line, then do nothing and return 0. |
| 645 |
sub emit_rule |
| 646 |
{ |
| 647 |
# compute tag to be used as lookup key in %replace_line and %addons |
| 648 |
my $tag = $non_term_id . ' ' . $line; |
| 649 |
$tag =~ tr/|//d; |
| 650 |
$tag = join(' ', split(/\s+/, $tag)); |
| 651 |
|
| 652 |
# apply replace_line substitution if any |
| 653 |
my $rep = $replace_line{$tag}; |
| 654 |
if (defined $rep) |
| 655 |
{ |
| 656 |
$replace_line_used{$tag}++; |
| 657 |
|
| 658 |
if ($rep eq 'ignore') |
| 659 |
{ |
| 660 |
return 0; |
| 661 |
} |
| 662 |
|
| 663 |
# non-ignore entries replace the line, but we'd better keep any '|'; |
| 664 |
# we don't bother to update $line_count here. |
| 665 |
if (index($line, '|') != -1) |
| 666 |
{ |
| 667 |
$line = '| ' . $rep; |
| 668 |
} |
| 669 |
else |
| 670 |
{ |
| 671 |
$line = $rep; |
| 672 |
} |
| 673 |
|
| 674 |
# recompute tag for use in emit_rule_action |
| 675 |
$tag = $non_term_id . ' ' . $line; |
| 676 |
$tag =~ tr/|//d; |
| 677 |
$tag = join(' ', split(/\s+/, $tag)); |
| 678 |
} |
| 679 |
|
| 680 |
# Emit $line, then print the appropriate action. |
| 681 |
add_to_buffer('rules', $line); |
| 682 |
emit_rule_action($tag); |
| 683 |
return 1; |
| 684 |
} |
| 685 |
|
| 686 |
=top |
| 687 |
load ecpg.addons into %addons hash. The result is something like |
| 688 |
%addons = { |
| 689 |
'stmt ClosePortalStmt' => { 'type' => 'block', 'lines' => [ "{", "if (INFORMIX_MODE)" ..., "}" ], 'used' => 0 }, |
| 690 |
'stmt ViewStmt' => { 'type' => 'rule', 'lines' => [ "| ECPGAllocateDescr", ... ], 'used' => 0 } |
| 691 |
} |
| 692 |
|
| 693 |
=cut |
| 694 |
|
| 695 |
sub preload_addons |
| 696 |
{ |
| 697 |
my $filename = $srcdir . "/ecpg.addons"; |
| 698 |
open(my $fh, '<', $filename) or die; |
| 699 |
|
| 700 |
# There may be multiple "ECPG:" lines and then multiple lines of code. |
| 701 |
# The block of code needs to be added to each of the consecutively- |
| 702 |
# preceding "ECPG:" records. |
| 703 |
my (@needsRules, @code); |
| 704 |
|
| 705 |
# there may be comments before the first "ECPG:" line, skip them |
| 706 |
my $skip = 1; |
| 707 |
while (<$fh>) |
| 708 |
{ |
| 709 |
if (/^ECPG:\s+(\w+)\s+(.*)$/) |
| 710 |
{ |
| 711 |
# Found an "ECPG:" line, so we're done skipping the header |
| 712 |
$skip = 0; |
| 713 |
my $type = $1; |
| 714 |
my $target = $2; |
| 715 |
# Normalize target so there's exactly one space between tokens |
| 716 |
$target = join(' ', split(/\s+/, $target)); |
| 717 |
# Validate record type and target |
| 718 |
die "invalid record type $type in addon rule for $target\n" |
| 719 |
unless ($type eq 'block' |
| 720 |
or $type eq 'addon' |
| 721 |
or $type eq 'rule'); |
| 722 |
die "duplicate addon rule for $target\n" |
| 723 |
if (exists $addons{$target}); |
| 724 |
# If we had some preceding code lines, attach them to all |
| 725 |
# as-yet-unfinished records. |
| 726 |
if (@code) |
| 727 |
{ |
| 728 |
for my $x (@needsRules) |
| 729 |
{ |
| 730 |
push(@{ $x->{lines} }, @code); |
| 731 |
} |
| 732 |
@code = (); |
| 733 |
@needsRules = (); |
| 734 |
} |
| 735 |
my $record = {}; |
| 736 |
$record->{type} = $type; |
| 737 |
$record->{lines} = []; |
| 738 |
$record->{used} = 0; |
| 739 |
$addons{$target} = $record; |
| 740 |
push(@needsRules, $record); |
| 741 |
} |
| 742 |
elsif (/^ECPG:/) |
| 743 |
{ |
| 744 |
# Complain if preceding regex failed to match |
| 745 |
die "incorrect syntax in ECPG line: $_\n"; |
| 746 |
} |
| 747 |
else |
| 748 |
{ |
| 749 |
# Non-ECPG line: add to @code unless we're still skipping |
| 750 |
next if $skip; |
| 751 |
push(@code, $_); |
| 752 |
} |
| 753 |
} |
| 754 |
close($fh); |
| 755 |
# Deal with final code block |
| 756 |
if (@code) |
| 757 |
{ |
| 758 |
for my $x (@needsRules) |
| 759 |
{ |
| 760 |
push(@{ $x->{lines} }, @code); |
| 761 |
} |
| 762 |
} |
| 763 |
return; |
| 764 |
} |