4 * Copyright (C) 1999-2000 the Free Software Foundation.
5 * Copyright (C) 2000 Eazel, Inc.
6 * Copyright (C) 2001-2011 George (Jiri) Lebl
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
34 #include "treefuncs.h"
39 static int parenth_depth = 0;
40 static int before_comment
41 /* New flex is on drugs */
42 #if defined(FLEX_SCANNER) && ! defined(INITIAL)
47 static gboolean class_after_c = FALSE;
48 static int code_type = CCODE;
49 static int before_string;
50 static int property_paren_depth = 0;
53 static char *gtk_doc_func = NULL; /* current gtk-doc func */
54 GHashTable *gtk_doc_hash = NULL;
56 static GString *cbuf = NULL;
59 GList *include_files = NULL;
60 /* 0 no, 1 means yes, 2+ means don't even start looking anymore */
61 static int look_for_includes = 0;
64 /* last filename parsed from a #line directive */
65 char *hline_filename = NULL;
71 cbuf = g_string_new(NULL);
73 cbuf = g_string_assign(cbuf, "");
81 cbuf = g_string_new(s);
83 cbuf = g_string_append(cbuf,s);
88 add_gtk_doc_func(void)
94 gtk_doc_hash = g_hash_table_new(g_str_hash, g_str_equal);
95 g_hash_table_insert(gtk_doc_hash, gtk_doc_func, g_strdup(cbuf->str));
101 /* Ugly warning / error avoidings */
104 int yywrap(void) { return 1; }
117 %x GTK_DOC_BEFORE_NAME
126 /* Avoid warning from flex, cuz flex sucks */
128 yy_current_state = 0;
133 <*>\n { line_no++; REJECT; }
135 <COMMENT>^(I(S.RI).E\(([1-9][0-9]+|[2-9]))/(\)) {
136 /* Thy evil easter egg */
138 #define KK(x) =atoi(oo(&,,x,))
139 #define MM(x,a) {QQ(i);for(i=2;i<x;i++){a}}
140 #define PP(Q) (P%Q)==0
141 #define ooo(x,y,z) if(x z y)
142 #define oo(a,x,y,z) a(yytext[y] x z)
143 #define OooO(a,b) fprintf(stderr,"%s is %s!\n",a,b)
144 QQ(m)=1;ooo(oo(,==,2,'P'),oo(,==,5,'M'),&&
145 ){QQ(P)KK(8);MM(P,ooo(PP(i),,)m=0;)}oo(,=,
146 7,0);ooo(,,m){OooO(oo( &,,8,),oo(&,,2,));}
157 \/\/.*$ { ; /*comment, ignore*/ }
159 <*>^#[ \t]*line[ \t]+[0-9]+([ \t]\"[^\n\r\f\"]*\")? {
163 char *str=g_strdup(yytext);
165 /* find first digit of line number */
167 while(*p&&!isdigit(*p)) p++;
170 /* find end of line number */
171 while(*p&&isdigit(*p)) p++;
174 /* find beginning of filename */
179 /* find end of filename */
180 if(p) p=strchr(p,'"');
183 /* stash number (minus one because we don't count this line) */
184 if(number) line_no=atoi(number)-1;
188 if(hline_filename) g_free(hline_filename);
189 hline_filename=g_strdup(filename);
196 <C_CODE>^#[ \t]*include[ \t][<"][^\n\r">]*[>"] {
197 if(look_for_includes==1) {
200 char *str = g_strdup(yytext);
201 file = strchr(str,'"');
202 if(!file) file = strchr(str,'<');
204 p = strchr(file,'"');
205 if(!p) p = strchr(file,'>');
208 include_files = g_list_prepend(include_files,g_strdup(file));
215 <CLASS_CODE_I>\/\*\*[ \t]*$ {
216 /* eat out gtk doc stuff */
217 BEGIN(GTK_DOC_BEFORE_NAME);
220 <GTK_DOC_BEFORE_NAME>^[ \t]*\*[ \t]*$ {
221 /* empty doc lines */
224 <GTK_DOC_BEFORE_NAME>^[ \t]*\*\ [_a-zA-Z][_a-zA-Z0-9]*:?[ \t]*$ {
227 p = strchr(yytext, '*');
228 g_free(gtk_doc_func);
229 gtk_doc_func = g_strdup(p+2);
230 p = strchr(gtk_doc_func, ':');
232 g_strstrip(gtk_doc_func);
234 <GTK_DOC_BEFORE_NAME>\*\/ {
237 <GTK_DOC_BEFORE_NAME>. {
239 before_comment = CLASS_CODE_I;
241 <GTK_DOC>^[ \t]*\*[ \t]*$ {
242 /* empty doc lines */
245 <GTK_DOC>^[ \t]*\*?\*\/ {
249 <GTK_DOC>^[ \t]*\*[ \t] {
259 before_comment = CLASS_CODE_I;
276 <C_CODE>\/\/.*$ { add_to_cbuf(yytext); /*comment, ignore*/ }
277 <CLASS_CODE>\/\/.*$ { ; /*comment, ignore*/ }
278 <CLASS_CODE_I>\/\/.*$ { ; /*comment, ignore*/ }
279 <PROPERTY_CODE_I>\/\/.*$ { ; /*comment, ignore*/ }
280 \/\* {BEGIN(COMMENT); before_comment = INITIAL; }
284 before_comment = C_CODE;
286 <CLASS_CODE>\/\* {BEGIN(COMMENT); before_comment = CLASS_CODE; }
287 <CLASS_CODE_I>\/\* {BEGIN(COMMENT); before_comment = CLASS_CODE_I; }
288 <PROPERTY_CODE_I>\/\* {BEGIN(COMMENT); before_comment = PROPERTY_CODE_I; }
290 if(before_comment == C_CODE) add_to_cbuf(yytext);
291 BEGIN(before_comment);
294 /* comment, ignore */
295 if(before_comment == C_CODE) add_to_cbuf(yytext);
298 /* comment, ignore */
299 if(before_comment == C_CODE) add_to_cbuf(yytext);
305 class_after_c = FALSE;
308 ccode_line = line_no;
313 class_after_c = FALSE;
316 ccode_line = line_no;
319 ^\%(ht|headertop)\{ {
322 class_after_c = FALSE;
325 ccode_line = line_no;
327 ^\%(ph|privateheader)\{ {
330 class_after_c = FALSE;
333 ccode_line = line_no;
338 class_after_c = FALSE;
341 ccode_line = line_no;
346 class_after_c = FALSE;
349 ccode_line = line_no;
350 if(look_for_includes==0)
356 class_after_c = FALSE;
359 ccode_line = line_no;
360 if(look_for_includes==0)
363 ^\%(ad|afterdecls)\{ {
366 class_after_c = FALSE;
369 ccode_line = line_no;
375 if(look_for_includes==1)
380 <C_CODE>\'\{\' { add_to_cbuf(yytext); }
381 <C_CODE>\'\\\{\' { add_to_cbuf(yytext); }
382 <C_CODE>\'\}\' { add_to_cbuf(yytext); }
383 <C_CODE>\'\\\}\' { add_to_cbuf(yytext); }
384 <C_CODE>\'\"\' { add_to_cbuf(yytext); }
385 <C_CODE>\'\\\"\' { add_to_cbuf(yytext); }
387 <C_CODE>\\. { add_to_cbuf(yytext); }
392 before_string = C_CODE;
395 <PROPERTY_CODE_I>\" {
397 before_string = PROPERTY_CODE_I;
400 <CODE_STRING>\\. { add_to_cbuf(yytext); }
402 BEGIN(before_string);
404 if (before_string == PROPERTY_CODE_I) {
405 yylval.id = cbuf->str;
406 g_string_free (cbuf, FALSE);
411 <CODE_STRING>. { add_to_cbuf(yytext); }
412 <CODE_STRING>\n { add_to_cbuf(yytext); }
420 if(parenth_depth<0) {
422 } else if(parenth_depth==0 && class_after_c) {
431 <C_CODE>. { add_to_cbuf(yytext); }
432 <C_CODE>\n { add_to_cbuf(yytext); }
435 static int found_classes = 0;
436 look_for_includes = 2;
439 if(++found_classes > 1) {
440 error_print(GOB_ERROR, line_no,
441 "Only one class per file allowed");
447 error { return ERROR; }
448 enum { return ENUM; }
449 flags { return FLAGS; }
451 ^[ \t]*requires[ \t]+[0-9]+\.[0-9]+\.[0-9]+[\t ]*$ {
452 int maj = 0, min = 0, pl = 0;
453 int rmaj = 0, rmin = 0, rpl = 0;
454 int effective_maj = 0;
455 int effective_rmaj = 0;
458 sscanf (VERSION, "%d.%d.%d", &rmaj, &rmin, &rpl);
459 effective_rmaj = rmaj;
461 effective_rmaj = rmaj + 1;
463 p = strchr (yytext,'r');
464 g_assert (p); /* we MUST have found it */
465 sscanf (p, "requires %d.%d.%d", &maj, &min, &pl);
468 effective_maj = maj + 1;
471 (rmaj == maj && rmin < min) ||
472 (rmaj == maj && rmin == min && rpl < pl)) {
473 error_printf (GOB_ERROR, line_no,
474 "GOB version at least %d.%d.%d required "
476 "To upgrade your gob, see: "
477 "http://www.5z.com/jirka/gob.html",
478 maj, min, pl, VERSION);
481 if(effective_rmaj != effective_maj) {
482 error_printf(GOB_ERROR, line_no,
483 "GOB major version %d required "
485 "To upgrade your gob, see: "
486 "http://www.5z.com/jirka/gob.html",
487 effective_maj, VERSION);
492 <CLASS_CODE,CLASS_CODE_I>class|this {
494 error_printf(GOB_WARN, line_no,
495 "'%s' keyword should not "
496 "be used when generating "
502 <CLASS_CODE>from {return FROM;}
505 before_string = CLASS_CODE;
508 <CLASS_STRING>\\. { add_to_cbuf(yytext); }
510 BEGIN(before_string);
512 yylval.id = cbuf->str;
513 g_string_free (cbuf, FALSE);
517 <CLASS_STRING>. { add_to_cbuf(yytext); }
518 <CLASS_STRING>\n { add_to_cbuf(yytext); }
522 before_string = CLASS_CODE_I;
526 <CLASS_CODE_I,PROPERTY_CODE_I>void {return VOID;}
527 <CLASS_CODE_I,PROPERTY_CODE_I>struct {return STRUCT;}
528 <CLASS_CODE_I,PROPERTY_CODE_I>union {return UNION;}
529 <CLASS_CODE_I,PROPERTY_CODE_I>enum {return ENUM;}
530 <CLASS_CODE_I,PROPERTY_CODE_I>signed {return SIGNED;}
531 <CLASS_CODE_I,PROPERTY_CODE_I>unsigned {return UNSIGNED;}
532 <CLASS_CODE_I,PROPERTY_CODE_I>long {return LONG;}
533 <CLASS_CODE_I,PROPERTY_CODE_I>short {return SHORT;}
534 <CLASS_CODE_I,PROPERTY_CODE_I>int {return INT;}
535 <CLASS_CODE_I,PROPERTY_CODE_I>float {return FLOAT;}
536 <CLASS_CODE_I,PROPERTY_CODE_I>double {return DOUBLE;}
537 <CLASS_CODE_I,PROPERTY_CODE_I>char {return CHAR;}
538 <CLASS_CODE_I,PROPERTY_CODE_I>const {return CONST;}
540 <CLASS_CODE_I>\.\.\. {return THREEDOTS;}
542 <CLASS_CODE_I>public {yylval.line = line_no; return PUBLIC;}
543 <CLASS_CODE_I>private {yylval.line = line_no; return PRIVATE;}
544 <CLASS_CODE_I>protected {yylval.line = line_no; return PROTECTED;}
545 <CLASS_CODE_I>classwide {yylval.line = line_no; return CLASSWIDE;}
546 <CLASS_CODE_I>argument {yylval.line = line_no; return ARGUMENT;}
547 <CLASS_CODE_I>virtual {yylval.line = line_no; return VIRTUAL;}
548 <CLASS_CODE_I>signal {yylval.line = line_no; return SIGNAL;}
549 <CLASS_CODE_I>override {yylval.line = line_no; return OVERRIDE;}
550 <CLASS_CODE_I>property {
551 yylval.line = line_no;
552 BEGIN(PROPERTY_CODE);
555 <PROPERTY_CODE_I>nick { yylval.line = line_no; return NICK; }
556 <PROPERTY_CODE_I>name { yylval.line = line_no; return NAME; }
557 <PROPERTY_CODE_I>blurb { yylval.line = line_no; return BLURB; }
558 <PROPERTY_CODE_I>maximum { yylval.line = line_no; return MAXIMUM; }
559 <PROPERTY_CODE_I>minimum { yylval.line = line_no; return MINIMUM; }
560 <PROPERTY_CODE_I>default_value { yylval.line = line_no; return DEFAULT_VALUE; }
561 <PROPERTY_CODE_I>flags { yylval.line = line_no; return FLAGS; }
562 <PROPERTY_CODE_I>type { yylval.line = line_no; return TYPE; }
563 <PROPERTY_CODE_I>flags_type { yylval.line = line_no; return FLAGS_TYPE; }
564 <PROPERTY_CODE_I>enum_type { yylval.line = line_no; return ENUM_TYPE; }
565 <PROPERTY_CODE_I>param_type { yylval.line = line_no; return PARAM_TYPE; }
566 <PROPERTY_CODE_I>boxed_type { yylval.line = line_no; return BOXED_TYPE; }
567 <PROPERTY_CODE_I>object_type { yylval.line = line_no; return OBJECT_TYPE; }
569 yylval.line = line_no;
570 property_paren_depth = 1;
571 BEGIN(PROPERTY_CODE_I);
574 <PROPERTY_CODE_I>[(] {
575 yylval.line = line_no;
576 property_paren_depth++;
579 <PROPERTY_CODE_I>[)] {
580 yylval.line = line_no;
581 property_paren_depth--;
582 if (property_paren_depth == 0) {
588 <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]* {
589 yylval.id = g_strdup(yytext);
592 <CLASS_CODE,CLASS_CODE_I,PROPERTY_CODE,PROPERTY_CODE_I,INITIAL>[A-Za-z_][A-Za-z0-9_]*(::[A-Za-z0-9_]*)+ {
593 /* This is cpp kind of token thingie */
595 yylval.id = g_strdup(yytext);
601 <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_]*)+ {
602 /* this one is for a classname with a namespace */
603 yylval.id = g_strdup(yytext);
606 <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_]*)* {
607 /* this is for a classname with an empty namespace */
608 yylval.id = g_strdup(yytext);
611 <CLASS_CODE,CLASS_CODE_I,PROPERTY_CODE,PROPERTY_CODE_I,INITIAL>[A-Za-z_][A-Za-z0-9_]* {
612 yylval.id = g_strdup(yytext);
615 <CLASS_CODE,CLASS_CODE_I,PROPERTY_CODE,PROPERTY_CODE_I,INITIAL>\'\\.\'|\'.\' {
616 yylval.id = g_strdup(yytext);
620 <CLASS_CODE_I>(\[[0-9]*\]|\[[A-Za-z_][A-Za-z0-9_]*\])+ {
621 yylval.id = g_strdup(yytext);
624 <CLASS_CODE_I>:[0-9]+ {
625 /* cheat for bitfield */
626 yylval.id = g_strdup(yytext);
633 <CLASS_CODE_I,PROPERTY_CODE>\{ {
636 class_after_c = TRUE;
637 yylval.line = line_no;
639 ccode_line = line_no;
647 <CLASS_CODE,CLASS_CODE_I,INITIAL,PROPERTY_CODE,PROPERTY_CODE_I>[\f\t ] ; /*ignore*/
649 <*>[\n\r] ; /*ignore*/
653 yylval.line = line_no;
657 ^[ \t]*prealloc[ \t]+[0-9]+[ \t]*$ {
660 p = strchr (yytext,'p');
661 g_assert (p); /* we MUST have found it */
662 sscanf (p, "prealloc %d", &t);