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;
52 extern int ccode_line;
63 if(strcmp(yytext,"\n")==0) {
64 out=g_strconcat("Error: ",str," before newline",NULL);
65 } else if(yytext[0]=='\0') {
66 out=g_strconcat("Error: ",str," at end of input",NULL);
68 char *tmp = g_strdup(yytext);
69 while((p=strchr(tmp,'\n')))
72 out=g_strconcat("Error: ",str," before '",tmp,"'",NULL);
76 fprintf(stderr,"%s:%d: %s\n",filename,line_no,out);
83 push_variable(char *name, int scope, int line_no)
86 Type *type = typestack->data;
87 typestack = g_list_remove(typestack,typestack->data);
89 var = new_variable(scope,type,name,line_no);
90 class_nodes = g_list_append(class_nodes, var);
94 push_function(int scope, char *oid, char *id, char *onerror,
95 GString *cbuf,int line_no, int ccode_line, int vararg)
100 if(scope!=INIT_METHOD && scope!=CLASS_INIT_METHOD) {
101 type = typestack->data;
102 typestack = g_list_remove(typestack,typestack->data);
104 type = (Type *)new_type(0,g_strdup("void"));
107 node = new_method(scope,type,oid,gtktypes,id,funcargs,
108 onerror,cbuf,line_no,ccode_line,vararg);
112 class_nodes = g_list_append(class_nodes, node);
116 push_funcarg(char *name)
119 Type *type = typestack->data;
120 typestack = g_list_remove(typestack,typestack->data);
122 node = new_funcarg(type,name,checks);
125 funcargs = g_list_append(funcargs, node);
129 push_init_arg(char *name, int is_class)
136 tn = g_strconcat(((Class *)class)->otype,":Class",NULL);
138 tn = g_strdup(((Class *)class)->otype);
140 type = new_type(1,tn);
141 node = new_funcarg((Type *)type,name,NULL);
142 funcargs = g_list_prepend(funcargs, node);
151 type = new_type(1,g_strdup(((Class *)class)->otype));
152 ch = g_list_append(ch,new_check(NULL_CHECK,NULL));
153 ch = g_list_append(ch,new_check(TYPE_CHECK,NULL));
154 node = new_funcarg((Type *)type,id,ch);
155 funcargs = g_list_prepend(funcargs, node);
169 %token CONST VOID STRUCT UNION ENUM THREEDOTS
170 %token SIGNED UNSIGNED LONG SHORT INT FLOAT DOUBLE CHAR
173 %token <id> TOKEN NUMBER TYPETOKEN
174 %token <cbuf> CCODE HCODE
175 %token <line> PUBLIC PRIVATE ARGUMENT VIRTUAL SIGNAL OVERRIDE
179 prog: ccodes class ccodes { ; }
185 ccodes: ccodes CCODE {
186 Node *node = new_ccode(FALSE,$<cbuf>2,ccode_line);
187 nodes = g_list_append(nodes,node);
190 Node *node = new_ccode(TRUE,$<cbuf>2,ccode_line);
191 nodes = g_list_append(nodes,node);
194 Node *node = new_ccode(FALSE,$<cbuf>1,ccode_line);
195 nodes = g_list_append(nodes,node);
198 Node *node = new_ccode(TRUE,$<cbuf>1,ccode_line);
199 nodes = g_list_append(nodes,node);
203 class: classdec '{' classcode '}' {
204 ((Class *)class)->nodes = class_nodes;
206 nodes = g_list_append(nodes,class);
210 classdec: CLASS TYPETOKEN FROM TYPETOKEN {
211 class = new_class($<id>2,$<id>4,NULL);
215 classcode: classcode method { ; }
216 | classcode variable { ; }
217 | classcode argument { ; }
223 variable: PUBLIC type TOKEN ';' {
224 push_variable($<id>3,PUBLIC_SCOPE,$<line>1);
226 | PRIVATE type TOKEN ';' {
227 push_variable($<id>3,PRIVATE_SCOPE,$<line>1);
230 argument: ARGUMENT argflags TOKEN TOKEN TOKEN '{' CCODE TOKEN '{' CCODE ';' {
231 if(strcmp($<id>5,"get")==0 &&
232 strcmp($<id>8,"set")==0) {
234 g_free($<id>5); g_free($<id>8);
235 node = new_argument($<id>3,$<list>2,$<id>4,
239 class_nodes = g_list_append(class_nodes,node);
240 } else if(strcmp($<id>5,"set")==0 &&
241 strcmp($<id>8,"get")==0) {
243 g_free($<id>5); g_free($<id>8);
244 node = new_argument($<id>3,$<list>2,$<id>4,
248 class_nodes = g_list_append(class_nodes,node);
250 g_free($<id>3); g_free($<id>4);
251 g_free($<id>5); g_free($<id>8);
252 g_list_foreach($<list>2,(GFunc)g_free,NULL);
253 g_string_free($<cbuf>10,TRUE);
254 g_string_free($<cbuf>7,TRUE);
255 yyerror(_("parse error"));
259 | ARGUMENT argflags TOKEN TOKEN TOKEN '{' CCODE ';' {
260 if(strcmp($<id>5,"get")==0) {
263 node = new_argument($<id>3,$<list>2,$<id>4,
264 $<cbuf>7,$<line>6,NULL,0,
266 class_nodes = g_list_append(class_nodes,node);
267 } else if(strcmp($<id>5,"set")==0) {
270 node = new_argument($<id>3,$<list>2,$<id>4,
271 NULL,0,$<cbuf>7,$<line>6,
273 class_nodes = g_list_append(class_nodes,node);
275 g_free($<id>5); g_free($<id>3);
277 g_list_foreach($<list>2,(GFunc)g_free,NULL);
278 g_string_free($<cbuf>7,TRUE);
279 yyerror(_("parse error"));
285 argflags: '(' flaglist ')' { $<list>$ = $<list>2; }
286 | { $<list>$ = NULL; }
289 flaglist: TOKEN '|' flaglist {
290 $<list>$ = g_list_append($<list>3,$<id>1);
293 $<list>$ = g_list_append(NULL,$<id>1);
300 Type *type = typestack->data;
301 char *oldname = type->name;
302 type->name = g_strconcat("const ",oldname,NULL);
308 Node *node = new_type(0,$<id>1);
309 typestack = g_list_prepend(typestack,node);
312 Node *node = new_type(stars,$<id>1);
314 typestack = g_list_prepend(typestack,node);
318 type2: UNSIGNED integer {
319 $<id>$ = g_strconcat("unsigned ",$<id>2,NULL);
322 $<id>$ = g_strconcat("signed ",$<id>2,NULL);
325 $<id>$ = g_strdup($<id>1);
328 $<id>$ = g_strdup("unsigned char");
331 $<id>$ = g_strdup("signed char");
334 $<id>$ = g_strdup("char");
337 $<id>$ = g_strdup("double");
340 $<id>$ = g_strdup("float");
346 $<id>$ = g_strconcat($<id>1,$<id>2,NULL);
353 $<id>$ = g_strdup("void");
364 $<id>$ = "short int";
385 stars: '*' stars { stars++; }
389 fullsigtype: PRIVATE TOKEN sigtype {
390 if(strcmp($<id>2,"first")==0)
391 $<sigtype>$ = PRIVATE_SIGNAL_FIRST_METHOD;
392 else if(strcmp($<id>2,"last")==0)
393 $<sigtype>$ = PRIVATE_SIGNAL_LAST_METHOD;
395 yyerror(_("signal must be 'first' or 'last'"));
401 | TOKEN PRIVATE sigtype {
402 if(strcmp($<id>1,"first")==0)
403 $<sigtype>$ = PRIVATE_SIGNAL_FIRST_METHOD;
404 else if(strcmp($<id>1,"last")==0)
405 $<sigtype>$ = PRIVATE_SIGNAL_LAST_METHOD;
407 yyerror(_("signal must be 'first' or 'last'"));
414 $<sigtype>$ = PRIVATE_SIGNAL_LAST_METHOD;
417 if(strcmp($<id>1,"first")==0)
418 $<sigtype>$ = SIGNAL_FIRST_METHOD;
419 else if(strcmp($<id>1,"last")==0)
420 $<sigtype>$ = SIGNAL_LAST_METHOD;
422 yyerror(_("signal must be 'first' or 'last'"));
429 $<sigtype>$ = SIGNAL_LAST_METHOD;
433 sigtype: TOKEN '(' tokenlist ')' {
434 gtktypes = g_list_prepend(gtktypes,$<id>1);
438 tokenlist: tokenlist ',' TOKEN {
439 gtktypes = g_list_append(gtktypes,$<id>3);
442 gtktypes = g_list_append(gtktypes,$<id>1);
446 codenocode: '{' CCODE { $<cbuf>$=$<cbuf>2; }
447 | ';' { $<cbuf>$ = NULL; }
450 /*here CCODE will include the ending '}' */
451 method: SIGNAL fullsigtype type TOKEN '(' funcargs ')' onerror codenocode {
453 yyerror(_("signal without 'self' as "
457 push_function($<sigtype>2,NULL,
458 $<id>4, $<id>8, $<cbuf>9,$<line>1,
461 | VIRTUAL PRIVATE type TOKEN '(' funcargs ')' onerror codenocode {
463 yyerror(_("virtual method without 'self' as "
467 push_function(PRIVATE_VIRTUAL_METHOD, NULL, $<id>4,
468 $<id>8, $<cbuf>9,$<line>1,
471 | VIRTUAL type TOKEN '(' funcargs ')' onerror codenocode {
473 yyerror(_("virtual method without 'self' as "
477 push_function(VIRTUAL_METHOD, NULL, $<id>3,
478 $<id>7, $<cbuf>8,$<line>1,
481 | OVERRIDE '(' TYPETOKEN ')' type TOKEN '(' funcargs ')' onerror '{' CCODE {
482 push_function(OVERRIDE_METHOD, $<id>3,
483 $<id>6, $<id>10, $<cbuf>12,
487 | PUBLIC type TOKEN '(' funcargs ')' onerror '{' CCODE {
488 push_function(PUBLIC_SCOPE, NULL, $<id>3,
489 $<id>7, $<cbuf>9,$<line>1,$<line>8,
492 | PRIVATE type TOKEN '(' funcargs ')' onerror '{' CCODE {
493 push_function(PRIVATE_SCOPE, NULL, $<id>3,
494 $<id>7, $<cbuf>9,$<line>1,$<line>8,
497 | TOKEN '(' TOKEN ')' codenocode {
498 if(strcmp($<id>1,"init")==0) {
499 push_init_arg($<id>3,FALSE);
500 push_function(INIT_METHOD, NULL,
501 $<id>1, NULL, $<cbuf>5,$<line>2,
503 } else if(strcmp($<id>1,"class_init")==0) {
504 push_init_arg($<id>3,TRUE);
505 push_function(CLASS_INIT_METHOD, NULL,
506 $<id>1, NULL, $<cbuf>5,$<line>2,
511 g_string_free($<cbuf>3,TRUE);
512 yyerror(_("parse error"));
518 onerror: ONERROR numtok { $<id>$ = $<id>2; }
519 | ONERROR '{' CCODE {
520 $<id>$ = ($<cbuf>3)->str;
521 g_string_free($<cbuf>3,FALSE);
527 funcargs: VOID { vararg = FALSE; has_self = FALSE; }
531 if(strcmp($<id>1,"self")==0)
535 yyerror(_("parse error"));
539 | TOKEN ',' arglist {
541 if(strcmp($<id>1,"self")==0)
545 yyerror(_("parse error"));
549 | arglist { has_self = FALSE; }
552 arglist: arglist1 ',' THREEDOTS { vararg = TRUE; }
553 | arglist1 { vararg = FALSE; }
556 arglist1: arglist1 ',' arg { ; }
561 push_funcarg($<id>2);
563 | type TOKEN '(' TOKEN checklist ')' {
564 if(strcmp($<id>4,"check")!=0) {
565 yyerror(_("parse error"));
569 push_funcarg($<id>2);
573 checklist: checklist check { ; }
578 if(strcmp($<id>1,"type")==0) {
579 Node *node = new_check(TYPE_CHECK,NULL);
580 checks = g_list_append(checks,node);
581 } else if(strcmp($<id>1,"null")==0) {
582 Node *node = new_check(NULL_CHECK,NULL);
583 checks = g_list_append(checks,node);
585 yyerror(_("parse error"));
591 Node *node = new_check(GT_CHECK,$<id>2);
592 checks = g_list_append(checks,node);
595 Node *node = new_check(LT_CHECK,$<id>2);
596 checks = g_list_append(checks,node);
599 Node *node = new_check(GE_CHECK,$<id>3);
600 checks = g_list_append(checks,node);
603 Node *node = new_check(LE_CHECK,$<id>3);
604 checks = g_list_append(checks,node);
607 Node *node = new_check(EQ_CHECK,$<id>3);
608 checks = g_list_append(checks,node);
611 Node *node = new_check(NE_CHECK,$<id>3);
612 checks = g_list_append(checks,node);
616 numtok: NUMBER { $<id>$ = $<id>1; }
618 $<id>$ = g_strconcat("-",$<id>2,NULL);
621 | TOKEN { $<id>$ = $<id>1; }