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);
89 Type *type = typestack->data;
90 typestack = g_list_remove(typestack,typestack->data);
95 push_variable(char *name, int scope, int line_no, char *postfix)
98 Type *type = pop_type();
100 type->postfix = postfix;
102 var = new_variable(scope,type,name,line_no);
103 class_nodes = g_list_append(class_nodes, var);
107 push_function(int scope, int method, char *oid, char *id, char *onerror,
108 GString *cbuf,int line_no, int ccode_line, int 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,id,funcargs,
143 onerror,c_cbuf,line_no,ccode_line,vararg);
147 /*only free segment if we haven't passed it
153 class_nodes = g_list_append(class_nodes, node);
157 push_funcarg(char *name, char *postfix)
160 Type *type = pop_type();
162 type->postfix = postfix;
164 node = new_funcarg(type,name,checks);
167 funcargs = g_list_append(funcargs, node);
171 push_init_arg(char *name, int is_class)
178 tn = g_strconcat(((Class *)class)->otype,":Class",NULL);
180 tn = g_strdup(((Class *)class)->otype);
182 type = new_type(1,tn,NULL);
183 node = new_funcarg((Type *)type,name,NULL);
184 funcargs = g_list_prepend(funcargs, node);
193 type = new_type(1,g_strdup(((Class *)class)->otype),NULL);
194 ch = g_list_append(ch,new_check(NULL_CHECK,NULL));
195 ch = g_list_append(ch,new_check(TYPE_CHECK,NULL));
196 node = new_funcarg((Type *)type,id,ch);
197 funcargs = g_list_prepend(funcargs, node);
211 %token CONST VOID STRUCT UNION ENUM THREEDOTS
212 %token SIGNED UNSIGNED LONG SHORT INT FLOAT DOUBLE CHAR
215 %token <id> TOKEN NUMBER TYPETOKEN ARRAY_DIM
216 %token <cbuf> CCODE HCODE
217 %token <line> PUBLIC PRIVATE PROTECTED ARGUMENT VIRTUAL SIGNAL OVERRIDE
221 prog: ccodes class ccodes { ; }
227 ccodes: ccodes CCODE {
228 Node *node = new_ccode(FALSE,($<cbuf>2)->str,
230 nodes = g_list_append(nodes,node);
231 g_string_free($<cbuf>2,FALSE);
234 Node *node = new_ccode(TRUE,($<cbuf>2)->str,ccode_line);
235 nodes = g_list_append(nodes,node);
236 g_string_free($<cbuf>2,FALSE);
239 Node *node = new_ccode(FALSE,($<cbuf>1)->str,
241 nodes = g_list_append(nodes,node);
242 g_string_free($<cbuf>1,FALSE);
245 Node *node = new_ccode(TRUE,($<cbuf>1)->str,ccode_line);
246 nodes = g_list_append(nodes,node);
247 g_string_free($<cbuf>1,FALSE);
251 class: classdec '{' classcode '}' {
252 ((Class *)class)->nodes = class_nodes;
254 nodes = g_list_append(nodes,class);
257 ((Class *)class)->nodes = NULL;
259 nodes = g_list_append(nodes,class);
263 classdec: CLASS TYPETOKEN FROM TYPETOKEN {
264 class = new_class($<id>2,$<id>4,NULL);
268 classcode: classcode method { ; }
269 | classcode variable { ; }
270 | classcode argument { ; }
276 scope: PUBLIC { the_scope = PUBLIC_SCOPE; }
277 | PRIVATE { the_scope = PRIVATE_SCOPE; }
278 | PROTECTED { the_scope = PROTECTED_SCOPE; }
281 variable: scope type TOKEN ';' {
282 push_variable($<id>3,the_scope,$<line>1,NULL);
284 | scope type TOKEN ARRAY_DIM ';' {
285 push_variable($<id>3,the_scope,$<line>1,$<id>4);
288 argument: ARGUMENT argflags argtype TOKEN TOKEN '{' CCODE TOKEN '{' CCODE ';' {
289 if(strcmp($<id>5,"get")==0 &&
290 strcmp($<id>8,"set")==0) {
292 Type *type = pop_type();
293 g_free($<id>5); g_free($<id>8);
294 node = new_argument($<id>3,type,$<list>2,$<id>4,
295 ($<cbuf>7)->str,$<line>6,
296 ($<cbuf>10)->str,$<line>9,
298 g_string_free($<cbuf>7,FALSE);
299 g_string_free($<cbuf>10,FALSE);
300 class_nodes = g_list_append(class_nodes,node);
301 } else if(strcmp($<id>5,"set")==0 &&
302 strcmp($<id>8,"get")==0) {
304 Type *type = pop_type();
305 g_free($<id>5); g_free($<id>8);
306 node = new_argument($<id>3,type,$<list>2,$<id>4,
307 ($<cbuf>10)->str,$<line>9,
308 ($<cbuf>7)->str,$<line>6,
310 g_string_free($<cbuf>10,FALSE);
311 g_string_free($<cbuf>7,FALSE);
312 class_nodes = g_list_append(class_nodes,node);
314 g_free($<id>3); g_free($<id>4);
315 g_free($<id>5); g_free($<id>8);
316 g_list_foreach($<list>2,(GFunc)g_free,NULL);
317 g_string_free($<cbuf>10,TRUE);
318 g_string_free($<cbuf>7,TRUE);
319 yyerror(_("parse error"));
323 | ARGUMENT argflags argtype TOKEN TOKEN '{' CCODE ';' {
324 if(strcmp($<id>5,"get")==0) {
326 Type *type = pop_type();
328 node = new_argument($<id>3,type,$<list>2,$<id>4,
329 ($<cbuf>7)->str,$<line>6,
331 g_string_free($<cbuf>7,FALSE);
332 class_nodes = g_list_append(class_nodes,node);
333 } else if(strcmp($<id>5,"set")==0) {
335 Type *type = pop_type();
337 node = new_argument($<id>3,type,$<list>2,$<id>4,
338 NULL,0,($<cbuf>7)->str,
340 g_string_free($<cbuf>7,FALSE);
341 class_nodes = g_list_append(class_nodes,node);
343 g_free($<id>5); g_free($<id>3);
345 g_list_foreach($<list>2,(GFunc)g_free,NULL);
346 g_string_free($<cbuf>7,TRUE);
347 yyerror(_("parse error"));
353 argtype: TOKEN '(' TOKEN type ')' {
354 if(strcmp($<id>3,"type")!=0) {
357 yyerror(_("parse error"));
364 typestack = g_list_prepend(typestack,NULL);
368 argflags: '(' flaglist ')' { $<list>$ = $<list>2; }
369 | { $<list>$ = NULL; }
372 flaglist: TOKEN '|' flaglist {
373 $<list>$ = g_list_append($<list>3,$<id>1);
376 $<list>$ = g_list_append(NULL,$<id>1);
383 Type *type = typestack->data;
384 char *oldname = type->name;
385 type->name = g_strconcat("const ",oldname,NULL);
391 Node *node = new_type(0,$<id>1,NULL);
392 typestack = g_list_prepend(typestack,node);
395 Node *node = new_type(stars,$<id>1,NULL);
397 typestack = g_list_prepend(typestack,node);
401 type2: UNSIGNED integer {
402 $<id>$ = g_strconcat("unsigned ",$<id>2,NULL);
405 $<id>$ = g_strconcat("signed ",$<id>2,NULL);
408 $<id>$ = g_strdup($<id>1);
411 $<id>$ = g_strdup("unsigned char");
414 $<id>$ = g_strdup("signed char");
417 $<id>$ = g_strdup("char");
420 $<id>$ = g_strdup("double");
423 $<id>$ = g_strdup("float");
429 $<id>$ = g_strconcat($<id>1,$<id>2,NULL);
436 $<id>$ = g_strdup("void");
447 $<id>$ = "short int";
468 stars: '*' stars { stars++; }
472 /* this never sets the_scope */
473 simplesigtype: TOKEN sigtype {
474 if(strcmp($<id>1,"first")==0)
475 $<sigtype>$ = SIGNAL_FIRST_METHOD;
476 else if(strcmp($<id>1,"last")==0)
477 $<sigtype>$ = SIGNAL_LAST_METHOD;
479 yyerror(_("signal must be 'first' or 'last'"));
486 $<sigtype>$ = SIGNAL_LAST_METHOD;
490 /* this always sets the_scope */
491 fullsigtype: scope TOKEN sigtype {
492 if(strcmp($<id>2,"first")==0)
493 $<sigtype>$ = SIGNAL_FIRST_METHOD;
494 else if(strcmp($<id>2,"last")==0)
495 $<sigtype>$ = SIGNAL_LAST_METHOD;
497 yyerror(_("signal must be 'first' or 'last'"));
503 | TOKEN scope sigtype {
504 if(strcmp($<id>1,"first")==0)
505 $<sigtype>$ = SIGNAL_FIRST_METHOD;
506 else if(strcmp($<id>1,"last")==0)
507 $<sigtype>$ = SIGNAL_LAST_METHOD;
509 yyerror(_("signal must be 'first' or 'last'"));
516 $<sigtype>$ = SIGNAL_LAST_METHOD;
519 /* the_scope was default thus public */
520 the_scope = PUBLIC_SCOPE;
524 sigtype: TOKEN '(' tokenlist ')' {
525 gtktypes = g_list_prepend(gtktypes,$<id>1);
529 tokenlist: tokenlist ',' TOKEN {
530 gtktypes = g_list_append(gtktypes,$<id>3);
533 gtktypes = g_list_append(gtktypes,$<id>1);
537 codenocode: '{' CCODE { $<cbuf>$ = $<cbuf>2; }
538 | ';' { $<cbuf>$ = NULL; }
541 /*here CCODE will include the ending '}' */
542 method: SIGNAL fullsigtype type TOKEN '(' funcargs ')' onerror codenocode {
544 yyerror(_("signal without 'self' as "
548 push_function(the_scope, $<sigtype>2,NULL,
549 $<id>4, $<id>8, $<cbuf>9,$<line>1,
552 | scope SIGNAL simplesigtype type TOKEN '(' funcargs ')' onerror codenocode {
554 yyerror(_("signal without 'self' as "
558 push_function(the_scope, $<sigtype>3,NULL,
559 $<id>5, $<id>9, $<cbuf>10,$<line>2,
562 | VIRTUAL scope type TOKEN '(' funcargs ')' onerror codenocode {
564 yyerror(_("virtual method without 'self' as "
568 push_function(the_scope, VIRTUAL_METHOD, NULL, $<id>4,
569 $<id>8, $<cbuf>9,$<line>1,
572 | scope VIRTUAL type TOKEN '(' funcargs ')' onerror codenocode {
574 yyerror(_("virtual method without 'self' as "
578 push_function(the_scope, VIRTUAL_METHOD, NULL, $<id>4,
579 $<id>8, $<cbuf>9,$<line>2,
582 | VIRTUAL type TOKEN '(' funcargs ')' onerror codenocode {
584 yyerror(_("virtual method without 'self' as "
588 push_function(PUBLIC_SCOPE, VIRTUAL_METHOD, NULL,
589 $<id>3, $<id>7, $<cbuf>8,$<line>1,
592 | OVERRIDE '(' TYPETOKEN ')' type TOKEN '(' funcargs ')' onerror codenocode {
593 push_function(NO_SCOPE, OVERRIDE_METHOD, $<id>3,
594 $<id>6, $<id>10, $<cbuf>11,
598 | scope type TOKEN '(' funcargs ')' onerror codenocode {
599 push_function(the_scope, REGULAR_METHOD, NULL, $<id>3,
600 $<id>7, $<cbuf>8,$<line>1,ccode_line,
603 | TOKEN '(' TOKEN ')' codenocode {
604 if(strcmp($<id>1,"init")==0) {
605 push_init_arg($<id>3,FALSE);
606 push_function(NO_SCOPE, INIT_METHOD, NULL,
607 $<id>1, NULL, $<cbuf>5,$<line>2,
609 } else if(strcmp($<id>1,"class_init")==0) {
610 push_init_arg($<id>3,TRUE);
611 push_function(NO_SCOPE, CLASS_INIT_METHOD, NULL,
612 $<id>1, NULL, $<cbuf>5,$<line>2,
617 g_string_free($<cbuf>3,TRUE);
618 yyerror(_("parse error"));
624 onerror: ONERROR numtok { $<id>$ = $<id>2; }
625 | ONERROR '{' CCODE {
626 $<id>$ = ($<cbuf>3)->str;
627 g_string_free($<cbuf>3,FALSE);
632 funcargs: VOID { vararg = FALSE; has_self = FALSE; }
636 if(strcmp($<id>1,"self")==0)
640 yyerror(_("parse error"));
644 | TOKEN ',' arglist {
646 if(strcmp($<id>1,"self")==0)
650 yyerror(_("parse error"));
654 | arglist { has_self = FALSE; }
657 arglist: arglist1 ',' THREEDOTS { vararg = TRUE; }
658 | arglist1 { vararg = FALSE; }
661 arglist1: arglist1 ',' arg { ; }
666 push_funcarg($<id>2,NULL);
668 | type TOKEN ARRAY_DIM {
669 push_funcarg($<id>2,$<id>3);
671 | type TOKEN '(' TOKEN checklist ')' {
672 if(strcmp($<id>4,"check")!=0) {
673 yyerror(_("parse error"));
677 push_funcarg($<id>2,NULL);
679 | type TOKEN ARRAY_DIM '(' TOKEN checklist ')' {
680 if(strcmp($<id>5,"check")!=0) {
681 yyerror(_("parse error"));
685 push_funcarg($<id>2,$<id>3);
689 checklist: checklist check { ; }
694 if(strcmp($<id>1,"type")==0) {
695 Node *node = new_check(TYPE_CHECK,NULL);
696 checks = g_list_append(checks,node);
697 } else if(strcmp($<id>1,"null")==0) {
698 Node *node = new_check(NULL_CHECK,NULL);
699 checks = g_list_append(checks,node);
701 yyerror(_("parse error"));
707 Node *node = new_check(GT_CHECK,$<id>2);
708 checks = g_list_append(checks,node);
711 Node *node = new_check(LT_CHECK,$<id>2);
712 checks = g_list_append(checks,node);
715 Node *node = new_check(GE_CHECK,$<id>3);
716 checks = g_list_append(checks,node);
719 Node *node = new_check(LE_CHECK,$<id>3);
720 checks = g_list_append(checks,node);
723 Node *node = new_check(EQ_CHECK,$<id>3);
724 checks = g_list_append(checks,node);
727 Node *node = new_check(NE_CHECK,$<id>3);
728 checks = g_list_append(checks,node);
732 numtok: NUMBER { $<id>$ = $<id>1; }
734 $<id>$ = g_strconcat("-",$<id>2,NULL);
737 | TOKEN { $<id>$ = $<id>1; }