1
- # !/usr/bin/perl -w
1
+ # !/usr/bin/perl
2
2
3
+ # Check that the keyword lists in gram.y and kwlist.h are sane.
4
+ # Usage: check_keywords.pl gram.y kwlist.h
5
+
6
+ # src/backend/parser/check_keywords.pl
7
+ # Copyright (c) 2009-2012, PostgreSQL Global Development Group
8
+
9
+ use warnings;
3
10
use strict;
4
11
5
- # Check that the keyword lists in gram.y and kwlist.h are sane. Run from
6
- # the top directory, or pass a path to a top directory as argument.
7
- #
8
- # src/tools/check_keywords.pl
12
+ my $gram_filename = $ARGV [0];
13
+ my $kwlist_filename = $ARGV [1];
9
14
10
15
my $errors = 0;
11
- my $path ;
12
16
13
17
sub error (@)
14
18
{
15
19
print STDERR @_ ;
16
20
$errors = 1;
17
21
}
18
22
19
- if (@ARGV )
20
- {
21
- $path = $ARGV [0];
22
- shift @ARGV ;
23
- }
24
- else
25
- {
26
- $path = " ." ;
27
- }
28
-
29
23
$, = ' ' ; # set output field separator
30
24
$\ = " \n " ; # set output record separator
31
25
35
29
$keyword_categories {' type_func_name_keyword' } = ' TYPE_FUNC_NAME_KEYWORD' ;
36
30
$keyword_categories {' reserved_keyword' } = ' RESERVED_KEYWORD' ;
37
31
38
- my $gram_filename = " $path /src/backend/parser/gram.y" ;
39
32
open (GRAM, $gram_filename ) || die (" Could not open : $gram_filename " );
40
33
41
34
my ($S , $s , $k , $n , $kcat );
59
52
60
53
if (!($kcat ))
61
54
{
62
-
63
55
# Is this the beginning of a keyword list?
64
56
foreach $k (keys %keyword_categories )
65
57
{
89
81
}
90
82
elsif ($arr [$fieldIndexer ] eq ' /*' )
91
83
{
92
-
93
84
# start of a multiline comment
94
85
$comment = 1;
95
86
next ;
101
92
102
93
if ($arr [$fieldIndexer ] eq ' ;' )
103
94
{
104
-
105
95
# end of keyword list
106
96
$kcat = ' ' ;
107
97
next ;
@@ -118,46 +108,43 @@ (@)
118
108
}
119
109
close GRAM;
120
110
121
- # Check that all keywords are in alphabetical order
111
+ # Check that each keyword list is in alphabetical order (just for neatnik-ism)
122
112
my ($prevkword , $kword , $bare_kword );
123
113
foreach $kcat (keys %keyword_categories )
124
114
{
125
115
$prevkword = ' ' ;
126
116
127
117
foreach $kword (@{ $keywords {$kcat } })
128
118
{
129
-
130
119
# Some keyword have a _P suffix. Remove it for the comparison.
131
120
$bare_kword = $kword ;
132
121
$bare_kword =~ s / _P$// ;
133
122
if ($bare_kword le $prevkword )
134
123
{
135
124
error
136
125
" '$bare_kword ' after '$prevkword ' in $kcat list is misplaced" ;
137
- $errors = 1;
138
126
}
139
127
$prevkword = $bare_kword ;
140
128
}
141
129
}
142
130
143
131
# Transform the keyword lists into hashes.
144
- # kwhashes is a hash of hashes, keyed by keyword category id, e.g.
145
- # UNRESERVED_KEYWORD. Each inner hash is a keyed by keyword id, e.g. ABORT_P
146
- # with a dummy value.
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.
147
135
my %kwhashes ;
148
136
while (my ($kcat , $kcat_id ) = each (%keyword_categories ))
149
137
{
150
138
@arr = @{ $keywords {$kcat } };
151
139
152
140
my $hash ;
153
- foreach my $item (@arr ) { $hash -> {$item } = 1 }
141
+ foreach my $item (@arr ) { $hash -> {$item } = 1; }
154
142
155
143
$kwhashes {$kcat_id } = $hash ;
156
144
}
157
145
158
146
# Now read in kwlist.h
159
147
160
- my $kwlist_filename = " $path /src/include/parser/kwlist.h" ;
161
148
open (KWLIST, $kwlist_filename ) || die (" Could not open : $kwlist_filename " );
162
149
163
150
my $prevkwstring = ' ' ;
173
160
my ($kwname ) = $2 ;
174
161
my ($kwcat_id ) = $3 ;
175
162
176
- # Check that the list is in alphabetical order
163
+ # Check that the list is in alphabetical order (critical!)
177
164
if ($kwstring le $prevkwstring )
178
165
{
179
166
error
@@ -182,14 +169,14 @@ (@)
182
169
$prevkwstring = $kwstring ;
183
170
184
171
# Check that the keyword string is valid: all lower-case ASCII chars
185
- if ($kwstring !~ / ^[a-z_]* $ / )
172
+ if ($kwstring !~ / ^[a-z_]+ $ / )
186
173
{
187
174
error
188
175
" '$kwstring ' is not a valid keyword string, must be all lower-case ASCII chars" ;
189
176
}
190
177
191
178
# Check that the keyword name is valid: all upper-case ASCII chars
192
- if ($kwname !~ / ^[A-Z_]* $ / )
179
+ if ($kwname !~ / ^[A-Z_]+ $ / )
193
180
{
194
181
error
195
182
" '$kwname ' is not a valid keyword name, must be all upper-case ASCII chars" ;
209
196
210
197
if (!(%kwhash ))
211
198
{
212
-
213
- # error "Unknown kwcat_id: $kwcat_id";
199
+ error " Unknown keyword category: $kwcat_id " ;
214
200
}
215
201
else
216
202
{
220
206
}
221
207
else
222
208
{
223
-
224
- # Remove it from the hash, so that we can complain at the end
225
- # if there's keywords left that were not found in kwlist.h
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
226
212
delete $kwhashes {$kwcat_id }-> {$kwname };
227
213
}
228
214
}
0 commit comments