| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# Generate the keywords table for the documentation's SQL Key Words appendix |
| 4 |
# |
| 5 |
# Copyright (c) 2019-2025, PostgreSQL Global Development Group |
| 6 |
|
| 7 |
use strict; |
| 8 |
use warnings FATAL => 'all'; |
| 9 |
|
| 10 |
my @sql_versions = reverse sort ('1992', '2016', '2023'); |
| 11 |
|
| 12 |
my $srcdir = $ARGV[0]; |
| 13 |
|
| 14 |
my %keywords; |
| 15 |
my %as_keywords; |
| 16 |
|
| 17 |
# read SQL-spec keywords |
| 18 |
|
| 19 |
foreach my $ver (@sql_versions) |
| 20 |
{ |
| 21 |
foreach my $res ('reserved', 'nonreserved') |
| 22 |
{ |
| 23 |
foreach my $file (glob "$srcdir/keywords/sql${ver}*-${res}.txt") |
| 24 |
{ |
| 25 |
open my $fh, '<', $file or die; |
| 26 |
|
| 27 |
while (<$fh>) |
| 28 |
{ |
| 29 |
chomp; |
| 30 |
$keywords{$_}{$ver}{$res} = 1; |
| 31 |
} |
| 32 |
|
| 33 |
close $fh; |
| 34 |
} |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
# read PostgreSQL keywords |
| 39 |
|
| 40 |
open my $fh, '<', "$srcdir/../../../src/include/parser/kwlist.h" or die; |
| 41 |
|
| 42 |
while (<$fh>) |
| 43 |
{ |
| 44 |
if (/^PG_KEYWORD\("(\w+)", \w+, (\w+)_KEYWORD\, (\w+)\)/) |
| 45 |
{ |
| 46 |
$keywords{ uc $1 }{'pg'}{ lc $2 } = 1; |
| 47 |
$as_keywords{ uc $1 } = 1 if $3 eq 'AS_LABEL'; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
close $fh; |
| 52 |
|
| 53 |
# print output |
| 54 |
|
| 55 |
print "<!-- autogenerated, do not edit -->\n"; |
| 56 |
|
| 57 |
print <<END; |
| 58 |
<table id="keywords-table"> |
| 59 |
<title><acronym>SQL</acronym> Key Words</title> |
| 60 |
|
| 61 |
<tgroup cols="5"> |
| 62 |
<colspec colname="col1" colwidth="5*"/> |
| 63 |
<colspec colname="col2" colwidth="3*"/> |
| 64 |
<colspec colname="col3" colwidth="2*"/> |
| 65 |
<colspec colname="col4" colwidth="2*"/> |
| 66 |
<colspec colname="col5" colwidth="2*"/> |
| 67 |
<thead> |
| 68 |
<row> |
| 69 |
<entry>Key Word</entry> |
| 70 |
<entry><productname>PostgreSQL</productname></entry> |
| 71 |
END |
| 72 |
|
| 73 |
foreach my $ver (@sql_versions) |
| 74 |
{ |
| 75 |
my $s = ($ver eq '1992' ? 'SQL-92' : "SQL:$ver"); |
| 76 |
print " <entry>$s</entry>\n"; |
| 77 |
} |
| 78 |
|
| 79 |
print <<END; |
| 80 |
</row> |
| 81 |
</thead> |
| 82 |
|
| 83 |
<tbody> |
| 84 |
END |
| 85 |
|
| 86 |
foreach my $word (sort keys %keywords) |
| 87 |
{ |
| 88 |
# Insert zwsp's into very long keywords, so that they can be broken |
| 89 |
# into multiple lines in PDF format (or narrow HTML windows). |
| 90 |
my $printword = $word; |
| 91 |
$printword =~ s/_/_&zwsp;/g if (length($printword) > 20); |
| 92 |
|
| 93 |
print " <row>\n"; |
| 94 |
print " <entry><token>$printword</token></entry>\n"; |
| 95 |
|
| 96 |
print " <entry>"; |
| 97 |
if ($keywords{$word}{pg}{'unreserved'}) |
| 98 |
{ |
| 99 |
print "non-reserved"; |
| 100 |
} |
| 101 |
elsif ($keywords{$word}{pg}{'col_name'}) |
| 102 |
{ |
| 103 |
print "non-reserved (cannot be function or type)"; |
| 104 |
} |
| 105 |
elsif ($keywords{$word}{pg}{'type_func_name'}) |
| 106 |
{ |
| 107 |
print "reserved (can be function or type)"; |
| 108 |
} |
| 109 |
elsif ($keywords{$word}{pg}{'reserved'}) |
| 110 |
{ |
| 111 |
print "reserved"; |
| 112 |
} |
| 113 |
if ($as_keywords{$word}) |
| 114 |
{ |
| 115 |
print ", requires <literal>AS</literal>"; |
| 116 |
} |
| 117 |
print "</entry>\n"; |
| 118 |
|
| 119 |
foreach my $ver (@sql_versions) |
| 120 |
{ |
| 121 |
print " <entry>"; |
| 122 |
if ($keywords{$word}{$ver}{'reserved'}) |
| 123 |
{ |
| 124 |
print "reserved"; |
| 125 |
} |
| 126 |
elsif ($keywords{$word}{$ver}{'nonreserved'}) |
| 127 |
{ |
| 128 |
print "non-reserved"; |
| 129 |
} |
| 130 |
print "</entry>\n"; |
| 131 |
} |
| 132 |
print " </row>\n"; |
| 133 |
} |
| 134 |
|
| 135 |
print <<END; |
| 136 |
</tbody> |
| 137 |
</tgroup> |
| 138 |
</table> |
| 139 |
END |