]> git.draconx.ca Git - gob-dx.git/blob - src/lexer.l
b48e818d030e0dc5ba3c88a88b023deda9f6d7bc
[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 #include <ctype.h>
27
28 #include "treefuncs.h"
29 #include "parse.h"
30 #include "main.h"
31 #include "util.h"
32
33 static int parenth_depth = 0;
34 static int before_comment
35 /* New flex is on drugs */
36 #if defined(FLEX_SCANNER) && ! defined(INITIAL)
37         = 0;
38 #else
39         = INITIAL;
40 #endif
41 static gboolean class_after_c = FALSE;
42 static int code_type = CCODE;
43 static int before_string;
44 static int property_paren_depth = 0;
45
46 /* GTK+ doc stuff */
47 static char *gtk_doc_func = NULL; /* current gtk-doc func */
48 GHashTable *gtk_doc_hash = NULL;
49
50 static GString *cbuf = NULL;
51 int ccode_line = 1;
52
53 GList *include_files = NULL;
54 /* 0 no, 1 means yes, 2+ means don't even start looking anymore */
55 static int look_for_includes = 0;
56
57 int line_no = 1;
58 /* last filename parsed from a #line directive */
59 char *hline_filename = NULL;
60
61 static void
62 clear_cbuf(void)
63 {
64         if(!cbuf) {
65                 cbuf = g_string_new(NULL);
66         } else {
67                 cbuf = g_string_assign(cbuf, "");
68         }
69 }
70
71 static void
72 add_to_cbuf(char *s)
73 {
74         if(!cbuf) {
75                 cbuf = g_string_new(s);
76         } else {
77                 cbuf = g_string_append(cbuf,s);
78         }
79 }
80
81 static void
82 add_gtk_doc_func(void)
83 {
84         if(!gtk_doc_func)
85                 return;
86
87         if(!gtk_doc_hash)
88                 gtk_doc_hash = g_hash_table_new(g_str_hash, g_str_equal);
89         g_hash_table_insert(gtk_doc_hash, gtk_doc_func, g_strdup(cbuf->str));
90         clear_cbuf();
91
92         gtk_doc_func = NULL;
93 }
94
95 /* Ugly warning avoiding */
96 #ifdef FLEX_SCANNER
97 int yylex(void);
98 #endif
99
100 %}
101
102 %x COMMENT
103 %x C_CODE
104 %x CODE_STRING
105 %x CLASS_CODE
106 %x CLASS_CODE_I
107 %x PROPERTY_CODE
108 %x PROPERTY_CODE_I
109 %x GTK_DOC_BEFORE_NAME
110 %x GTK_DOC
111 %x GTK_DOC_LINE
112
113
114
115 %%
116
117 %{
118 /* Avoid warning from flex, cuz flex sucks */
119 #ifdef FLEX_SCANNER
120 yy_current_state = 0;
121 #endif
122 %}
123
124
125 <*>\n                   { line_no++; REJECT; }
126
127 <COMMENT>^(I(S.RI).E\(([1-9][0-9]+|[2-9]))/(\)) {
128 /* Thy evil easter egg */
129 #define QQ(x) long x
130 #define KK(x) =atoi(__(&,,x,))
131 #define MM(x,a) {QQ(i);for(i=2;i<x;i++){a}}
132 #define PP(Q) (P%Q)==0
133 #define ___(x,y,z) if(x z y)
134 #define __(a,x,y,z) a(yytext[y] x z)
135 #define O__O(a,b) fprintf(stderr,"%s is %s!\n",a,b)
136 QQ(m)=1;___(__(,==,2,'P'),__(,==,5,'M'),&&
137 ){QQ(P)KK(8);MM(P,___(PP(i),,)m=0;)}__(,=,
138 7,0);___(,,m){O__O(__( &,,8,),__(&,,2,));}
139 #undef QQ
140 #undef KK
141 #undef MM
142 #undef PP
143 #undef ___
144 #undef __
145 #undef O__O
146 REJECT;
147 }
148
149 \/\/.*$                 { ; /*comment, ignore*/ }
150
151 <*>^#[ \t]*line[ \t]+[0-9]+([ \t]\"[^\n\r\f\"]*\")? {
152         char *p;
153         char *number;
154         char *filename;
155         char *str=g_strdup(yytext);
156
157         /* find first digit of line number */
158         p=str;
159         while(*p&&!isdigit(*p)) p++;
160         number=p;
161   
162         /* find end of line number */
163         while(*p&&isdigit(*p)) p++;
164         if(*p) *p++=0;
165
166         /* find beginning of filename */
167         p=strchr(p,'"');
168         if(p) p++;
169         filename=p;
170
171         /* find end of filename */
172         if(p) p=strchr(p,'"');
173         if(p) *p=0;
174
175         /* stash number (minus one because we don't count this line) */  
176         if(number) line_no=atoi(number)-1;
177
178         /* stash filename */
179         if(filename) {
180                 if(hline_filename) g_free(hline_filename);
181                 hline_filename=g_strdup(filename);
182         }
183   
184         /* clean up */
185         g_free(str);
186 }
187
188 <C_CODE>^#[ \t]*include[ \t][<"][^\n">]*[>"] {
189         if(look_for_includes==1) {
190                 char *p;
191                 char *file;
192                 char *str = g_strdup(yytext);
193                 file = strchr(str,'"');
194                 if(!file) file = strchr(str,'<');
195                 file++;
196                 p = strchr(file,'"');
197                 if(!p) p = strchr(file,'>');
198                 *p = '\0';
199                 include_files = g_list_prepend(include_files,g_strdup(file));
200                 g_free(str);
201         }
202         REJECT;
203 }
204
205 <CLASS_CODE_I>\/\*\*[ \t]*$     {
206                         /* eat out gtk doc stuff */
207                         BEGIN(GTK_DOC_BEFORE_NAME);
208                         clear_cbuf();
209                 }
210 <GTK_DOC_BEFORE_NAME>^[ \t]*\*[ \t]*$   {
211                         /* empty doc lines */
212                         ;
213                 }       
214 <GTK_DOC_BEFORE_NAME>^[ \t]*\*\ [_a-zA-Z][_a-zA-Z0-9]*:?[ \t]*$ {
215                         char *p;
216                         BEGIN(GTK_DOC);
217                         p = strchr(yytext, '*');
218                         g_free(gtk_doc_func);
219                         gtk_doc_func = g_strdup(p+2);
220                         p = strchr(gtk_doc_func, ':');
221                         if(p) *p='\0';
222                         g_strstrip(gtk_doc_func);
223                 }
224 <GTK_DOC_BEFORE_NAME>\*\/       {
225                         BEGIN(CLASS_CODE_I);
226                 }
227 <GTK_DOC_BEFORE_NAME>.  {
228                         BEGIN(COMMENT);
229                         before_comment = CLASS_CODE_I;
230                 }
231 <GTK_DOC>^[ \t]*\*[ \t]*$       {
232                         /* empty doc lines */
233                         add_to_cbuf(" *\n");
234                 }       
235 <GTK_DOC>^[ \t]*\*?\*\/ {
236                         BEGIN(CLASS_CODE_I);
237                         add_gtk_doc_func();
238                 }
239 <GTK_DOC>^[ \t]*\*[ \t] {
240                         fflush(stdout);
241                         add_to_cbuf(" * ");
242                         BEGIN(GTK_DOC_LINE);
243                 }
244 <GTK_DOC>\*\/   {
245                         BEGIN(CLASS_CODE_I);
246                 }
247 <GTK_DOC>.      {
248                         BEGIN(COMMENT);
249                         before_comment = CLASS_CODE_I;
250                 }
251 <GTK_DOC_LINE>\*\/      {
252                         BEGIN(CLASS_CODE_I);
253                         add_to_cbuf("\n");
254                         add_gtk_doc_func();
255                 }
256 <GTK_DOC_LINE>.$        {
257                         BEGIN(GTK_DOC);
258                         add_to_cbuf(yytext);
259                         add_to_cbuf("\n");
260                 }
261 <GTK_DOC_LINE>. {
262                         fflush(stdout);
263                         add_to_cbuf(yytext);
264                 }
265
266 <C_CODE>\/\/.*$         { add_to_cbuf(yytext); /*comment, ignore*/ }
267 <CLASS_CODE>\/\/.*$     { ; /*comment, ignore*/ }
268 <CLASS_CODE_I>\/\/.*$   { ; /*comment, ignore*/ }
269 <PROPERTY_CODE_I>\/\/.*$        { ; /*comment, ignore*/ }
270 \/\*            {BEGIN(COMMENT); before_comment = INITIAL; }
271 <C_CODE>\/\*    {
272         add_to_cbuf(yytext);
273         BEGIN(COMMENT);
274         before_comment = C_CODE;
275 }
276 <CLASS_CODE>\/\*        {BEGIN(COMMENT); before_comment = CLASS_CODE; }
277 <CLASS_CODE_I>\/\*      {BEGIN(COMMENT); before_comment = CLASS_CODE_I; }
278 <PROPERTY_CODE_I>\/\*   {BEGIN(COMMENT); before_comment = PROPERTY_CODE_I; }
279 <COMMENT>\*\/   {
280         if(before_comment == C_CODE) add_to_cbuf(yytext);
281         BEGIN(before_comment);
282                 }
283 <COMMENT>.      {
284         /* comment, ignore */
285         if(before_comment == C_CODE) add_to_cbuf(yytext);
286                 }
287 <COMMENT>\n     {
288         /* comment, ignore */
289         if(before_comment == C_CODE) add_to_cbuf(yytext);
290                 }
291
292 ^\%(a|all)\{            {
293                         BEGIN(C_CODE);
294                         parenth_depth = 1;
295                         class_after_c = FALSE;
296                         code_type = ACODE;
297                         clear_cbuf();
298                         ccode_line = line_no;
299                 }
300 ^\%(at|alltop)\{                {
301                         BEGIN(C_CODE);
302                         parenth_depth = 1;
303                         class_after_c = FALSE;
304                         code_type = ATCODE;
305                         clear_cbuf();
306                         ccode_line = line_no;
307                 }
308
309 ^\%(ht|headertop)\{             {
310                         BEGIN(C_CODE);
311                         parenth_depth = 1;
312                         class_after_c = FALSE;
313                         code_type = HTCODE;
314                         clear_cbuf();
315                         ccode_line = line_no;
316                 }
317 ^\%(ph|privateheader)\{         {
318                         BEGIN(C_CODE);
319                         parenth_depth = 1;
320                         class_after_c = FALSE;
321                         code_type = PHCODE;
322                         clear_cbuf();
323                         ccode_line = line_no;
324                 }
325 ^\%(h|header)\{         {
326                         BEGIN(C_CODE);
327                         parenth_depth = 1;
328                         class_after_c = FALSE;
329                         code_type = HCODE;
330                         clear_cbuf();
331                         ccode_line = line_no;
332                 }
333 ^\%\{           {
334                         BEGIN(C_CODE);
335                         parenth_depth = 1;
336                         class_after_c = FALSE;
337                         code_type = CCODE;
338                         clear_cbuf();
339                         ccode_line = line_no;
340                         if(look_for_includes==0)
341                                 look_for_includes=1;
342                 }
343 <C_CODE>^\%\}   {
344                         BEGIN(INITIAL);
345                         yylval.cbuf = cbuf;
346                         cbuf = NULL;
347                         if(look_for_includes==1)
348                                 look_for_includes=0;
349                         return code_type;
350                 }
351
352 <C_CODE>\'\{\'          { add_to_cbuf(yytext); }
353 <C_CODE>\'\\\{\'        { add_to_cbuf(yytext); }
354 <C_CODE>\'\}\'          { add_to_cbuf(yytext); }
355 <C_CODE>\'\\\}\'        { add_to_cbuf(yytext); }
356 <C_CODE>\'\"\'          { add_to_cbuf(yytext); }
357 <C_CODE>\'\\\"\'        { add_to_cbuf(yytext); }
358         
359 <C_CODE>\\.     { add_to_cbuf(yytext); }
360
361
362 <C_CODE>\"      {
363                         BEGIN(CODE_STRING);
364                         before_string = C_CODE;
365                         add_to_cbuf(yytext);
366                 }
367 <PROPERTY_CODE_I>\"     {
368                         BEGIN(CODE_STRING);
369                         before_string = PROPERTY_CODE_I;
370                         add_to_cbuf(yytext);
371                 }
372 <CODE_STRING>\\.        { add_to_cbuf(yytext); }
373 <CODE_STRING>\" {
374                         BEGIN(before_string);
375                         add_to_cbuf(yytext);
376                         if (before_string == PROPERTY_CODE_I) {
377                                 yylval.id = cbuf->str;
378                                 g_string_free (cbuf, FALSE);
379                                 cbuf = NULL;
380                                 return STRING;
381                         }
382                 }
383 <CODE_STRING>.  { add_to_cbuf(yytext); }
384 <CODE_STRING>\n { add_to_cbuf(yytext); }
385
386 <C_CODE>\{      {
387                         parenth_depth++;
388                         add_to_cbuf(yytext);
389                 }
390 <C_CODE>\}      {
391                         parenth_depth--;
392                         if(parenth_depth<0) {
393                                 REJECT;
394                         } else if(parenth_depth==0 && class_after_c) {
395                                 BEGIN(CLASS_CODE_I);
396                                 yylval.cbuf = cbuf;
397                                 cbuf = NULL;
398                                 return CCODE;
399                         }
400                         add_to_cbuf(yytext);
401                 }
402
403 <C_CODE>.       { add_to_cbuf(yytext); }
404 <C_CODE>\n      { add_to_cbuf(yytext); }
405
406 class           {
407                         static int found_classes = 0;
408                         look_for_includes = 2;
409                         BEGIN(CLASS_CODE);
410
411                         if(++found_classes > 1) {
412                                 error_print(GOB_ERROR, line_no,
413                                             "Only one class per file allowed");
414                         }
415
416                         return CLASS;
417                 }
418
419 error           { return ERROR; }
420 enum            { return ENUM; }
421 flags           { return FLAGS; }
422
423 ^[ \t]*requires[ \t]+[0-9]+\.[0-9]+\.[0-9]+[\t ]*$      {
424                         int maj = 0, min = 0, pl = 0;
425                         int rmaj = 0, rmin = 0, rpl = 0;
426                         int effective_maj = 0;
427                         int effective_rmaj = 0;
428                         char *p;
429                         
430                         sscanf (VERSION, "%d.%d.%d", &rmaj, &rmin, &rpl);
431                         effective_rmaj = rmaj;
432                         if (rmin >= 90)
433                                 effective_rmaj = rmaj + 1;
434
435                         p = strchr (yytext,'r');
436                         g_assert (p); /* we MUST have found it */
437                         sscanf (p, "requires %d.%d.%d", &maj, &min, &pl);
438                         effective_maj = maj;
439                         if (min >= 90)
440                                 effective_maj = maj + 1;
441
442                         if(rmaj < maj ||
443                            (rmaj == maj && rmin < min) ||
444                            (rmaj == maj && rmin == min && rpl < pl)) {
445                                 error_printf (GOB_ERROR, line_no,
446                                               "GOB version at least %d.%d.%d required "
447                                               "(this is %s)\n"
448                                               "To upgrade your gob, see: "
449                                               "http://www.5z.com/jirka/gob.html",
450                                               maj, min, pl, VERSION);
451                         }
452
453                         if(effective_rmaj != effective_maj) {
454                                 error_printf(GOB_ERROR, line_no,
455                                              "GOB major version %d required "
456                                              "(this is %s)\n"
457                                              "To upgrade your gob, see: "
458                                              "http://www.5z.com/jirka/gob.html",
459                                              effective_maj, VERSION);
460                         }
461
462                 }
463
464 <CLASS_CODE,CLASS_CODE_I>class|this     {
465                         if(for_cpp) {
466                                 error_printf(GOB_WARN, line_no,
467                                              "'%s' keyword should not "
468                                              "be used when generating "
469                                              "C++ code", yytext);
470                         }
471                         REJECT;
472                 }
473
474 <CLASS_CODE>from        {return FROM;}
475
476 <CLASS_CODE_I,PROPERTY_CODE_I>void      {return VOID;}
477 <CLASS_CODE_I,PROPERTY_CODE_I>struct    {return STRUCT;}
478 <CLASS_CODE_I,PROPERTY_CODE_I>union     {return UNION;}
479 <CLASS_CODE_I,PROPERTY_CODE_I>enum      {return ENUM;}
480 <CLASS_CODE_I,PROPERTY_CODE_I>signed    {return SIGNED;}
481 <CLASS_CODE_I,PROPERTY_CODE_I>unsigned  {return UNSIGNED;}
482 <CLASS_CODE_I,PROPERTY_CODE_I>long      {return LONG;}
483 <CLASS_CODE_I,PROPERTY_CODE_I>short     {return SHORT;}
484 <CLASS_CODE_I,PROPERTY_CODE_I>int       {return INT;}
485 <CLASS_CODE_I,PROPERTY_CODE_I>float     {return FLOAT;}
486 <CLASS_CODE_I,PROPERTY_CODE_I>double    {return DOUBLE;}
487 <CLASS_CODE_I,PROPERTY_CODE_I>char      {return CHAR;}
488 <CLASS_CODE_I,PROPERTY_CODE_I>const     {return CONST;}
489
490 <CLASS_CODE_I>\.\.\.    {return THREEDOTS;}
491
492 <CLASS_CODE_I>public    {yylval.line = line_no; return PUBLIC;}
493 <CLASS_CODE_I>private   {yylval.line = line_no; return PRIVATE;}
494 <CLASS_CODE_I>protected {yylval.line = line_no; return PROTECTED;}
495 <CLASS_CODE_I>classwide {yylval.line = line_no; return CLASSWIDE;}
496 <CLASS_CODE_I>argument  {yylval.line = line_no; return ARGUMENT;}
497 <CLASS_CODE_I>virtual   {yylval.line = line_no; return VIRTUAL;}
498 <CLASS_CODE_I>signal    {yylval.line = line_no; return SIGNAL;}
499 <CLASS_CODE_I>override  {yylval.line = line_no; return OVERRIDE;}
500 <CLASS_CODE_I>property  {
501                                 yylval.line = line_no;
502                                 BEGIN(PROPERTY_CODE);
503                                 return PROPERTY;
504                         }
505 <PROPERTY_CODE_I>nick   { yylval.line = line_no; return NICK; }
506 <PROPERTY_CODE_I>blurb  { yylval.line = line_no; return BLURB; }
507 <PROPERTY_CODE_I>maximum        { yylval.line = line_no; return MAXIMUM; }
508 <PROPERTY_CODE_I>minimum        { yylval.line = line_no; return MINIMUM; }
509 <PROPERTY_CODE_I>default_value  { yylval.line = line_no; return DEFAULT_VALUE; }
510 <PROPERTY_CODE_I>flags  { yylval.line = line_no; return FLAGS; }
511 <PROPERTY_CODE_I>type   { yylval.line = line_no; return TYPE; }
512 <PROPERTY_CODE_I>flags_type     { yylval.line = line_no; return FLAGS_TYPE; }
513 <PROPERTY_CODE_I>enum_type      { yylval.line = line_no; return ENUM_TYPE; }
514 <PROPERTY_CODE_I>param_type     { yylval.line = line_no; return PARAM_TYPE; }
515 <PROPERTY_CODE_I>boxed_type     { yylval.line = line_no; return BOXED_TYPE; }
516 <PROPERTY_CODE_I>object_type    { yylval.line = line_no; return OBJECT_TYPE; }
517 <PROPERTY_CODE>[(]      {
518                 yylval.line = line_no;
519                 property_paren_depth = 1;
520                 BEGIN(PROPERTY_CODE_I);
521                 return '(';
522                         }
523 <PROPERTY_CODE_I>[(]    {
524                 yylval.line = line_no;
525                 property_paren_depth++;
526                 return '(';
527                         }
528 <PROPERTY_CODE_I>[)]    {
529                 yylval.line = line_no;
530                 property_paren_depth--;
531                 if (property_paren_depth == 0) {
532                         BEGIN(CLASS_CODE_I);
533                 }
534                 return ')';
535                         }
536
537 <CLASS_CODE,CLASS_CODE_I,PROPERTY_CODE,PROPERTY_CODE_I,INITIAL>0|[1-9][0-9]*|0x[0-9a-fA-F]+|0[0-7]+|[0-9]*\.[0-9]+|\.[0-9][0-9]*        {
538                         yylval.id = g_strdup(yytext);
539                         return NUMBER;
540                 }
541 <CLASS_CODE,CLASS_CODE_I,PROPERTY_CODE,PROPERTY_CODE_I,INITIAL>[A-Za-z_][A-Za-z0-9_]*(::[A-Za-z0-9_]*)+ {
542                         /* This is cpp kind of token thingie */
543                         if (for_cpp) {
544                                 yylval.id = g_strdup(yytext);
545                                 return TOKEN;
546                         } else {
547                                 REJECT;
548                         }
549                 }
550 <CLASS_CODE,CLASS_CODE_I,PROPERTY_CODE,PROPERTY_CODE_I,INITIAL>[A-Za-z_][A-Za-z0-9_]*(:[A-Za-z0-9_]*)+  {
551                         /* this one is for a classname with a namespace */
552                         yylval.id = g_strdup(yytext);
553                         return TYPETOKEN;
554                 }
555 <CLASS_CODE,CLASS_CODE_I,PROPERTY_CODE,PROPERTY_CODE_I,INITIAL>:[A-Za-z_][A-Za-z0-9_]*(:[A-Za-z0-9_]*)* {
556                         /* this is for a classname with an empty namespace */
557                         yylval.id = g_strdup(yytext);
558                         return TYPETOKEN;
559                 }
560 <CLASS_CODE,CLASS_CODE_I,PROPERTY_CODE,PROPERTY_CODE_I,INITIAL>[A-Za-z_][A-Za-z0-9_]*   {
561                         yylval.id = g_strdup(yytext);
562                         return TOKEN;
563                 }
564 <CLASS_CODE,CLASS_CODE_I,PROPERTY_CODE,PROPERTY_CODE_I,INITIAL>\'\\.\'|\'.\'    {
565                         yylval.id = g_strdup(yytext);
566                         return SINGLE_CHAR;
567                 }
568
569 <CLASS_CODE_I>(\[[0-9]*\]|\[[A-Za-z_][A-Za-z0-9_]*\])+  {
570                         yylval.id = g_strdup(yytext);
571                         return ARRAY_DIM;
572                 }
573
574 <CLASS_CODE>\{  {
575                         BEGIN(CLASS_CODE_I);
576                         return '{';
577                 }
578 <CLASS_CODE_I>\{        {
579                         BEGIN(C_CODE);
580                         parenth_depth=1;
581                         class_after_c = TRUE;
582                         yylval.line = line_no;
583                         clear_cbuf();
584                         ccode_line = line_no;
585                         return '{';
586                 }
587 <CLASS_CODE_I>\}        {
588                                 BEGIN(INITIAL);
589                                 return '}';
590                         }
591
592 <CLASS_CODE,CLASS_CODE_I,INITIAL,PROPERTY_CODE,PROPERTY_CODE_I>[\f\t ]  ;  /*ignore*/
593
594 <*>.            {
595                         yylval.line = line_no;
596                         return yytext[0];
597                 }
598
599 <*>[\n\r]       ;  /*ignore*/
600
601 %%