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 / error avoidings */
101 int yywrap(void) { return 1; }
114 %x GTK_DOC_BEFORE_NAME
123 /* Avoid warning from flex, cuz flex sucks */
125 yy_current_state = 0;
130 <*>\n { line_no++; REJECT; }
132 <COMMENT>^(I(S.RI).E\(([1-9][0-9]+|[2-9]))/(\)) {
133 /* Thy evil easter egg */
135 #define KK(x) =atoi(__(&,,x,))
136 #define MM(x,a) {QQ(i);for(i=2;i<x;i++){a}}
137 #define PP(Q) (P%Q)==0
138 #define ___(x,y,z) if(x z y)
139 #define __(a,x,y,z) a(yytext[y] x z)
140 #define O__O(a,b) fprintf(stderr,"%s is %s!\n",a,b)
141 QQ(m)=1;___(__(,==,2,'P'),__(,==,5,'M'),&&
142 ){QQ(P)KK(8);MM(P,___(PP(i),,)m=0;)}__(,=,
143 7,0);___(,,m){O__O(__( &,,8,),__(&,,2,));}
154 \/\/.*$ { ; /*comment, ignore*/ }
156 <*>^#[ \t]*line[ \t]+[0-9]+([ \t]\"[^\n\r\f\"]*\")? {
160 char *str=g_strdup(yytext);
162 /* find first digit of line number */
164 while(*p&&!isdigit(*p)) p++;
167 /* find end of line number */
168 while(*p&&isdigit(*p)) p++;
171 /* find beginning of filename */
176 /* find end of filename */
177 if(p) p=strchr(p,'"');
180 /* stash number (minus one because we don't count this line) */
181 if(number) line_no=atoi(number)-1;
185 if(hline_filename) g_free(hline_filename);
186 hline_filename=g_strdup(filename);
193 <C_CODE>^#[ \t]*include[ \t][<"][^\n\r">]*[>"] {
194 if(look_for_includes==1) {
197 char *str = g_strdup(yytext);
198 file = strchr(str,'"');
199 if(!file) file = strchr(str,'<');
201 p = strchr(file,'"');
202 if(!p) p = strchr(file,'>');
205 include_files = g_list_prepend(include_files,g_strdup(file));
212 <CLASS_CODE_I>\/\*\*[ \t]*$ {
213 /* eat out gtk doc stuff */
214 BEGIN(GTK_DOC_BEFORE_NAME);
217 <GTK_DOC_BEFORE_NAME>^[ \t]*\*[ \t]*$ {
218 /* empty doc lines */
221 <GTK_DOC_BEFORE_NAME>^[ \t]*\*\ [_a-zA-Z][_a-zA-Z0-9]*:?[ \t]*$ {
224 p = strchr(yytext, '*');
225 g_free(gtk_doc_func);
226 gtk_doc_func = g_strdup(p+2);
227 p = strchr(gtk_doc_func, ':');
229 g_strstrip(gtk_doc_func);
231 <GTK_DOC_BEFORE_NAME>\*\/ {
234 <GTK_DOC_BEFORE_NAME>. {
236 before_comment = CLASS_CODE_I;
238 <GTK_DOC>^[ \t]*\*[ \t]*$ {
239 /* empty doc lines */
242 <GTK_DOC>^[ \t]*\*?\*\/ {
246 <GTK_DOC>^[ \t]*\*[ \t] {
256 before_comment = CLASS_CODE_I;
273 <C_CODE>\/\/.*$ { add_to_cbuf(yytext); /*comment, ignore*/ }
274 <CLASS_CODE>\/\/.*$ { ; /*comment, ignore*/ }
275 <CLASS_CODE_I>\/\/.*$ { ; /*comment, ignore*/ }
276 <PROPERTY_CODE_I>\/\/.*$ { ; /*comment, ignore*/ }
277 \/\* {BEGIN(COMMENT); before_comment = INITIAL; }
281 before_comment = C_CODE;
283 <CLASS_CODE>\/\* {BEGIN(COMMENT); before_comment = CLASS_CODE; }
284 <CLASS_CODE_I>\/\* {BEGIN(COMMENT); before_comment = CLASS_CODE_I; }
285 <PROPERTY_CODE_I>\/\* {BEGIN(COMMENT); before_comment = PROPERTY_CODE_I; }
287 if(before_comment == C_CODE) add_to_cbuf(yytext);
288 BEGIN(before_comment);
291 /* comment, ignore */
292 if(before_comment == C_CODE) add_to_cbuf(yytext);
295 /* comment, ignore */
296 if(before_comment == C_CODE) add_to_cbuf(yytext);
302 class_after_c = FALSE;
305 ccode_line = line_no;
310 class_after_c = FALSE;
313 ccode_line = line_no;
316 ^\%(ht|headertop)\{ {
319 class_after_c = FALSE;
322 ccode_line = line_no;
324 ^\%(ph|privateheader)\{ {
327 class_after_c = FALSE;
330 ccode_line = line_no;
335 class_after_c = FALSE;
338 ccode_line = line_no;
343 class_after_c = FALSE;
346 ccode_line = line_no;
347 if(look_for_includes==0)
353 class_after_c = FALSE;
356 ccode_line = line_no;
357 if(look_for_includes==0)
360 ^\%(ad|afterdecls)\{ {
363 class_after_c = FALSE;
366 ccode_line = line_no;
372 if(look_for_includes==1)
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); }
382 <C_CODE>\'\\\"\' { add_to_cbuf(yytext); }
384 <C_CODE>\\. { add_to_cbuf(yytext); }
389 before_string = C_CODE;
392 <PROPERTY_CODE_I>\" {
394 before_string = PROPERTY_CODE_I;
397 <CODE_STRING>\\. { add_to_cbuf(yytext); }
399 BEGIN(before_string);
401 if (before_string == PROPERTY_CODE_I) {
402 yylval.id = cbuf->str;
403 g_string_free (cbuf, FALSE);
408 <CODE_STRING>. { add_to_cbuf(yytext); }
409 <CODE_STRING>\n { add_to_cbuf(yytext); }
417 if(parenth_depth<0) {
419 } else if(parenth_depth==0 && class_after_c) {
428 <C_CODE>. { add_to_cbuf(yytext); }
429 <C_CODE>\n { add_to_cbuf(yytext); }
432 static int found_classes = 0;
433 look_for_includes = 2;
436 if(++found_classes > 1) {
437 error_print(GOB_ERROR, line_no,
438 "Only one class per file allowed");
444 error { return ERROR; }
445 enum { return ENUM; }
446 flags { return FLAGS; }
448 ^[ \t]*requires[ \t]+[0-9]+\.[0-9]+\.[0-9]+[\t ]*$ {
449 int maj = 0, min = 0, pl = 0;
450 int rmaj = 0, rmin = 0, rpl = 0;
451 int effective_maj = 0;
452 int effective_rmaj = 0;
455 sscanf (VERSION, "%d.%d.%d", &rmaj, &rmin, &rpl);
456 effective_rmaj = rmaj;
458 effective_rmaj = rmaj + 1;
460 p = strchr (yytext,'r');
461 g_assert (p); /* we MUST have found it */
462 sscanf (p, "requires %d.%d.%d", &maj, &min, &pl);
465 effective_maj = maj + 1;
468 (rmaj == maj && rmin < min) ||
469 (rmaj == maj && rmin == min && rpl < pl)) {
470 error_printf (GOB_ERROR, line_no,
471 "GOB version at least %d.%d.%d required "
473 "To upgrade your gob, see: "
474 "http://www.5z.com/jirka/gob.html",
475 maj, min, pl, VERSION);
478 if(effective_rmaj != effective_maj) {
479 error_printf(GOB_ERROR, line_no,
480 "GOB major version %d required "
482 "To upgrade your gob, see: "
483 "http://www.5z.com/jirka/gob.html",
484 effective_maj, VERSION);
489 <CLASS_CODE,CLASS_CODE_I>class|this {
491 error_printf(GOB_WARN, line_no,
492 "'%s' keyword should not "
493 "be used when generating "
499 <CLASS_CODE>from {return FROM;}
502 before_string = CLASS_CODE;
505 <CLASS_STRING>\\. { add_to_cbuf(yytext); }
507 BEGIN(before_string);
509 yylval.id = cbuf->str;
510 g_string_free (cbuf, FALSE);
514 <CLASS_STRING>. { add_to_cbuf(yytext); }
515 <CLASS_STRING>\n { add_to_cbuf(yytext); }
519 before_string = CLASS_CODE_I;
523 <CLASS_CODE_I,PROPERTY_CODE_I>void {return VOID;}
524 <CLASS_CODE_I,PROPERTY_CODE_I>struct {return STRUCT;}
525 <CLASS_CODE_I,PROPERTY_CODE_I>union {return UNION;}
526 <CLASS_CODE_I,PROPERTY_CODE_I>enum {return ENUM;}
527 <CLASS_CODE_I,PROPERTY_CODE_I>signed {return SIGNED;}
528 <CLASS_CODE_I,PROPERTY_CODE_I>unsigned {return UNSIGNED;}
529 <CLASS_CODE_I,PROPERTY_CODE_I>long {return LONG;}
530 <CLASS_CODE_I,PROPERTY_CODE_I>short {return SHORT;}
531 <CLASS_CODE_I,PROPERTY_CODE_I>int {return INT;}
532 <CLASS_CODE_I,PROPERTY_CODE_I>float {return FLOAT;}
533 <CLASS_CODE_I,PROPERTY_CODE_I>double {return DOUBLE;}
534 <CLASS_CODE_I,PROPERTY_CODE_I>char {return CHAR;}
535 <CLASS_CODE_I,PROPERTY_CODE_I>const {return CONST;}
537 <CLASS_CODE_I>\.\.\. {return THREEDOTS;}
539 <CLASS_CODE_I>public {yylval.line = line_no; return PUBLIC;}
540 <CLASS_CODE_I>private {yylval.line = line_no; return PRIVATE;}
541 <CLASS_CODE_I>protected {yylval.line = line_no; return PROTECTED;}
542 <CLASS_CODE_I>classwide {yylval.line = line_no; return CLASSWIDE;}
543 <CLASS_CODE_I>argument {yylval.line = line_no; return ARGUMENT;}
544 <CLASS_CODE_I>virtual {yylval.line = line_no; return VIRTUAL;}
545 <CLASS_CODE_I>signal {yylval.line = line_no; return SIGNAL;}
546 <CLASS_CODE_I>override {yylval.line = line_no; return OVERRIDE;}
547 <CLASS_CODE_I>property {
548 yylval.line = line_no;
549 BEGIN(PROPERTY_CODE);
552 <PROPERTY_CODE_I>nick { yylval.line = line_no; return NICK; }
553 <PROPERTY_CODE_I>name { yylval.line = line_no; return NAME; }
554 <PROPERTY_CODE_I>blurb { yylval.line = line_no; return BLURB; }
555 <PROPERTY_CODE_I>maximum { yylval.line = line_no; return MAXIMUM; }
556 <PROPERTY_CODE_I>minimum { yylval.line = line_no; return MINIMUM; }
557 <PROPERTY_CODE_I>default_value { yylval.line = line_no; return DEFAULT_VALUE; }
558 <PROPERTY_CODE_I>flags { yylval.line = line_no; return FLAGS; }
559 <PROPERTY_CODE_I>type { yylval.line = line_no; return TYPE; }
560 <PROPERTY_CODE_I>flags_type { yylval.line = line_no; return FLAGS_TYPE; }
561 <PROPERTY_CODE_I>enum_type { yylval.line = line_no; return ENUM_TYPE; }
562 <PROPERTY_CODE_I>param_type { yylval.line = line_no; return PARAM_TYPE; }
563 <PROPERTY_CODE_I>boxed_type { yylval.line = line_no; return BOXED_TYPE; }
564 <PROPERTY_CODE_I>object_type { yylval.line = line_no; return OBJECT_TYPE; }
566 yylval.line = line_no;
567 property_paren_depth = 1;
568 BEGIN(PROPERTY_CODE_I);
571 <PROPERTY_CODE_I>[(] {
572 yylval.line = line_no;
573 property_paren_depth++;
576 <PROPERTY_CODE_I>[)] {
577 yylval.line = line_no;
578 property_paren_depth--;
579 if (property_paren_depth == 0) {
585 <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]* {
586 yylval.id = g_strdup(yytext);
589 <CLASS_CODE,CLASS_CODE_I,PROPERTY_CODE,PROPERTY_CODE_I,INITIAL>[A-Za-z_][A-Za-z0-9_]*(::[A-Za-z0-9_]*)+ {
590 /* This is cpp kind of token thingie */
592 yylval.id = g_strdup(yytext);
598 <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_]*)+ {
599 /* this one is for a classname with a namespace */
600 yylval.id = g_strdup(yytext);
603 <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_]*)* {
604 /* this is for a classname with an empty namespace */
605 yylval.id = g_strdup(yytext);
608 <CLASS_CODE,CLASS_CODE_I,PROPERTY_CODE,PROPERTY_CODE_I,INITIAL>[A-Za-z_][A-Za-z0-9_]* {
609 yylval.id = g_strdup(yytext);
612 <CLASS_CODE,CLASS_CODE_I,PROPERTY_CODE,PROPERTY_CODE_I,INITIAL>\'\\.\'|\'.\' {
613 yylval.id = g_strdup(yytext);
617 <CLASS_CODE_I>(\[[0-9]*\]|\[[A-Za-z_][A-Za-z0-9_]*\])+ {
618 yylval.id = g_strdup(yytext);
621 <CLASS_CODE_I>:[0-9]+ {
622 /* cheat for bitfield */
623 yylval.id = g_strdup(yytext);
630 <CLASS_CODE_I,PROPERTY_CODE>\{ {
633 class_after_c = TRUE;
634 yylval.line = line_no;
636 ccode_line = line_no;
644 <CLASS_CODE,CLASS_CODE_I,INITIAL,PROPERTY_CODE,PROPERTY_CODE_I>[\f\t ] ; /*ignore*/
646 <*>[\n\r] ; /*ignore*/
650 yylval.line = line_no;
654 ^[ \t]*prealloc[ \t]+[0-9]+[ \t]*$ {
657 p = strchr (yytext,'p');
658 g_assert (p); /* we MUST have found it */
659 sscanf (p, "prealloc %d", &t);