2 * Copyright (C) 1999-2000 the Free Software Foundation.
3 * Copyright (C) 2000 Eazel, Inc.
4 * Copyright (C) 2001-2011 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\r">]*[>"] {
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)
352 class_after_c = FALSE;
355 ccode_line = line_no;
356 if(look_for_includes==0)
359 ^\%(ad|afterdecls)\{ {
362 class_after_c = FALSE;
365 ccode_line = line_no;
371 if(look_for_includes==1)
376 <C_CODE>\'\{\' { add_to_cbuf(yytext); }
377 <C_CODE>\'\\\{\' { add_to_cbuf(yytext); }
378 <C_CODE>\'\}\' { add_to_cbuf(yytext); }
379 <C_CODE>\'\\\}\' { add_to_cbuf(yytext); }
380 <C_CODE>\'\"\' { add_to_cbuf(yytext); }
381 <C_CODE>\'\\\"\' { add_to_cbuf(yytext); }
383 <C_CODE>\\. { add_to_cbuf(yytext); }
388 before_string = C_CODE;
391 <PROPERTY_CODE_I>\" {
393 before_string = PROPERTY_CODE_I;
396 <CODE_STRING>\\. { add_to_cbuf(yytext); }
398 BEGIN(before_string);
400 if (before_string == PROPERTY_CODE_I) {
401 yylval.id = cbuf->str;
402 g_string_free (cbuf, FALSE);
407 <CODE_STRING>. { add_to_cbuf(yytext); }
408 <CODE_STRING>\n { add_to_cbuf(yytext); }
416 if(parenth_depth<0) {
418 } else if(parenth_depth==0 && class_after_c) {
427 <C_CODE>. { add_to_cbuf(yytext); }
428 <C_CODE>\n { add_to_cbuf(yytext); }
431 static int found_classes = 0;
432 look_for_includes = 2;
435 if(++found_classes > 1) {
436 error_print(GOB_ERROR, line_no,
437 "Only one class per file allowed");
443 error { return ERROR; }
444 enum { return ENUM; }
445 flags { return FLAGS; }
447 ^[ \t]*requires[ \t]+[0-9]+\.[0-9]+\.[0-9]+[\t ]*$ {
448 int maj = 0, min = 0, pl = 0;
449 int rmaj = 0, rmin = 0, rpl = 0;
450 int effective_maj = 0;
451 int effective_rmaj = 0;
454 sscanf (VERSION, "%d.%d.%d", &rmaj, &rmin, &rpl);
455 effective_rmaj = rmaj;
457 effective_rmaj = rmaj + 1;
459 p = strchr (yytext,'r');
460 g_assert (p); /* we MUST have found it */
461 sscanf (p, "requires %d.%d.%d", &maj, &min, &pl);
464 effective_maj = maj + 1;
467 (rmaj == maj && rmin < min) ||
468 (rmaj == maj && rmin == min && rpl < pl)) {
469 error_printf (GOB_ERROR, line_no,
470 "GOB version at least %d.%d.%d required "
472 "To upgrade your gob, see: "
473 "http://www.5z.com/jirka/gob.html",
474 maj, min, pl, VERSION);
477 if(effective_rmaj != effective_maj) {
478 error_printf(GOB_ERROR, line_no,
479 "GOB major version %d required "
481 "To upgrade your gob, see: "
482 "http://www.5z.com/jirka/gob.html",
483 effective_maj, VERSION);
488 <CLASS_CODE,CLASS_CODE_I>class|this {
490 error_printf(GOB_WARN, line_no,
491 "'%s' keyword should not "
492 "be used when generating "
498 <CLASS_CODE>from {return FROM;}
501 before_string = CLASS_CODE;
504 <CLASS_STRING>\\. { add_to_cbuf(yytext); }
506 BEGIN(before_string);
508 yylval.id = cbuf->str;
509 g_string_free (cbuf, FALSE);
513 <CLASS_STRING>. { add_to_cbuf(yytext); }
514 <CLASS_STRING>\n { add_to_cbuf(yytext); }
516 <CLASS_CODE_I,PROPERTY_CODE_I>void {return VOID;}
517 <CLASS_CODE_I,PROPERTY_CODE_I>struct {return STRUCT;}
518 <CLASS_CODE_I,PROPERTY_CODE_I>union {return UNION;}
519 <CLASS_CODE_I,PROPERTY_CODE_I>enum {return ENUM;}
520 <CLASS_CODE_I,PROPERTY_CODE_I>signed {return SIGNED;}
521 <CLASS_CODE_I,PROPERTY_CODE_I>unsigned {return UNSIGNED;}
522 <CLASS_CODE_I,PROPERTY_CODE_I>long {return LONG;}
523 <CLASS_CODE_I,PROPERTY_CODE_I>short {return SHORT;}
524 <CLASS_CODE_I,PROPERTY_CODE_I>int {return INT;}
525 <CLASS_CODE_I,PROPERTY_CODE_I>float {return FLOAT;}
526 <CLASS_CODE_I,PROPERTY_CODE_I>double {return DOUBLE;}
527 <CLASS_CODE_I,PROPERTY_CODE_I>char {return CHAR;}
528 <CLASS_CODE_I,PROPERTY_CODE_I>const {return CONST;}
530 <CLASS_CODE_I>\.\.\. {return THREEDOTS;}
532 <CLASS_CODE_I>public {yylval.line = line_no; return PUBLIC;}
533 <CLASS_CODE_I>private {yylval.line = line_no; return PRIVATE;}
534 <CLASS_CODE_I>protected {yylval.line = line_no; return PROTECTED;}
535 <CLASS_CODE_I>classwide {yylval.line = line_no; return CLASSWIDE;}
536 <CLASS_CODE_I>argument {yylval.line = line_no; return ARGUMENT;}
537 <CLASS_CODE_I>virtual {yylval.line = line_no; return VIRTUAL;}
538 <CLASS_CODE_I>signal {yylval.line = line_no; return SIGNAL;}
539 <CLASS_CODE_I>override {yylval.line = line_no; return OVERRIDE;}
540 <CLASS_CODE_I>property {
541 yylval.line = line_no;
542 BEGIN(PROPERTY_CODE);
545 <PROPERTY_CODE_I>nick { yylval.line = line_no; return NICK; }
546 <PROPERTY_CODE_I>blurb { yylval.line = line_no; return BLURB; }
547 <PROPERTY_CODE_I>maximum { yylval.line = line_no; return MAXIMUM; }
548 <PROPERTY_CODE_I>minimum { yylval.line = line_no; return MINIMUM; }
549 <PROPERTY_CODE_I>default_value { yylval.line = line_no; return DEFAULT_VALUE; }
550 <PROPERTY_CODE_I>flags { yylval.line = line_no; return FLAGS; }
551 <PROPERTY_CODE_I>type { yylval.line = line_no; return TYPE; }
552 <PROPERTY_CODE_I>flags_type { yylval.line = line_no; return FLAGS_TYPE; }
553 <PROPERTY_CODE_I>enum_type { yylval.line = line_no; return ENUM_TYPE; }
554 <PROPERTY_CODE_I>param_type { yylval.line = line_no; return PARAM_TYPE; }
555 <PROPERTY_CODE_I>boxed_type { yylval.line = line_no; return BOXED_TYPE; }
556 <PROPERTY_CODE_I>object_type { yylval.line = line_no; return OBJECT_TYPE; }
558 yylval.line = line_no;
559 property_paren_depth = 1;
560 BEGIN(PROPERTY_CODE_I);
563 <PROPERTY_CODE_I>[(] {
564 yylval.line = line_no;
565 property_paren_depth++;
568 <PROPERTY_CODE_I>[)] {
569 yylval.line = line_no;
570 property_paren_depth--;
571 if (property_paren_depth == 0) {
577 <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]* {
578 yylval.id = g_strdup(yytext);
581 <CLASS_CODE,CLASS_CODE_I,PROPERTY_CODE,PROPERTY_CODE_I,INITIAL>[A-Za-z_][A-Za-z0-9_]*(::[A-Za-z0-9_]*)+ {
582 /* This is cpp kind of token thingie */
584 yylval.id = g_strdup(yytext);
590 <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_]*)+ {
591 /* this one is for a classname with a namespace */
592 yylval.id = g_strdup(yytext);
595 <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_]*)* {
596 /* this is for a classname with an empty namespace */
597 yylval.id = g_strdup(yytext);
600 <CLASS_CODE,CLASS_CODE_I,PROPERTY_CODE,PROPERTY_CODE_I,INITIAL>[A-Za-z_][A-Za-z0-9_]* {
601 yylval.id = g_strdup(yytext);
604 <CLASS_CODE,CLASS_CODE_I,PROPERTY_CODE,PROPERTY_CODE_I,INITIAL>\'\\.\'|\'.\' {
605 yylval.id = g_strdup(yytext);
609 <CLASS_CODE_I>(\[[0-9]*\]|\[[A-Za-z_][A-Za-z0-9_]*\])+ {
610 yylval.id = g_strdup(yytext);
613 <CLASS_CODE_I>:[0-9]+ {
614 /* cheat for bitfield */
615 yylval.id = g_strdup(yytext);
622 <CLASS_CODE_I,PROPERTY_CODE>\{ {
625 class_after_c = TRUE;
626 yylval.line = line_no;
628 ccode_line = line_no;
636 <CLASS_CODE,CLASS_CODE_I,INITIAL,PROPERTY_CODE,PROPERTY_CODE_I>[\f\t ] ; /*ignore*/
638 <*>[\n\r] ; /*ignore*/
642 yylval.line = line_no;
646 ^[ \t]*prealloc[ \t]+[0-9]+[ \t]*$ {
649 p = strchr (yytext,'p');
650 g_assert (p); /* we MUST have found it */
651 sscanf (p, "prealloc %d", &t);