]> git.draconx.ca Git - cdecl99.git/blob - src/keywords.gperf
libcdecl: Use macros for packing tokens into bytes.
[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 %readonly-tables
29 %language=ANSI-C
30
31 /* Note: the following options enable gperf-wordwrap.awk to do its thing */
32 %define word-array-name wordlist_wrapped
33 %null-strings
34 %struct-type
35
36 struct keyword {
37         uint_least8_t token;
38 };
39 %%
40 _Bool,           PACK_TOKEN(T_BOOL     )
41 _Complex,        PACK_TOKEN(T_COMPLEX  )
42 _Imaginary,      PACK_TOKEN(T_IMAGINARY)
43 auto,            PACK_TOKEN(T_AUTO     )
44 char,            PACK_TOKEN(T_CHAR     )
45 const,           PACK_TOKEN(T_CONST    )
46 double,          PACK_TOKEN(T_DOUBLE   )
47 enum,            PACK_TOKEN(T_ENUM     )
48 extern,          PACK_TOKEN(T_EXTERN   )
49 float,           PACK_TOKEN(T_FLOAT    )
50 inline,          PACK_TOKEN(T_INLINE   )
51 int,             PACK_TOKEN(T_INT      )
52 long,            PACK_TOKEN(T_LONG     )
53 register,        PACK_TOKEN(T_REGISTER )
54 restrict,        PACK_TOKEN(T_RESTRICT )
55 short,           PACK_TOKEN(T_SHORT    )
56 signed,          PACK_TOKEN(T_SIGNED   )
57 static,          PACK_TOKEN(T_STATIC   )
58 struct,          PACK_TOKEN(T_STRUCT   )
59 typedef,         PACK_TOKEN(T_TYPEDEF  )
60 union,           PACK_TOKEN(T_UNION    )
61 unsigned,        PACK_TOKEN(T_UNSIGNED )
62 void,            PACK_TOKEN(T_VOID     )
63 volatile,        PACK_TOKEN(T_VOLATILE )
64 # english keywords
65 array,           PACK_TOKEN(T_ARRAY    ) | 0x80
66 as,              PACK_TOKEN(T_AS       ) | 0x80
67 declare,         PACK_TOKEN(T_DECLARE  ) | 0x80
68 function,        PACK_TOKEN(T_FUNCTION ) | 0x80
69 of,              PACK_TOKEN(T_OF       ) | 0x80
70 pointer,         PACK_TOKEN(T_POINTER  ) | 0x80
71 returning,       PACK_TOKEN(T_RETURNING) | 0x80
72 to,              PACK_TOKEN(T_TO       ) | 0x80
73 type,            PACK_TOKEN(T_TYPE     ) | 0x80
74 variable-length, PACK_TOKEN(T_VLA      ) | 0x80
75 %%
76 int cdecl__to_keyword(const char *s, int len, int english_mode)
77 {
78         const struct keyword *k;
79
80         if ((k = in_word_set(s, len))) {
81                 unsigned x = (k->token & 0x7fu);
82
83                 if (english_mode || !(k->token & ~0x7fu)) {
84                         return UNPACK_TOKEN(x);
85                 }
86         }
87
88         return T_IDENT;
89 }
90
91 static const char *wordlist_func(const struct keyword *k)
92 {
93         unsigned x = k->token & 0x7f;
94
95         if (!x)
96                 return NULL;
97         return cdecl__token_name(UNPACK_TOKEN(x));
98 }