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,
33 extern char *filename;
37 static GList *class_nodes = NULL;
40 static GList *typestack = NULL;
42 static GList *funcargs = NULL;
43 static GList *checks = NULL;
44 static int has_self = FALSE;
45 static int vararg = FALSE;
47 static GList *gtktypes = NULL;
49 /* this can be a global as we will only do one function at a time
51 static int the_scope = NO_SCOPE;
56 extern int ccode_line;
67 if(strcmp(yytext,"\n")==0) {
68 out=g_strconcat("Error: ",str," before newline",NULL);
69 } else if(yytext[0]=='\0') {
70 out=g_strconcat("Error: ",str," at end of input",NULL);
72 char *tmp = g_strdup(yytext);
73 while((p=strchr(tmp,'\n')))
76 out=g_strconcat("Error: ",str," before '",tmp,"'",NULL);
80 fprintf(stderr,"%s:%d: %s\n",filename,line_no,out);
87 push_variable(char *name, int scope, int line_no, char *postfix)
90 Type *type = typestack->data;
91 typestack = g_list_remove(typestack,typestack->data);
93 type->postfix = postfix;
95 var = new_variable(scope,type,name,line_no);
96 class_nodes = g_list_append(class_nodes, var);
100 push_function(int scope, int method, char *oid, char *id, char *onerror,
101 GString *cbuf,int line_no, int ccode_line, int vararg)
107 if(method!=INIT_METHOD && method!=CLASS_INIT_METHOD) {
108 type = typestack->data;
109 typestack = g_list_remove(typestack,typestack->data);
111 type = (Type *)new_type(0,g_strdup("void"),NULL);
114 /* a complicated and ugly test to figure out if we have
115 the wrong number of types for a signal */
116 if((method == SIGNAL_FIRST_METHOD ||
117 method == SIGNAL_LAST_METHOD) &&
118 g_list_length(gtktypes) != g_list_length(funcargs) &&
119 !(g_list_length(funcargs) == 1 &&
120 g_list_length(gtktypes) == 2 &&
121 strcmp(gtktypes->next->data,"NONE")==0)) {
122 print_error(TRUE, _("The number of GTK arguments and "
123 "function arguments for a signal "
124 "don't seem to match"),line_no);
128 c_cbuf = p = cbuf->str;
129 while(p && *p && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
136 node = new_method(scope,method,type,oid,gtktypes,id,funcargs,
137 onerror,c_cbuf,line_no,ccode_line,vararg);
141 /*only free segment if we haven't passed it
147 class_nodes = g_list_append(class_nodes, node);
151 push_funcarg(char *name, char *postfix)
154 Type *type = typestack->data;
155 typestack = g_list_remove(typestack,typestack->data);
157 type->postfix = postfix;
159 node = new_funcarg(type,name,checks);
162 funcargs = g_list_append(funcargs, node);
166 push_init_arg(char *name, int is_class)
173 tn = g_strconcat(((Class *)class)->otype,":Class",NULL);
175 tn = g_strdup(((Class *)class)->otype);
177 type = new_type(1,tn,NULL);
178 node = new_funcarg((Type *)type,name,NULL);
179 funcargs = g_list_prepend(funcargs, node);
188 type = new_type(1,g_strdup(((Class *)class)->otype),NULL);
189 ch = g_list_append(ch,new_check(NULL_CHECK,NULL));
190 ch = g_list_append(ch,new_check(TYPE_CHECK,NULL));
191 node = new_funcarg((Type *)type,id,ch);
192 funcargs = g_list_prepend(funcargs, node);
206 %token CONST VOID STRUCT UNION ENUM THREEDOTS
207 %token SIGNED UNSIGNED LONG SHORT INT FLOAT DOUBLE CHAR
210 %token <id> TOKEN NUMBER TYPETOKEN ARRAY_DIM
211 %token <cbuf> CCODE HCODE
212 %token <line> PUBLIC PRIVATE PROTECTED ARGUMENT VIRTUAL SIGNAL OVERRIDE
216 prog: ccodes class ccodes { ; }
222 ccodes: ccodes CCODE {
223 Node *node = new_ccode(FALSE,($<cbuf>2)->str,
225 nodes = g_list_append(nodes,node);
226 g_string_free($<cbuf>2,FALSE);
229 Node *node = new_ccode(TRUE,($<cbuf>2)->str,ccode_line);
230 nodes = g_list_append(nodes,node);
231 g_string_free($<cbuf>2,FALSE);
234 Node *node = new_ccode(FALSE,($<cbuf>1)->str,
236 nodes = g_list_append(nodes,node);
237 g_string_free($<cbuf>1,FALSE);
240 Node *node = new_ccode(TRUE,($<cbuf>1)->str,ccode_line);
241 nodes = g_list_append(nodes,node);
242 g_string_free($<cbuf>1,FALSE);
246 class: classdec '{' classcode '}' {
247 ((Class *)class)->nodes = class_nodes;
249 nodes = g_list_append(nodes,class);
252 ((Class *)class)->nodes = NULL;
254 nodes = g_list_append(nodes,class);
258 classdec: CLASS TYPETOKEN FROM TYPETOKEN {
259 class = new_class($<id>2,$<id>4,NULL);
263 classcode: classcode method { ; }
264 | classcode variable { ; }
265 | classcode argument { ; }
271 scope: PUBLIC { the_scope = PUBLIC_SCOPE; }
272 | PRIVATE { the_scope = PRIVATE_SCOPE; }
273 | PROTECTED { the_scope = PROTECTED_SCOPE; }
276 variable: scope type TOKEN ';' {
277 push_variable($<id>3,the_scope,$<line>1,NULL);
279 | scope type TOKEN ARRAY_DIM ';' {
280 push_variable($<id>3,the_scope,$<line>1,$<id>4);
283 argument: ARGUMENT argflags TOKEN TOKEN TOKEN '{' CCODE TOKEN '{' CCODE ';' {
284 if(strcmp($<id>5,"get")==0 &&
285 strcmp($<id>8,"set")==0) {
287 g_free($<id>5); g_free($<id>8);
288 node = new_argument($<id>3,$<list>2,$<id>4,
289 ($<cbuf>7)->str,$<line>6,
290 ($<cbuf>10)->str,$<line>9,
292 g_string_free($<cbuf>7,FALSE);
293 g_string_free($<cbuf>10,FALSE);
294 class_nodes = g_list_append(class_nodes,node);
295 } else if(strcmp($<id>5,"set")==0 &&
296 strcmp($<id>8,"get")==0) {
298 g_free($<id>5); g_free($<id>8);
299 node = new_argument($<id>3,$<list>2,$<id>4,
300 ($<cbuf>10)->str,$<line>9,
301 ($<cbuf>7)->str,$<line>6,
303 g_string_free($<cbuf>10,FALSE);
304 g_string_free($<cbuf>7,FALSE);
305 class_nodes = g_list_append(class_nodes,node);
307 g_free($<id>3); g_free($<id>4);
308 g_free($<id>5); g_free($<id>8);
309 g_list_foreach($<list>2,(GFunc)g_free,NULL);
310 g_string_free($<cbuf>10,TRUE);
311 g_string_free($<cbuf>7,TRUE);
312 yyerror(_("parse error"));
316 | ARGUMENT argflags TOKEN TOKEN TOKEN '{' CCODE ';' {
317 if(strcmp($<id>5,"get")==0) {
320 node = new_argument($<id>3,$<list>2,$<id>4,
321 ($<cbuf>7)->str,$<line>6,
323 g_string_free($<cbuf>7,FALSE);
324 class_nodes = g_list_append(class_nodes,node);
325 } else if(strcmp($<id>5,"set")==0) {
328 node = new_argument($<id>3,$<list>2,$<id>4,
329 NULL,0,($<cbuf>7)->str,
331 g_string_free($<cbuf>7,FALSE);
332 class_nodes = g_list_append(class_nodes,node);
334 g_free($<id>5); g_free($<id>3);
336 g_list_foreach($<list>2,(GFunc)g_free,NULL);
337 g_string_free($<cbuf>7,TRUE);
338 yyerror(_("parse error"));
344 argflags: '(' flaglist ')' { $<list>$ = $<list>2; }
345 | { $<list>$ = NULL; }
348 flaglist: TOKEN '|' flaglist {
349 $<list>$ = g_list_append($<list>3,$<id>1);
352 $<list>$ = g_list_append(NULL,$<id>1);
359 Type *type = typestack->data;
360 char *oldname = type->name;
361 type->name = g_strconcat("const ",oldname,NULL);
367 Node *node = new_type(0,$<id>1,NULL);
368 typestack = g_list_prepend(typestack,node);
371 Node *node = new_type(stars,$<id>1,NULL);
373 typestack = g_list_prepend(typestack,node);
377 type2: UNSIGNED integer {
378 $<id>$ = g_strconcat("unsigned ",$<id>2,NULL);
381 $<id>$ = g_strconcat("signed ",$<id>2,NULL);
384 $<id>$ = g_strdup($<id>1);
387 $<id>$ = g_strdup("unsigned char");
390 $<id>$ = g_strdup("signed char");
393 $<id>$ = g_strdup("char");
396 $<id>$ = g_strdup("double");
399 $<id>$ = g_strdup("float");
405 $<id>$ = g_strconcat($<id>1,$<id>2,NULL);
412 $<id>$ = g_strdup("void");
423 $<id>$ = "short int";
444 stars: '*' stars { stars++; }
448 /* this never sets the_scope */
449 simplesigtype: TOKEN sigtype {
450 if(strcmp($<id>1,"first")==0)
451 $<sigtype>$ = SIGNAL_FIRST_METHOD;
452 else if(strcmp($<id>1,"last")==0)
453 $<sigtype>$ = SIGNAL_LAST_METHOD;
455 yyerror(_("signal must be 'first' or 'last'"));
462 $<sigtype>$ = SIGNAL_LAST_METHOD;
466 /* this always sets the_scope */
467 fullsigtype: scope TOKEN sigtype {
468 if(strcmp($<id>2,"first")==0)
469 $<sigtype>$ = SIGNAL_FIRST_METHOD;
470 else if(strcmp($<id>2,"last")==0)
471 $<sigtype>$ = SIGNAL_LAST_METHOD;
473 yyerror(_("signal must be 'first' or 'last'"));
479 | TOKEN scope sigtype {
480 if(strcmp($<id>1,"first")==0)
481 $<sigtype>$ = SIGNAL_FIRST_METHOD;
482 else if(strcmp($<id>1,"last")==0)
483 $<sigtype>$ = SIGNAL_LAST_METHOD;
485 yyerror(_("signal must be 'first' or 'last'"));
492 $<sigtype>$ = SIGNAL_LAST_METHOD;
495 /* the_scope was default thus public */
496 the_scope = PUBLIC_SCOPE;
500 sigtype: TOKEN '(' tokenlist ')' {
501 gtktypes = g_list_prepend(gtktypes,$<id>1);
505 tokenlist: tokenlist ',' TOKEN {
506 gtktypes = g_list_append(gtktypes,$<id>3);
509 gtktypes = g_list_append(gtktypes,$<id>1);
513 codenocode: '{' CCODE { $<cbuf>$ = $<cbuf>2; }
514 | ';' { $<cbuf>$ = NULL; }
517 /*here CCODE will include the ending '}' */
518 method: SIGNAL fullsigtype type TOKEN '(' funcargs ')' onerror codenocode {
520 yyerror(_("signal without 'self' as "
524 push_function(the_scope, $<sigtype>2,NULL,
525 $<id>4, $<id>8, $<cbuf>9,$<line>1,
528 | scope SIGNAL simplesigtype type TOKEN '(' funcargs ')' onerror codenocode {
530 yyerror(_("signal without 'self' as "
534 push_function(the_scope, $<sigtype>3,NULL,
535 $<id>5, $<id>9, $<cbuf>10,$<line>2,
538 | VIRTUAL scope type TOKEN '(' funcargs ')' onerror codenocode {
540 yyerror(_("virtual method without 'self' as "
544 push_function(the_scope, VIRTUAL_METHOD, NULL, $<id>4,
545 $<id>8, $<cbuf>9,$<line>1,
548 | scope VIRTUAL type TOKEN '(' funcargs ')' onerror codenocode {
550 yyerror(_("virtual method without 'self' as "
554 push_function(the_scope, VIRTUAL_METHOD, NULL, $<id>4,
555 $<id>8, $<cbuf>9,$<line>2,
558 | VIRTUAL type TOKEN '(' funcargs ')' onerror codenocode {
560 yyerror(_("virtual method without 'self' as "
564 push_function(PUBLIC_SCOPE, VIRTUAL_METHOD, NULL,
565 $<id>3, $<id>7, $<cbuf>8,$<line>1,
568 | OVERRIDE '(' TYPETOKEN ')' type TOKEN '(' funcargs ')' onerror codenocode {
569 push_function(NO_SCOPE, OVERRIDE_METHOD, $<id>3,
570 $<id>6, $<id>10, $<cbuf>11,
574 | scope type TOKEN '(' funcargs ')' onerror codenocode {
575 push_function(the_scope, REGULAR_METHOD, NULL, $<id>3,
576 $<id>7, $<cbuf>8,$<line>1,ccode_line,
579 | TOKEN '(' TOKEN ')' codenocode {
580 if(strcmp($<id>1,"init")==0) {
581 push_init_arg($<id>3,FALSE);
582 push_function(NO_SCOPE, INIT_METHOD, NULL,
583 $<id>1, NULL, $<cbuf>5,$<line>2,
585 } else if(strcmp($<id>1,"class_init")==0) {
586 push_init_arg($<id>3,TRUE);
587 push_function(NO_SCOPE, CLASS_INIT_METHOD, NULL,
588 $<id>1, NULL, $<cbuf>5,$<line>2,
593 g_string_free($<cbuf>3,TRUE);
594 yyerror(_("parse error"));
600 onerror: ONERROR numtok { $<id>$ = $<id>2; }
601 | ONERROR '{' CCODE {
602 $<id>$ = ($<cbuf>3)->str;
603 g_string_free($<cbuf>3,FALSE);
608 funcargs: VOID { vararg = FALSE; has_self = FALSE; }
612 if(strcmp($<id>1,"self")==0)
616 yyerror(_("parse error"));
620 | TOKEN ',' arglist {
622 if(strcmp($<id>1,"self")==0)
626 yyerror(_("parse error"));
630 | arglist { has_self = FALSE; }
633 arglist: arglist1 ',' THREEDOTS { vararg = TRUE; }
634 | arglist1 { vararg = FALSE; }
637 arglist1: arglist1 ',' arg { ; }
642 push_funcarg($<id>2,NULL);
644 | type TOKEN ARRAY_DIM {
645 push_funcarg($<id>2,$<id>3);
647 | type TOKEN '(' TOKEN checklist ')' {
648 if(strcmp($<id>4,"check")!=0) {
649 yyerror(_("parse error"));
653 push_funcarg($<id>2,NULL);
655 | type TOKEN ARRAY_DIM '(' TOKEN checklist ')' {
656 if(strcmp($<id>5,"check")!=0) {
657 yyerror(_("parse error"));
661 push_funcarg($<id>2,$<id>3);
665 checklist: checklist check { ; }
670 if(strcmp($<id>1,"type")==0) {
671 Node *node = new_check(TYPE_CHECK,NULL);
672 checks = g_list_append(checks,node);
673 } else if(strcmp($<id>1,"null")==0) {
674 Node *node = new_check(NULL_CHECK,NULL);
675 checks = g_list_append(checks,node);
677 yyerror(_("parse error"));
683 Node *node = new_check(GT_CHECK,$<id>2);
684 checks = g_list_append(checks,node);
687 Node *node = new_check(LT_CHECK,$<id>2);
688 checks = g_list_append(checks,node);
691 Node *node = new_check(GE_CHECK,$<id>3);
692 checks = g_list_append(checks,node);
695 Node *node = new_check(LE_CHECK,$<id>3);
696 checks = g_list_append(checks,node);
699 Node *node = new_check(EQ_CHECK,$<id>3);
700 checks = g_list_append(checks,node);
703 Node *node = new_check(NE_CHECK,$<id>3);
704 checks = g_list_append(checks,node);
708 numtok: NUMBER { $<id>$ = $<id>1; }
710 $<id>$ = g_strconcat("-",$<id>2,NULL);
713 | TOKEN { $<id>$ = $<id>1; }