OSDN Git Service

contrib/
[pf3gnuchains/gcc-fork.git] / gcc / gengtype-lex.l
1 /* -*- indented-text -*- */
2 /* Process source files and output type information.
3    Copyright (C) 2002, 2003, 2004, 2005, 2007 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 3, 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 COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 %{
22 #include "bconfig.h"
23 #include "system.h"
24
25 #define malloc xmalloc
26 #define realloc xrealloc
27
28 #include "gengtype.h"
29
30 #define YY_DECL int yylex (const char **yylval)
31 #define yyterminate() return EOF_TOKEN
32
33 struct fileloc lexer_line;
34 int lexer_toplevel_done;
35
36 static void 
37 update_lineno (const char *l, size_t len)
38 {
39   while (len-- > 0)
40     if (*l++ == '\n')
41       lexer_line.line++;
42 }
43
44 %}
45
46 ID      [[:alpha:]_][[:alnum:]_]*
47 WS      [[:space:]]+
48 HWS     [ \t\r\v\f]*
49 IWORD   short|long|(un)?signed|char|int|HOST_WIDE_INT|HOST_WIDEST_INT|bool|size_t|BOOL_BITFIELD|CPPCHAR_SIGNED_T|ino_t|dev_t
50 ITYPE   {IWORD}({WS}{IWORD})*
51 EOID    [^[:alnum:]_]
52
53 %x in_struct in_struct_comment in_comment
54 %option warn noyywrap nounput nodefault perf-report
55 %option 8bit never-interactive
56 %%
57   /* Do this on entry to yylex():  */
58   *yylval = 0;
59   if (lexer_toplevel_done)
60     {
61       BEGIN(INITIAL);
62       lexer_toplevel_done = 0;
63     }
64
65   /* Things we look for in skipping mode: */
66 <INITIAL>{
67 ^{HWS}typedef/{EOID} {
68   BEGIN(in_struct);
69   return TYPEDEF;
70 }
71 ^{HWS}struct/{EOID} {
72   BEGIN(in_struct);
73   return STRUCT;
74 }
75 ^{HWS}union/{EOID} {
76   BEGIN(in_struct);
77   return UNION;
78 }
79 ^{HWS}extern/{EOID} {
80   BEGIN(in_struct);
81   return EXTERN;
82 }
83 ^{HWS}static/{EOID} {
84   BEGIN(in_struct);
85   return STATIC;
86 }
87
88 ^{HWS}DEF_VEC_[OP]/{EOID} {
89   BEGIN(in_struct);
90   return DEFVEC_OP;
91 }
92 ^{HWS}DEF_VEC_I/{EOID} {
93   BEGIN(in_struct);
94   return DEFVEC_I;
95 }
96 ^{HWS}DEF_VEC_ALLOC_[IOP]/{EOID} {
97   BEGIN(in_struct);
98   return DEFVEC_ALLOC;
99 }
100 }
101
102 <in_struct>{
103
104 "/*"                            { BEGIN(in_struct_comment); }
105
106 {WS}                            { update_lineno (yytext, yyleng); }
107 \\\n                            { lexer_line.line++; }
108
109 "const"/{EOID}                  /* don't care */
110 "GTY"/{EOID}                    { return GTY_TOKEN; }
111 "VEC"/{EOID}                    { return VEC_TOKEN; }
112 "union"/{EOID}                  { return UNION; }
113 "struct"/{EOID}                 { return STRUCT; }
114 "enum"/{EOID}                   { return ENUM; }
115 "ptr_alias"/{EOID}              { return PTR_ALIAS; }
116 "nested_ptr"/{EOID}             { return NESTED_PTR; }
117 [0-9]+                          { return NUM; }
118 "param"[0-9]*"_is"/{EOID}               {
119   *yylval = XDUPVAR (const char, yytext, yyleng, yyleng+1);
120   return PARAM_IS;
121 }
122
123 {IWORD}({WS}{IWORD})*/{EOID}            |
124 "ENUM_BITFIELD"{WS}?"("{WS}?{ID}{WS}?")"        {
125   size_t len;
126
127   for (len = yyleng; ISSPACE (yytext[len-1]); len--)
128     ;
129
130   *yylval = XDUPVAR (const char, yytext, len, len+1);
131   update_lineno (yytext, yyleng);
132   return SCALAR;
133 }
134
135
136 {ID}/{EOID}                     {
137   *yylval = XDUPVAR (const char, yytext, yyleng, yyleng+1);
138   return ID;
139 }
140
141 \"([^"\\]|\\.)*\"               {
142   *yylval = XDUPVAR (const char, yytext+1, yyleng-2, yyleng-1);
143   return STRING;
144 }
145   /* This "terminal" avoids having to parse integer constant expressions.  */
146 "["[^\[\]]*"]"                  {
147   *yylval = XDUPVAR (const char, yytext+1, yyleng-2, yyleng-1);
148   return ARRAY;
149 }
150 "'"("\\".|[^\\])"'"             {
151   *yylval = XDUPVAR (const char, yytext+1, yyleng-2, yyleng);
152   return CHAR;
153 }
154
155 "..."                           { return ELLIPSIS; }
156 [(){},*:<>;=%|-]                { return yytext[0]; }
157
158    /* ignore pp-directives */
159 ^{HWS}"#"{HWS}[a-z_]+[^\n]*\n   {lexer_line.line++;}
160
161 .                               {
162   error_at_line (&lexer_line, "unexpected character `%s'", yytext);
163 }
164 }
165
166 "/*"                    { BEGIN(in_comment); }
167 \n                      { lexer_line.line++; }
168 {ID}                    |
169 "'"("\\".|[^\\])"'"     |
170 [^"/\n]                 /* do nothing */
171 \"([^"\\]|\\.|\\\n)*\"  { update_lineno (yytext, yyleng); }
172 "/"/[^*]                /* do nothing */
173
174 <in_comment,in_struct_comment>{
175 \n              { lexer_line.line++; }
176 [^*\n]{16}      |
177 [^*\n]          /* do nothing */
178 "*"/[^/]        /* do nothing */
179 }
180 <in_comment>"*/"        { BEGIN(INITIAL); } 
181 <in_struct_comment>"*/" { BEGIN(in_struct); }
182
183 ["/]                    |
184 <in_struct_comment,in_comment>"*"       {
185   error_at_line (&lexer_line, 
186                  "unterminated comment or string; unexpected EOF");
187 }
188
189 ^{HWS}"#"{HWS}"define"{WS}"GTY(" /* do nothing */
190 {WS}"GTY"{WS}?"("       {
191   error_at_line (&lexer_line, "stray GTY marker");
192 }
193
194 %%
195
196 void
197 yybegin (const char *fname)
198 {
199   yyin = fopen (fname, "r");
200   if (yyin == NULL)
201     {
202       perror (fname);
203       exit (1);
204     }
205   lexer_line.file = fname;
206   lexer_line.line = 1;
207 }
208
209 void
210 yyend (void)
211 {
212   fclose (yyin);
213 }