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 static GList *gtktypes = NULL;
48 /* this can be a global as we will only do one function at a time
50 static int the_scope = NO_SCOPE;
55 extern int ccode_line;
66 if(strcmp(yytext,"\n")==0) {
67 out=g_strconcat("Error: ",str," before newline",NULL);
68 } else if(yytext[0]=='\0') {
69 out=g_strconcat("Error: ",str," at end of input",NULL);
71 char *tmp = g_strdup(yytext);
72 while((p=strchr(tmp,'\n')))
75 out=g_strconcat("Error: ",str," before '",tmp,"'",NULL);
79 fprintf(stderr,"%s:%d: %s\n",filename,line_no,out);
88 Type *type = typestack->data;
89 typestack = g_list_remove(typestack,typestack->data);
94 push_variable(char *name, int scope, int line_no, char *postfix)
97 Type *type = pop_type();
99 type->postfix = postfix;
101 var = new_variable(scope,type,name,line_no);
102 class_nodes = g_list_append(class_nodes, var);
106 push_function(int scope, int method, char *oid, char *id, char *onerror,
107 GString *cbuf, int line_no, int ccode_line, gboolean vararg,
114 if(method!=INIT_METHOD && method!=CLASS_INIT_METHOD) {
117 type = (Type *)new_type(0,g_strdup("void"),NULL);
120 /* a complicated and ugly test to figure out if we have
121 the wrong number of types for a signal */
122 if((method == SIGNAL_FIRST_METHOD ||
123 method == SIGNAL_LAST_METHOD) &&
124 g_list_length(gtktypes) != g_list_length(funcargs) &&
125 !(g_list_length(funcargs) == 1 &&
126 g_list_length(gtktypes) == 2 &&
127 strcmp(gtktypes->next->data,"NONE")==0)) {
128 print_error(TRUE, _("The number of GTK arguments and "
129 "function arguments for a signal "
130 "don't seem to match"),line_no);
134 c_cbuf = p = cbuf->str;
135 while(p && *p && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
142 node = new_method(scope, method, type, oid, gtktypes, flags,
143 id, funcargs, onerror, c_cbuf, line_no,
148 /*only free segment if we haven't passed it
154 class_nodes = g_list_append(class_nodes, node);
158 push_funcarg(char *name, char *postfix)
161 Type *type = pop_type();
163 type->postfix = postfix;
165 node = new_funcarg(type,name,checks);
168 funcargs = g_list_append(funcargs, node);
172 push_init_arg(char *name, int is_class)
179 tn = g_strconcat(((Class *)class)->otype,":Class",NULL);
181 tn = g_strdup(((Class *)class)->otype);
183 type = new_type(1,tn,NULL);
184 node = new_funcarg((Type *)type,name,NULL);
185 funcargs = g_list_prepend(funcargs, node);
194 type = new_type(1,g_strdup(((Class *)class)->otype),NULL);
195 ch = g_list_append(ch,new_check(NULL_CHECK,NULL));
196 ch = g_list_append(ch,new_check(TYPE_CHECK,NULL));
197 node = new_funcarg((Type *)type,id,ch);
198 funcargs = g_list_prepend(funcargs, node);
212 %token CONST VOID STRUCT UNION ENUM THREEDOTS
213 %token SIGNED UNSIGNED LONG SHORT INT FLOAT DOUBLE CHAR
216 %token <id> TOKEN NUMBER TYPETOKEN ARRAY_DIM
217 %token <cbuf> CCODE HTCODE PHCODE HCODE ACODE ATCODE
218 %token <line> PUBLIC PRIVATE PROTECTED ARGUMENT VIRTUAL SIGNAL OVERRIDE
222 prog: ccodes class ccodes { ; }
229 Node *node = new_ccode(C_CCODE,($<cbuf>1)->str,
231 nodes = g_list_append(nodes,node);
232 g_string_free($<cbuf>1,FALSE);
235 Node *node = new_ccode(H_CCODE,($<cbuf>1)->str,
237 nodes = g_list_append(nodes,node);
238 g_string_free($<cbuf>1,FALSE);
241 Node *node = new_ccode(HT_CCODE,($<cbuf>1)->str,
243 nodes = g_list_append(nodes,node);
244 g_string_free($<cbuf>1,FALSE);
247 Node *node = new_ccode(PH_CCODE,($<cbuf>1)->str,
249 nodes = g_list_append(nodes,node);
250 g_string_free($<cbuf>1,FALSE);
253 Node *node = new_ccode(A_CCODE,($<cbuf>1)->str,
255 nodes = g_list_append(nodes,node);
256 g_string_free($<cbuf>1,FALSE);
259 Node *node = new_ccode(AT_CCODE,($<cbuf>1)->str,
261 nodes = g_list_append(nodes,node);
262 g_string_free($<cbuf>1,FALSE);
266 ccodes: ccodes ccode { ; }
270 class: classdec '{' classcode '}' {
271 ((Class *)class)->nodes = class_nodes;
273 nodes = g_list_append(nodes,class);
276 ((Class *)class)->nodes = NULL;
278 nodes = g_list_append(nodes,class);
282 classdec: CLASS TYPETOKEN FROM TYPETOKEN {
283 class = new_class($<id>2,$<id>4,NULL);
287 classcode: classcode method { ; }
288 | classcode variable { ; }
289 | classcode argument { ; }
295 scope: PUBLIC { the_scope = PUBLIC_SCOPE; }
296 | PRIVATE { the_scope = PRIVATE_SCOPE; }
297 | PROTECTED { the_scope = PROTECTED_SCOPE; }
300 variable: scope type TOKEN ';' {
301 push_variable($<id>3,the_scope,$<line>1,NULL);
303 | scope type TOKEN ARRAY_DIM ';' {
304 push_variable($<id>3,the_scope,$<line>1,$<id>4);
307 argument: ARGUMENT flags argtype TOKEN TOKEN '{' CCODE TOKEN '{' CCODE ';' {
308 if(strcmp($<id>5,"get")==0 &&
309 strcmp($<id>8,"set")==0) {
311 Type *type = pop_type();
312 g_free($<id>5); g_free($<id>8);
313 node = new_argument($<id>3,type,$<list>2,$<id>4,
314 ($<cbuf>7)->str,$<line>6,
315 ($<cbuf>10)->str,$<line>9,
317 g_string_free($<cbuf>7,FALSE);
318 g_string_free($<cbuf>10,FALSE);
319 class_nodes = g_list_append(class_nodes,node);
320 } else if(strcmp($<id>5,"set")==0 &&
321 strcmp($<id>8,"get")==0) {
323 Type *type = pop_type();
324 g_free($<id>5); g_free($<id>8);
325 node = new_argument($<id>3,type,$<list>2,$<id>4,
326 ($<cbuf>10)->str,$<line>9,
327 ($<cbuf>7)->str,$<line>6,
329 g_string_free($<cbuf>10,FALSE);
330 g_string_free($<cbuf>7,FALSE);
331 class_nodes = g_list_append(class_nodes,node);
333 g_free($<id>3); g_free($<id>4);
334 g_free($<id>5); g_free($<id>8);
335 g_list_foreach($<list>2,(GFunc)g_free,NULL);
336 g_string_free($<cbuf>10,TRUE);
337 g_string_free($<cbuf>7,TRUE);
338 yyerror(_("parse error"));
342 | ARGUMENT flags argtype TOKEN TOKEN '{' CCODE ';' {
343 if(strcmp($<id>5,"get")==0) {
345 Type *type = pop_type();
347 node = new_argument($<id>3,type,$<list>2,$<id>4,
348 ($<cbuf>7)->str,$<line>6,
350 g_string_free($<cbuf>7,FALSE);
351 class_nodes = g_list_append(class_nodes,node);
352 } else if(strcmp($<id>5,"set")==0) {
354 Type *type = pop_type();
356 node = new_argument($<id>3,type,$<list>2,$<id>4,
357 NULL,0,($<cbuf>7)->str,
359 g_string_free($<cbuf>7,FALSE);
360 class_nodes = g_list_append(class_nodes,node);
362 g_free($<id>5); g_free($<id>3);
364 g_list_foreach($<list>2,(GFunc)g_free,NULL);
365 g_string_free($<cbuf>7,TRUE);
366 yyerror(_("parse error"));
372 argtype: TOKEN '(' TOKEN type ')' {
373 if(strcmp($<id>3,"type")!=0) {
376 yyerror(_("parse error"));
383 typestack = g_list_prepend(typestack,NULL);
387 flags: '(' flaglist ')' { $<list>$ = $<list>2; }
388 | { $<list>$ = NULL; }
391 flaglist: TOKEN '|' flaglist {
392 $<list>$ = g_list_append($<list>3,$<id>1);
395 $<list>$ = g_list_append(NULL,$<id>1);
402 Type *type = typestack->data;
403 char *oldname = type->name;
404 type->name = g_strconcat("const ",oldname,NULL);
410 Node *node = new_type(0,$<id>1,NULL);
411 typestack = g_list_prepend(typestack,node);
414 Node *node = new_type(stars,$<id>1,NULL);
416 typestack = g_list_prepend(typestack,node);
420 type2: UNSIGNED integer {
421 $<id>$ = g_strconcat("unsigned ",$<id>2,NULL);
424 $<id>$ = g_strconcat("signed ",$<id>2,NULL);
427 $<id>$ = g_strdup($<id>1);
430 $<id>$ = g_strdup("unsigned char");
433 $<id>$ = g_strdup("signed char");
436 $<id>$ = g_strdup("char");
439 $<id>$ = g_strdup("double");
442 $<id>$ = g_strdup("float");
448 $<id>$ = g_strconcat($<id>1,$<id>2,NULL);
455 $<id>$ = g_strdup("void");
466 $<id>$ = "short int";
487 stars: '*' stars { stars++; }
491 /* this never sets the_scope */
492 simplesigtype: TOKEN sigtype {
493 if(strcmp($<id>1,"first")==0)
494 $<sigtype>$ = SIGNAL_FIRST_METHOD;
495 else if(strcmp($<id>1,"last")==0)
496 $<sigtype>$ = SIGNAL_LAST_METHOD;
498 yyerror(_("signal must be 'first' or 'last'"));
505 $<sigtype>$ = SIGNAL_LAST_METHOD;
509 /* this always sets the_scope */
510 fullsigtype: scope TOKEN sigtype {
511 if(strcmp($<id>2,"first")==0)
512 $<sigtype>$ = SIGNAL_FIRST_METHOD;
513 else if(strcmp($<id>2,"last")==0)
514 $<sigtype>$ = SIGNAL_LAST_METHOD;
516 yyerror(_("signal must be 'first' or 'last'"));
522 | TOKEN scope sigtype {
523 if(strcmp($<id>1,"first")==0)
524 $<sigtype>$ = SIGNAL_FIRST_METHOD;
525 else if(strcmp($<id>1,"last")==0)
526 $<sigtype>$ = SIGNAL_LAST_METHOD;
528 yyerror(_("signal must be 'first' or 'last'"));
535 $<sigtype>$ = SIGNAL_LAST_METHOD;
538 /* the_scope was default thus public */
539 the_scope = PUBLIC_SCOPE;
543 sigtype: TOKEN '(' tokenlist ')' {
544 gtktypes = g_list_prepend(gtktypes,$<id>1);
548 tokenlist: tokenlist ',' TOKEN {
549 gtktypes = g_list_append(gtktypes,$<id>3);
552 gtktypes = g_list_append(gtktypes,$<id>1);
556 codenocode: '{' CCODE { $<cbuf>$ = $<cbuf>2; }
557 | ';' { $<cbuf>$ = NULL; }
560 /*here CCODE will include the ending '}' */
561 method: SIGNAL flags fullsigtype type TOKEN '(' funcargs ')' onerror codenocode {
563 yyerror(_("signal without 'self' as "
567 push_function(the_scope, $<sigtype>3,NULL,
568 $<id>5, $<id>9, $<cbuf>10,$<line>1,
569 ccode_line, vararg, $<list>2);
571 | scope SIGNAL flags simplesigtype type TOKEN '(' funcargs ')' onerror codenocode {
573 yyerror(_("signal without 'self' as "
577 push_function(the_scope, $<sigtype>4, NULL,
578 $<id>6, $<id>10, $<cbuf>11, $<line>2,
579 ccode_line, vararg, $<list>3);
581 | VIRTUAL scope type TOKEN '(' funcargs ')' onerror codenocode {
583 yyerror(_("virtual method without 'self' as "
587 push_function(the_scope, VIRTUAL_METHOD, NULL, $<id>4,
588 $<id>8, $<cbuf>9, $<line>1,
589 ccode_line, vararg, NULL);
591 | scope VIRTUAL type TOKEN '(' funcargs ')' onerror codenocode {
593 yyerror(_("virtual method without 'self' as "
597 push_function(the_scope, VIRTUAL_METHOD, NULL, $<id>4,
598 $<id>8, $<cbuf>9, $<line>2,
599 ccode_line, vararg, NULL);
601 | VIRTUAL type TOKEN '(' funcargs ')' onerror codenocode {
603 yyerror(_("virtual method without 'self' as "
607 push_function(PUBLIC_SCOPE, VIRTUAL_METHOD, NULL,
608 $<id>3, $<id>7, $<cbuf>8, $<line>1,
609 ccode_line, vararg, NULL);
611 | OVERRIDE '(' TYPETOKEN ')' type TOKEN '(' funcargs ')' onerror codenocode {
612 push_function(NO_SCOPE, OVERRIDE_METHOD, $<id>3,
613 $<id>6, $<id>10, $<cbuf>11,
614 $<line>1, ccode_line,
617 | scope type TOKEN '(' funcargs ')' onerror codenocode {
618 push_function(the_scope, REGULAR_METHOD, NULL, $<id>3,
619 $<id>7, $<cbuf>8, $<line>1, ccode_line,
622 | TOKEN '(' TOKEN ')' codenocode {
623 if(strcmp($<id>1,"init")==0) {
624 push_init_arg($<id>3,FALSE);
625 push_function(NO_SCOPE, INIT_METHOD, NULL,
626 $<id>1, NULL, $<cbuf>5, $<line>2,
627 ccode_line, FALSE, NULL);
628 } else if(strcmp($<id>1,"class_init")==0) {
629 push_init_arg($<id>3,TRUE);
630 push_function(NO_SCOPE, CLASS_INIT_METHOD, NULL,
631 $<id>1, NULL, $<cbuf>5, $<line>2,
632 ccode_line, FALSE, NULL);
636 g_string_free($<cbuf>3,TRUE);
637 yyerror(_("parse error"));
643 onerror: ONERROR numtok { $<id>$ = $<id>2; }
644 | ONERROR '{' CCODE {
645 $<id>$ = ($<cbuf>3)->str;
646 g_string_free($<cbuf>3,FALSE);
651 funcargs: VOID { vararg = FALSE; has_self = FALSE; }
655 if(strcmp($<id>1,"self")==0)
659 yyerror(_("parse error"));
663 | TOKEN ',' arglist {
665 if(strcmp($<id>1,"self")==0)
669 yyerror(_("parse error"));
673 | arglist { has_self = FALSE; }
676 arglist: arglist1 ',' THREEDOTS { vararg = TRUE; }
677 | arglist1 { vararg = FALSE; }
680 arglist1: arglist1 ',' arg { ; }
685 push_funcarg($<id>2,NULL);
687 | type TOKEN ARRAY_DIM {
688 push_funcarg($<id>2,$<id>3);
690 | type TOKEN '(' TOKEN checklist ')' {
691 if(strcmp($<id>4,"check")!=0) {
692 yyerror(_("parse error"));
696 push_funcarg($<id>2,NULL);
698 | type TOKEN ARRAY_DIM '(' TOKEN checklist ')' {
699 if(strcmp($<id>5,"check")!=0) {
700 yyerror(_("parse error"));
704 push_funcarg($<id>2,$<id>3);
708 checklist: checklist check { ; }
713 if(strcmp($<id>1,"type")==0) {
714 Node *node = new_check(TYPE_CHECK,NULL);
715 checks = g_list_append(checks,node);
716 } else if(strcmp($<id>1,"null")==0) {
717 Node *node = new_check(NULL_CHECK,NULL);
718 checks = g_list_append(checks,node);
720 yyerror(_("parse error"));
726 Node *node = new_check(GT_CHECK,$<id>2);
727 checks = g_list_append(checks,node);
730 Node *node = new_check(LT_CHECK,$<id>2);
731 checks = g_list_append(checks,node);
734 Node *node = new_check(GE_CHECK,$<id>3);
735 checks = g_list_append(checks,node);
738 Node *node = new_check(LE_CHECK,$<id>3);
739 checks = g_list_append(checks,node);
742 Node *node = new_check(EQ_CHECK,$<id>3);
743 checks = g_list_append(checks,node);
746 Node *node = new_check(NE_CHECK,$<id>3);
747 checks = g_list_append(checks,node);
751 numtok: NUMBER { $<id>$ = $<id>1; }
753 $<id>$ = g_strconcat("-",$<id>2,NULL);
756 | TOKEN { $<id>$ = $<id>1; }