| 1 |
#!/usr/bin/env python3 |
| 2 |
# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
|
| 5 |
C_KEYWORDS = ( |
| 6 |
"_Atomic", |
| 7 |
"_Bool", |
| 8 |
"char", |
| 9 |
"class", |
| 10 |
"const", |
| 11 |
"double", |
| 12 |
"enum", |
| 13 |
"float", |
| 14 |
"int", |
| 15 |
"long", |
| 16 |
"restrict", |
| 17 |
"short", |
| 18 |
"signed", |
| 19 |
"struct", |
| 20 |
"union", |
| 21 |
"unsigned", |
| 22 |
"void", |
| 23 |
"volatile", |
| 24 |
) |
| 25 |
|
| 26 |
|
| 27 |
def main() -> None: |
| 28 |
keywords = [ |
| 29 |
("C_TOKEN_" + keyword.replace("_", "").upper(), keyword) |
| 30 |
for keyword in C_KEYWORDS |
| 31 |
] |
| 32 |
print("/* Generated by libdrgn/build-aux/gen_c_keywords_inc_strswitch.py. */") |
| 33 |
print("static const char * const keyword_spelling[] = {") |
| 34 |
for token_kind, keyword in keywords: |
| 35 |
print(f'\t[{token_kind}] = "{keyword}",') |
| 36 |
print("};") |
| 37 |
print() |
| 38 |
print("static int identifier_token_kind(const char *s, size_t len, bool cpp)") |
| 39 |
print("{") |
| 40 |
print("\t@memswitch (s, len)@") |
| 41 |
for token_kind, keyword in keywords: |
| 42 |
print(f'\t@case "{keyword}"@') |
| 43 |
if keyword == "class": |
| 44 |
print(f"\t\treturn cpp ? {token_kind} : C_TOKEN_IDENTIFIER;") |
| 45 |
else: |
| 46 |
print(f"\t\treturn {token_kind};") |
| 47 |
print("\t@default@") |
| 48 |
print("\t\treturn C_TOKEN_IDENTIFIER;") |
| 49 |
print("\t@endswitch@") |
| 50 |
print("}") |
| 51 |
|
| 52 |
|
| 53 |
if __name__ == "__main__": |
| 54 |
main() |