1 /**************************************************************************
4 * Author: Maria M. Gutierrez *
5 * Modified by: Kevin Hammond *
6 * Last date revised: December 13 1991. KH. *
7 * Modification: Haskell 1.1 Syntax. *
10 * Description: This file contains the LALR(1) grammar for Haskell. *
12 * Entry Point: module *
14 * Problems: None known. *
17 * LALR(1) Syntax for Haskell 1.2 *
19 **************************************************************************/
31 #include "constants.h"
34 /**********************************************************************
37 * Imported Variables and Functions *
40 **********************************************************************/
42 static BOOLEAN expect_ccurly = FALSE; /* Used to signal that a CCURLY could be inserted here */
45 extern char *input_filename;
46 static char *the_module_name;
47 static maybe module_exports;
52 /* For FN, PREVPATT and SAMEFN macros */
54 extern BOOLEAN samefn[];
55 extern tree prevpatt[];
56 extern short icontexts;
59 extern int hsplineno, hspcolno;
60 extern int modulelineno;
61 extern int startlineno;
64 /**********************************************************************
67 * Fixity and Precedence Declarations *
70 **********************************************************************/
72 static int Fixity = 0, Precedence = 0;
74 char *ineg PROTO((char *));
76 long source_version = 0;
101 /**********************************************************************
104 * These are lexemes. *
107 **********************************************************************/
110 %token VARID CONID QVARID QCONID
111 VARSYM CONSYM QVARSYM QCONSYM
113 %token INTEGER FLOAT CHAR STRING
114 CHARPRIM STRINGPRIM INTPRIM FLOATPRIM
119 /**********************************************************************
125 **********************************************************************/
127 %token OCURLY CCURLY VCCURLY SEMI
128 %token OBRACK CBRACK OPAREN CPAREN
132 /**********************************************************************
135 * Reserved Operators *
138 **********************************************************************/
140 %token DOTDOT DCOLON EQUAL
141 %token LAMBDA VBAR RARROW
145 /**********************************************************************
148 * Reserved Identifiers *
151 **********************************************************************/
153 %token CASE CLASS DATA
154 %token DEFAULT DERIVING DO
155 %token ELSE IF IMPORT
156 %token IN INFIX INFIXL
157 %token INFIXR INSTANCE LET
158 %token MODULE NEWTYPE OF
159 %token THEN TYPE WHERE
162 %token CCALL CCALL_GC CASM CASM_GC
165 /**********************************************************************
168 * Valid symbols/identifiers which need to be recognised *
171 **********************************************************************/
173 %token WILDCARD AT LAZY BANG
174 %token AS HIDING QUALIFIED
177 /**********************************************************************
180 * Special Symbols for the Lexer *
183 **********************************************************************/
185 %token INTERFACE_UPRAGMA SPECIALISE_UPRAGMA
186 %token INLINE_UPRAGMA MAGIC_UNFOLDING_UPRAGMA
187 %token DEFOREST_UPRAGMA END_UPRAGMA
189 /**********************************************************************
192 * Precedences of the various tokens *
195 **********************************************************************/
200 SCC CASM CCALL CASM_GC CCALL_GC
202 %left VARSYM CONSYM QVARSYM QCONSYM
203 MINUS BQUOTE BANG DARROW
209 %left OCURLY OBRACK OPAREN
215 /**********************************************************************
218 * Type Declarations *
221 **********************************************************************/
224 %type <ulist> caserest alts alt quals
226 rbinds rpats list_exps
228 constrs constr1 fields
231 pats context context_list tyvar_list
234 impdecls maybeimpdecls impdecl
235 maybefixes fixes fix ops
240 %type <umaybe> maybeexports impas maybeimpspec deriving
242 %type <ueither> impspec
244 %type <uliteral> lit_constant
246 %type <utree> exp oexp dexp kexp fexp aexp rbind texps
247 expL oexpL kexpL expLno oexpLno dexpLno kexpLno
249 apat bpat pat apatc conpat dpat fpat opat aapat
250 dpatk fpatk opatk aapatk rpat
253 %type <uid> MINUS DARROW AS LAZY
254 VARID CONID VARSYM CONSYM
255 var con varop conop op
256 vark varid varsym varsym_nominus
257 tycon modid impmod ccallid
259 %type <uqid> QVARID QCONID QVARSYM QCONSYM
260 qvarid qconid qvarsym qconsym
261 qvar qcon qvarop qconop qop
262 qvark qconk qtycon qtycls
263 gcon gconk gtycon qop1 qvarop1
266 %type <ubinding> topdecl topdecls letdecls
267 typed datad newtd classd instd defaultd
268 decl decls valdef instdef instdefs
269 maybe_where cbody rinst type_and_maybe_id
271 %type <upbinding> valrhs1 altrest
273 %type <uttype> simple ctype type atype btype
274 gtyconapp ntyconapp ntycon gtyconvars
275 bbtype batype btyconapp
276 class restrict_inst general_inst tyvar
278 %type <uconstr> constr field
280 %type <ustring> FLOAT INTEGER INTPRIM
281 FLOATPRIM DOUBLEPRIM CLITLIT
283 %type <uhstring> STRING STRINGPRIM CHAR CHARPRIM
285 %type <uentid> export import
287 %type <ulong> commas impqual
289 /**********************************************************************
292 * Start Symbol for the Parser *
295 **********************************************************************/
300 module : modulekey modid maybeexports
302 modulelineno = startlineno;
303 the_module_name = $2;
309 the_module_name = install_literal("Main");
310 module_exports = mknothing();
315 body : ocurly { setstartlineno(); } interface_pragma orestm
316 | vocurly interface_pragma vrestm
319 interface_pragma : /* empty */
320 | INTERFACE_UPRAGMA INTEGER END_UPRAGMA SEMI
322 source_version = atoi($2);
326 orestm : maybeimpdecls maybefixes topdecls ccurly
328 root = mkhmodule(the_module_name,$1,module_exports,
329 $2,$3,source_version,modulelineno);
333 root = mkhmodule(the_module_name,$1,module_exports,
334 Lnil,mknullbind(),source_version,modulelineno);
337 vrestm : maybeimpdecls maybefixes topdecls vccurly
339 root = mkhmodule(the_module_name,$1,module_exports,
340 $2,$3,source_version,modulelineno);
344 root = mkhmodule(the_module_name,$1,module_exports,
345 Lnil,mknullbind(),source_version,modulelineno);
348 maybeexports : /* empty */ { $$ = mknothing(); }
349 | OPAREN export_list CPAREN { $$ = mkjust($2); }
350 | OPAREN export_list COMMA CPAREN { $$ = mkjust($2); }
354 export { $$ = lsing($1); }
355 | export_list COMMA export { $$ = lapp($1, $3); }
358 export : qvar { $$ = mkentid($1); }
359 | gtycon { $$ = mkenttype($1); }
360 | gtycon OPAREN DOTDOT CPAREN { $$ = mkenttypeall($1); }
361 | gtycon OPAREN CPAREN { $$ = mkenttypenamed($1,Lnil); }
362 | gtycon OPAREN enames CPAREN { $$ = mkenttypenamed($1,$3); }
363 | MODULE modid { $$ = mkentmod($2); }
366 enames : ename { $$ = lsing($1); }
367 | enames COMMA ename { $$ = lapp($1,$3); }
374 maybeimpdecls : /* empty */ { $$ = Lnil; }
375 | impdecls SEMI { $$ = $1; }
378 impdecls: impdecl { $$ = $1; }
379 | impdecls SEMI impdecl { $$ = lconc($1,$3); }
383 impdecl : importkey impqual impmod impas maybeimpspec
385 $$ = lsing(mkimport($3,$2,$4,$5,startlineno));
389 impmod : modid { $$ = $1; }
392 impqual : /* noqual */ { $$ = 0; }
393 | QUALIFIED { $$ = 1; }
396 impas : /* noas */ { $$ = mknothing(); }
397 | AS modid { $$ = mkjust($2); }
400 maybeimpspec : /* empty */ { $$ = mknothing(); }
401 | impspec { $$ = mkjust($1); }
404 impspec : OPAREN CPAREN { $$ = mkleft(Lnil); }
405 | OPAREN import_list CPAREN { $$ = mkleft($2); }
406 | OPAREN import_list COMMA CPAREN { $$ = mkleft($2); }
407 | HIDING OPAREN import_list CPAREN { $$ = mkright($3); }
408 | HIDING OPAREN import_list COMMA CPAREN { $$ = mkright($3); }
412 import { $$ = lsing($1); }
413 | import_list COMMA import { $$ = lapp($1, $3); }
416 import : var { $$ = mkentid(mknoqual($1)); }
417 | tycon { $$ = mkenttype(mknoqual($1)); }
418 | tycon OPAREN DOTDOT CPAREN { $$ = mkenttypeall(mknoqual($1)); }
419 | tycon OPAREN CPAREN { $$ = mkenttypenamed(mknoqual($1),Lnil); }
420 | tycon OPAREN inames CPAREN { $$ = mkenttypenamed(mknoqual($1),$3); }
423 inames : iname { $$ = lsing($1); }
424 | inames COMMA iname { $$ = lapp($1,$3); }
426 iname : var { $$ = mknoqual($1); }
427 | con { $$ = mknoqual($1); }
430 /**********************************************************************
433 * Fixes and Decls etc *
436 **********************************************************************/
438 maybefixes: /* empty */ { $$ = Lnil; }
439 | fixes SEMI { $$ = $1; }
442 fixes : fix { $$ = $1; }
443 | fixes SEMI fix { $$ = lconc($1,$3); }
446 fix : INFIXL INTEGER { Precedence = checkfixity($2); Fixity = INFIXL; }
448 | INFIXR INTEGER { Precedence = checkfixity($2); Fixity = INFIXR; }
450 | INFIX INTEGER { Precedence = checkfixity($2); Fixity = INFIX; }
452 | INFIXL { Fixity = INFIXL; Precedence = 9; }
454 | INFIXR { Fixity = INFIXR; Precedence = 9; }
456 | INFIX { Fixity = INFIX; Precedence = 9; }
460 ops : op { $$ = lsing(mkfixop(mknoqual($1),infixint(Fixity),Precedence)); }
461 | ops COMMA op { $$ = lapp($1,mkfixop(mknoqual($3),infixint(Fixity),Precedence)); }
465 | topdecls SEMI topdecl
484 topdecl : typed { $$ = $1; }
487 | classd { $$ = $1; }
489 | defaultd { $$ = $1; }
493 typed : typekey simple EQUAL type { $$ = mknbind($2,$4,startlineno); }
497 datad : datakey simple EQUAL constrs deriving
498 { $$ = mktbind(Lnil,$2,$4,$5,startlineno); }
499 | datakey context DARROW simple EQUAL constrs deriving
500 { $$ = mktbind($2,$4,$6,$7,startlineno); }
503 newtd : newtypekey simple EQUAL constr1 deriving
504 { $$ = mkntbind(Lnil,$2,$4,$5,startlineno); }
505 | newtypekey context DARROW simple EQUAL constr1 deriving
506 { $$ = mkntbind($2,$4,$6,$7,startlineno); }
509 deriving: /* empty */ { $$ = mknothing(); }
510 | DERIVING dtyclses { $$ = mkjust($2); }
513 classd : classkey context DARROW class cbody
514 { $$ = mkcbind($2,$4,$5,startlineno); }
515 | classkey class cbody
516 { $$ = mkcbind(Lnil,$2,$3,startlineno); }
519 cbody : /* empty */ { $$ = mknullbind(); }
520 | WHERE ocurly decls ccurly { checkorder($3); $$ = $3; }
521 | WHERE vocurly decls vccurly { checkorder($3); $$ = $3; }
524 instd : instkey context DARROW gtycon restrict_inst rinst
525 { $$ = mkibind($2,$4,$5,$6,startlineno); }
526 | instkey gtycon general_inst rinst
527 { $$ = mkibind(Lnil,$2,$3,$4,startlineno); }
530 rinst : /* empty */ { $$ = mknullbind(); }
531 | WHERE ocurly instdefs ccurly { $$ = $3; }
532 | WHERE vocurly instdefs vccurly { $$ = $3; }
535 restrict_inst : gtycon { $$ = mktname($1); }
536 | OPAREN gtyconvars CPAREN { $$ = $2; }
537 | OPAREN tyvar COMMA tyvar_list CPAREN { $$ = mkttuple(mklcons($2,$4)); }
538 | OBRACK tyvar CBRACK { $$ = mktllist($2); }
539 | OPAREN tyvar RARROW tyvar CPAREN { $$ = mktfun($2,$4); }
542 general_inst : gtycon { $$ = mktname($1); }
543 | OPAREN gtyconapp CPAREN { $$ = $2; }
544 | OPAREN type COMMA types CPAREN { $$ = mkttuple(mklcons($2,$4)); }
545 | OBRACK type CBRACK { $$ = mktllist($2); }
546 | OPAREN btype RARROW type CPAREN { $$ = mktfun($2,$4); }
549 defaultd: defaultkey OPAREN types CPAREN { $$ = mkdbind($3,startlineno); }
550 | defaultkey OPAREN CPAREN { $$ = mkdbind(Lnil,startlineno); }
567 Note: if there is an iclasop_pragma here, then we must be
568 doing a class-op in an interface -- unless the user is up
569 to real mischief (ugly, but likely to work).
572 decl : qvarsk DCOLON ctype
573 { $$ = mksbind($1,$3,startlineno);
574 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
577 /* User-specified pragmas come in as "signatures"...
578 They are similar in that they can appear anywhere in the module,
579 and have to be "joined up" with their related entity.
581 Have left out the case specialising to an overloaded type.
582 Let's get real, OK? (WDP)
584 | SPECIALISE_UPRAGMA qvark DCOLON types_and_maybe_ids END_UPRAGMA
586 $$ = mkvspec_uprag($2, $4, startlineno);
587 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
590 | SPECIALISE_UPRAGMA INSTANCE gtycon general_inst END_UPRAGMA
592 $$ = mkispec_uprag($3, $4, startlineno);
593 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
596 | SPECIALISE_UPRAGMA DATA gtycon atypes END_UPRAGMA
598 $$ = mkdspec_uprag($3, $4, startlineno);
599 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
602 | INLINE_UPRAGMA qvark END_UPRAGMA
604 $$ = mkinline_uprag($2, startlineno);
605 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
608 | MAGIC_UNFOLDING_UPRAGMA qvark vark END_UPRAGMA
610 $$ = mkmagicuf_uprag($2, $3, startlineno);
611 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
614 | DEFOREST_UPRAGMA qvark END_UPRAGMA
616 $$ = mkdeforest_uprag($2, startlineno);
617 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
620 /* end of user-specified pragmas */
623 | /* empty */ { $$ = mknullbind(); PREVPATT = NULL; FN = NULL; SAMEFN = 0; }
626 qvarsk : qvark COMMA qvars_list { $$ = mklcons($1,$3); }
627 | qvark { $$ = lsing($1); }
630 qvars_list: qvar { $$ = lsing($1); }
631 | qvars_list COMMA qvar { $$ = lapp($1,$3); }
634 types_and_maybe_ids :
635 type_and_maybe_id { $$ = lsing($1); }
636 | types_and_maybe_ids COMMA type_and_maybe_id { $$ = lapp($1,$3); }
640 type { $$ = mkvspec_ty_and_id($1,mknothing()); }
641 | type EQUAL qvark { $$ = mkvspec_ty_and_id($1,mkjust($3)); }
644 /**********************************************************************
650 **********************************************************************/
652 /* "DCOLON context => type" vs "DCOLON type" is a problem,
653 because you can't distinguish between
655 foo :: (Baz a, Baz a)
656 bar :: (Baz a, Baz a) => [a] -> [a] -> [a]
658 with one token of lookahead. The HACK is to have "DCOLON ttype"
659 [tuple type] in the first case, then check that it has the right
660 form C a, or (C1 a, C2 b, ... Cn z) and convert it into a
664 /* 1 S/R conflict at DARROW -> shift */
665 ctype : type DARROW type { $$ = mkcontext(type2context($1),$3); }
669 /* 1 S/R conflict at RARROW -> shift */
670 type : btype { $$ = $1; }
671 | btype RARROW type { $$ = mktfun($1,$3); }
674 /* btype is split so we can parse gtyconapp without S/R conflicts */
675 btype : gtyconapp { $$ = $1; }
676 | ntyconapp { $$ = $1; }
679 ntyconapp: ntycon { $$ = $1; }
680 | ntyconapp atype { $$ = mktapp($1,$2); }
683 gtyconapp: gtycon { $$ = mktname($1); }
684 | gtyconapp atype { $$ = mktapp($1,$2); }
688 atype : gtycon { $$ = mktname($1); }
689 | ntycon { $$ = $1; }
692 ntycon : tyvar { $$ = $1; }
693 | OPAREN type COMMA types CPAREN { $$ = mkttuple(mklcons($2,$4)); }
694 | OBRACK type CBRACK { $$ = mktllist($2); }
695 | OPAREN type CPAREN { $$ = $2; }
699 | OPAREN RARROW CPAREN { $$ = creategid(-2); }
700 | OBRACK CBRACK { $$ = creategid(-1); }
701 | OPAREN CPAREN { $$ = creategid(0); }
702 | OPAREN commas CPAREN { $$ = creategid($2); }
705 atypes : atype { $$ = lsing($1); }
706 | atypes atype { $$ = lapp($1,$2); }
709 types : type { $$ = lsing($1); }
710 | types COMMA type { $$ = lapp($1,$3); }
713 commas : COMMA { $$ = 1; }
714 | commas COMMA { $$ = $1 + 1; }
717 /**********************************************************************
720 * Declaration stuff *
723 **********************************************************************/
725 simple : gtycon { $$ = mktname($1); }
726 | gtyconvars { $$ = $1; }
729 gtyconvars: gtycon tyvar { $$ = mktapp(mktname($1),$2); }
730 | gtyconvars tyvar { $$ = mktapp($1,$2); }
733 context : OPAREN context_list CPAREN { $$ = $2; }
734 | class { $$ = lsing($1); }
737 context_list: class { $$ = lsing($1); }
738 | context_list COMMA class { $$ = lapp($1,$3); }
741 class : gtycon tyvar { $$ = mktapp(mktname($1),$2); }
744 constrs : constr { $$ = lsing($1); }
745 | constrs VBAR constr { $$ = lapp($1,$3); }
748 constr : btyconapp { qid tyc; list tys;
749 splittyconapp($1, &tyc, &tys);
750 $$ = mkconstrpre(tyc,tys,hsplineno); }
751 | OPAREN qconsym CPAREN { $$ = mkconstrpre($2,Lnil,hsplineno); }
752 | OPAREN qconsym CPAREN batypes { $$ = mkconstrpre($2,$4,hsplineno); }
753 | btyconapp qconop bbtype { checknobangs($1);
754 $$ = mkconstrinf($1,$2,$3,hsplineno); }
755 | ntyconapp qconop bbtype { $$ = mkconstrinf($1,$2,$3,hsplineno); }
756 | BANG atype qconop bbtype { $$ = mkconstrinf(mktbang($2),$3,$4,hsplineno); }
758 /* 1 S/R conflict on OCURLY -> shift */
759 | gtycon OCURLY fields CCURLY { $$ = mkconstrrec($1,$3,hsplineno); }
762 btyconapp: gtycon { $$ = mktname($1); }
763 | btyconapp batype { $$ = mktapp($1,$2); }
766 bbtype : btype { $$ = $1; }
767 | BANG atype { $$ = mktbang($2); }
770 batype : atype { $$ = $1; }
771 | BANG atype { $$ = mktbang($2); }
774 batypes : batype { $$ = lsing($1); }
775 | batypes batype { $$ = lapp($1,$2); }
779 fields : field { $$ = lsing($1); }
780 | fields COMMA field { $$ = lapp($1,$3); }
783 field : qvars_list DCOLON type { $$ = mkfield($1,$3); }
784 | qvars_list DCOLON BANG atype { $$ = mkfield($1,mktbang($4)); }
787 constr1 : gtycon atype { $$ = lsing(mkconstrnew($1,$2,hsplineno)); }
791 dtyclses: OPAREN dtycls_list CPAREN { $$ = $2; }
792 | OPAREN CPAREN { $$ = Lnil; }
793 | qtycls { $$ = lsing($1); }
796 dtycls_list: qtycls { $$ = lsing($1); }
797 | dtycls_list COMMA qtycls { $$ = lapp($1,$3); }
800 instdefs : /* empty */ { $$ = mknullbind(); }
801 | instdef { $$ = $1; }
802 | instdefs SEMI instdef
814 /* instdef: same as valdef, except certain user-pragmas may appear */
816 SPECIALISE_UPRAGMA qvark DCOLON types_and_maybe_ids END_UPRAGMA
818 $$ = mkvspec_uprag($2, $4, startlineno);
819 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
822 | INLINE_UPRAGMA qvark END_UPRAGMA
824 $$ = mkinline_uprag($2, startlineno);
825 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
828 | MAGIC_UNFOLDING_UPRAGMA qvark vark END_UPRAGMA
830 $$ = mkmagicuf_uprag($2, $3, startlineno);
831 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
840 tree fn = function($1);
843 if(ttree(fn) == ident)
845 qid fun_id = gident((struct Sident *) fn);
850 else if (ttree(fn) == infixap)
852 qid fun_id = ginffun((struct Sinfixap *) fn);
859 printf("%u\n",startlineno);
861 fprintf(stderr,"%u\tvaldef\n",startlineno);
866 if ( lhs_is_patt($1) )
868 $$ = mkpbind($3, startlineno);
872 else /* lhs is function */
873 $$ = mkfbind($3,startlineno);
879 valrhs : valrhs1 maybe_where { $$ = lsing(createpat($1, $2)); }
882 valrhs1 : gdrhs { $$ = mkpguards($1); }
883 | EQUAL exp { $$ = mkpnoguards($2); }
886 gdrhs : gd EQUAL exp { $$ = lsing(mkpgdexp($1,$3)); }
887 | gd EQUAL exp gdrhs { $$ = mklcons(mkpgdexp($1,$3),$4); }
891 WHERE ocurly decls ccurly { $$ = $3; }
892 | WHERE vocurly decls vccurly { $$ = $3; }
893 | /* empty */ { $$ = mknullbind(); }
896 gd : VBAR oexp { $$ = $2; }
900 /**********************************************************************
906 **********************************************************************/
908 exp : oexp DCOLON ctype { $$ = mkrestr($1,$3); }
913 Operators must be left-associative at the same precedence for
914 precedence parsing to work.
916 /* 9 S/R conflicts on qop -> shift */
917 oexp : oexp qop oexp %prec MINUS { $$ = mkinfixap($2,$1,$3); }
922 This comes here because of the funny precedence rules concerning
925 dexp : MINUS kexp { $$ = mknegate($2); }
930 We need to factor out a leading let expression so we can set
931 inpat=TRUE when parsing (non let) expressions inside stmts and quals
933 expLno : oexpLno DCOLON ctype { $$ = mkrestr($1,$3); }
936 oexpLno : oexpLno qop oexp %prec MINUS { $$ = mkinfixap($2,$1,$3); }
939 dexpLno : MINUS kexp { $$ = mknegate($2); }
943 expL : oexpL DCOLON ctype { $$ = mkrestr($1,$3); }
946 oexpL : oexpL qop oexp %prec MINUS { $$ = mkinfixap($2,$1,$3); }
951 let/if/lambda/case have higher precedence than infix operators.
958 kexpL : letdecls IN exp { $$ = mklet($1,$3); }
962 { hsincindent(); /* push new context for FN = NULL; */
963 FN = NULL; /* not actually concerned about indenting */
964 $<ulong>$ = hsplineno; /* remember current line number */
969 RARROW exp /* lambda abstraction */
971 $$ = mklambda($3, $6, $<ulong>2);
975 | IF {$<ulong>$ = hsplineno;}
976 exp THEN exp ELSE exp { $$ = mkife($3,$5,$7,$<ulong>2); }
978 /* Case Expression */
979 | CASE {$<ulong>$ = hsplineno;}
980 exp OF caserest { $$ = mkcasee($3,$5,$<ulong>2); }
983 | DO {$<ulong>$ = hsplineno;}
984 dorest { $$ = mkdoe($3,$<ulong>2); }
986 /* CCALL/CASM Expression */
987 | CCALL ccallid cexps { $$ = mkccall($2,install_literal("n"),$3); }
988 | CCALL ccallid { $$ = mkccall($2,install_literal("n"),Lnil); }
989 | CCALL_GC ccallid cexps { $$ = mkccall($2,install_literal("p"),$3); }
990 | CCALL_GC ccallid { $$ = mkccall($2,install_literal("p"),Lnil); }
991 | CASM CLITLIT cexps { $$ = mkccall($2,install_literal("N"),$3); }
992 | CASM CLITLIT { $$ = mkccall($2,install_literal("N"),Lnil); }
993 | CASM_GC CLITLIT cexps { $$ = mkccall($2,install_literal("P"),$3); }
994 | CASM_GC CLITLIT { $$ = mkccall($2,install_literal("P"),Lnil); }
1007 fexp : fexp aexp { $$ = mkap($1,$2); }
1011 /* simple expressions */
1012 aexp : qvar { $$ = mkident($1); }
1013 | gcon { $$ = mkident($1); }
1014 | lit_constant { $$ = mklit($1); }
1015 | OPAREN exp CPAREN { $$ = mkpar($2); } /* mkpar: stop infix parsing at ()'s */
1016 | qcon OCURLY CCURLY { $$ = mkrecord($1,Lnil); }
1017 | qcon OCURLY rbinds CCURLY { $$ = mkrecord($1,$3); } /* 1 S/R conflict on OCURLY -> shift */
1018 | OBRACK list_exps CBRACK { $$ = mkllist($2); }
1019 | OPAREN exp COMMA texps CPAREN { if (ttree($4) == tuple)
1020 $$ = mktuple(mklcons($2, gtuplelist((struct Stuple *) $4)));
1022 $$ = mktuple(ldub($2, $4)); }
1024 /* only in expressions ... */
1025 | aexp OCURLY rbinds CCURLY { $$ = mkrupdate($1,$3); }
1026 | OBRACK exp VBAR quals CBRACK { $$ = mkcomprh($2,$4); }
1027 | OBRACK exp COMMA exp DOTDOT exp CBRACK {$$= mkeenum($2,mkjust($4),mkjust($6)); }
1028 | OBRACK exp COMMA exp DOTDOT CBRACK { $$ = mkeenum($2,mkjust($4),mknothing()); }
1029 | OBRACK exp DOTDOT exp CBRACK { $$ = mkeenum($2,mknothing(),mkjust($4)); }
1030 | OBRACK exp DOTDOT CBRACK { $$ = mkeenum($2,mknothing(),mknothing()); }
1031 | OPAREN oexp qop CPAREN { $$ = mklsection($2,$3); }
1032 | OPAREN qop1 oexp CPAREN { $$ = mkrsection($2,$3); }
1034 /* only in patterns ... */
1035 /* these add 2 S/R conflict with with aexp . OCURLY rbinds CCURLY */
1036 | qvar AT aexp { checkinpat(); $$ = mkas($1,$3); }
1037 | LAZY aexp { checkinpat(); $$ = mklazyp($2); }
1038 | WILDCARD { checkinpat(); $$ = mkwildp(); }
1041 /* ccall arguments */
1042 cexps : cexps aexp { $$ = lapp($1,$2); }
1043 | aexp { $$ = lsing($1); }
1046 caserest: ocurly alts ccurly { $$ = $2; }
1047 | vocurly alts vccurly { $$ = $2; }
1049 dorest : ocurly stmts ccurly { checkdostmts($2); $$ = $2; }
1050 | vocurly stmts vccurly { checkdostmts($2); $$ = $2; }
1053 rbinds : rbind { $$ = lsing($1); }
1054 | rbinds COMMA rbind { $$ = lapp($1,$3); }
1057 rbind : qvar { $$ = mkrbind($1,mknothing()); }
1058 | qvar EQUAL exp { $$ = mkrbind($1,mkjust($3)); }
1061 texps : exp { $$ = mkpar($1); } /* mkpar: so we don't flatten last element in tuple */
1063 { if (ttree($3) == tuple)
1064 $$ = mktuple(mklcons($1, gtuplelist((struct Stuple *) $3)));
1065 else if (ttree($3) == par)
1066 $$ = mktuple(ldub($1, gpare((struct Spar *) $3)));
1068 hsperror("hsparser:texps: panic");
1070 /* right recursion? WDP */
1075 exp { $$ = lsing($1); }
1076 | exp COMMA list_exps { $$ = mklcons($1, $3); }
1077 /* right recursion? (WDP)
1079 It has to be this way, though, otherwise you
1080 may do the wrong thing to distinguish between...
1082 [ e1 , e2 .. ] -- an enumeration ...
1083 [ e1 , e2 , e3 ] -- a list
1085 (In fact, if you change the grammar and throw yacc/bison
1086 at it, it *will* do the wrong thing [WDP 94/06])
1090 letdecls: LET ocurly decls ccurly { $$ = $3 }
1091 | LET vocurly decls vccurly { $$ = $3 }
1094 quals : qual { $$ = lsing($1); }
1095 | quals COMMA qual { $$ = lapp($1,$3); }
1098 qual : letdecls { $$ = mkseqlet($1); }
1100 | {inpat=TRUE;} expLno {inpat=FALSE;}leftexp
1102 expORpat(LEGIT_EXPR,$2);
1105 expORpat(LEGIT_PATT,$2);
1111 alts : alt { $$ = $1; }
1112 | alts SEMI alt { $$ = lconc($1,$3); }
1115 alt : pat { PREVPATT = $1; } altrest { $$ = lsing($3); PREVPATT = NULL; }
1116 | /* empty */ { $$ = Lnil; }
1119 altrest : gdpat maybe_where { $$ = createpat(mkpguards($1), $2); }
1120 | RARROW exp maybe_where { $$ = createpat(mkpnoguards($2),$3); }
1123 gdpat : gd RARROW exp { $$ = lsing(mkpgdexp($1,$3)); }
1124 | gd RARROW exp gdpat { $$ = mklcons(mkpgdexp($1,$3),$4); }
1127 stmts : stmt { $$ = $1; }
1128 | stmts SEMI stmt { $$ = lconc($1,$3); }
1131 stmt : /* empty */ { $$ = Lnil; }
1132 | letdecls { $$ = lsing(mkseqlet($1)); }
1133 | expL { $$ = lsing($1); }
1134 | {inpat=TRUE;} expLno {inpat=FALSE;} leftexp
1136 expORpat(LEGIT_EXPR,$2);
1137 $$ = lsing(mkdoexp($2,endlineno));
1139 expORpat(LEGIT_PATT,$2);
1140 $$ = lsing(mkdobind($2,$4,endlineno));
1145 leftexp : LARROW exp { $$ = $2; }
1146 | /* empty */ { $$ = NULL; }
1149 /**********************************************************************
1155 **********************************************************************/
1158 The xpatk business is to do with accurately recording
1159 the starting line for definitions.
1163 | opatk qop opat %prec MINUS { $$ = mkinfixap($2,$1,$3); }
1167 | opat qop opat %prec MINUS { $$ = mkinfixap($2,$1,$3); }
1171 This comes here because of the funny precedence rules concerning
1176 dpat : MINUS fpat { $$ = mknegate($2); }
1180 /* Function application */
1181 fpat : fpat aapat { $$ = mkap($1,$2); }
1185 dpatk : minuskey fpat { $$ = mknegate($2); }
1189 /* Function application */
1190 fpatk : fpatk aapat { $$ = mkap($1,$2); }
1194 aapat : qvar { $$ = mkident($1); }
1195 | qvar AT apat { $$ = mkas($1,$3); }
1196 | gcon { $$ = mkident($1); }
1197 | qcon OCURLY rpats CCURLY { $$ = mkrecord($1,$3); }
1198 | lit_constant { $$ = mklit($1); }
1199 | WILDCARD { $$ = mkwildp(); }
1200 | OPAREN opat CPAREN { $$ = mkpar($2); }
1201 | OPAREN opat COMMA pats CPAREN { $$ = mktuple(mklcons($2,$4)); }
1202 | OBRACK pats CBRACK { $$ = mkllist($2); }
1203 | LAZY apat { $$ = mklazyp($2); }
1207 aapatk : qvark { $$ = mkident($1); }
1208 | qvark AT apat { $$ = mkas($1,$3); }
1209 | gconk { $$ = mkident($1); }
1210 | qconk OCURLY rpats CCURLY { $$ = mkrecord($1,$3); }
1211 | lit_constant { $$ = mklit($1); setstartlineno(); }
1212 | WILDCARD { $$ = mkwildp(); setstartlineno(); }
1213 | oparenkey opat CPAREN { $$ = mkpar($2); }
1214 | oparenkey opat COMMA pats CPAREN { $$ = mktuple(mklcons($2,$4)); }
1215 | obrackkey pats CBRACK { $$ = mkllist($2); }
1216 | lazykey apat { $$ = mklazyp($2); }
1220 | OBRACK CBRACK { $$ = creategid(-1); }
1221 | OPAREN CPAREN { $$ = creategid(0); }
1222 | OPAREN commas CPAREN { $$ = creategid($2); }
1226 | obrackkey CBRACK { $$ = creategid(-1); }
1227 | oparenkey CPAREN { $$ = creategid(0); }
1228 | oparenkey commas CPAREN { $$ = creategid($2); }
1231 lampats : apat lampats { $$ = mklcons($1,$2); }
1232 | apat { $$ = lsing($1); }
1233 /* right recursion? (WDP) */
1236 pats : pat COMMA pats { $$ = mklcons($1, $3); }
1237 | pat { $$ = lsing($1); }
1238 /* right recursion? (WDP) */
1241 pat : pat qconop bpat { $$ = mkinfixap($2,$1,$3); }
1247 | qcon OCURLY rpats CCURLY { $$ = mkrecord($1,$3); }
1248 | MINUS INTEGER { $$ = mklit(mkinteger(ineg($2))); }
1249 | MINUS FLOAT { $$ = mklit(mkfloatr(ineg($2))); }
1252 conpat : gcon { $$ = mkident($1); }
1253 | conpat apat { $$ = mkap($1,$2); }
1256 apat : gcon { $$ = mkident($1); }
1257 | qcon OCURLY rpats CCURLY { $$ = mkrecord($1,$3); }
1261 apatc : qvar { $$ = mkident($1); }
1262 | qvar AT apat { $$ = mkas($1,$3); }
1263 | lit_constant { $$ = mklit($1); }
1264 | WILDCARD { $$ = mkwildp(); }
1265 | OPAREN pat CPAREN { $$ = mkpar($2); }
1266 | OPAREN pat COMMA pats CPAREN { $$ = mktuple(mklcons($2,$4)); }
1267 | OBRACK pats CBRACK { $$ = mkllist($2); }
1268 | LAZY apat { $$ = mklazyp($2); }
1272 INTEGER { $$ = mkinteger($1); }
1273 | FLOAT { $$ = mkfloatr($1); }
1274 | CHAR { $$ = mkcharr($1); }
1275 | STRING { $$ = mkstring($1); }
1276 | CHARPRIM { $$ = mkcharprim($1); }
1277 | STRINGPRIM { $$ = mkstringprim($1); }
1278 | INTPRIM { $$ = mkintprim($1); }
1279 | FLOATPRIM { $$ = mkfloatprim($1); }
1280 | DOUBLEPRIM { $$ = mkdoubleprim($1); }
1281 | CLITLIT /* yurble yurble */ { $$ = mkclitlit($1); }
1284 rpats : rpat { $$ = lsing($1); }
1285 | rpats COMMA rpat { $$ = lapp($1,$3); }
1288 rpat : qvar { $$ = mkrbind($1,mknothing()); }
1289 | qvar EQUAL pat { $$ = mkrbind($1,mkjust($3)); }
1293 /**********************************************************************
1296 * Keywords which record the line start *
1299 **********************************************************************/
1301 importkey: IMPORT { setstartlineno(); }
1304 datakey : DATA { setstartlineno();
1307 printf("%u\n",startlineno);
1309 fprintf(stderr,"%u\tdata\n",startlineno);
1314 typekey : TYPE { setstartlineno();
1317 printf("%u\n",startlineno);
1319 fprintf(stderr,"%u\ttype\n",startlineno);
1324 newtypekey : NEWTYPE { setstartlineno();
1327 printf("%u\n",startlineno);
1329 fprintf(stderr,"%u\tnewtype\n",startlineno);
1334 instkey : INSTANCE { setstartlineno();
1337 printf("%u\n",startlineno);
1340 fprintf(stderr,"%u\tinstance\n",startlineno);
1345 defaultkey: DEFAULT { setstartlineno(); }
1348 classkey: CLASS { setstartlineno();
1351 printf("%u\n",startlineno);
1353 fprintf(stderr,"%u\tclass\n",startlineno);
1358 minuskey: MINUS { setstartlineno(); }
1361 modulekey: MODULE { setstartlineno();
1364 printf("%u\n",startlineno);
1366 fprintf(stderr,"%u\tmodule\n",startlineno);
1371 oparenkey: OPAREN { setstartlineno(); }
1374 obrackkey: OBRACK { setstartlineno(); }
1377 lazykey : LAZY { setstartlineno(); }
1381 /**********************************************************************
1384 * Basic qualified/unqualified ids/ops *
1387 **********************************************************************/
1390 | OPAREN qvarsym CPAREN { $$ = $2; }
1393 | OPAREN qconsym CPAREN { $$ = $2; }
1396 | BQUOTE qvarid BQUOTE { $$ = $2; }
1399 | BQUOTE qconid BQUOTE { $$ = $2; }
1405 /* Non "-" op, used in right sections */
1410 /* Non "-" varop, used in right sections */
1412 | varsym_nominus { $$ = mknoqual($1); }
1413 | BQUOTE qvarid BQUOTE { $$ = $2; }
1418 | OPAREN varsym CPAREN { $$ = $2; }
1420 con : tycon /* using tycon removes conflicts */
1421 | OPAREN CONSYM CPAREN { $$ = $2; }
1424 | BQUOTE varid BQUOTE { $$ = $2; }
1427 | BQUOTE CONID BQUOTE { $$ = $2; }
1433 qvark : qvarid { setstartlineno(); $$ = $1; }
1434 | oparenkey qvarsym CPAREN { $$ = $2; }
1436 qconk : qconid { setstartlineno(); $$ = $1; }
1437 | oparenkey qconsym CPAREN { $$ = $2; }
1439 vark : varid { setstartlineno(); $$ = $1; }
1440 | oparenkey varsym CPAREN { $$ = $2; }
1444 | varid { $$ = mknoqual($1); }
1447 | varsym { $$ = mknoqual($1); }
1450 | tycon { $$ = mknoqual($1); } /* using tycon removes conflicts */
1453 | CONSYM { $$ = mknoqual($1); }
1456 | tycon { $$ = mknoqual($1); } /* using tycon removes conflicts */
1459 | tycon { $$ = mknoqual($1); } /* using tycon removes conflicts */
1462 varsym : varsym_nominus
1463 | MINUS { $$ = install_literal("-"); }
1466 /* AS HIDING QUALIFIED are valid varids */
1468 | AS { $$ = install_literal("as"); }
1469 | HIDING { $$ = install_literal("hiding"); }
1470 | QUALIFIED { $$ = install_literal("qualified"); }
1473 /* DARROW BANG are valid varsyms */
1474 varsym_nominus : VARSYM
1475 | DARROW { $$ = install_literal("=>"); }
1476 | BANG { $$ = install_literal("!"); }
1483 tyvar : varid { $$ = mknamedtvar(mknoqual($1)); }
1490 tyvar_list: tyvar { $$ = lsing($1); }
1491 | tyvar_list COMMA tyvar { $$ = lapp($1,$3); }
1494 /**********************************************************************
1497 * Stuff to do with layout *
1500 **********************************************************************/
1502 ocurly : layout OCURLY { hsincindent(); }
1504 vocurly : layout { hssetindent(); }
1507 layout : { hsindentoff(); }
1513 FN = NULL; SAMEFN = 0; PREVPATT = NULL;
1518 vccurly : { expect_ccurly = 1; } vccurly1 { expect_ccurly = 0; }
1524 FN = NULL; SAMEFN = 0; PREVPATT = NULL;
1530 FN = NULL; SAMEFN = 0; PREVPATT = NULL;
1537 /**********************************************************************
1539 * Error Processing and Reporting *
1541 * (This stuff is here in case we want to use Yacc macros and such.) *
1543 **********************************************************************/
1549 hsperror("pattern syntax used in expression");
1553 /* The parser calls "hsperror" when it sees a
1554 `report this and die' error. It sets the stage
1555 and calls "yyerror".
1557 There should be no direct calls in the parser to
1558 "yyerror", except for the one from "hsperror". Thus,
1559 the only other calls will be from the error productions
1560 introduced by yacc/bison/whatever.
1562 We need to be able to recognise the from-error-production
1563 case, because we sometimes want to say, "Oh, never mind",
1564 because the layout rule kicks into action and may save
1568 static BOOLEAN error_and_I_mean_it = FALSE;
1574 error_and_I_mean_it = TRUE;
1578 extern char *yytext;
1585 /* We want to be able to distinguish 'error'-raised yyerrors
1586 from yyerrors explicitly coded by the parser hacker.
1588 if (expect_ccurly && ! error_and_I_mean_it ) {
1592 fprintf(stderr, "\"%s\", line %d, column %d: %s on input: ",
1593 input_filename, hsplineno, hspcolno + 1, s);
1595 if (yyleng == 1 && *yytext == '\0')
1596 fprintf(stderr, "<EOF>");
1600 format_string(stderr, (unsigned char *) yytext, yyleng);
1603 fputc('\n', stderr);
1605 /* a common problem */
1606 if (strcmp(yytext, "#") == 0)
1607 fprintf(stderr, "\t(Perhaps you forgot a `-cpp' or `-fglasgow-exts' flag?)\n");
1614 format_string(fp, s, len)
1621 case '\0': fputs("\\NUL", fp); break;
1622 case '\007': fputs("\\a", fp); break;
1623 case '\010': fputs("\\b", fp); break;
1624 case '\011': fputs("\\t", fp); break;
1625 case '\012': fputs("\\n", fp); break;
1626 case '\013': fputs("\\v", fp); break;
1627 case '\014': fputs("\\f", fp); break;
1628 case '\015': fputs("\\r", fp); break;
1629 case '\033': fputs("\\ESC", fp); break;
1630 case '\034': fputs("\\FS", fp); break;
1631 case '\035': fputs("\\GS", fp); break;
1632 case '\036': fputs("\\RS", fp); break;
1633 case '\037': fputs("\\US", fp); break;
1634 case '\177': fputs("\\DEL", fp); break;
1639 fprintf(fp, "\\^%c", *s + '@');