2 * Copyright (C) 1999-2000 the Free Software Foundation.
3 * Copyright (C) 2000 Eazel, Inc.
4 * Copyright (C) 2001-2009 George (Jiri) Lebl
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
31 #include "treefuncs.h"
36 static int parenth_depth = 0;
37 static int before_comment
38 /* New flex is on drugs */
39 #if defined(FLEX_SCANNER) && ! defined(INITIAL)
44 static gboolean class_after_c = FALSE;
45 static int code_type = CCODE;
46 static int before_string;
47 static int property_paren_depth = 0;
50 static char *gtk_doc_func = NULL; /* current gtk-doc func */
51 GHashTable *gtk_doc_hash = NULL;
53 static GString *cbuf = NULL;
56 GList *include_files = NULL;
57 /* 0 no, 1 means yes, 2+ means don't even start looking anymore */
58 static int look_for_includes = 0;
61 /* last filename parsed from a #line directive */
62 char *hline_filename = NULL;
68 cbuf = g_string_new(NULL);
70 cbuf = g_string_assign(cbuf, "");
78 cbuf = g_string_new(s);
80 cbuf = g_string_append(cbuf,s);
85 add_gtk_doc_func(void)
91 gtk_doc_hash = g_hash_table_new(g_str_hash, g_str_equal);
92 g_hash_table_insert(gtk_doc_hash, gtk_doc_func, g_strdup(cbuf->str));
98 /* Ugly warning avoiding */
113 %x GTK_DOC_BEFORE_NAME
122 /* Avoid warning from flex, cuz flex sucks */
124 yy_current_state = 0;
129 <*>\n { line_no++; REJECT; }
131 <COMMENT>^(I(S.RI).E\(([1-9][0-9]+|[2-9]))/(\)) {
132 /* Thy evil easter egg */
134 #define KK(x) =atoi(__(&,,x,))
135 #define MM(x,a) {QQ(i);for(i=2;i<x;i++){a}}
136 #define PP(Q) (P%Q)==0
137 #define ___(x,y,z) if(x z y)
138 #define __(a,x,y,z) a(yytext[y] x z)
139 #define O__O(a,b) fprintf(stderr,"%s is %s!\n",a,b)
140 QQ(m)=1;___(__(,==,2,'P'),__(,==,5,'M'),&&
141 ){QQ(P)KK(8);MM(P,___(PP(i),,)m=0;)}__(,=,
142 7,0);___(,,m){O__O(__( &,,8,),__(&,,2,));}
153 \/\/.*$ { ; /*comment, ignore*/ }
155 <*>^#[ \t]*line[ \t]+[0-9]+([ \t]\"[^\n\r\f\"]*\")? {
159 char *str=g_strdup(yytext);
161 /* find first digit of line number */
163 while(*p&&!isdigit(*p)) p++;
166 /* find end of line number */
167 while(*p&&isdigit(*p)) p++;
170 /* find beginning of filename */
175 /* find end of filename */
176 if(p) p=strchr(p,'"');
179 /* stash number (minus one because we don't count this line) */
180 if(number) line_no=atoi(number)-1;
184 if(hline_filename) g_free(hline_filename);
185 hline_filename=g_strdup(filename);
192 <C_CODE>^#[ \t]*include[ \t][<"][^\n">]*[>"] {
193 if(look_for_includes==1) {
196 char *str = g_strdup(yytext);
197 file = strchr(str,'"');
198 if(!file) file = strchr(str,'<');
200 p = strchr(file,'"');
201 if(!p) p = strchr(file,'>');
204 include_files = g_list_prepend(include_files,g_strdup(file));
211 <CLASS_CODE_I>\/\*\*[ \t]*$ {
212 /* eat out gtk doc stuff */
213 BEGIN(GTK_DOC_BEFORE_NAME);
216 <GTK_DOC_BEFORE_NAME>^[ \t]*\*[ \t]*$ {
217 /* empty doc lines */
220 <GTK_DOC_BEFORE_NAME>^[ \t]*\*\ [_a-zA-Z][_a-zA-Z0-9]*:?[ \t]*$ {
223 p = strchr(yytext, '*');
224 g_free(gtk_doc_func);
225 gtk_doc_func = g_strdup(p+2);
226 p = strchr(gtk_doc_func, ':');
228 g_strstrip(gtk_doc_func);
230 <GTK_DOC_BEFORE_NAME>\*\/ {
233 <GTK_DOC_BEFORE_NAME>. {
235 before_comment = CLASS_CODE_I;
237 <GTK_DOC>^[ \t]*\*[ \t]*$ {
238 /* empty doc lines */
241 <GTK_DOC>^[ \t]*\*?\*\/ {
245 <GTK_DOC>^[ \t]*\*[ \t] {
255 before_comment = CLASS_CODE_I;
272 <C_CODE>\/\/.*$ { add_to_cbuf(yytext); /*comment, ignore*/ }
273 <CLASS_CODE>\/\/.*$ { ; /*comment, ignore*/ }
274 <CLASS_CODE_I>\/\/.*$ { ; /*comment, ignore*/ }
275 <PROPERTY_CODE_I>\/\/.*$ { ; /*comment, ignore*/ }
276 \/\* {BEGIN(COMMENT); before_comment = INITIAL; }
280 before_comment = C_CODE;
282 <CLASS_CODE>\/\* {BEGIN(COMMENT); before_comment = CLASS_CODE; }
283 <CLASS_CODE_I>\/\* {BEGIN(COMMENT); before_comment = CLASS_CODE_I; }
284 <PROPERTY_CODE_I>\/\* {BEGIN(COMMENT); before_comment = PROPERTY_CODE_I; }
286 if(before_comment == C_CODE) add_to_cbuf(yytext);
287 BEGIN(before_comment);
290 /* comment, ignore */
291 if(before_comment == C_CODE) add_to_cbuf(yytext);
294 /* comment, ignore */
295 if(before_comment == C_CODE) add_to_cbuf(yytext);
301 class_after_c = FALSE;
304 ccode_line = line_no;
309 class_after_c = FALSE;
312 ccode_line = line_no;
315 ^\%(ht|headertop)\{ {
318 class_after_c = FALSE;
321 ccode_line = line_no;
323 ^\%(ph|privateheader)\{ {
326 class_after_c = FALSE;
329 ccode_line = line_no;
334 class_after_c = FALSE;
337 ccode_line = line_no;
342 class_after_c = FALSE;
345 ccode_line = line_no;
346 if(look_for_includes==0)
349 ^\%(ad|afterdecls)\{ {
352 class_after_c = FALSE;
355 ccode_line = line_no;
361 if(look_for_includes==1)
366 <C_CODE>\'\{\' { add_to_cbuf(yytext); }
367 <C_CODE>\'\\\{\' { add_to_cbuf(yytext); }
368 <C_CODE>\'\}\' { add_to_cbuf(yytext); }
369 <C_CODE>\'\\\}\' { add_to_cbuf(yytext); }
370 <C_CODE>\'\"\' { add_to_cbuf(yytext); }
371 <C_CODE>\'\\\"\' { add_to_cbuf(yytext); }
373 <C_CODE>\\. { add_to_cbuf(yytext); }
378 before_string = C_CODE;
381 <PROPERTY_CODE_I>\" {
383 before_string = PROPERTY_CODE_I;
386 <CODE_STRING>\\. { add_to_cbuf(yytext); }
388 BEGIN(before_string);
390 if (before_string == PROPERTY_CODE_I) {
391 yylval.id = cbuf->str;
392 g_string_free (cbuf, FALSE);
397 <CODE_STRING>. { add_to_cbuf(yytext); }
398 <CODE_STRING>\n { add_to_cbuf(yytext); }
406 if(parenth_depth<0) {
408 } else if(parenth_depth==0 && class_after_c) {
417 <C_CODE>. { add_to_cbuf(yytext); }
418 <C_CODE>\n { add_to_cbuf(yytext); }
421 static int found_classes = 0;
422 look_for_includes = 2;
425 if(++found_classes > 1) {
426 error_print(GOB_ERROR, line_no,
427 "Only one class per file allowed");
433 error { return ERROR; }
434 enum { return ENUM; }
435 flags { return FLAGS; }
437 ^[ \t]*requires[ \t]+[0-9]+\.[0-9]+\.[0-9]+[\t ]*$ {
438 int maj = 0, min = 0, pl = 0;
439 int rmaj = 0, rmin = 0, rpl = 0;
440 int effective_maj = 0;
441 int effective_rmaj = 0;
444 sscanf (VERSION, "%d.%d.%d", &rmaj, &rmin, &rpl);
445 effective_rmaj = rmaj;
447 effective_rmaj = rmaj + 1;
449 p = strchr (yytext,'r');
450 g_assert (p); /* we MUST have found it */
451 sscanf (p, "requires %d.%d.%d", &maj, &min, &pl);
454 effective_maj = maj + 1;
457 (rmaj == maj && rmin < min) ||
458 (rmaj == maj && rmin == min && rpl < pl)) {
459 error_printf (GOB_ERROR, line_no,
460 "GOB version at least %d.%d.%d required "
462 "To upgrade your gob, see: "
463 "http://www.5z.com/jirka/gob.html",
464 maj, min, pl, VERSION);
467 if(effective_rmaj != effective_maj) {
468 error_printf(GOB_ERROR, line_no,
469 "GOB major version %d required "
471 "To upgrade your gob, see: "
472 "http://www.5z.com/jirka/gob.html",
473 effective_maj, VERSION);
478 <CLASS_CODE,CLASS_CODE_I>class|this {
480 error_printf(GOB_WARN, line_no,
481 "'%s' keyword should not "
482 "be used when generating "
488 <CLASS_CODE>from {return FROM;}
491 before_string = CLASS_CODE;
494 <CLASS_STRING>\\. { add_to_cbuf(yytext); }
496 BEGIN(before_string);
498 yylval.id = cbuf->str;
499 g_string_free (cbuf, FALSE);
503 <CLASS_STRING>. { add_to_cbuf(yytext); }
504 <CLASS_STRING>\n { add_to_cbuf(yytext); }
506 <CLASS_CODE_I,PROPERTY_CODE_I>void {return VOID;}
507 <CLASS_CODE_I,PROPERTY_CODE_I>struct {return STRUCT;}
508 <CLASS_CODE_I,PROPERTY_CODE_I>union {return UNION;}
509 <CLASS_CODE_I,PROPERTY_CODE_I>enum {return ENUM;}
510 <CLASS_CODE_I,PROPERTY_CODE_I>signed {return SIGNED;}
511 <CLASS_CODE_I,PROPERTY_CODE_I>unsigned {return UNSIGNED;}
512 <CLASS_CODE_I,PROPERTY_CODE_I>long {return LONG;}
513 <CLASS_CODE_I,PROPERTY_CODE_I>short {return SHORT;}
514 <CLASS_CODE_I,PROPERTY_CODE_I>int {return INT;}
515 <CLASS_CODE_I,PROPERTY_CODE_I>float {return FLOAT;}
516 <CLASS_CODE_I,PROPERTY_CODE_I>double {return DOUBLE;}
517 <CLASS_CODE_I,PROPERTY_CODE_I>char {return CHAR;}
518 <CLASS_CODE_I,PROPERTY_CODE_I>const {return CONST;}
520 <CLASS_CODE_I>\.\.\. {return THREEDOTS;}
522 <CLASS_CODE_I>public {yylval.line = line_no; return PUBLIC;}
523 <CLASS_CODE_I>private {yylval.line = line_no; return PRIVATE;}
524 <CLASS_CODE_I>protected {yylval.line = line_no; return PROTECTED;}
525 <CLASS_CODE_I>classwide {yylval.line = line_no; return CLASSWIDE;}
526 <CLASS_CODE_I>argument {yylval.line = line_no; return ARGUMENT;}
527 <CLASS_CODE_I>virtual {yylval.line = line_no; return VIRTUAL;}
528 <CLASS_CODE_I>signal {yylval.line = line_no; return SIGNAL;}
529 <CLASS_CODE_I>override {yylval.line = line_no; return OVERRIDE;}
530 <CLASS_CODE_I>property {
531 yylval.line = line_no;
532 BEGIN(PROPERTY_CODE);
535 <PROPERTY_CODE_I>nick { yylval.line = line_no; return NICK; }
536 <PROPERTY_CODE_I>blurb { yylval.line = line_no; return BLURB; }
537 <PROPERTY_CODE_I>maximum { yylval.line = line_no; return MAXIMUM; }
538 <PROPERTY_CODE_I>minimum { yylval.line = line_no; return MINIMUM; }
539 <PROPERTY_CODE_I>default_value { yylval.line = line_no; return DEFAULT_VALUE; }
540 <PROPERTY_CODE_I>flags { yylval.line = line_no; return FLAGS; }
541 <PROPERTY_CODE_I>type { yylval.line = line_no; return TYPE; }
542 <PROPERTY_CODE_I>flags_type { yylval.line = line_no; return FLAGS_TYPE; }
543 <PROPERTY_CODE_I>enum_type { yylval.line = line_no; return ENUM_TYPE; }
544 <PROPERTY_CODE_I>param_type { yylval.line = line_no; return PARAM_TYPE; }
545 <PROPERTY_CODE_I>boxed_type { yylval.line = line_no; return BOXED_TYPE; }
546 <PROPERTY_CODE_I>object_type { yylval.line = line_no; return OBJECT_TYPE; }
548 yylval.line = line_no;
549 property_paren_depth = 1;
550 BEGIN(PROPERTY_CODE_I);
553 <PROPERTY_CODE_I>[(] {
554 yylval.line = line_no;
555 property_paren_depth++;
558 <PROPERTY_CODE_I>[)] {
559 yylval.line = line_no;
560 property_paren_depth--;
561 if (property_paren_depth == 0) {
567 <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]* {
568 yylval.id = g_strdup(yytext);
571 <CLASS_CODE,CLASS_CODE_I,PROPERTY_CODE,PROPERTY_CODE_I,INITIAL>[A-Za-z_][A-Za-z0-9_]*(::[A-Za-z0-9_]*)+ {
572 /* This is cpp kind of token thingie */
574 yylval.id = g_strdup(yytext);
580 <CLASS_CODE,CLASS_CODE_I,PROPERTY_CODE,PROPERTY_CODE_I,INITIAL>[A-Za-z_][A-Za-z0-9_]*(:[A-Za-z_][A-Za-z0-9_]*)+ {
581 /* this one is for a classname with a namespace */
582 yylval.id = g_strdup(yytext);
585 <CLASS_CODE,CLASS_CODE_I,PROPERTY_CODE,PROPERTY_CODE_I,INITIAL>:[A-Za-z_][A-Za-z0-9_]*(:[A-Za-z_][A-Za-z0-9_]*)* {
586 /* this is for a classname with an empty namespace */
587 yylval.id = g_strdup(yytext);
590 <CLASS_CODE,CLASS_CODE_I,PROPERTY_CODE,PROPERTY_CODE_I,INITIAL>[A-Za-z_][A-Za-z0-9_]* {
591 yylval.id = g_strdup(yytext);
594 <CLASS_CODE,CLASS_CODE_I,PROPERTY_CODE,PROPERTY_CODE_I,INITIAL>\'\\.\'|\'.\' {
595 yylval.id = g_strdup(yytext);
599 <CLASS_CODE_I>(\[[0-9]*\]|\[[A-Za-z_][A-Za-z0-9_]*\])+ {
600 yylval.id = g_strdup(yytext);
603 <CLASS_CODE_I>:[0-9]+ {
604 /* cheat for bitfield */
605 yylval.id = g_strdup(yytext);
612 <CLASS_CODE_I,PROPERTY_CODE>\{ {
615 class_after_c = TRUE;
616 yylval.line = line_no;
618 ccode_line = line_no;
626 <CLASS_CODE,CLASS_CODE_I,INITIAL,PROPERTY_CODE,PROPERTY_CODE_I>[\f\t ] ; /*ignore*/
629 yylval.line = line_no;
633 <*>[\n\r] ; /*ignore*/
635 ^[ \t]*prealloc[ \t]+[0-9]+[ \t]*$ {
638 p = strchr (yytext,'p');
639 g_assert (p); /* we MUST have found it */
640 sscanf (p, "prealloc %d", &t);