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, int vararg)
113 if(method!=INIT_METHOD && method!=CLASS_INIT_METHOD) {
116 type = (Type *)new_type(0,g_strdup("void"),NULL);
119 /* a complicated and ugly test to figure out if we have
120 the wrong number of types for a signal */
121 if((method == SIGNAL_FIRST_METHOD ||
122 method == SIGNAL_LAST_METHOD) &&
123 g_list_length(gtktypes) != g_list_length(funcargs) &&
124 !(g_list_length(funcargs) == 1 &&
125 g_list_length(gtktypes) == 2 &&
126 strcmp(gtktypes->next->data,"NONE")==0)) {
127 print_error(TRUE, _("The number of GTK arguments and "
128 "function arguments for a signal "
129 "don't seem to match"),line_no);
133 c_cbuf = p = cbuf->str;
134 while(p && *p && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
141 node = new_method(scope,method,type,oid,gtktypes,id,funcargs,
142 onerror,c_cbuf,line_no,ccode_line,vararg);
146 /*only free segment if we haven't passed it
152 class_nodes = g_list_append(class_nodes, node);
156 push_funcarg(char *name, char *postfix)
159 Type *type = pop_type();
161 type->postfix = postfix;
163 node = new_funcarg(type,name,checks);
166 funcargs = g_list_append(funcargs, node);
170 push_init_arg(char *name, int is_class)
177 tn = g_strconcat(((Class *)class)->otype,":Class",NULL);
179 tn = g_strdup(((Class *)class)->otype);
181 type = new_type(1,tn,NULL);
182 node = new_funcarg((Type *)type,name,NULL);
183 funcargs = g_list_prepend(funcargs, node);
192 type = new_type(1,g_strdup(((Class *)class)->otype),NULL);
193 ch = g_list_append(ch,new_check(NULL_CHECK,NULL));
194 ch = g_list_append(ch,new_check(TYPE_CHECK,NULL));
195 node = new_funcarg((Type *)type,id,ch);
196 funcargs = g_list_prepend(funcargs, node);
210 %token CONST VOID STRUCT UNION ENUM THREEDOTS
211 %token SIGNED UNSIGNED LONG SHORT INT FLOAT DOUBLE CHAR
214 %token <id> TOKEN NUMBER TYPETOKEN ARRAY_DIM
215 %token <cbuf> CCODE HTCODE PHCODE HCODE
216 %token <line> PUBLIC PRIVATE PROTECTED ARGUMENT VIRTUAL SIGNAL OVERRIDE
220 prog: ccodes class ccodes { ; }
227 Node *node = new_ccode(C_CCODE,($<cbuf>1)->str,
229 nodes = g_list_append(nodes,node);
230 g_string_free($<cbuf>1,FALSE);
233 Node *node = new_ccode(H_CCODE,($<cbuf>1)->str,
235 nodes = g_list_append(nodes,node);
236 g_string_free($<cbuf>1,FALSE);
239 Node *node = new_ccode(HT_CCODE,($<cbuf>1)->str,
241 nodes = g_list_append(nodes,node);
242 g_string_free($<cbuf>1,FALSE);
245 Node *node = new_ccode(PH_CCODE,($<cbuf>1)->str,
247 nodes = g_list_append(nodes,node);
248 g_string_free($<cbuf>1,FALSE);
252 ccodes: ccodes ccode { ; }
256 class: classdec '{' classcode '}' {
257 ((Class *)class)->nodes = class_nodes;
259 nodes = g_list_append(nodes,class);
262 ((Class *)class)->nodes = NULL;
264 nodes = g_list_append(nodes,class);
268 classdec: CLASS TYPETOKEN FROM TYPETOKEN {
269 class = new_class($<id>2,$<id>4,NULL);
273 classcode: classcode method { ; }
274 | classcode variable { ; }
275 | classcode argument { ; }
281 scope: PUBLIC { the_scope = PUBLIC_SCOPE; }
282 | PRIVATE { the_scope = PRIVATE_SCOPE; }
283 | PROTECTED { the_scope = PROTECTED_SCOPE; }
286 variable: scope type TOKEN ';' {
287 push_variable($<id>3,the_scope,$<line>1,NULL);
289 | scope type TOKEN ARRAY_DIM ';' {
290 push_variable($<id>3,the_scope,$<line>1,$<id>4);
293 argument: ARGUMENT argflags argtype TOKEN TOKEN '{' CCODE TOKEN '{' CCODE ';' {
294 if(strcmp($<id>5,"get")==0 &&
295 strcmp($<id>8,"set")==0) {
297 Type *type = pop_type();
298 g_free($<id>5); g_free($<id>8);
299 node = new_argument($<id>3,type,$<list>2,$<id>4,
300 ($<cbuf>7)->str,$<line>6,
301 ($<cbuf>10)->str,$<line>9,
303 g_string_free($<cbuf>7,FALSE);
304 g_string_free($<cbuf>10,FALSE);
305 class_nodes = g_list_append(class_nodes,node);
306 } else if(strcmp($<id>5,"set")==0 &&
307 strcmp($<id>8,"get")==0) {
309 Type *type = pop_type();
310 g_free($<id>5); g_free($<id>8);
311 node = new_argument($<id>3,type,$<list>2,$<id>4,
312 ($<cbuf>10)->str,$<line>9,
313 ($<cbuf>7)->str,$<line>6,
315 g_string_free($<cbuf>10,FALSE);
316 g_string_free($<cbuf>7,FALSE);
317 class_nodes = g_list_append(class_nodes,node);
319 g_free($<id>3); g_free($<id>4);
320 g_free($<id>5); g_free($<id>8);
321 g_list_foreach($<list>2,(GFunc)g_free,NULL);
322 g_string_free($<cbuf>10,TRUE);
323 g_string_free($<cbuf>7,TRUE);
324 yyerror(_("parse error"));
328 | ARGUMENT argflags argtype TOKEN TOKEN '{' CCODE ';' {
329 if(strcmp($<id>5,"get")==0) {
331 Type *type = pop_type();
333 node = new_argument($<id>3,type,$<list>2,$<id>4,
334 ($<cbuf>7)->str,$<line>6,
336 g_string_free($<cbuf>7,FALSE);
337 class_nodes = g_list_append(class_nodes,node);
338 } else if(strcmp($<id>5,"set")==0) {
340 Type *type = pop_type();
342 node = new_argument($<id>3,type,$<list>2,$<id>4,
343 NULL,0,($<cbuf>7)->str,
345 g_string_free($<cbuf>7,FALSE);
346 class_nodes = g_list_append(class_nodes,node);
348 g_free($<id>5); g_free($<id>3);
350 g_list_foreach($<list>2,(GFunc)g_free,NULL);
351 g_string_free($<cbuf>7,TRUE);
352 yyerror(_("parse error"));
358 argtype: TOKEN '(' TOKEN type ')' {
359 if(strcmp($<id>3,"type")!=0) {
362 yyerror(_("parse error"));
369 typestack = g_list_prepend(typestack,NULL);
373 argflags: '(' flaglist ')' { $<list>$ = $<list>2; }
374 | { $<list>$ = NULL; }
377 flaglist: TOKEN '|' flaglist {
378 $<list>$ = g_list_append($<list>3,$<id>1);
381 $<list>$ = g_list_append(NULL,$<id>1);
388 Type *type = typestack->data;
389 char *oldname = type->name;
390 type->name = g_strconcat("const ",oldname,NULL);
396 Node *node = new_type(0,$<id>1,NULL);
397 typestack = g_list_prepend(typestack,node);
400 Node *node = new_type(stars,$<id>1,NULL);
402 typestack = g_list_prepend(typestack,node);
406 type2: UNSIGNED integer {
407 $<id>$ = g_strconcat("unsigned ",$<id>2,NULL);
410 $<id>$ = g_strconcat("signed ",$<id>2,NULL);
413 $<id>$ = g_strdup($<id>1);
416 $<id>$ = g_strdup("unsigned char");
419 $<id>$ = g_strdup("signed char");
422 $<id>$ = g_strdup("char");
425 $<id>$ = g_strdup("double");
428 $<id>$ = g_strdup("float");
434 $<id>$ = g_strconcat($<id>1,$<id>2,NULL);
441 $<id>$ = g_strdup("void");
452 $<id>$ = "short int";
473 stars: '*' stars { stars++; }
477 /* this never sets the_scope */
478 simplesigtype: TOKEN sigtype {
479 if(strcmp($<id>1,"first")==0)
480 $<sigtype>$ = SIGNAL_FIRST_METHOD;
481 else if(strcmp($<id>1,"last")==0)
482 $<sigtype>$ = SIGNAL_LAST_METHOD;
484 yyerror(_("signal must be 'first' or 'last'"));
491 $<sigtype>$ = SIGNAL_LAST_METHOD;
495 /* this always sets the_scope */
496 fullsigtype: scope TOKEN sigtype {
497 if(strcmp($<id>2,"first")==0)
498 $<sigtype>$ = SIGNAL_FIRST_METHOD;
499 else if(strcmp($<id>2,"last")==0)
500 $<sigtype>$ = SIGNAL_LAST_METHOD;
502 yyerror(_("signal must be 'first' or 'last'"));
508 | TOKEN scope sigtype {
509 if(strcmp($<id>1,"first")==0)
510 $<sigtype>$ = SIGNAL_FIRST_METHOD;
511 else if(strcmp($<id>1,"last")==0)
512 $<sigtype>$ = SIGNAL_LAST_METHOD;
514 yyerror(_("signal must be 'first' or 'last'"));
521 $<sigtype>$ = SIGNAL_LAST_METHOD;
524 /* the_scope was default thus public */
525 the_scope = PUBLIC_SCOPE;
529 sigtype: TOKEN '(' tokenlist ')' {
530 gtktypes = g_list_prepend(gtktypes,$<id>1);
534 tokenlist: tokenlist ',' TOKEN {
535 gtktypes = g_list_append(gtktypes,$<id>3);
538 gtktypes = g_list_append(gtktypes,$<id>1);
542 codenocode: '{' CCODE { $<cbuf>$ = $<cbuf>2; }
543 | ';' { $<cbuf>$ = NULL; }
546 /*here CCODE will include the ending '}' */
547 method: SIGNAL fullsigtype type TOKEN '(' funcargs ')' onerror codenocode {
549 yyerror(_("signal without 'self' as "
553 push_function(the_scope, $<sigtype>2,NULL,
554 $<id>4, $<id>8, $<cbuf>9,$<line>1,
557 | scope SIGNAL simplesigtype type TOKEN '(' funcargs ')' onerror codenocode {
559 yyerror(_("signal without 'self' as "
563 push_function(the_scope, $<sigtype>3,NULL,
564 $<id>5, $<id>9, $<cbuf>10,$<line>2,
567 | VIRTUAL scope type TOKEN '(' funcargs ')' onerror codenocode {
569 yyerror(_("virtual method without 'self' as "
573 push_function(the_scope, VIRTUAL_METHOD, NULL, $<id>4,
574 $<id>8, $<cbuf>9,$<line>1,
577 | scope VIRTUAL type TOKEN '(' funcargs ')' onerror codenocode {
579 yyerror(_("virtual method without 'self' as "
583 push_function(the_scope, VIRTUAL_METHOD, NULL, $<id>4,
584 $<id>8, $<cbuf>9,$<line>2,
587 | VIRTUAL type TOKEN '(' funcargs ')' onerror codenocode {
589 yyerror(_("virtual method without 'self' as "
593 push_function(PUBLIC_SCOPE, VIRTUAL_METHOD, NULL,
594 $<id>3, $<id>7, $<cbuf>8,$<line>1,
597 | OVERRIDE '(' TYPETOKEN ')' type TOKEN '(' funcargs ')' onerror codenocode {
598 push_function(NO_SCOPE, OVERRIDE_METHOD, $<id>3,
599 $<id>6, $<id>10, $<cbuf>11,
603 | scope type TOKEN '(' funcargs ')' onerror codenocode {
604 push_function(the_scope, REGULAR_METHOD, NULL, $<id>3,
605 $<id>7, $<cbuf>8,$<line>1,ccode_line,
608 | TOKEN '(' TOKEN ')' codenocode {
609 if(strcmp($<id>1,"init")==0) {
610 push_init_arg($<id>3,FALSE);
611 push_function(NO_SCOPE, INIT_METHOD, NULL,
612 $<id>1, NULL, $<cbuf>5,$<line>2,
614 } else if(strcmp($<id>1,"class_init")==0) {
615 push_init_arg($<id>3,TRUE);
616 push_function(NO_SCOPE, CLASS_INIT_METHOD, NULL,
617 $<id>1, NULL, $<cbuf>5,$<line>2,
622 g_string_free($<cbuf>3,TRUE);
623 yyerror(_("parse error"));
629 onerror: ONERROR numtok { $<id>$ = $<id>2; }
630 | ONERROR '{' CCODE {
631 $<id>$ = ($<cbuf>3)->str;
632 g_string_free($<cbuf>3,FALSE);
637 funcargs: VOID { vararg = FALSE; has_self = FALSE; }
641 if(strcmp($<id>1,"self")==0)
645 yyerror(_("parse error"));
649 | TOKEN ',' arglist {
651 if(strcmp($<id>1,"self")==0)
655 yyerror(_("parse error"));
659 | arglist { has_self = FALSE; }
662 arglist: arglist1 ',' THREEDOTS { vararg = TRUE; }
663 | arglist1 { vararg = FALSE; }
666 arglist1: arglist1 ',' arg { ; }
671 push_funcarg($<id>2,NULL);
673 | type TOKEN ARRAY_DIM {
674 push_funcarg($<id>2,$<id>3);
676 | type TOKEN '(' TOKEN checklist ')' {
677 if(strcmp($<id>4,"check")!=0) {
678 yyerror(_("parse error"));
682 push_funcarg($<id>2,NULL);
684 | type TOKEN ARRAY_DIM '(' TOKEN checklist ')' {
685 if(strcmp($<id>5,"check")!=0) {
686 yyerror(_("parse error"));
690 push_funcarg($<id>2,$<id>3);
694 checklist: checklist check { ; }
699 if(strcmp($<id>1,"type")==0) {
700 Node *node = new_check(TYPE_CHECK,NULL);
701 checks = g_list_append(checks,node);
702 } else if(strcmp($<id>1,"null")==0) {
703 Node *node = new_check(NULL_CHECK,NULL);
704 checks = g_list_append(checks,node);
706 yyerror(_("parse error"));
712 Node *node = new_check(GT_CHECK,$<id>2);
713 checks = g_list_append(checks,node);
716 Node *node = new_check(LT_CHECK,$<id>2);
717 checks = g_list_append(checks,node);
720 Node *node = new_check(GE_CHECK,$<id>3);
721 checks = g_list_append(checks,node);
724 Node *node = new_check(LE_CHECK,$<id>3);
725 checks = g_list_append(checks,node);
728 Node *node = new_check(EQ_CHECK,$<id>3);
729 checks = g_list_append(checks,node);
732 Node *node = new_check(NE_CHECK,$<id>3);
733 checks = g_list_append(checks,node);
737 numtok: NUMBER { $<id>$ = $<id>1; }
739 $<id>$ = g_strconcat("-",$<id>2,NULL);
742 | TOKEN { $<id>$ = $<id>1; }