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-2012, 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(GRAM, $gram_filename) || die("Could not open : $gram_filename");
34 my ($S, $s, $k, $n, $kcat);
41 chomp; # 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;
55 # Is this the beginning of a keyword list?
56 foreach $k (keys %keyword_categories)
67 # Now split the line into individual fields
68 $n = (@arr = split(' ', $S));
70 # Ok, we're in a keyword list. Go through each field in turn
71 for (my $fieldIndexer = 0; $fieldIndexer < $n; $fieldIndexer++)
73 if ($arr[$fieldIndexer] eq '*/' && $comment)
82 elsif ($arr[$fieldIndexer] eq '/*')
84 # start of a multiline comment
88 elsif ($arr[$fieldIndexer] eq '//')
93 if ($arr[$fieldIndexer] eq ';')
100 if ($arr[$fieldIndexer] eq '|')
105 # Put this keyword into the right list
106 push @{ $keywords{$kcat} }, $arr[$fieldIndexer];
111 # Check that each keyword list is in alphabetical order (just for neatnik-ism)
112 my ($prevkword, $kword, $bare_kword);
113 foreach $kcat (keys %keyword_categories)
117 foreach $kword (@{ $keywords{$kcat} })
119 # Some keyword have a _P suffix. Remove it for the comparison.
120 $bare_kword = $kword;
121 $bare_kword =~ s/_P$//;
122 if ($bare_kword le $prevkword)
125 "'$bare_kword' after '$prevkword' in $kcat list is misplaced";
127 $prevkword = $bare_kword;
131 # Transform the keyword lists into hashes.
132 # kwhashes is a hash of hashes, keyed by keyword category id,
133 # e.g. UNRESERVED_KEYWORD.
134 # Each inner hash is keyed by keyword id, e.g. ABORT_P, with a dummy value.
136 while (my ($kcat, $kcat_id) = each(%keyword_categories))
138 @arr = @{ $keywords{$kcat} };
141 foreach my $item (@arr) { $hash->{$item} = 1; }
143 $kwhashes{$kcat_id} = $hash;
146 # Now read in kwlist.h
148 open(KWLIST, $kwlist_filename) || die("Could not open : $kwlist_filename");
150 my $prevkwstring = '';
153 kwlist_line: while (<KWLIST>)
157 if ($line =~ /^PG_KEYWORD\(\"(.*)\", (.*), (.*)\)/)
163 # Check that the list is in alphabetical order (critical!)
164 if ($kwstring le $prevkwstring)
167 "'$kwstring' after '$prevkwstring' in kwlist.h is misplaced";
169 $prevkwstring = $kwstring;
171 # Check that the keyword string is valid: all lower-case ASCII chars
172 if ($kwstring !~ /^[a-z_]+$/)
175 "'$kwstring' is not a valid keyword string, must be all lower-case ASCII chars";
178 # Check that the keyword name is valid: all upper-case ASCII chars
179 if ($kwname !~ /^[A-Z_]+$/)
182 "'$kwname' is not a valid keyword name, must be all upper-case ASCII chars";
185 # Check that the keyword string matches keyword name
186 $bare_kwname = $kwname;
187 $bare_kwname =~ s/_P$//;
188 if ($bare_kwname ne uc($kwstring))
191 "keyword name '$kwname' doesn't match keyword string '$kwstring'";
194 # Check that the keyword is present in the grammar
195 %kwhash = %{ $kwhashes{$kwcat_id} };
199 error "Unknown keyword category: $kwcat_id";
203 if (!($kwhash{$kwname}))
205 error "'$kwname' not present in $kwcat_id section of gram.y";
209 # Remove it from the hash, so that we can
210 # complain at the end if there's keywords left
211 # that were not found in kwlist.h
212 delete $kwhashes{$kwcat_id}->{$kwname};
219 # Check that we've paired up all keywords from gram.y with lines in kwlist.h
220 while (my ($kwcat, $kwcat_id) = each(%keyword_categories))
222 %kwhash = %{ $kwhashes{$kwcat_id} };
224 for my $kw (keys %kwhash)
226 error "'$kw' found in gram.y $kwcat category, but not in kwlist.h";