pub const C23_KEYWORDS: &[&str] = &[
"auto",
"break",
"case",
"char",
"const",
"constexpr",
"continue",
"default",
"do",
"double",
"else",
"enum",
"extern",
"float",
"for",
"goto",
"if",
"inline",
"int",
"long",
"register",
"restrict",
"return",
"short",
"signed",
"sizeof",
"static",
"struct",
"switch",
"typedef",
"union",
"unsigned",
"void",
"volatile",
"while",
"alignas",
"alignof",
"bool",
"false",
"nullptr",
"static_assert",
"thread_local",
"true",
"typeof",
"typeof_unqual",
"_Alignas",
"_Alignof",
"_Atomic",
"_BitInt",
"_Bool",
"_Complex",
"_Decimal32",
"_Decimal64",
"_Decimal128",
"_Generic",
"_Imaginary",
"_Noreturn",
"_Static_assert",
"_Thread_local",
];
pub const EXT_KEYWORDS: &[&str] = &[
"__attribute__",
"__attribute",
"__asm__",
"__asm",
"asm",
"__inline__",
"__inline",
"__signed__",
"__signed",
"__volatile__",
"__volatile",
"__const__",
"__const",
"__restrict__",
"__restrict",
"__typeof__",
"__typeof",
"__alignof__",
"__alignof",
"__extension__",
"__label__",
"__thread",
"__auto_type",
"__builtin_va_arg",
"__builtin_va_list",
"__builtin_offsetof",
"__builtin_choose_expr",
"__builtin_types_compatible_p",
"__real__",
"__imag__",
"__complex__",
"__func__",
"__FUNCTION__",
"__PRETTY_FUNCTION__",
"__declspec",
"__cdecl",
"__stdcall",
"__fastcall",
"__thiscall",
"__vectorcall",
"__forceinline",
"__int8",
"__int16",
"__int32",
"__int64",
"__ptr32",
"__ptr64",
"__unaligned",
"__w64",
"__pragma",
"_Nonnull",
"_Nullable",
"_Null_unspecified",
];
pub const PUNCTUATORS: &[(&str, &str)] = &[
("PUNC_ELLIPSIS", "..."),
("PUNC_LSHIFT_ASSIGN", "<<="),
("PUNC_RSHIFT_ASSIGN", ">>="),
("PUNC_HASH_HASH_ALT", "%:%:"),
("PUNC_ARROW", "->"),
("PUNC_PLUS_PLUS", "++"),
("PUNC_MINUS_MINUS", "--"),
("PUNC_LSHIFT", "<<"),
("PUNC_RSHIFT", ">>"),
("PUNC_LE", "<="),
("PUNC_GE", ">="),
("PUNC_EQ", "=="),
("PUNC_NE", "!="),
("PUNC_AND_AND", "&&"),
("PUNC_OR_OR", "||"),
("PUNC_PLUS_ASSIGN", "+="),
("PUNC_MINUS_ASSIGN", "-="),
("PUNC_STAR_ASSIGN", "*="),
("PUNC_SLASH_ASSIGN", "/="),
("PUNC_PERCENT_ASSIGN", "%="),
("PUNC_AMP_ASSIGN", "&="),
("PUNC_CARET_ASSIGN", "^="),
("PUNC_PIPE_ASSIGN", "|="),
("PUNC_HASH_HASH", "##"),
("PUNC_LBRACKET_ALT", "<:"),
("PUNC_RBRACKET_ALT", ":>"),
("PUNC_LBRACE_ALT", "<%"),
("PUNC_RBRACE_ALT", "%>"),
("PUNC_HASH_ALT", "%:"),
("PUNC_LPAREN", "("),
("PUNC_RPAREN", ")"),
("PUNC_LBRACE", "{"),
("PUNC_RBRACE", "}"),
("PUNC_LBRACKET", "["),
("PUNC_RBRACKET", "]"),
("PUNC_SEMI", ";"),
("PUNC_COMMA", ","),
("PUNC_DOT", "."),
("PUNC_QUESTION", "?"),
("PUNC_COLON", ":"),
("PUNC_ASSIGN", "="),
("PUNC_PLUS", "+"),
("PUNC_MINUS", "-"),
("PUNC_STAR", "*"),
("PUNC_SLASH", "/"),
("PUNC_PERCENT", "%"),
("PUNC_AMP", "&"),
("PUNC_PIPE", "|"),
("PUNC_CARET", "^"),
("PUNC_TILDE", "~"),
("PUNC_BANG", "!"),
("PUNC_LT", "<"),
("PUNC_GT", ">"),
("PUNC_HASH", "#"),
("PUNC_AT", "@"),
("PUNC_BACKSLASH", "\\"),
];
pub const SPECIAL_TOKENS: &[&str] = &[
"ID",
"TYPEDEF_NAME",
"MACRO_NAME",
"LIT_INT",
"LIT_FLOAT",
"LIT_CHAR",
"LIT_STRING",
"LIT_HEADER_NAME",
"PP_HASH",
"PP_NEWLINE",
"PP_RAW",
"TRIVIA_LINE_COMMENT",
"TRIVIA_BLOCK_COMMENT",
"TRIVIA_LINE_CONT",
];
pub fn is_reserved(word: &str) -> bool {
C23_KEYWORDS.contains(&word) || EXT_KEYWORDS.contains(&word)
}
pub fn keyword_token_name_unchecked(word: &str) -> String {
let mut out = String::with_capacity(word.len() + 3);
out.push_str("KW_");
for ch in word.chars() {
if ch.is_ascii_alphanumeric() || ch == '_' {
out.extend(ch.to_uppercase());
} else {
out.push('_');
}
}
out
}
pub fn keyword_token_names() -> Vec<String> {
C23_KEYWORDS
.iter()
.chain(EXT_KEYWORDS.iter())
.map(|keyword| keyword_token_name_unchecked(keyword))
.collect()
}
pub fn any_c_token_names() -> Vec<String> {
let mut names: Vec<String> = [
"ID",
"TYPEDEF_NAME",
"MACRO_NAME",
"LIT_INT",
"LIT_FLOAT",
"LIT_CHAR",
"LIT_STRING",
"LIT_HEADER_NAME",
"PP_HASH",
"PP_NEWLINE",
"PP_RAW",
]
.iter()
.map(|name| (*name).to_string())
.collect();
for (name, _) in PUNCTUATORS {
names.push((*name).to_string());
}
names.extend(keyword_token_names());
names
}