]> git.draconx.ca Git - cdecl99.git/blob - src/keywords.gperf
libcdecl: Use gperf to identify keywords during scanning.
[cdecl99.git] / src / keywords.gperf
1 %{
2 /*
3  * Copyright © 2023 Nick Bowler
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17  */
18
19 #include <config.h>
20 #include <string.h>
21 #include <inttypes.h>
22 #include "cdecl-internal.h"
23 #include "parse.h"
24
25 static const struct keyword *in_word_set();
26 %}
27
28 %struct-type
29 %readonly-tables
30 %language=ANSI-C
31 %global-table
32 %pic
33
34 struct keyword {
35         int_least16_t  name;
36         uint_least8_t token;
37 };
38 %%
39 _Bool,           (T_BOOL      & 0x7f)
40 _Complex,        (T_COMPLEX   & 0x7f)
41 _Imaginary,      (T_IMAGINARY & 0x7f)
42 auto,            (T_AUTO      & 0x7f)
43 char,            (T_CHAR      & 0x7f)
44 const,           (T_CONST     & 0x7f)
45 double,          (T_DOUBLE    & 0x7f)
46 enum,            (T_ENUM      & 0x7f)
47 extern,          (T_EXTERN    & 0x7f)
48 float,           (T_FLOAT     & 0x7f)
49 inline,          (T_INLINE    & 0x7f)
50 int,             (T_INT       & 0x7f)
51 long,            (T_LONG      & 0x7f)
52 register,        (T_REGISTER  & 0x7f)
53 restrict,        (T_RESTRICT  & 0x7f)
54 short,           (T_SHORT     & 0x7f)
55 signed,          (T_SIGNED    & 0x7f)
56 static,          (T_STATIC    & 0x7f)
57 struct,          (T_STRUCT    & 0x7f)
58 typedef,         (T_TYPEDEF   & 0x7f)
59 union,           (T_UNION     & 0x7f)
60 unsigned,        (T_UNSIGNED  & 0x7f)
61 void,            (T_VOID      & 0x7f)
62 volatile,        (T_VOLATILE  & 0x7f)
63 # english keywords
64 array,           (T_ARRAY     & 0x7f) | 0x80
65 as,              (T_AS        & 0x7f) | 0x80
66 declare,         (T_DECLARE   & 0x7f) | 0x80
67 function,        (T_FUNCTION  & 0x7f) | 0x80
68 of,              (T_OF        & 0x7f) | 0x80
69 pointer,         (T_POINTER   & 0x7f) | 0x80
70 returning,       (T_RETURNING & 0x7f) | 0x80
71 to,              (T_TO        & 0x7f) | 0x80
72 type,            (T_TYPE      & 0x7f) | 0x80
73 variable-length, (T_VLA       & 0x7f) | 0x80
74 %%
75 int cdecl__to_keyword(const char *s, int len, int english_mode)
76 {
77         const struct keyword *k;
78
79         if ((k = in_word_set(s, len))) {
80                 unsigned x = (k->token & 0x7fu);
81
82                 if (english_mode || !(k->token & ~0x7fu)) {
83                         if (T_VOID >= 256)
84                                 x += 256;
85                         return x;
86                 }
87         }
88
89         return T_IDENT;
90 }