]> git.draconx.ca Git - cdecl99.git/blob - src/keywords.gperf
22566aa8b84936183195087b6e4927e368addef3
[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,           (T_BOOL      & 0x7f)
41 _Complex,        (T_COMPLEX   & 0x7f)
42 _Imaginary,      (T_IMAGINARY & 0x7f)
43 auto,            (T_AUTO      & 0x7f)
44 char,            (T_CHAR      & 0x7f)
45 const,           (T_CONST     & 0x7f)
46 double,          (T_DOUBLE    & 0x7f)
47 enum,            (T_ENUM      & 0x7f)
48 extern,          (T_EXTERN    & 0x7f)
49 float,           (T_FLOAT     & 0x7f)
50 inline,          (T_INLINE    & 0x7f)
51 int,             (T_INT       & 0x7f)
52 long,            (T_LONG      & 0x7f)
53 register,        (T_REGISTER  & 0x7f)
54 restrict,        (T_RESTRICT  & 0x7f)
55 short,           (T_SHORT     & 0x7f)
56 signed,          (T_SIGNED    & 0x7f)
57 static,          (T_STATIC    & 0x7f)
58 struct,          (T_STRUCT    & 0x7f)
59 typedef,         (T_TYPEDEF   & 0x7f)
60 union,           (T_UNION     & 0x7f)
61 unsigned,        (T_UNSIGNED  & 0x7f)
62 void,            (T_VOID      & 0x7f)
63 volatile,        (T_VOLATILE  & 0x7f)
64 # english keywords
65 array,           (T_ARRAY     & 0x7f) | 0x80
66 as,              (T_AS        & 0x7f) | 0x80
67 declare,         (T_DECLARE   & 0x7f) | 0x80
68 function,        (T_FUNCTION  & 0x7f) | 0x80
69 of,              (T_OF        & 0x7f) | 0x80
70 pointer,         (T_POINTER   & 0x7f) | 0x80
71 returning,       (T_RETURNING & 0x7f) | 0x80
72 to,              (T_TO        & 0x7f) | 0x80
73 type,            (T_TYPE      & 0x7f) | 0x80
74 variable-length, (T_VLA       & 0x7f) | 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                         if (T_VOID >= 256)
85                                 x += 256;
86                         return x;
87                 }
88         }
89
90         return T_IDENT;
91 }
92
93 static const char *wordlist_func(const struct keyword *k)
94 {
95         unsigned x = k->token & 0x7f;
96
97         if (!x)
98                 return NULL;
99
100         if (T_VOID >= 256)
101                 x += 256;
102
103         return cdecl__token_name(x);
104 }