| ... | @@ -277,83 +277,79 @@ pub const Token = struct { | ... | @@ -277,83 +277,79 @@ pub const Token = struct { |
| 277 | }; | 277 | }; |
| 278 | | 278 | |
| 279 | // TODO extensions | 279 | // TODO extensions |
| 280 | pub const keywords = [_]Keyword{ | 280 | pub const keywords = std.ComptimeStringMap(Id, .{ |
| 281 | Keyword.init("auto", .Keyword_auto), | 281 | .{"auto", .Keyword_auto}, |
| 282 | Keyword.init("break", .Keyword_break), | 282 | .{"break", .Keyword_break}, |
| 283 | Keyword.init("case", .Keyword_case), | 283 | .{"case", .Keyword_case}, |
| 284 | Keyword.init("char", .Keyword_char), | 284 | .{"char", .Keyword_char}, |
| 285 | Keyword.init("const", .Keyword_const), | 285 | .{"const", .Keyword_const}, |
| 286 | Keyword.init("continue", .Keyword_continue), | 286 | .{"continue", .Keyword_continue}, |
| 287 | Keyword.init("default", .Keyword_default), | 287 | .{"default", .Keyword_default}, |
| 288 | Keyword.init("do", .Keyword_do), | 288 | .{"do", .Keyword_do}, |
| 289 | Keyword.init("double", .Keyword_double), | 289 | .{"double", .Keyword_double}, |
| 290 | Keyword.init("else", .Keyword_else), | 290 | .{"else", .Keyword_else}, |
| 291 | Keyword.init("enum", .Keyword_enum), | 291 | .{"enum", .Keyword_enum}, |
| 292 | Keyword.init("extern", .Keyword_extern), | 292 | .{"extern", .Keyword_extern}, |
| 293 | Keyword.init("float", .Keyword_float), | 293 | .{"float", .Keyword_float}, |
| 294 | Keyword.init("for", .Keyword_for), | 294 | .{"for", .Keyword_for}, |
| 295 | Keyword.init("goto", .Keyword_goto), | 295 | .{"goto", .Keyword_goto}, |
| 296 | Keyword.init("if", .Keyword_if), | 296 | .{"if", .Keyword_if}, |
| 297 | Keyword.init("int", .Keyword_int), | 297 | .{"int", .Keyword_int}, |
| 298 | Keyword.init("long", .Keyword_long), | 298 | .{"long", .Keyword_long}, |
| 299 | Keyword.init("register", .Keyword_register), | 299 | .{"register", .Keyword_register}, |
| 300 | Keyword.init("return", .Keyword_return), | 300 | .{"return", .Keyword_return}, |
| 301 | Keyword.init("short", .Keyword_short), | 301 | .{"short", .Keyword_short}, |
| 302 | Keyword.init("signed", .Keyword_signed), | 302 | .{"signed", .Keyword_signed}, |
| 303 | Keyword.init("sizeof", .Keyword_sizeof), | 303 | .{"sizeof", .Keyword_sizeof}, |
| 304 | Keyword.init("static", .Keyword_static), | 304 | .{"static", .Keyword_static}, |
| 305 | Keyword.init("struct", .Keyword_struct), | 305 | .{"struct", .Keyword_struct}, |
| 306 | Keyword.init("switch", .Keyword_switch), | 306 | .{"switch", .Keyword_switch}, |
| 307 | Keyword.init("typedef", .Keyword_typedef), | 307 | .{"typedef", .Keyword_typedef}, |
| 308 | Keyword.init("union", .Keyword_union), | 308 | .{"union", .Keyword_union}, |
| 309 | Keyword.init("unsigned", .Keyword_unsigned), | 309 | .{"unsigned", .Keyword_unsigned}, |
| 310 | Keyword.init("void", .Keyword_void), | 310 | .{"void", .Keyword_void}, |
| 311 | Keyword.init("volatile", .Keyword_volatile), | 311 | .{"volatile", .Keyword_volatile}, |
| 312 | Keyword.init("while", .Keyword_while), | 312 | .{"while", .Keyword_while}, |
| 313 | | 313 | |
| 314 | // ISO C99 | 314 | // ISO C99 |
| 315 | Keyword.init("_Bool", .Keyword_bool), | 315 | .{"_Bool", .Keyword_bool}, |
| 316 | Keyword.init("_Complex", .Keyword_complex), | 316 | .{"_Complex", .Keyword_complex}, |
| 317 | Keyword.init("_Imaginary", .Keyword_imaginary), | 317 | .{"_Imaginary", .Keyword_imaginary}, |
| 318 | Keyword.init("inline", .Keyword_inline), | 318 | .{"inline", .Keyword_inline}, |
| 319 | Keyword.init("restrict", .Keyword_restrict), | 319 | .{"restrict", .Keyword_restrict}, |
| 320 | | 320 | |
| 321 | // ISO C11 | 321 | // ISO C11 |
| 322 | Keyword.init("_Alignas", .Keyword_alignas), | 322 | .{"_Alignas", .Keyword_alignas}, |
| 323 | Keyword.init("_Alignof", .Keyword_alignof), | 323 | .{"_Alignof", .Keyword_alignof}, |
| 324 | Keyword.init("_Atomic", .Keyword_atomic), | 324 | .{"_Atomic", .Keyword_atomic}, |
| 325 | Keyword.init("_Generic", .Keyword_generic), | 325 | .{"_Generic", .Keyword_generic}, |
| 326 | Keyword.init("_Noreturn", .Keyword_noreturn), | 326 | .{"_Noreturn", .Keyword_noreturn}, |
| 327 | Keyword.init("_Static_assert", .Keyword_static_assert), | 327 | .{"_Static_assert", .Keyword_static_assert}, |
| 328 | Keyword.init("_Thread_local", .Keyword_thread_local), | 328 | .{"_Thread_local", .Keyword_thread_local}, |
| 329 | | 329 | |
| 330 | // Preprocessor directives | 330 | // Preprocessor directives |
| 331 | Keyword.init("include", .Keyword_include), | 331 | .{"include", .Keyword_include}, |
| 332 | Keyword.init("define", .Keyword_define), | 332 | .{"define", .Keyword_define}, |
| 333 | Keyword.init("ifdef", .Keyword_ifdef), | 333 | .{"ifdef", .Keyword_ifdef}, |
| 334 | Keyword.init("ifndef", .Keyword_ifndef), | 334 | .{"ifndef", .Keyword_ifndef}, |
| 335 | Keyword.init("error", .Keyword_error), | 335 | .{"error", .Keyword_error}, |
| 336 | Keyword.init("pragma", .Keyword_pragma), | 336 | .{"pragma", .Keyword_pragma}, |
| 337 | }; | 337 | }); |
| 338 | | 338 | |
| 339 | // TODO perfect hash at comptime | | |
| 340 | // TODO do this in the preprocessor | 339 | // TODO do this in the preprocessor |
| 341 | pub fn getKeyword(bytes: []const u8, pp_directive: bool) ?Id { | 340 | pub fn getKeyword(bytes: []const u8, pp_directive: bool) ?Id { |
| 342 | var hash = std.hash_map.hashString(bytes); | 341 | if (keywords.get(bytes)) |id| { |
| 343 | for (keywords) |kw| { | 342 | switch (id) { |
| 344 | if (kw.hash == hash and mem.eql(u8, kw.bytes, bytes)) { | 343 | .Keyword_include, |
| 345 | switch (kw.id) { | 344 | .Keyword_define, |
| 346 | .Keyword_include, | 345 | .Keyword_ifdef, |
| 347 | .Keyword_define, | 346 | .Keyword_ifndef, |
| 348 | .Keyword_ifdef, | 347 | .Keyword_error, |
| 349 | .Keyword_ifndef, | 348 | .Keyword_pragma, |
| 350 | .Keyword_error, | 349 | => if (!pp_directive) return null, |
| 351 | .Keyword_pragma, | 350 | else => {}, |
| 352 | => if (!pp_directive) return null, | | |
| 353 | else => {}, | | |
| 354 | } | | |
| 355 | return kw.id; | | |
| 356 | } | 351 | } |
| | 352 | return id; |
| 357 | } | 353 | } |
| 358 | return null; | 354 | return null; |
| 359 | } | 355 | } |