-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathgen_keyword_list.cc
194 lines (158 loc) · 5.89 KB
/
gen_keyword_list.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
Copyright (c) 2017, 2024, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include <cassert>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <fstream> // IWYU pragma: keep
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <unicode/errorcode.h>
#include <unicode/regex.h>
#include <unicode/unistr.h>
#include <unicode/utypes.h>
#include "lex.h" // symbols[]
#include "sql/lex_symbol.h"
#include "template_utils.h"
#include "welcome_copyright_notice.h" // ORACLE_WELCOME_COPYRIGHT_NOTICE
using icu::RegexMatcher;
using icu::UnicodeString;
bool icu_error(const UErrorCode &status) {
if (!U_FAILURE(status)) {
return false;
}
icu::ErrorCode error_code;
error_code.set(status);
std::cerr << "Error: " << error_code.errorName() << std::endl;
return true;
}
int main(int argc, const char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <YACC file>\n", argv[0]);
return EXIT_FAILURE;
}
const char *yacc_filename = argv[1];
std::ifstream yacc(yacc_filename);
if (!yacc.is_open()) {
fprintf(stderr, "Failed to open \"%s\"", yacc_filename);
return EXIT_FAILURE;
}
UErrorCode status = U_ZERO_ERROR;
UnicodeString rx(
"^%(token|left|right|nonassoc)[[:space:]]*" // g.1: %token, %left etc.
"(<[._[:alnum:]]+>)?[[:space:]]*" // g.2: opt. '<' type '>'
"([_[:alnum:]]+)" // g.3: token name
"([[:space:]]+[0-9]+)?" // g.4: opt token number
".*$"); // opt. rest of line
RegexMatcher match(rx, 0, status);
if (icu_error(status)) {
return EXIT_FAILURE;
}
std::set<size_t> keyword_tokens;
std::string s;
size_t input_line = 0;
while (getline(yacc, s)) {
//
// Collect non-reserved keyword token numbers in keyword_tokens, where
// non-reserved keyword tokens are declared like this:
//
// %token <lexer.keyword> token_name token_number
//
// At the same time, validate if all %token declarations have explicit
// token numbers.
//
input_line++;
UnicodeString sample(s.data(), s.length());
match.reset(sample);
if (match.matches(status)) {
assert(match.groupCount() == 4);
//
// Check if %token definition contains an explicit token number
//
UnicodeString uc_declaration = match.group(1, status);
if (icu_error(status)) {
return EXIT_FAILURE;
}
static const UnicodeString token_declaration("token");
if (uc_declaration == token_declaration) {
//
// This is %token ...
//
UnicodeString uc_token_number = match.group(4, status);
if (icu_error(status)) {
return EXIT_FAILURE;
}
std::string utf8_token_number;
uc_token_number.toUTF8String(utf8_token_number);
if (uc_token_number.length() == 0) {
fprintf(stderr, "%s:%zu: error: missing token number\n",
yacc_filename, input_line);
exit(EXIT_FAILURE);
}
UnicodeString uc_semantic_type = match.group(2, status);
if (icu_error(status)) {
return EXIT_FAILURE;
}
static const UnicodeString keyword_semantic_type("<lexer.keyword>");
if (uc_semantic_type == keyword_semantic_type) {
//
// This is %token <lexer.keyword> ...
//
int token_num = std::stoi(utf8_token_number);
keyword_tokens.insert(token_num);
}
}
}
}
std::map<std::string, bool> words;
for (size_t i = 0; i < array_elements(symbols); i++) {
const SYMBOL *sym = &symbols[i];
if (sym->group != SG_KEYWORDS && sym->group != SG_HINTABLE_KEYWORDS)
continue; // Function or optimizer hint name.
if (!isalpha(sym->name[0])) continue; // Operator.
bool is_reserved = keyword_tokens.count(sym->tok) == 0;
if (!words.insert(std::make_pair(sym->name, is_reserved)).second) {
fprintf(stderr,
"This should not happen: \"%s\" has duplicates."
" See symbols[] in lex.h",
sym->name);
assert(false);
return EXIT_FAILURE;
}
}
auto &out = std::cout;
out << ORACLE_GPL_COPYRIGHT_NOTICE("2017") << std::endl;
out << "#ifndef GEN_KEYWORD_LIST_H__INCLUDED\n";
out << "#define GEN_KEYWORD_LIST_H__INCLUDED\n\n";
out << "/*\n";
out << " This file is generated, do not edit.\n";
out << " See file sql/gen_keyword_list.cc.\n";
out << "*/\n\n";
out << "typedef struct { const char *word; int reserved; } keyword_t;\n\n";
out << "static const keyword_t keyword_list[]= {\n";
for (auto p : words)
out << " { \"" << p.first << "\", " << (p.second ? 1 : 0) << " },\n";
out << "};/*keyword_list*/\n\n";
out << "#endif/*GEN_KEYWORD_LIST_H__INCLUDED*/\n";
return 0;
}