]> git.draconx.ca Git - cdecl99.git/blob - src/scan.l
libcdecl: Implement portable alternative to strtoumax.
[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 #include "intconv.h"
37
38 static char *to_octal(char *dst, unsigned val)
39 {
40         unsigned i;
41
42         for (i = 0; i < 3; i++) {
43                 *dst++ = '0' + ((val >> 6) & 7u);
44                 val <<= 3;
45         }
46
47         return dst;
48 }
49
50 /*
51  * Convert a single character to a C-style character constant, including quote
52  * characters.  At most 7 bytes are written to the buffer for the longest
53  * octal encoding, e.g., '\177'
54  */
55 static void to_readable_ch(char *dst, char c)
56 {
57         unsigned char uc = c;
58         unsigned i;
59         char esc;
60
61         /*
62          * The 7 standard C control characters are contiguous in ASCII,
63          * permitting a simple and compact lookup table; separating their
64          * handling from backslash and quote characters hopefully allows
65          * the compiler to recognize that.
66          */
67         switch (c) {
68         case '\a': i = 0; break;
69         case '\b': i = 1; break;
70         case '\t': i = 2; break;
71         case '\n': i = 3; break;
72         case '\v': i = 4; break;
73         case '\f': i = 5; break;
74         case '\r': i = 6; break;
75         default:   i = 7; break;
76         }
77         esc = "abtnvfr"[i];
78
79         /* Otherwise printable characters that should still be escaped. */
80         switch (c) {
81         case '\\': case '\'': esc = c; break;
82         }
83
84         *dst++ = '\'';
85         if (esc) {
86                 *dst++ = '\\';
87                 *dst++ = esc;
88         } else if (isprint(uc)) {
89                 *dst++ = c;
90         } else {
91                 *dst++ = '\\';
92                 dst = to_octal(dst, uc);
93         }
94         *dst++ = '\'';
95         *dst++ = 0;
96 }
97
98 %}
99
100 IDENT [_[:alpha:]][-_[:alnum:]]*
101
102 %%
103
104 %{
105         int intconv_base;
106         char *c;
107 %}
108
109 "..."|[][;*(),] {
110         unsigned char *match;
111         static const unsigned char tab[2][8] = {
112                 "*[](),.;",
113                 {
114                         PACK_TOKEN(T_ASTERISK),
115                         PACK_TOKEN(T_LBRACKET),
116                         PACK_TOKEN(T_RBRACKET),
117                         PACK_TOKEN(T_LPAREN),
118                         PACK_TOKEN(T_RPAREN),
119                         PACK_TOKEN(T_COMMA),
120                         PACK_TOKEN(T_ELLIPSIS),
121                         PACK_TOKEN(T_SEMICOLON)
122                 }
123         };
124
125         match = memchr(&tab, yytext[0], sizeof tab[0]);
126         return UNPACK_TOKEN(match[sizeof tab[0]]);
127 }
128
129 0[0-7]* { intconv_base = INTCONV_OCTAL; goto int_parse; }
130 [1-9][0-9]* { intconv_base = INTCONV_DECIMAL; goto int_parse; }
131 0[Xx][[:xdigit:]]+ {
132         unsigned char d;
133         uintmax_t v;
134
135         yytext += 2;
136         intconv_base = INTCONV_HEXADECIMAL;
137 int_parse:
138         for (v = 0; (d = *yytext++);) {
139                 if (!intconv_shift(&v, intconv_base, intconv_digit(d))) {
140                         cdecl__errmsg(CDECL__ERANGE);
141                         return T_LEX_ERROR;
142                 }
143         }
144
145         yylval->uintval = v;
146         return T_UINT;
147 }
148 0[Xx]|[0-9]+ {
149         cdecl__errmsg(CDECL__EBADINT);
150         return T_LEX_ERROR;
151 }
152
153 {IDENT} {
154         int len = yyleng, tok;
155         unsigned x;
156
157         x = cdecl__to_keyword(yytext, len, yyextra);
158         yylval->spectype = UNPACK_SPEC(x & 0xff);
159         if ((tok = (x >> 8)) == PACK_TOKEN(T_IDENT)) {
160                 /*
161                  * Our IDENT pattern includes hyphens so we can match
162                  * "variable-length" as a keyword.  In all other cases a
163                  * hyphen is an error.
164                  *
165                  * We could use yyless to re-scan the hyphen and hit the
166                  * error catch-all, but jumping straight to the error code
167                  * seems to produce better results with gcc with no obvious
168                  * downsides.
169                  */
170 #if 1
171                 if ((c = memchr(yytext, '-', len)))
172                         goto invalid_char;
173 #else
174                 yyless(strcspn(yytext, "-"));
175 #endif
176                 if (!(yylval->item = cdecl__alloc_item(len+1)))
177                         return T_LEX_ERROR;
178                 memcpy(yylval->item->s, yytext, len+1);
179         }
180         return UNPACK_TOKEN(tok);
181 }
182
183 [[:space:]]+
184 . {
185         char buf[8];
186
187         c = yytext;
188 invalid_char:
189         to_readable_ch(buf, *c);
190         cdecl__err(CDECL_ENOPARSE, _("syntax error, unexpected %s"), buf);
191         return T_LEX_ERROR;
192 }