OSDN Git Service

* objc/objc-act.c (UTAG_STATICS, UTAG_PROTOCOL_LIST, USERTYPE):
[pf3gnuchains/gcc-fork.git] / gcc / gengtype-lex.l
1 /* -*- indented-text -*- */
2 /* Process source files and output type information.
3    Copyright (C) 2002 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
22 %{
23 #define malloc xmalloc
24 #define realloc xrealloc
25
26 #include "hconfig.h"
27 #include "system.h"
28 #include "gengtype.h"
29 #include "gengtype-yacc.h"
30
31 static void update_lineno PARAMS ((const char *l, size_t len));
32
33 struct fileloc lexer_line;
34 int lexer_toplevel_done;
35
36 static void 
37 update_lineno (l, len)
38      const char *l;
39      size_t len;
40 {
41   while (len-- > 0)
42     if (*l++ == '\n')
43       lexer_line.line++;
44 }
45
46 %}
47
48 ID      [[:alpha:]][[:alnum:]_]*
49 WS      [[:space:]]+
50 IWORD   short|long|(un)?signed|char|int|HOST_WIDE_INT|bool|size_t
51 ITYPE   {IWORD}({WS}{IWORD})*
52
53 %x in_struct in_struct_comment in_comment in_yacc_escape
54 %option warn noyywrap nounput nodefault perf-report
55 %option 8bit never-interactive
56 %%
57
58 [^[:alnum:]_]typedef{WS}(struct|union){WS}{ID}{WS}?[*[:space:]]{WS}?{ID}{WS}?";" {
59   char *tagstart;
60   size_t taglen;
61   char *namestart;
62   size_t namelen;
63   int is_pointer = 0;
64   struct type *t;
65   int union_p;
66
67   tagstart = yytext + strlen (" typedef ");
68   while (ISSPACE (*tagstart))
69     tagstart++;
70   union_p = tagstart[0] == 'u';
71   tagstart += strlen ("union ");
72   while (ISSPACE (*tagstart))
73     tagstart++;
74   for (taglen = 1; ISIDNUM (tagstart[taglen]); taglen++)
75     ;
76   for (namestart = tagstart + taglen; 
77        ! ISIDNUM (*namestart);
78        namestart++)
79     if (*namestart == '*')
80       is_pointer = 1;
81   for (namelen = 1; ISIDNUM (namestart[namelen]); namelen++)
82     ;
83   t = find_structure (xmemdup (tagstart, taglen, taglen+1), union_p);
84   if (is_pointer)
85     t = create_pointer (t);
86   do_typedef (xmemdup (namestart, namelen, namelen+1), t, &lexer_line);
87   update_lineno (yytext, yyleng);
88 }
89
90 [^[:alnum:]_]typedef{WS}{ITYPE}{WS}{ID}{WS}?";" {
91
92   char *namestart;
93   size_t namelen;
94   struct type *t;
95   char *typestart;
96   size_t typelen;
97
98   for (namestart = yytext + yyleng - 2; ISSPACE (*namestart); namestart--)
99     ;
100   for (namelen = 1; !ISSPACE (namestart[-namelen]); namelen++)
101     ;
102   namestart -= namelen - 1;
103   for (typestart = yytext + strlen (" typedef "); 
104        ISSPACE(*typestart);
105        typestart++)
106     ;
107   for (typelen = namestart - typestart; 
108        ISSPACE(typestart[typelen-1]); 
109        typelen--)
110     ;
111
112   t = create_scalar_type (typestart, typelen);
113   do_typedef (xmemdup (namestart, namelen, namelen+1), t, &lexer_line);
114   update_lineno (yytext, yyleng);
115 }
116
117 [^[:alnum:]_]typedef{WS}{ID}{WS}{ID}{WS}PARAMS {
118   char *namestart;
119   size_t namelen;
120   struct type *t;
121
122   for (namestart = yytext + yyleng - 7; ISSPACE (*namestart); namestart--)
123     ;
124   for (namelen = 1; !ISSPACE (namestart[-namelen]); namelen++)
125     ;
126   namestart -= namelen - 1;
127
128   t = create_scalar_type ("function type", sizeof ("function type")-1);
129   do_typedef (xmemdup (namestart, namelen, namelen+1), t, &lexer_line);
130   update_lineno (yytext, yyleng);
131 }
132 [^[:alnum:]_]typedef{WS}{ID}{WS}?"("{WS}?"*"{WS}?{ID}{WS}?")"{WS}?PARAMS {
133   char *namestart;
134   size_t namelen;
135   struct type *t;
136
137   for (namestart = yytext + yyleng - 7; !ISIDNUM (*namestart); namestart--)
138     ;
139   for (namelen = 1; ISIDNUM (namestart[-namelen]); namelen++)
140     ;
141   namestart -= namelen - 1;
142
143   t = create_scalar_type ("function type", sizeof ("function type")-1);
144   do_typedef (xmemdup (namestart, namelen, namelen+1), t, &lexer_line);
145   update_lineno (yytext, yyleng);
146 }
147
148 [^[:alnum:]_](typedef{WS})?(struct|union){WS}{ID}{WS}/"GTY" {
149   char *tagstart;
150   size_t taglen;
151   int typedef_p;
152   int union_p;
153
154   typedef_p = yytext[1] == 't';
155   if (typedef_p)
156     for (tagstart = yytext + strlen (" typedef "); 
157          ISSPACE(*tagstart);
158          tagstart++)
159       ;
160   else
161     tagstart = yytext + 1;
162
163   union_p = tagstart[0] == 'u';
164   tagstart += strlen ("union ");
165   while (ISSPACE (*tagstart))
166     tagstart++;
167   for (taglen = 1; ISIDNUM (tagstart[taglen]); taglen++)
168     ;
169
170   yylval.t = find_structure (xmemdup (tagstart, taglen, taglen + 1), union_p);
171   BEGIN(in_struct);
172   update_lineno (yytext, yyleng);
173   return typedef_p ? ENT_TYPEDEF_STRUCT : ENT_STRUCT;
174 }
175
176 [^[:alnum:]_](extern|static){WS}/"GTY" {
177   BEGIN(in_struct);
178   update_lineno (yytext, yyleng);
179   return ENT_EXTERNSTATIC;
180 }
181
182 ^"%union"{WS}"{"{WS}/"GTY" {
183   BEGIN(in_struct);
184   update_lineno (yytext, yyleng);
185   return ENT_YACCUNION;
186 }
187
188 <in_struct>{
189
190 "/*"                            { BEGIN(in_struct_comment); }
191
192 ^"%{"                           { BEGIN(in_yacc_escape); }
193
194 {WS}                            { update_lineno (yytext, yyleng); }
195
196 "const"/[^[:alnum:]_]           /* don't care */
197
198 "GTY"/[^[:alnum:]_]             { return GTY_TOKEN; }
199 "union"/[^[:alnum:]_]           { return UNION; }
200 "struct"/[^[:alnum:]_]          { return STRUCT; }
201 "enum"/[^[:alnum:]_]            { return ENUM; }
202 "ptr_alias"/[^[:alnum:]_]       { return ALIAS; }
203 "param_is"/[^[:alnum:]_]        { return PARAM_IS; }
204 [0-9]+                          { return NUM; }
205
206 {IWORD}({WS}{IWORD})*/[^[:alnum:]_]             |
207 "ENUM_BITFIELD"{WS}?"("{WS}?{ID}{WS}?")"        {
208   size_t len;
209
210   for (len = yyleng; ISSPACE (yytext[len-1]); len--)
211     ;
212
213   yylval.t = create_scalar_type (yytext, len);
214   update_lineno (yytext, yyleng);
215   return SCALAR;
216 }
217
218 {ID}/[^[:alnum:]_]              {
219   yylval.s = xmemdup (yytext, yyleng, yyleng+1);
220   return ID;
221 }
222
223 \"([^"\\]|\\.)*\"               {
224   yylval.s = xmemdup (yytext+1, yyleng-2, yyleng-1);
225   return STRING;
226 }
227 "["[^\[\]]*"]"                  {
228   yylval.s = xmemdup (yytext+1, yyleng-2, yyleng-1);
229   return ARRAY;
230 }
231 ^"%"{ID}                        {
232   yylval.s = xmemdup (yytext+1, yyleng-1, yyleng);
233   return PERCENT_ID;
234 }
235 "'"("\\".|[^\\])"'"             {
236   yylval.s = xmemdup (yytext+1, yyleng-2, yyleng);
237   return CHAR;
238 }
239
240 [(){},*:<>]                     { return yytext[0]; }
241
242 [;=]                            {
243   if (lexer_toplevel_done)
244     {
245       BEGIN(INITIAL);
246       lexer_toplevel_done = 0;
247     }
248   return yytext[0];
249 }
250
251 ^"%%"                           {
252   BEGIN(INITIAL);
253   return PERCENTPERCENT;
254 }
255
256 .                               {
257   error_at_line (&lexer_line, "unexpected character `%s'", yytext);
258 }
259 }
260
261 "/*"                    { BEGIN(in_comment); }
262 \n                      { lexer_line.line++; }
263 {ID}                    |
264 "'"("\\".|[^\\])"'"     |
265 [^"/\n]                 /* do nothing */
266 \"([^"\\]|\\.|\\\n)*\"  { update_lineno (yytext, yyleng); }
267 "/"/[^*]                /* do nothing */
268
269 <in_comment,in_struct_comment>{
270 \n              { lexer_line.line++; }
271 [^*\n]{16}      |
272 [^*\n]          /* do nothing */
273 "*"/[^/]        /* do nothing */
274 }
275 <in_comment>"*/"        { BEGIN(INITIAL); } 
276 <in_struct_comment>"*/" { BEGIN(in_struct); }
277
278 <in_yacc_escape>{
279 \n              { lexer_line.line++; }
280 [^%]{16}        |
281 [^%]            /* do nothing */
282 "%"/[^}]        /* do nothing */
283 "%}"            { BEGIN(in_struct); }
284 "%"             {
285   error_at_line (&lexer_line, 
286                  "unterminated %%{; unexpected EOF");
287 }
288 }
289
290
291 ["/]                    |
292 <in_struct_comment,in_comment>"*"       {
293   error_at_line (&lexer_line, 
294                  "unterminated comment or string; unexpected EOF");
295 }
296
297 %%
298
299 void
300 yyerror (s)
301      const char *s;
302 {
303   error_at_line (&lexer_line, s);
304 }
305
306 void
307 parse_file (fname)
308       const char *fname;
309 {
310   yyin = fopen (fname, "r");
311   lexer_line.file = fname;
312   lexer_line.line = 1;
313   if (yyin == NULL)
314     {
315       perror (fname);
316       exit (1);
317     }
318   if (yyparse() != 0)
319     exit (1);
320   fclose (yyin);
321 }