| 1 |
// Copyright (C) 2016 TheQt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
| 3 |
#include <stdio.h> |
| 4 |
#include <string.h> |
| 5 |
#include <qbytearray.h> |
| 6 |
#include <qlist.h> |
| 7 |
|
| 8 |
struct Keyword |
| 9 |
{ |
| 10 |
const char *lexem; |
| 11 |
const char *token; |
| 12 |
}; |
| 13 |
|
| 14 |
static const Keyword pp_keywords[] = { |
| 15 |
{ "<", "PP_LANGLE" }, |
| 16 |
{ ">", "PP_RANGLE" }, |
| 17 |
{ "(", "PP_LPAREN"}, |
| 18 |
{ ")", "PP_RPAREN"}, |
| 19 |
{ ",", "PP_COMMA"}, |
| 20 |
{ "\n", "PP_NEWLINE"}, |
| 21 |
{ "#define", "PP_DEFINE"}, |
| 22 |
{ "#if", "PP_IF"}, |
| 23 |
{ "#undef", "PP_UNDEF"}, |
| 24 |
{ "#ifdef", "PP_IFDEF"}, |
| 25 |
{ "#ifndef", "PP_IFNDEF"}, |
| 26 |
{ "#elif", "PP_ELIF"}, |
| 27 |
{ "#else", "PP_ELSE"}, |
| 28 |
{ "#endif", "PP_ENDIF"}, |
| 29 |
{ "#include", "PP_INCLUDE"}, |
| 30 |
{ "__has_include", "PP_HAS_INCLUDE"}, |
| 31 |
{ "defined", "PP_DEFINED"}, |
| 32 |
{ "+", "PP_PLUS" }, |
| 33 |
{ "-", "PP_MINUS" }, |
| 34 |
{ "*", "PP_STAR" }, |
| 35 |
{ "/", "PP_SLASH" }, |
| 36 |
{ "%", "PP_PERCENT" }, |
| 37 |
{ "^", "PP_HAT" }, |
| 38 |
{ "&", "PP_AND" }, |
| 39 |
{ "bitand", "PP_AND" }, |
| 40 |
{ "|", "PP_OR" }, |
| 41 |
{ "bitor", "PP_OR" }, |
| 42 |
{ "~", "PP_TILDE" }, |
| 43 |
{ "compl", "PP_TILDE" }, |
| 44 |
{ "!", "PP_NOT" }, |
| 45 |
{ "not", "PP_NOT" }, |
| 46 |
{ "<<", "PP_LTLT" }, |
| 47 |
{ ">>", "PP_GTGT" }, |
| 48 |
{ "==", "PP_EQEQ" }, |
| 49 |
{ "!=", "PP_NE" }, |
| 50 |
{ "not_eq", "PP_NE" }, |
| 51 |
{ "<=", "PP_LE" }, |
| 52 |
{ ">=", "PP_GE" }, |
| 53 |
{ "&&", "PP_ANDAND" }, |
| 54 |
{ "||", "PP_OROR" }, |
| 55 |
{ "?", "PP_QUESTION" }, |
| 56 |
{ ":", "PP_COLON" }, |
| 57 |
{ "##", "PP_HASHHASH"}, |
| 58 |
{ "%:%:", "PP_HASHHASH"}, |
| 59 |
{ "#", "PP_HASH"}, |
| 60 |
{ "\"", "PP_QUOTE"}, |
| 61 |
{ "\'", "PP_SINGLEQUOTE"}, |
| 62 |
{ " ", "PP_WHITESPACE" }, |
| 63 |
{ "\t", "PP_WHITESPACE" }, |
| 64 |
{ "//", "PP_CPP_COMMENT" }, |
| 65 |
{ "/*", "PP_C_COMMENT" }, |
| 66 |
{ "\\", "PP_BACKSLASH" }, |
| 67 |
{ 0, "PP_NOTOKEN"} |
| 68 |
}; |
| 69 |
|
| 70 |
static const Keyword keywords[] = { |
| 71 |
{ "<", "LANGLE" }, |
| 72 |
{ ">", "RANGLE" }, |
| 73 |
{ "(", "LPAREN" }, |
| 74 |
{ ")", "RPAREN" }, |
| 75 |
{ "...", "ELIPSIS" }, |
| 76 |
{ ",", "COMMA" }, |
| 77 |
{ "[", "LBRACK" }, |
| 78 |
{ "]", "RBRACK" }, |
| 79 |
{ "<:", "LBRACK" }, |
| 80 |
{ ":>", "RBRACK" }, |
| 81 |
{ "<::", "LANGLE_SCOPE" }, |
| 82 |
{ "{", "LBRACE" }, |
| 83 |
{ "<%", "LBRACE" }, |
| 84 |
{ "}", "RBRACE" }, |
| 85 |
{ "%>", "RBRACE" }, |
| 86 |
{ "=", "EQ" }, |
| 87 |
{ "::", "SCOPE" }, |
| 88 |
{ ";", "SEMIC" }, |
| 89 |
{ ":", "COLON" }, |
| 90 |
{ ".*", "DOTSTAR" }, |
| 91 |
{ "?", "QUESTION" }, |
| 92 |
{ ".", "DOT" }, |
| 93 |
{ "dynamic_cast", "DYNAMIC_CAST" }, |
| 94 |
{ "static_cast", "STATIC_CAST" }, |
| 95 |
{ "reinterpret_cast", "REINTERPRET_CAST" }, |
| 96 |
{ "const_cast", "CONST_CAST" }, |
| 97 |
{ "typeid", "TYPEID" }, |
| 98 |
{ "this", "THIS" }, |
| 99 |
{ "template", "TEMPLATE" }, |
| 100 |
{ "throw", "THROW" }, |
| 101 |
{ "try", "TRY" }, |
| 102 |
{ "catch", "CATCH" }, |
| 103 |
{ "typedef", "TYPEDEF" }, |
| 104 |
{ "friend", "FRIEND" }, |
| 105 |
{ "class", "CLASS" }, |
| 106 |
{ "namespace", "NAMESPACE" }, |
| 107 |
{ "enum", "ENUM" }, |
| 108 |
{ "struct", "STRUCT" }, |
| 109 |
{ "union", "UNION" }, |
| 110 |
{ "virtual", "VIRTUAL" }, |
| 111 |
{ "private", "PRIVATE" }, |
| 112 |
{ "protected", "PROTECTED" }, |
| 113 |
{ "public", "PUBLIC" }, |
| 114 |
{ "export", "EXPORT" }, |
| 115 |
{ "auto", "AUTO" }, |
| 116 |
{ "register", "REGISTER" }, |
| 117 |
{ "extern", "EXTERN" }, |
| 118 |
{ "mutable", "MUTABLE" }, |
| 119 |
{ "asm", "ASM" }, |
| 120 |
{ "using", "USING" }, |
| 121 |
{ "inline", "INLINE" }, |
| 122 |
{ "explicit", "EXPLICIT" }, |
| 123 |
{ "static", "STATIC" }, |
| 124 |
{ "const", "CONST" }, |
| 125 |
{ "constexpr", "CONSTEXPR" }, |
| 126 |
{ "volatile", "VOLATILE" }, |
| 127 |
{ "operator", "OPERATOR" }, |
| 128 |
{ "sizeof", "SIZEOF" }, |
| 129 |
{ "new", "NEW" }, |
| 130 |
{ "delete", "DELETE" }, |
| 131 |
{ "+", "PLUS" }, |
| 132 |
{ "-", "MINUS" }, |
| 133 |
{ "*", "STAR" }, |
| 134 |
{ "/", "SLASH" }, |
| 135 |
{ "%", "PERCENT" }, |
| 136 |
{ "^", "HAT" }, |
| 137 |
{ "&", "AND" }, |
| 138 |
{ "bitand", "AND" }, |
| 139 |
{ "|", "OR" }, |
| 140 |
{ "bitor", "OR" }, |
| 141 |
{ "~", "TILDE" }, |
| 142 |
{ "compl", "TILDE" }, |
| 143 |
{ "!", "NOT" }, |
| 144 |
{ "not", "NOT" }, |
| 145 |
{ "+=", "PLUS_EQ" }, |
| 146 |
{ "-=", "MINUS_EQ" }, |
| 147 |
{ "*=", "STAR_EQ" }, |
| 148 |
{ "/=", "SLASH_EQ" }, |
| 149 |
{ "%=", "PERCENT_EQ" }, |
| 150 |
{ "^=", "HAT_EQ" }, |
| 151 |
{ "&=", "AND_EQ" }, |
| 152 |
{ "|=", "OR_EQ" }, |
| 153 |
{ "<<", "LTLT" }, |
| 154 |
{ ">>", "GTGT" }, |
| 155 |
{ ">>=", "GTGT_EQ" }, |
| 156 |
{ "<<=", "LTLT_EQ" }, |
| 157 |
{ "==", "EQEQ" }, |
| 158 |
{ "!=", "NE" }, |
| 159 |
{ "not_eq", "NE" }, |
| 160 |
{ "<=", "LE" }, |
| 161 |
{ ">=", "GE" }, |
| 162 |
{ "&&", "ANDAND" }, |
| 163 |
{ "||", "OROR" }, |
| 164 |
{ "++", "INCR" }, |
| 165 |
{ "--", "DECR" }, |
| 166 |
{ ",", "COMMA" }, |
| 167 |
{ "->*", "ARROW_STAR" }, |
| 168 |
{ "->", "ARROW" }, |
| 169 |
{ "char", "CHAR" }, |
| 170 |
{ "wchar", "WCHAR" }, |
| 171 |
{ "bool", "BOOL" }, |
| 172 |
{ "short", "SHORT" }, |
| 173 |
{ "int", "INT" }, |
| 174 |
{ "long", "LONG" }, |
| 175 |
{ "signed", "SIGNED" }, |
| 176 |
{ "unsigned", "UNSIGNED" }, |
| 177 |
{ "float", "FLOAT" }, |
| 178 |
{ "double", "DOUBLE" }, |
| 179 |
{ "void", "VOID" }, |
| 180 |
{ "case", "CASE" }, |
| 181 |
{ "default", "DEFAULT" }, |
| 182 |
{ "if", "IF" }, |
| 183 |
{ "else", "ELSE" }, |
| 184 |
{ "switch", "SWITCH" }, |
| 185 |
{ "while", "WHILE" }, |
| 186 |
{ "do", "DO" }, |
| 187 |
{ "for", "FOR" }, |
| 188 |
{ "break", "BREAK" }, |
| 189 |
{ "continue", "CONTINUE" }, |
| 190 |
{ "goto", "GOTO" }, |
| 191 |
{ "return", "RETURN" }, |
| 192 |
{ "Q_OBJECT", "Q_OBJECT_TOKEN" }, |
| 193 |
{ "Q_NAMESPACE", "Q_NAMESPACE_TOKEN" }, |
| 194 |
{ "Q_NAMESPACE_EXPORT", "Q_NAMESPACE_EXPORT_TOKEN" }, |
| 195 |
{ "Q_GADGET", "Q_GADGET_TOKEN" }, |
| 196 |
{ "Q_GADGET_EXPORT", "Q_GADGET_EXPORT_TOKEN" }, |
| 197 |
{ "Q_PROPERTY", "Q_PROPERTY_TOKEN" }, |
| 198 |
{ "QT_ANONYMOUS_PROPERTY", "QT_ANONYMOUS_PROPERTY_TOKEN" }, |
| 199 |
{ "Q_PLUGIN_METADATA", "Q_PLUGIN_METADATA_TOKEN" }, |
| 200 |
{ "Q_ENUMS", "Q_ENUMS_TOKEN" }, |
| 201 |
{ "Q_ENUM", "Q_ENUM_TOKEN" }, |
| 202 |
{ "Q_ENUM_NS", "Q_ENUM_NS_TOKEN" }, |
| 203 |
{ "Q_FLAGS", "Q_FLAGS_TOKEN" }, |
| 204 |
{ "Q_FLAG", "Q_FLAG_TOKEN" }, |
| 205 |
{ "Q_FLAG_NS", "Q_FLAG_NS_TOKEN" }, |
| 206 |
{ "Q_DECLARE_FLAGS", "Q_DECLARE_FLAGS_TOKEN" }, |
| 207 |
{ "Q_DECLARE_INTERFACE", "Q_DECLARE_INTERFACE_TOKEN" }, |
| 208 |
{ "Q_DECLARE_METATYPE", "Q_DECLARE_METATYPE_TOKEN" }, |
| 209 |
{ "Q_DECLARE_EXTENSION_INTERFACE", "Q_DECLARE_INTERFACE_TOKEN" }, |
| 210 |
{ "Q_SETS", "Q_FLAGS_TOKEN" }, |
| 211 |
{ "Q_CLASSINFO", "Q_CLASSINFO_TOKEN" }, |
| 212 |
{ "Q_INTERFACES", "Q_INTERFACES_TOKEN" }, |
| 213 |
{ "signals", "SIGNALS" }, |
| 214 |
{ "slots", "SLOTS" }, |
| 215 |
{ "Q_SIGNALS", "Q_SIGNALS_TOKEN" }, |
| 216 |
{ "Q_SLOTS", "Q_SLOTS_TOKEN" }, |
| 217 |
{ "Q_PRIVATE_SLOT", "Q_PRIVATE_SLOT_TOKEN" }, |
| 218 |
{ "QT_MOC_COMPAT", "Q_MOC_COMPAT_TOKEN" }, |
| 219 |
{ "Q_INVOKABLE", "Q_INVOKABLE_TOKEN" }, |
| 220 |
{ "Q_SIGNAL", "Q_SIGNAL_TOKEN" }, |
| 221 |
{ "Q_SLOT", "Q_SLOT_TOKEN" }, |
| 222 |
{ "Q_SCRIPTABLE", "Q_SCRIPTABLE_TOKEN" }, |
| 223 |
{ "Q_PRIVATE_PROPERTY", "Q_PRIVATE_PROPERTY_TOKEN" }, |
| 224 |
{ "QT_ANONYMOUS_PRIVATE_PROPERTY", "QT_ANONYMOUS_PRIVATE_PROPERTY_TOKEN" }, |
| 225 |
{ "Q_REVISION", "Q_REVISION_TOKEN" }, |
| 226 |
{ "Q_MOC_INCLUDE", "Q_MOC_INCLUDE_TOKEN" }, |
| 227 |
{ "\n", "NEWLINE" }, |
| 228 |
{ "\"", "QUOTE" }, |
| 229 |
{ "\'", "SINGLEQUOTE" }, |
| 230 |
{ " ", "WHITESPACE" }, |
| 231 |
{ "\t", "WHITESPACE" }, |
| 232 |
{ "#", "HASH" }, |
| 233 |
{ "##", "PP_HASHHASH" }, |
| 234 |
{ "\\", "BACKSLASH" }, |
| 235 |
{ "//", "CPP_COMMENT" }, |
| 236 |
{ "/*", "C_COMMENT" }, |
| 237 |
{ 0, "NOTOKEN"} |
| 238 |
}; |
| 239 |
|
| 240 |
|
| 241 |
inline bool is_ident_start(char s) |
| 242 |
{ |
| 243 |
return ((s >= 'a' && s <= 'z') |
| 244 |
|| (s >= 'A' && s <= 'Z') |
| 245 |
|| s == '_' || s == '$' |
| 246 |
); |
| 247 |
} |
| 248 |
|
| 249 |
inline bool is_ident_char(char s) |
| 250 |
{ |
| 251 |
return ((s >= 'a' && s <= 'z') |
| 252 |
|| (s >= 'A' && s <= 'Z') |
| 253 |
|| (s >= '0' && s <= '9') |
| 254 |
|| s == '_' || s == '$' |
| 255 |
); |
| 256 |
} |
| 257 |
struct State |
| 258 |
{ |
| 259 |
State(const char* token):token(token), nextindex(0), |
| 260 |
defchar(0), defnext(0), ident(0) { |
| 261 |
memset( next, 0, sizeof(next)); |
| 262 |
} |
| 263 |
QByteArray token; |
| 264 |
int next[128]; |
| 265 |
int nextindex; |
| 266 |
|
| 267 |
char defchar; |
| 268 |
int defnext; |
| 269 |
|
| 270 |
const char *ident; |
| 271 |
|
| 272 |
bool operator==(const State& o) const |
| 273 |
{ |
| 274 |
return (token == o.token |
| 275 |
&& nextindex == o.nextindex |
| 276 |
&& defchar == o.defchar |
| 277 |
&& defnext == o.defnext |
| 278 |
&& ident == o.ident); |
| 279 |
} |
| 280 |
}; |
| 281 |
|
| 282 |
void newState(QList<State> &states, const char *token, const char *lexem, bool pre) |
| 283 |
{ |
| 284 |
const char *ident = nullptr; |
| 285 |
if (is_ident_start(*lexem)) |
| 286 |
ident = pre?"PP_CHARACTER" : "CHARACTER"; |
| 287 |
else if (*lexem == '#') |
| 288 |
ident = pre?"PP_HASH" : "HASH"; |
| 289 |
|
| 290 |
int state = 0; |
| 291 |
while (*lexem) { |
| 292 |
int next = states[state].next[(int)*lexem]; |
| 293 |
if (!next) { |
| 294 |
const char *t = nullptr; |
| 295 |
if (ident) |
| 296 |
t = ident; |
| 297 |
else |
| 298 |
t = pre?"PP_INCOMPLETE":"INCOMPLETE"; |
| 299 |
next = states.size(); |
| 300 |
states += State(t); |
| 301 |
states[state].next[(int)*lexem] = next; |
| 302 |
states[next].ident = ident; |
| 303 |
} |
| 304 |
state = next; |
| 305 |
++lexem; |
| 306 |
if (ident && !is_ident_char(*lexem)) |
| 307 |
ident = nullptr; |
| 308 |
} |
| 309 |
states[state].token = token; |
| 310 |
} |
| 311 |
|
| 312 |
void newState(QList<State> &states, const char *token, char lexem) |
| 313 |
{ |
| 314 |
int next = states[0].next[(int)lexem]; |
| 315 |
if (!next) { |
| 316 |
next = states.size(); |
| 317 |
states += State(token); |
| 318 |
states[0].next[(int)lexem] = next; |
| 319 |
} else { |
| 320 |
states[next].token = token; |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
void makeTable(const Keyword keywords[]) |
| 326 |
{ |
| 327 |
int i,c; |
| 328 |
bool pre = (keywords == pp_keywords); |
| 329 |
QList<State> states; |
| 330 |
states += State(pre?"PP_NOTOKEN":"NOTOKEN"); |
| 331 |
|
| 332 |
// identifiers |
| 333 |
for (c = 'a'; c <= 'z'; ++c) |
| 334 |
newState(states, pre?"PP_CHARACTER":"CHARACTER", c); |
| 335 |
for (c = 'A'; c <= 'Z'; ++c) |
| 336 |
newState(states, pre?"PP_CHARACTER":"CHARACTER", c); |
| 337 |
|
| 338 |
newState(states, pre?"PP_CHARACTER":"CHARACTER", '_'); |
| 339 |
newState(states, pre?"PP_CHARACTER":"CHARACTER", '$'); |
| 340 |
|
| 341 |
// add digits |
| 342 |
for (c = '0'; c <= '9'; ++c) |
| 343 |
newState(states, pre?"PP_DIGIT":"DIGIT", c); |
| 344 |
|
| 345 |
// keywords |
| 346 |
for (i = 0; keywords[i].lexem; ++i) |
| 347 |
newState(states, keywords[i].token, keywords[i].lexem, pre); |
| 348 |
|
| 349 |
// some floats |
| 350 |
for (c = '0'; c <= '9'; ++c) |
| 351 |
newState(states, pre?"PP_FLOATING_LITERAL":"FLOATING_LITERAL", |
| 352 |
QByteArray(".") + char(c), pre); |
| 353 |
|
| 354 |
// simplify table with default transitions |
| 355 |
int transindex = -1; |
| 356 |
for (i = 0; i < states.size(); ++i) { |
| 357 |
int n = 0; |
| 358 |
int defchar = -1; |
| 359 |
for (c = 0; c < 128; ++c) |
| 360 |
if (states[i].next[c]) { |
| 361 |
++n; |
| 362 |
defchar = c; |
| 363 |
} |
| 364 |
if (!n) |
| 365 |
continue; |
| 366 |
if (n == 1) { |
| 367 |
states[i].defnext = states[i].next[defchar]; |
| 368 |
states[i].defchar = defchar; |
| 369 |
continue; |
| 370 |
} |
| 371 |
states[i].nextindex = ++transindex; |
| 372 |
} |
| 373 |
|
| 374 |
#if 1 |
| 375 |
// compress table |
| 376 |
int j, k; |
| 377 |
for (i = 0; i < states.size(); ++i) { |
| 378 |
for (j = i + 1; j < states.size(); ++j) { |
| 379 |
if ( states[i] == states[j] ) { |
| 380 |
for (k = 0; k < states.size(); ++k) { |
| 381 |
if (states[k].defnext == j) |
| 382 |
states[k].defnext = i; |
| 383 |
if (states[k].defnext > j) |
| 384 |
--states[k].defnext; |
| 385 |
for (c = 0; c < 128; ++c) { |
| 386 |
if (states[k].next[c] == j) |
| 387 |
states[k].next[c] = i; |
| 388 |
if (states[k].next[c] > j) |
| 389 |
--states[k].next[c]; |
| 390 |
} |
| 391 |
} |
| 392 |
states.removeAt(j); |
| 393 |
--j; |
| 394 |
} |
| 395 |
} |
| 396 |
} |
| 397 |
#endif |
| 398 |
printf("static const short %skeyword_trans[][128] = {\n", |
| 399 |
pre?"pp_":""); |
| 400 |
for (i = 0; i < states.size(); ++i) { |
| 401 |
if (i && !states[i].nextindex) |
| 402 |
continue; |
| 403 |
printf("%s {", i?",\n":""); |
| 404 |
for (c = 0; c < 128; ++c) |
| 405 |
printf("%s%s%d", |
| 406 |
c?",":"", |
| 407 |
(!c || c%16)?"":"\n ", |
| 408 |
states[i].next[c] |
| 409 |
); |
| 410 |
printf("}"); |
| 411 |
} |
| 412 |
printf("\n};\n\n"); |
| 413 |
|
| 414 |
printf("static const struct\n{\n" |
| 415 |
" %sToken token;\n" |
| 416 |
" short next;\n" |
| 417 |
" char defchar;\n" |
| 418 |
" short defnext;\n" |
| 419 |
" %sToken ident;\n" |
| 420 |
"} %skeywords[] = {\n", |
| 421 |
pre ? "PP_":"", |
| 422 |
pre ? "PP_":"", |
| 423 |
pre ? "pp_":""); |
| 424 |
for (i = 0; i < states.size(); ++i) { |
| 425 |
printf("%s {%s, %d, %d, %d, %s}", |
| 426 |
i?",\n":"", |
| 427 |
states[i].token.data(), |
| 428 |
states[i].nextindex, |
| 429 |
states[i].defchar, |
| 430 |
states[i].defnext, |
| 431 |
states[i].ident?states[i].ident:(pre?"PP_NOTOKEN":"NOTOKEN")); |
| 432 |
} |
| 433 |
printf("\n};\n"); |
| 434 |
} |
| 435 |
|
| 436 |
int main(int argc, char **) |
| 437 |
{ |
| 438 |
printf("// auto generated\n" |
| 439 |
"// DO NOT EDIT.\n\n"); |
| 440 |
if ( argc > 1 ) |
| 441 |
makeTable(pp_keywords); |
| 442 |
else |
| 443 |
makeTable(keywords); |
| 444 |
return 0; |
| 445 |
} |