2 * Copyright (C) 1999 the Free Software Foundation.
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.
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.
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,
36 static GList *class_nodes = NULL;
39 static GList *typestack = NULL;
41 static GList *funcargs = NULL;
42 static GList *checks = NULL;
43 static int has_self = FALSE;
44 static int vararg = FALSE;
46 /* destructor and initializer for variables */
47 static char *destructor = NULL;
48 static int destructor_line = 0;
49 static gboolean destructor_simple = TRUE;
50 static char *initializer = NULL;
51 static int initializer_line = 0;
53 static GList *gtktypes = NULL;
55 /* this can be a global as we will only do one function at a time
57 static int the_scope = NO_SCOPE;
62 extern int ccode_line;
73 if(strcmp(yytext,"\n")==0) {
74 out=g_strconcat("Error: ",str," before newline",NULL);
75 } else if(yytext[0]=='\0') {
76 out=g_strconcat("Error: ", str, " at end of input", NULL);
78 char *tmp = g_strdup(yytext);
79 while((p=strchr(tmp, '\n')))
82 out=g_strconcat("Error: ", str, " before '", tmp, "'", NULL);
86 fprintf(stderr, "%s:%d: %s\n", filename, line_no, out);
95 Type *type = typestack->data;
96 typestack = g_list_remove(typestack,typestack->data);
101 push_variable(char *name, int scope, int line_no, char *postfix)
104 Type *type = pop_type();
106 type->postfix = postfix;
108 var = new_variable(scope, type, name, line_no,
109 destructor, destructor_line,
111 initializer, initializer_line);
112 class_nodes = g_list_append(class_nodes, var);
116 push_function(int scope, int method, char *oid, char *id, char *onerror,
117 GString *cbuf, int line_no, int ccode_line, gboolean vararg,
124 if(method!=INIT_METHOD && method!=CLASS_INIT_METHOD) {
127 type = (Type *)new_type(0,g_strdup("void"),NULL);
130 /* a complicated and ugly test to figure out if we have
131 the wrong number of types for a signal */
132 if((method == SIGNAL_FIRST_METHOD ||
133 method == SIGNAL_LAST_METHOD) &&
134 g_list_length(gtktypes) != g_list_length(funcargs) &&
135 !(g_list_length(funcargs) == 1 &&
136 g_list_length(gtktypes) == 2 &&
137 strcmp(gtktypes->next->data,"NONE")==0)) {
138 print_error(TRUE, _("The number of GTK arguments and "
139 "function arguments for a signal "
140 "don't seem to match"), line_no);
144 c_cbuf = p = cbuf->str;
145 while(p && *p && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
152 node = new_method(scope, method, type, oid, gtktypes, flags,
153 id, funcargs, onerror, c_cbuf, line_no,
158 /*only free segment if we haven't passed it
164 class_nodes = g_list_append(class_nodes, node);
168 push_funcarg(char *name, char *postfix)
171 Type *type = pop_type();
173 type->postfix = postfix;
175 node = new_funcarg(type,name,checks);
178 funcargs = g_list_append(funcargs, node);
182 push_init_arg(char *name, int is_class)
189 tn = g_strconcat(((Class *)class)->otype,":Class",NULL);
191 tn = g_strdup(((Class *)class)->otype);
193 type = new_type(1,tn,NULL);
194 node = new_funcarg((Type *)type,name,NULL);
195 funcargs = g_list_prepend(funcargs, node);
204 type = new_type(1,g_strdup(((Class *)class)->otype),NULL);
205 ch = g_list_append(ch,new_check(NULL_CHECK,NULL));
206 ch = g_list_append(ch,new_check(TYPE_CHECK,NULL));
207 node = new_funcarg((Type *)type,id,ch);
208 funcargs = g_list_prepend(funcargs, node);
212 find_var_or_die(const char *id, int line)
217 for(li = class_nodes; li != NULL; li = li->next) {
219 Node *node = li->data;
220 if(node->type != VARIABLE_NODE)
223 if(strcmp(var->id, id)==0)
227 s = g_strdup_printf(_("Variable %s not defined here"), id);
228 print_error(FALSE, s, line);
230 g_assert_not_reached();
245 %token CONST VOID STRUCT UNION ENUM THREEDOTS
246 %token SIGNED UNSIGNED LONG SHORT INT FLOAT DOUBLE CHAR
249 %token <id> TOKEN NUMBER TYPETOKEN ARRAY_DIM
250 %token <cbuf> CCODE HTCODE PHCODE HCODE ACODE ATCODE
251 %token <line> PUBLIC PRIVATE PROTECTED ARGUMENT VIRTUAL SIGNAL OVERRIDE
255 prog: ccodes class ccodes { ; }
262 Node *node = new_ccode(C_CCODE,($<cbuf>1)->str,
264 nodes = g_list_append(nodes,node);
265 g_string_free($<cbuf>1,FALSE);
268 Node *node = new_ccode(H_CCODE,($<cbuf>1)->str,
270 nodes = g_list_append(nodes,node);
271 g_string_free($<cbuf>1,FALSE);
274 Node *node = new_ccode(HT_CCODE,($<cbuf>1)->str,
276 nodes = g_list_append(nodes,node);
277 g_string_free($<cbuf>1,FALSE);
280 Node *node = new_ccode(PH_CCODE,($<cbuf>1)->str,
282 nodes = g_list_append(nodes,node);
283 g_string_free($<cbuf>1,FALSE);
286 Node *node = new_ccode(A_CCODE,($<cbuf>1)->str,
288 nodes = g_list_append(nodes,node);
289 g_string_free($<cbuf>1,FALSE);
292 Node *node = new_ccode(AT_CCODE,($<cbuf>1)->str,
294 nodes = g_list_append(nodes,node);
295 g_string_free($<cbuf>1,FALSE);
299 ccodes: ccodes ccode { ; }
303 class: classdec '{' classcode '}' {
304 ((Class *)class)->nodes = class_nodes;
306 nodes = g_list_append(nodes,class);
309 ((Class *)class)->nodes = NULL;
311 nodes = g_list_append(nodes,class);
315 classdec: CLASS TYPETOKEN FROM TYPETOKEN {
316 class = new_class($<id>2,$<id>4,NULL);
320 classcode: classcode thing { ; }
330 scope: PUBLIC { the_scope = PUBLIC_SCOPE; }
331 | PRIVATE { the_scope = PRIVATE_SCOPE; }
332 | PROTECTED { the_scope = PROTECTED_SCOPE; }
335 destructor: TOKEN TOKEN {
336 if(strcmp($<id>1, "destroywith")==0) {
339 destructor_line = line_no;
340 destructor_simple = TRUE;
344 yyerror(_("parse error"));
349 if(strcmp($<id>1, "destroy")==0) {
351 destructor = ($<cbuf>3)->str;
352 g_string_free($<cbuf>3, FALSE);
353 destructor_line = ccode_line;
354 destructor_simple = FALSE;
357 g_string_free($<cbuf>3, TRUE);
358 yyerror(_("parse error"));
364 initializer: '=' numtok {
365 initializer = $<id>2;
366 initializer_line = ccode_line;
369 initializer = ($<cbuf>3)->str;
370 initializer_line = ccode_line;
371 g_string_free($<cbuf>3, FALSE);
376 varoptions: destructor initializer { ; }
377 | initializer destructor { ; }
378 | initializer { destructor = NULL; }
379 | destructor { initializer = NULL; }
386 variable: scope type TOKEN varoptions ';' {
387 push_variable($<id>3, the_scope,$<line>1, NULL);
389 | scope type TOKEN ARRAY_DIM varoptions ';' {
390 push_variable($<id>3, the_scope, $<line>1, $<id>4);
393 argument: ARGUMENT flags argtype TOKEN TOKEN '{' CCODE TOKEN '{' CCODE ';' {
394 if(strcmp($<id>5,"get")==0 &&
395 strcmp($<id>8,"set")==0) {
397 Type *type = pop_type();
398 g_free($<id>5); g_free($<id>8);
399 node = new_argument($<id>3,type,$<list>2,$<id>4,
400 ($<cbuf>7)->str,$<line>6,
401 ($<cbuf>10)->str,$<line>9,
403 g_string_free($<cbuf>7,FALSE);
404 g_string_free($<cbuf>10,FALSE);
405 class_nodes = g_list_append(class_nodes,node);
406 } else if(strcmp($<id>5,"set")==0 &&
407 strcmp($<id>8,"get")==0) {
409 Type *type = pop_type();
410 g_free($<id>5); g_free($<id>8);
411 node = new_argument($<id>3,type,$<list>2,$<id>4,
412 ($<cbuf>10)->str,$<line>9,
413 ($<cbuf>7)->str,$<line>6,
415 g_string_free($<cbuf>10,FALSE);
416 g_string_free($<cbuf>7,FALSE);
417 class_nodes = g_list_append(class_nodes,node);
419 g_free($<id>3); g_free($<id>4);
420 g_free($<id>5); g_free($<id>8);
421 g_list_foreach($<list>2,(GFunc)g_free,NULL);
422 g_string_free($<cbuf>10,TRUE);
423 g_string_free($<cbuf>7,TRUE);
424 yyerror(_("parse error"));
428 | ARGUMENT flags argtype TOKEN TOKEN '{' CCODE ';' {
429 if(strcmp($<id>5,"get")==0) {
431 Type *type = pop_type();
433 node = new_argument($<id>3,type,$<list>2,$<id>4,
434 ($<cbuf>7)->str,$<line>6,
436 g_string_free($<cbuf>7,FALSE);
437 class_nodes = g_list_append(class_nodes,node);
438 } else if(strcmp($<id>5,"set")==0) {
440 Type *type = pop_type();
442 node = new_argument($<id>3,type,$<list>2,$<id>4,
443 NULL,0,($<cbuf>7)->str,
445 g_string_free($<cbuf>7,FALSE);
446 class_nodes = g_list_append(class_nodes,node);
448 g_free($<id>5); g_free($<id>3);
450 g_list_foreach($<list>2,(GFunc)g_free,NULL);
451 g_string_free($<cbuf>7,TRUE);
452 yyerror(_("parse error"));
456 | ARGUMENT flags argtype TOKEN TOKEN {
458 char *get, *set = NULL;
463 if(strcmp($<id>5,"link")!=0 &&
464 strcmp($<id>5,"stringlink")!=0 &&
465 strcmp($<id>5,"objectlink")!=0) {
466 g_free($<id>5); g_free($<id>3);
468 g_list_foreach($<list>2,(GFunc)g_free,NULL);
469 yyerror(_("parse error"));
475 var = find_var_or_die($<id>4, $<line>1);
476 if(var->scope == PRIVATE_SCOPE)
477 root = "self->_priv";
481 if(strcmp($<id>5,"link")==0) {
482 set = g_strdup_printf("%s->%s = ARG;",
484 } else if(strcmp($<id>5,"stringlink")==0) {
485 set = g_strdup_printf("g_free(%s->%s); "
486 "%s->%s = g_strdup(ARG);",
489 } else if(strcmp($<id>5,"objectlink")==0) {
490 set = g_strdup_printf(
492 "gtk_object_unref(GTK_OBJECT(%s->%s)); "
495 "gtk_object_ref(GTK_OBJECT(%s->%s)); ",
502 g_assert_not_reached();
505 /* get is the same for everything */
506 get = g_strdup_printf("ARG = %s->%s;", root, $<id>4);
512 type = copy_type(var->vtype);
514 node = new_argument($<id>3, type, $<list>2,
515 $<id>4, get, $<line>1,
516 set, $<line>1, $<line>1);
517 class_nodes = g_list_append(class_nodes,node);
521 argtype: TOKEN '(' TOKEN type ')' {
522 if(strcmp($<id>3,"type")!=0) {
525 yyerror(_("parse error"));
532 typestack = g_list_prepend(typestack,NULL);
536 flags: '(' flaglist ')' { $<list>$ = $<list>2; }
537 | { $<list>$ = NULL; }
540 flaglist: TOKEN '|' flaglist {
541 $<list>$ = g_list_append($<list>3,$<id>1);
544 $<list>$ = g_list_append(NULL,$<id>1);
551 Type *type = typestack->data;
552 char *oldname = type->name;
553 type->name = g_strconcat("const ",oldname,NULL);
559 Node *node = new_type(0,$<id>1,NULL);
560 typestack = g_list_prepend(typestack,node);
563 Node *node = new_type(stars,$<id>1,NULL);
565 typestack = g_list_prepend(typestack,node);
569 type2: UNSIGNED integer {
570 $<id>$ = g_strconcat("unsigned ",$<id>2,NULL);
573 $<id>$ = g_strconcat("signed ",$<id>2,NULL);
576 $<id>$ = g_strdup($<id>1);
579 $<id>$ = g_strdup("unsigned char");
582 $<id>$ = g_strdup("signed char");
585 $<id>$ = g_strdup("char");
588 $<id>$ = g_strdup("double");
591 $<id>$ = g_strdup("float");
597 $<id>$ = g_strconcat($<id>1,$<id>2,NULL);
604 $<id>$ = g_strdup("void");
615 $<id>$ = "short int";
636 stars: '*' stars { stars++; }
640 /* this never sets the_scope */
641 simplesigtype: TOKEN sigtype {
642 if(strcmp($<id>1,"first")==0)
643 $<sigtype>$ = SIGNAL_FIRST_METHOD;
644 else if(strcmp($<id>1,"last")==0)
645 $<sigtype>$ = SIGNAL_LAST_METHOD;
647 yyerror(_("signal must be 'first' or 'last'"));
654 $<sigtype>$ = SIGNAL_LAST_METHOD;
658 /* this always sets the_scope */
659 fullsigtype: scope TOKEN sigtype {
660 if(strcmp($<id>2,"first")==0)
661 $<sigtype>$ = SIGNAL_FIRST_METHOD;
662 else if(strcmp($<id>2,"last")==0)
663 $<sigtype>$ = SIGNAL_LAST_METHOD;
665 yyerror(_("signal must be 'first' or 'last'"));
671 | TOKEN scope sigtype {
672 if(strcmp($<id>1,"first")==0)
673 $<sigtype>$ = SIGNAL_FIRST_METHOD;
674 else if(strcmp($<id>1,"last")==0)
675 $<sigtype>$ = SIGNAL_LAST_METHOD;
677 yyerror(_("signal must be 'first' or 'last'"));
684 $<sigtype>$ = SIGNAL_LAST_METHOD;
687 /* the_scope was default thus public */
688 the_scope = PUBLIC_SCOPE;
692 sigtype: TOKEN '(' tokenlist ')' {
693 gtktypes = g_list_prepend(gtktypes,$<id>1);
697 tokenlist: tokenlist ',' TOKEN {
698 gtktypes = g_list_append(gtktypes,$<id>3);
701 gtktypes = g_list_append(gtktypes,$<id>1);
705 codenocode: '{' CCODE { $<cbuf>$ = $<cbuf>2; }
706 | ';' { $<cbuf>$ = NULL; }
709 /*here CCODE will include the ending '}' */
710 method: SIGNAL flags fullsigtype type TOKEN '(' funcargs ')' onerror codenocode {
712 yyerror(_("signal without 'self' as "
716 push_function(the_scope, $<sigtype>3,NULL,
717 $<id>5, $<id>9, $<cbuf>10,$<line>1,
718 ccode_line, vararg, $<list>2);
720 | scope SIGNAL flags simplesigtype type TOKEN '(' funcargs ')' onerror codenocode {
722 yyerror(_("signal without 'self' as "
726 push_function(the_scope, $<sigtype>4, NULL,
727 $<id>6, $<id>10, $<cbuf>11, $<line>2,
728 ccode_line, vararg, $<list>3);
730 | VIRTUAL scope type TOKEN '(' funcargs ')' onerror codenocode {
732 yyerror(_("virtual method without 'self' as "
736 push_function(the_scope, VIRTUAL_METHOD, NULL, $<id>4,
737 $<id>8, $<cbuf>9, $<line>1,
738 ccode_line, vararg, NULL);
740 | scope VIRTUAL type TOKEN '(' funcargs ')' onerror codenocode {
742 yyerror(_("virtual method without 'self' as "
746 push_function(the_scope, VIRTUAL_METHOD, NULL, $<id>4,
747 $<id>8, $<cbuf>9, $<line>2,
748 ccode_line, vararg, NULL);
750 | VIRTUAL type TOKEN '(' funcargs ')' onerror codenocode {
752 yyerror(_("virtual method without 'self' as "
756 push_function(PUBLIC_SCOPE, VIRTUAL_METHOD, NULL,
757 $<id>3, $<id>7, $<cbuf>8, $<line>1,
758 ccode_line, vararg, NULL);
760 | OVERRIDE '(' TYPETOKEN ')' type TOKEN '(' funcargs ')' onerror codenocode {
761 push_function(NO_SCOPE, OVERRIDE_METHOD, $<id>3,
762 $<id>6, $<id>10, $<cbuf>11,
763 $<line>1, ccode_line,
766 | scope type TOKEN '(' funcargs ')' onerror codenocode {
767 push_function(the_scope, REGULAR_METHOD, NULL, $<id>3,
768 $<id>7, $<cbuf>8, $<line>1, ccode_line,
771 | TOKEN '(' TOKEN ')' codenocode {
772 if(strcmp($<id>1,"init")==0) {
773 push_init_arg($<id>3,FALSE);
774 push_function(NO_SCOPE, INIT_METHOD, NULL,
775 $<id>1, NULL, $<cbuf>5, $<line>2,
776 ccode_line, FALSE, NULL);
777 } else if(strcmp($<id>1,"class_init")==0) {
778 push_init_arg($<id>3,TRUE);
779 push_function(NO_SCOPE, CLASS_INIT_METHOD, NULL,
780 $<id>1, NULL, $<cbuf>5, $<line>2,
781 ccode_line, FALSE, NULL);
785 g_string_free($<cbuf>3,TRUE);
786 yyerror(_("parse error"));
792 onerror: ONERROR numtok { $<id>$ = $<id>2; }
793 | ONERROR '{' CCODE {
794 $<id>$ = ($<cbuf>3)->str;
795 g_string_free($<cbuf>3,FALSE);
800 funcargs: VOID { vararg = FALSE; has_self = FALSE; }
804 if(strcmp($<id>1,"self")==0)
808 yyerror(_("parse error"));
812 | TOKEN ',' arglist {
814 if(strcmp($<id>1,"self")==0)
818 yyerror(_("parse error"));
822 | arglist { has_self = FALSE; }
825 arglist: arglist1 ',' THREEDOTS { vararg = TRUE; }
826 | arglist1 { vararg = FALSE; }
829 arglist1: arglist1 ',' arg { ; }
834 push_funcarg($<id>2,NULL);
836 | type TOKEN ARRAY_DIM {
837 push_funcarg($<id>2,$<id>3);
839 | type TOKEN '(' TOKEN checklist ')' {
840 if(strcmp($<id>4,"check")!=0) {
841 yyerror(_("parse error"));
845 push_funcarg($<id>2,NULL);
847 | type TOKEN ARRAY_DIM '(' TOKEN checklist ')' {
848 if(strcmp($<id>5,"check")!=0) {
849 yyerror(_("parse error"));
853 push_funcarg($<id>2,$<id>3);
857 checklist: checklist check { ; }
862 if(strcmp($<id>1,"type")==0) {
863 Node *node = new_check(TYPE_CHECK,NULL);
864 checks = g_list_append(checks,node);
865 } else if(strcmp($<id>1,"null")==0) {
866 Node *node = new_check(NULL_CHECK,NULL);
867 checks = g_list_append(checks,node);
869 yyerror(_("parse error"));
875 Node *node = new_check(GT_CHECK,$<id>2);
876 checks = g_list_append(checks,node);
879 Node *node = new_check(LT_CHECK,$<id>2);
880 checks = g_list_append(checks,node);
883 Node *node = new_check(GE_CHECK,$<id>3);
884 checks = g_list_append(checks,node);
887 Node *node = new_check(LE_CHECK,$<id>3);
888 checks = g_list_append(checks,node);
891 Node *node = new_check(EQ_CHECK,$<id>3);
892 checks = g_list_append(checks,node);
895 Node *node = new_check(NE_CHECK,$<id>3);
896 checks = g_list_append(checks,node);
900 numtok: NUMBER { $<id>$ = $<id>1; }
902 $<id>$ = g_strconcat("-",$<id>2,NULL);
905 | TOKEN { $<id>$ = $<id>1; }