]> git.draconx.ca Git - cdecl99.git/blob - src/scan.l
2b92846678a911fc2e5f92d5e46f0ce024623012
[cdecl99.git] / src / scan.l
1 %top{
2 /*
3  *  Scanner for C declarations.
4  *  Copyright © 2011, 2021, 2023 Nick Bowler
5  *
6  *  This program is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <config.h>
21 #include "parse.h"
22
23 #define YY_NO_INPUT 1
24 #define YY_NO_UNPUT 1
25 }
26
27 %option nodefault noyywrap bison-locations reentrant never-interactive
28 %option extra-type="int"
29 %option prefix="cdecl__yy"
30
31 %{
32 #include <ctype.h>
33 #include "cdecl-internal.h"
34 #include "cdecl.h"
35 #include "errmsg.h"
36
37 #if HAVE_STRTOUMAX
38 /* Best case, implementation provides strtoumax. */
39 #  define STRTOUMAX strtoumax
40 #elif HAVE_STRTOULL
41 /* Fall back to strtoull, with possibly reduced range. */
42 #define STRTOUMAX strtoull
43 #elif HAVE___STRTOULL
44 /* HP-UX 11 has __strtoull in <inttypes.h> */
45 #define STRTOUMAX __strtoull
46 #else
47 /* Fall back to strtoul, with possibly reduced range. */
48 #define STRTOUMAX strtoul
49 #endif
50
51 static char *to_octal(char *dst, unsigned val)
52 {
53         unsigned i;
54
55         for (i = 0; i < 3; i++) {
56                 *dst++ = '0' + ((val >> 6) & 7u);
57                 val <<= 3;
58         }
59
60         return dst;
61 }
62
63 /*
64  * Convert a single character to a C-style character constant, including quote
65  * characters.  At most 7 bytes are written to the buffer for the longest
66  * octal encoding, e.g., '\177'
67  */
68 static void to_readable_ch(char *dst, char c)
69 {
70         unsigned char uc = c;
71         unsigned i;
72         char esc;
73
74         /*
75          * The 7 standard C control characters are contiguous in ASCII,
76          * permitting a simple and compact lookup table; separating their
77          * handling from backslash and quote characters hopefully allows
78          * the compiler to recognize that.
79          */
80         switch (c) {
81         case '\a': i = 0; break;
82         case '\b': i = 1; break;
83         case '\t': i = 2; break;
84         case '\n': i = 3; break;
85         case '\v': i = 4; break;
86         case '\f': i = 5; break;
87         case '\r': i = 6; break;
88         default:   i = 7; break;
89         }
90         esc = "abtnvfr"[i];
91
92         /* Otherwise printable characters that should still be escaped. */
93         switch (c) {
94         case '\\': case '\'': esc = c; break;
95         }
96
97         *dst++ = '\'';
98         if (esc) {
99                 *dst++ = '\\';
100                 *dst++ = esc;
101         } else if (isprint(uc)) {
102                 *dst++ = c;
103         } else {
104                 *dst++ = '\\';
105                 dst = to_octal(dst, uc);
106         }
107         *dst++ = '\'';
108         *dst++ = 0;
109 }
110
111 %}
112
113 IDENT [_[:alpha:]][-_[:alnum:]]*
114 INTEGER 0x[[:xdigit:]]+|0[0-7]+|[[:digit:]]+
115
116 %%
117
118 %{
119         char *c;
120 %}
121
122 "..."|[][;*(),] {
123         unsigned char *match;
124         static const unsigned char tab[2][8] = {
125                 "*[](),.;",
126                 {
127                         PACK_TOKEN(T_ASTERISK),
128                         PACK_TOKEN(T_LBRACKET),
129                         PACK_TOKEN(T_RBRACKET),
130                         PACK_TOKEN(T_LPAREN),
131                         PACK_TOKEN(T_RPAREN),
132                         PACK_TOKEN(T_COMMA),
133                         PACK_TOKEN(T_ELLIPSIS),
134                         PACK_TOKEN(T_SEMICOLON)
135                 }
136         };
137
138         match = memchr(&tab, yytext[0], sizeof tab[0]);
139         return UNPACK_TOKEN(match[sizeof tab[0]]);
140 }
141
142 {INTEGER} {
143         char *end;
144
145         errno = 0;
146         yylval->uintval = STRTOUMAX(yytext, &end, 0);
147         if (errno == ERANGE) {
148                 cdecl__errmsg(CDECL__ERANGE);
149                 return T_LEX_ERROR;
150         }
151         if (*end) {
152                 cdecl__errmsg(CDECL__EBADINT);
153                 return T_LEX_ERROR;
154         }
155
156         return T_UINT;
157 }
158
159 {IDENT} {
160         int len = yyleng, tok;
161         unsigned x;
162
163         x = cdecl__to_keyword(yytext, len, yyextra);
164         yylval->spectype = UNPACK_SPEC(x & 0xff);
165         if ((tok = (x >> 8)) == PACK_TOKEN(T_IDENT)) {
166                 /*
167                  * Our IDENT pattern includes hyphens so we can match
168                  * "variable-length" as a keyword.  In all other cases a
169                  * hyphen is an error.
170                  *
171                  * We could use yyless to re-scan the hyphen and hit the
172                  * error catch-all, but jumping straight to the error code
173                  * seems to produce better results with gcc with no obvious
174                  * downsides.
175                  */
176 #if 1
177                 if ((c = memchr(yytext, '-', len)))
178                         goto invalid_char;
179 #else
180                 yyless(strcspn(yytext, "-"));
181 #endif
182                 if (!(yylval->item = cdecl__alloc_item(len+1)))
183                         return T_LEX_ERROR; \
184                 memcpy(yylval->item->s, yytext, len+1);
185         }
186         return UNPACK_TOKEN(tok);
187 }
188
189 [[:space:]]+
190 . {
191         char buf[8];
192
193         c = yytext;
194 invalid_char:
195         to_readable_ch(buf, *c);
196         cdecl__err(CDECL_ENOPARSE, _("syntax error, unexpected %s"), buf);
197         return T_LEX_ERROR;
198 }