]> git.draconx.ca Git - gob-dx.git/blob - src/lexer.l
392a6364aad9fe21548648fc56e3beea115a2eb8
[gob-dx.git] / src / lexer.l
1 /* GOB C Preprocessor
2  * Copyright (C) 1999 the Free Software Foundation.
3  *
4  * Author: George Lebl
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 2 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, write to the  Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19  * USA.
20  */
21 %{
22
23 #include "config.h"
24 #include <glib.h>
25 #include <string.h>
26
27 #include "parse.h"
28 #include "main.h"
29
30 extern gboolean for_cpp;
31
32 static int parenth_depth = 0;
33 static int before_comment = INITIAL;
34 static int class_after_c = FALSE;
35 static int header_c = FALSE;
36
37 static GString *cbuf = NULL;
38 int ccode_line = 1;
39
40 GList *include_files = NULL;
41 /* 0 no, 1 means yes, 2+ means don't even start looking anymore */
42 static int look_for_includes = 0;
43
44 int line_no = 1;
45
46 static void
47 clear_cbuf(void)
48 {
49         if(!cbuf) {
50                 cbuf = g_string_new("");
51         } else {
52                 cbuf = g_string_assign(cbuf,"");
53         }
54 }
55
56 static void
57 add_to_cbuf(char *s)
58 {
59         if(!cbuf) {
60                 cbuf = g_string_new(s);
61         } else {
62                 cbuf = g_string_append(cbuf,s);
63         }
64 }
65
66 %}
67
68 %x COMMENT
69 %x C_CODE
70 %x C_CODE_STRING
71 %x CLASS_CODE
72 %x CLASS_CODE_I
73
74 %%
75
76 <*>\n                   { line_no++; REJECT; }
77
78 <*>MOTHERFUCKER         { fprintf(stderr,"You are a bad bad person!\n"); REJECT; }
79
80 \/\/.*$                 { ; /*comment, ignore*/ }
81 <C_CODE>^#[ \t]*include[ \t][<"][^\n">]*[>"] {
82         if(look_for_includes==1) {
83                 char *p;
84                 char *file;
85                 char *str = g_strdup(yytext);
86                 file = strchr(str,'"');
87                 if(!file) file = strchr(str,'<');
88                 file++;
89                 p = strchr(file,'"');
90                 if(!p) p = strchr(file,'>');
91                 *p = '\0';
92                 include_files = g_list_prepend(include_files,g_strdup(file));
93                 g_free(str);
94         }
95         REJECT;
96 }
97
98 <C_CODE>\/\/.*$         { add_to_cbuf(yytext); /*comment, ignore*/ }
99 <CLASS_CODE>\/\/.*$     { ; /*comment, ignore*/ }
100 <CLASS_CODE_I>\/\/.*$   { ; /*comment, ignore*/ }
101 \/\*            {BEGIN(COMMENT); before_comment = INITIAL; }
102 <C_CODE>\/\*    {
103         add_to_cbuf(yytext);
104         BEGIN(COMMENT);
105         before_comment = C_CODE;
106 }
107 <CLASS_CODE>\/\*        {BEGIN(COMMENT); before_comment = CLASS_CODE; }
108 <CLASS_CODE_I>\/\*      {BEGIN(COMMENT); before_comment = CLASS_CODE_I; }
109 <COMMENT>\*\/   {
110         if(before_comment == C_CODE) add_to_cbuf(yytext);
111         BEGIN(before_comment);
112                 }
113 <COMMENT>.      {
114         /* comment, ignore */
115         if(before_comment == C_CODE) add_to_cbuf(yytext);
116                 }
117 <COMMENT>\n     {
118         /* comment, ignore */
119         if(before_comment == C_CODE) add_to_cbuf(yytext);
120                 }
121
122 ^\%h\{          {
123                         BEGIN(C_CODE);
124                         parenth_depth = 1;
125                         class_after_c = FALSE;
126                         header_c = TRUE;
127                         clear_cbuf();
128                         ccode_line = line_no;
129                 }
130 ^\%\{           {
131                         BEGIN(C_CODE);
132                         parenth_depth = 1;
133                         class_after_c = FALSE;
134                         header_c = FALSE;
135                         clear_cbuf();
136                         ccode_line = line_no;
137                         if(look_for_includes==0)
138                                 look_for_includes=1;
139                 }
140 <C_CODE>^\%\}   {
141                         BEGIN(INITIAL);
142                         yylval.cbuf = cbuf;
143                         cbuf = NULL;
144                         if(look_for_includes==1)
145                                 look_for_includes=0;
146                         if(header_c)
147                                 return HCODE;
148                         else
149                                 return CCODE;
150                 }
151
152 <C_CODE>\'\{\'          { add_to_cbuf(yytext); }
153 <C_CODE>\'\\\{\'        { add_to_cbuf(yytext); }
154 <C_CODE>\'\}\'          { add_to_cbuf(yytext); }
155 <C_CODE>\'\\\}\'        { add_to_cbuf(yytext); }
156 <C_CODE>\'\"\'          { add_to_cbuf(yytext); }
157 <C_CODE>\'\\\"\'        { add_to_cbuf(yytext); }
158         
159 <C_CODE>\\.     { add_to_cbuf(yytext); }
160 <C_CODE>\"      {
161                         BEGIN(C_CODE_STRING);
162                         add_to_cbuf(yytext);
163                 }
164 <C_CODE_STRING>\\.      { add_to_cbuf(yytext); }
165 <C_CODE_STRING>\"       {
166                                 BEGIN(C_CODE);
167                                 add_to_cbuf(yytext);
168                         }
169 <C_CODE_STRING>.        { add_to_cbuf(yytext); }
170 <C_CODE_STRING>\n       { add_to_cbuf(yytext); }
171
172 <C_CODE>\{      {
173                         parenth_depth++;
174                         add_to_cbuf(yytext);
175                 }
176 <C_CODE>\}      {
177                         parenth_depth--;
178                         if(parenth_depth<0) {
179                                 REJECT;
180                         } else if(parenth_depth==0 && class_after_c) {
181                                 BEGIN(CLASS_CODE_I);
182                                 yylval.cbuf = cbuf;
183                                 cbuf = NULL;
184                                 return CCODE;
185                         }
186                         add_to_cbuf(yytext);
187                 }
188
189 <C_CODE>.       { add_to_cbuf(yytext); }
190 <C_CODE>\n      { add_to_cbuf(yytext); }
191
192 class           {
193                         look_for_includes = 2;
194                         BEGIN(CLASS_CODE);
195                         return CLASS;
196                 }
197
198 ^[ \t]*requires[ \t]+[0-9]+\.[0-9]+\.[0-9]+[\t ]*$      {
199                         int maj = 0,min = 0,pl = 0;
200                         int rmaj = 0,rmin = 0,rpl = 0;
201                         char *p;
202                         
203                         sscanf(VERSION,"%d.%d.%d",&rmaj,&rmin,&rpl);
204                         p = strchr(yytext,'r');
205                         g_assert(p); /* we MUST have found it */
206                         sscanf(p,"requires %d.%d.%d",&maj,&min,&pl);
207                         if(rmaj < maj ||
208                            (rmaj == maj && rmin < min) ||
209                            (rmaj == maj && rmin == min && rpl < pl)) {
210                                 char *s;
211                                 s = g_strdup_printf(
212                                     "GOB version %d.%d.%d required "
213                                     "(this is %s)\n"
214                                     "To upgrade your gob, see: "
215                                     "http://www.5z.com/jirka/gob.html",
216                                     maj,min,pl,VERSION);
217                                 print_error(FALSE,s, line_no);
218                                 g_free(s);
219                         }
220                 }
221
222 <CLASS_CODE,CLASS_CODE_I>class|this     {
223                         if(for_cpp) {
224                                 char *s;
225                                 s = g_strdup_printf("'%s' keyword should not "
226                                                     "be used when generating "
227                                                     "C++ code",yytext);
228                                 print_error(TRUE,s, line_no);
229                                 g_free(s);
230                         }
231                         REJECT;
232                 }
233
234 <CLASS_CODE>from        {return FROM;}
235
236 <CLASS_CODE_I>void      {return VOID;}
237 <CLASS_CODE_I>struct    {return STRUCT;}
238 <CLASS_CODE_I>union     {return UNION;}
239 <CLASS_CODE_I>enum      {return ENUM;}
240 <CLASS_CODE_I>signed    {return SIGNED;}
241 <CLASS_CODE_I>unsigned  {return UNSIGNED;}
242 <CLASS_CODE_I>long      {return LONG;}
243 <CLASS_CODE_I>short     {return SHORT;}
244 <CLASS_CODE_I>int       {return INT;}
245 <CLASS_CODE_I>float     {return FLOAT;}
246 <CLASS_CODE_I>double    {return DOUBLE;}
247 <CLASS_CODE_I>char      {return CHAR;}
248 <CLASS_CODE_I>const     {return CONST;}
249
250 <CLASS_CODE_I>\.\.\.    {return THREEDOTS;}
251
252 <CLASS_CODE_I>public    {yylval.line = line_no; return PUBLIC;}
253 <CLASS_CODE_I>private   {yylval.line = line_no; return PRIVATE;}
254 <CLASS_CODE_I>protected {yylval.line = line_no; return PROTECTED;}
255 <CLASS_CODE_I>argument  {yylval.line = line_no; return ARGUMENT;}
256 <CLASS_CODE_I>virtual   {yylval.line = line_no; return VIRTUAL;}
257 <CLASS_CODE_I>signal    {yylval.line = line_no; return SIGNAL;}
258 <CLASS_CODE_I>override  {yylval.line = line_no; return OVERRIDE;}
259 <CLASS_CODE_I>onerror   {return ONERROR;}
260 <CLASS_CODE_I>0|[1-9][0-9]*|0x[0-9a-fA-F]+|0[0-7]+|[0-9]*\.[0-9]+|\.[0-9][0-9]* {
261                         yylval.id = g_strdup(yytext);
262                         return NUMBER;
263                 }
264 <CLASS_CODE,CLASS_CODE_I>:?[A-Za-z_][A-Za-z0-9_]*(:[A-Za-z0-9_]*)+      {
265                         yylval.id = g_strdup(yytext);
266                         return TYPETOKEN;
267                 }
268 <CLASS_CODE,CLASS_CODE_I>[A-Za-z_][A-Za-z0-9_]* {
269                         yylval.id = g_strdup(yytext);
270                         return TOKEN;
271                 }
272
273 <CLASS_CODE_I>(\[[0-9]*\])+     {
274                         yylval.id = g_strdup(yytext);
275                         return ARRAY_DIM;
276                 }
277
278 <CLASS_CODE>\{  {
279                         BEGIN(CLASS_CODE_I);
280                         return '{';
281                 }
282 <CLASS_CODE_I>\{        {
283                         BEGIN(C_CODE);
284                         parenth_depth=1;
285                         class_after_c = TRUE;
286                         yylval.line = line_no;
287                         clear_cbuf();
288                         ccode_line = line_no;
289                         return '{';
290                 }
291 <CLASS_CODE_I>\}        {
292                                 BEGIN(INITIAL);
293                                 return '}';
294                         }
295
296 <CLASS_CODE,CLASS_CODE_I,INITIAL>[\t ]  ;  /*ignore*/
297
298 <*>.            {
299                         yylval.line = line_no;
300                         return yytext[0];
301                 }
302
303 <*>[\n\r]       ;  /*ignore*/