3 # Check that the keyword lists in gram.y and kwlist.h are sane.
4 # Usage: check_keywords.pl gram.y kwlist.h
6 # src/backend/parser/check_keywords.pl
7 # Copyright (c) 2009-2018, PostgreSQL Global Development Group
12 my $gram_filename = $ARGV[0];
13 my $kwlist_filename = $ARGV[1];
23 $, = ' '; # set output field separator
24 $\ = "\n"; # set output record separator
26 my %keyword_categories;
27 $keyword_categories{'unreserved_keyword'} = 'UNRESERVED_KEYWORD';
28 $keyword_categories{'col_name_keyword'} = 'COL_NAME_KEYWORD';
29 $keyword_categories{'type_func_name_keyword'} = 'TYPE_FUNC_NAME_KEYWORD';
30 $keyword_categories{'reserved_keyword'} = 'RESERVED_KEYWORD';
32 open(my $gram, '<', $gram_filename) || die("Could not open : $gram_filename");
39 line: while (my $S = <$gram>)
41 chomp $S; # strip record separator
45 # Make sure any braces are split
46 $s = '{', $S =~ s/$s/ { /g;
47 $s = '}', $S =~ s/$s/ } /g;
49 # Any comments are split
50 $s = '[/][*]', $S =~ s#$s# /* #g;
51 $s = '[*][/]', $S =~ s#$s# */ #g;
56 # Is this the beginning of a keyword list?
57 foreach my $k (keys %keyword_categories)
68 # Now split the line into individual fields
69 my $n = (@arr = split(' ', $S));
71 # Ok, we're in a keyword list. Go through each field in turn
72 for (my $fieldIndexer = 0; $fieldIndexer < $n; $fieldIndexer++)
74 if ($arr[$fieldIndexer] eq '*/' && $comment)
83 elsif ($arr[$fieldIndexer] eq '/*')
86 # start of a multiline comment
90 elsif ($arr[$fieldIndexer] eq '//')
95 if ($arr[$fieldIndexer] eq ';')
103 if ($arr[$fieldIndexer] eq '|')
108 # Put this keyword into the right list
109 push @{ $keywords{$kcat} }, $arr[$fieldIndexer];
114 # Check that each keyword list is in alphabetical order (just for neatnik-ism)
115 my ($prevkword, $bare_kword);
116 foreach my $kcat (keys %keyword_categories)
120 foreach my $kword (@{ $keywords{$kcat} })
123 # Some keyword have a _P suffix. Remove it for the comparison.
124 $bare_kword = $kword;
125 $bare_kword =~ s/_P$//;
126 if ($bare_kword le $prevkword)
129 "'$bare_kword' after '$prevkword' in $kcat list is misplaced";
131 $prevkword = $bare_kword;
135 # Transform the keyword lists into hashes.
136 # kwhashes is a hash of hashes, keyed by keyword category id,
137 # e.g. UNRESERVED_KEYWORD.
138 # Each inner hash is keyed by keyword id, e.g. ABORT_P, with a dummy value.
140 while (my ($kcat, $kcat_id) = each(%keyword_categories))
142 @arr = @{ $keywords{$kcat} };
145 foreach my $item (@arr) { $hash->{$item} = 1; }
147 $kwhashes{$kcat_id} = $hash;
150 # Now read in kwlist.h
152 open(my $kwlist, '<', $kwlist_filename)
153 || die("Could not open : $kwlist_filename");
155 my $prevkwstring = '';
158 kwlist_line: while (<$kwlist>)
162 if ($line =~ /^PG_KEYWORD\(\"(.*)\", (.*), (.*)\)/)
168 # Check that the list is in alphabetical order (critical!)
169 if ($kwstring le $prevkwstring)
172 "'$kwstring' after '$prevkwstring' in kwlist.h is misplaced";
174 $prevkwstring = $kwstring;
176 # Check that the keyword string is valid: all lower-case ASCII chars
177 if ($kwstring !~ /^[a-z_]+$/)
180 "'$kwstring' is not a valid keyword string, must be all lower-case ASCII chars";
183 # Check that the keyword name is valid: all upper-case ASCII chars
184 if ($kwname !~ /^[A-Z_]+$/)
187 "'$kwname' is not a valid keyword name, must be all upper-case ASCII chars";
190 # Check that the keyword string matches keyword name
191 $bare_kwname = $kwname;
192 $bare_kwname =~ s/_P$//;
193 if ($bare_kwname ne uc($kwstring))
196 "keyword name '$kwname' doesn't match keyword string '$kwstring'";
199 # Check that the keyword is present in the grammar
200 %kwhash = %{ $kwhashes{$kwcat_id} };
204 error "Unknown keyword category: $kwcat_id";
208 if (!($kwhash{$kwname}))
210 error "'$kwname' not present in $kwcat_id section of gram.y";
215 # Remove it from the hash, so that we can
216 # complain at the end if there's keywords left
217 # that were not found in kwlist.h
218 delete $kwhashes{$kwcat_id}->{$kwname};
225 # Check that we've paired up all keywords from gram.y with lines in kwlist.h
226 while (my ($kwcat, $kwcat_id) = each(%keyword_categories))
228 %kwhash = %{ $kwhashes{$kwcat_id} };
230 for my $kw (keys %kwhash)
232 error "'$kw' found in gram.y $kwcat category, but not in kwlist.h";