OSDN Git Service

* reload1.c (move2add_last_cc0): New.
[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 #include "bconfig.h"
24 #include "coretypes.h"
25 #include "system.h"
26
27 #define malloc xmalloc
28 #define realloc xrealloc
29
30 #include "gengtype.h"
31 #include "gengtype-yacc.h"
32
33 #undef YY_USE_PROTOS
34 #define YY_DECL int yylex ()
35
36 static void update_lineno PARAMS ((const char *l, size_t len));
37
38 struct fileloc lexer_line;
39 int lexer_toplevel_done;
40
41 static void 
42 update_lineno (l, len)
43      const char *l;
44      size_t len;
45 {
46   while (len-- > 0)
47     if (*l++ == '\n')
48       lexer_line.line++;
49 }
50
51 %}
52
53 ID      [[:alpha:]_][[:alnum:]_]*
54 WS      [[:space:]]+
55 IWORD   short|long|(un)?signed|char|int|HOST_WIDE_INT|bool|size_t|CHAR_BITFIELD
56 ITYPE   {IWORD}({WS}{IWORD})*
57
58 %x in_struct in_struct_comment in_comment in_yacc_escape
59 %option warn noyywrap nounput nodefault perf-report
60 %option 8bit never-interactive
61 %%
62
63 [^[:alnum:]_]typedef{WS}(struct|union){WS}{ID}{WS}?[*[:space:]]{WS}?{ID}{WS}?";" {
64   char *tagstart;
65   size_t taglen;
66   char *namestart;
67   size_t namelen;
68   int is_pointer = 0;
69   struct type *t;
70   int union_p;
71
72   tagstart = yytext + strlen (" typedef ");
73   while (ISSPACE (*tagstart))
74     tagstart++;
75   union_p = tagstart[0] == 'u';
76   tagstart += strlen ("union ");
77   while (ISSPACE (*tagstart))
78     tagstart++;
79   for (taglen = 1; ISIDNUM (tagstart[taglen]); taglen++)
80     ;
81   for (namestart = tagstart + taglen; 
82        ! ISIDNUM (*namestart);
83        namestart++)
84     if (*namestart == '*')
85       is_pointer = 1;
86   for (namelen = 1; ISIDNUM (namestart[namelen]); namelen++)
87     ;
88   t = find_structure (xmemdup (tagstart, taglen, taglen+1), union_p);
89   if (is_pointer)
90     t = create_pointer (t);
91   do_typedef (xmemdup (namestart, namelen, namelen+1), t, &lexer_line);
92   update_lineno (yytext, yyleng);
93 }
94
95 [^[:alnum:]_]typedef{WS}{ITYPE}{WS}{ID}{WS}?";" {
96
97   char *namestart;
98   size_t namelen;
99   struct type *t;
100   char *typestart;
101   size_t typelen;
102
103   for (namestart = yytext + yyleng - 2; ISSPACE (*namestart); namestart--)
104     ;
105   for (namelen = 1; !ISSPACE (namestart[-namelen]); namelen++)
106     ;
107   namestart -= namelen - 1;
108   for (typestart = yytext + strlen (" typedef "); 
109        ISSPACE(*typestart);
110        typestart++)
111     ;
112   for (typelen = namestart - typestart; 
113        ISSPACE(typestart[typelen-1]); 
114        typelen--)
115     ;
116
117   t = create_scalar_type (typestart, typelen);
118   do_typedef (xmemdup (namestart, namelen, namelen+1), t, &lexer_line);
119   update_lineno (yytext, yyleng);
120 }
121
122 [^[:alnum:]_]typedef{WS}{ID}{WS}{ID}{WS}PARAMS {
123   char *namestart;
124   size_t namelen;
125   struct type *t;
126
127   for (namestart = yytext + yyleng - 7; ISSPACE (*namestart); namestart--)
128     ;
129   for (namelen = 1; !ISSPACE (namestart[-namelen]); namelen++)
130     ;
131   namestart -= namelen - 1;
132
133   t = create_scalar_type ("function type", sizeof ("function type")-1);
134   do_typedef (xmemdup (namestart, namelen, namelen+1), t, &lexer_line);
135   update_lineno (yytext, yyleng);
136 }
137 [^[:alnum:]_]typedef{WS}{ID}{WS}?"("{WS}?"*"{WS}?{ID}{WS}?")"{WS}?PARAMS {
138   char *namestart;
139   size_t namelen;
140   struct type *t;
141
142   for (namestart = yytext + yyleng - 7; !ISIDNUM (*namestart); namestart--)
143     ;
144   for (namelen = 1; ISIDNUM (namestart[-namelen]); namelen++)
145     ;
146   namestart -= namelen - 1;
147
148   t = create_scalar_type ("function type", sizeof ("function type")-1);
149   do_typedef (xmemdup (namestart, namelen, namelen+1), t, &lexer_line);
150   update_lineno (yytext, yyleng);
151 }
152
153 [^[:alnum:]_](typedef{WS})?(struct|union){WS}{ID}{WS}/"GTY" {
154   char *tagstart;
155   size_t taglen;
156   int typedef_p;
157   int union_p;
158
159   typedef_p = yytext[1] == 't';
160   if (typedef_p)
161     for (tagstart = yytext + strlen (" typedef "); 
162          ISSPACE(*tagstart);
163          tagstart++)
164       ;
165   else
166     tagstart = yytext + 1;
167
168   union_p = tagstart[0] == 'u';
169   tagstart += strlen ("union ");
170   while (ISSPACE (*tagstart))
171     tagstart++;
172   for (taglen = 1; ISIDNUM (tagstart[taglen]); taglen++)
173     ;
174
175   yylval.t = find_structure (xmemdup (tagstart, taglen, taglen + 1), union_p);
176   BEGIN(in_struct);
177   update_lineno (yytext, yyleng);
178   return typedef_p ? ENT_TYPEDEF_STRUCT : ENT_STRUCT;
179 }
180
181 [^[:alnum:]_](extern|static){WS}/"GTY" {
182   BEGIN(in_struct);
183   update_lineno (yytext, yyleng);
184   return ENT_EXTERNSTATIC;
185 }
186
187 ^"%union"{WS}"{"{WS}/"GTY" {
188   BEGIN(in_struct);
189   update_lineno (yytext, yyleng);
190   return ENT_YACCUNION;
191 }
192
193 <in_struct>{
194
195 "/*"                            { BEGIN(in_struct_comment); }
196
197 ^"%{"                           { BEGIN(in_yacc_escape); }
198
199 {WS}                            { update_lineno (yytext, yyleng); }
200
201 "const"/[^[:alnum:]_]           /* don't care */
202
203 "GTY"/[^[:alnum:]_]             { return GTY_TOKEN; }
204 "union"/[^[:alnum:]_]           { return UNION; }
205 "struct"/[^[:alnum:]_]          { return STRUCT; }
206 "enum"/[^[:alnum:]_]            { return ENUM; }
207 "ptr_alias"/[^[:alnum:]_]       { return ALIAS; }
208 [0-9]+                          { return NUM; }
209 "param"[0-9]*"_is"/[^[:alnum:]_]                { 
210   yylval.s = xmemdup (yytext, yyleng, yyleng+1);
211   return PARAM_IS;
212 }
213
214 {IWORD}({WS}{IWORD})*/[^[:alnum:]_]             |
215 "ENUM_BITFIELD"{WS}?"("{WS}?{ID}{WS}?")"        {
216   size_t len;
217
218   for (len = yyleng; ISSPACE (yytext[len-1]); len--)
219     ;
220
221   yylval.t = create_scalar_type (yytext, len);
222   update_lineno (yytext, yyleng);
223   return SCALAR;
224 }
225
226 {ID}/[^[:alnum:]_]              {
227   yylval.s = xmemdup (yytext, yyleng, yyleng+1);
228   return ID;
229 }
230
231 \"([^"\\]|\\.)*\"               {
232   yylval.s = xmemdup (yytext+1, yyleng-2, yyleng-1);
233   return STRING;
234 }
235 "["[^\[\]]*"]"                  {
236   yylval.s = xmemdup (yytext+1, yyleng-2, yyleng-1);
237   return ARRAY;
238 }
239 ^"%"{ID}                        {
240   yylval.s = xmemdup (yytext+1, yyleng-1, yyleng);
241   return PERCENT_ID;
242 }
243 "'"("\\".|[^\\])"'"             {
244   yylval.s = xmemdup (yytext+1, yyleng-2, yyleng);
245   return CHAR;
246 }
247
248 [(){},*:<>]                     { return yytext[0]; }
249
250 [;=]                            {
251   if (lexer_toplevel_done)
252     {
253       BEGIN(INITIAL);
254       lexer_toplevel_done = 0;
255     }
256   return yytext[0];
257 }
258
259 ^"%%"                           {
260   BEGIN(INITIAL);
261   return PERCENTPERCENT;
262 }
263
264 .                               {
265   error_at_line (&lexer_line, "unexpected character `%s'", yytext);
266 }
267 }
268
269 "/*"                    { BEGIN(in_comment); }
270 \n                      { lexer_line.line++; }
271 {ID}                    |
272 "'"("\\".|[^\\])"'"     |
273 [^"/\n]                 /* do nothing */
274 \"([^"\\]|\\.|\\\n)*\"  { update_lineno (yytext, yyleng); }
275 "/"/[^*]                /* do nothing */
276
277 <in_comment,in_struct_comment>{
278 \n              { lexer_line.line++; }
279 [^*\n]{16}      |
280 [^*\n]          /* do nothing */
281 "*"/[^/]        /* do nothing */
282 }
283 <in_comment>"*/"        { BEGIN(INITIAL); } 
284 <in_struct_comment>"*/" { BEGIN(in_struct); }
285
286 <in_yacc_escape>{
287 \n              { lexer_line.line++; }
288 [^%]{16}        |
289 [^%]            /* do nothing */
290 "%"/[^}]        /* do nothing */
291 "%}"            { BEGIN(in_struct); }
292 "%"             {
293   error_at_line (&lexer_line, 
294                  "unterminated %%{; unexpected EOF");
295 }
296 }
297
298
299 ["/]                    |
300 <in_struct_comment,in_comment>"*"       {
301   error_at_line (&lexer_line, 
302                  "unterminated comment or string; unexpected EOF");
303 }
304
305 %%
306
307 void
308 yyerror (s)
309      const char *s;
310 {
311   error_at_line (&lexer_line, s);
312 }
313
314 void
315 parse_file (fname)
316       const char *fname;
317 {
318   yyin = fopen (fname, "r");
319   lexer_line.file = fname;
320   lexer_line.line = 1;
321   if (yyin == NULL)
322     {
323       perror (fname);
324       exit (1);
325     }
326   if (yyparse() != 0)
327     exit (1);
328   fclose (yyin);
329 }