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
248 vallhs funlhs qual gd leftexp
249 pat bpat apat apatc conpat rpat
250 patk bpatk apatck conpatk
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);
873 $$ = mkfbind($3,startlineno);
879 vallhs : patk { $$ = $1; }
880 | patk qvarop pat { $$ = mkinfixap($2,$1,$3); }
881 | funlhs { $$ = $1; }
884 funlhs : qvark apat { $$ = mkap(mkident($1),$2); }
885 | funlhs apat { $$ = mkap($1,$2); }
889 valrhs : valrhs1 maybe_where { $$ = lsing(createpat($1, $2)); }
892 valrhs1 : gdrhs { $$ = mkpguards($1); }
893 | EQUAL exp { $$ = mkpnoguards($2); }
896 gdrhs : gd EQUAL exp { $$ = lsing(mkpgdexp($1,$3)); }
897 | gd EQUAL exp gdrhs { $$ = mklcons(mkpgdexp($1,$3),$4); }
901 WHERE ocurly decls ccurly { $$ = $3; }
902 | WHERE vocurly decls vccurly { $$ = $3; }
903 | /* empty */ { $$ = mknullbind(); }
906 gd : VBAR oexp { $$ = $2; }
910 /**********************************************************************
916 **********************************************************************/
918 exp : oexp DCOLON ctype { $$ = mkrestr($1,$3); }
923 Operators must be left-associative at the same precedence for
924 precedence parsing to work.
926 /* 9 S/R conflicts on qop -> shift */
927 oexp : oexp qop oexp %prec MINUS { $$ = mkinfixap($2,$1,$3); }
932 This comes here because of the funny precedence rules concerning
935 dexp : MINUS kexp { $$ = mknegate($2); }
940 We need to factor out a leading let expression so we can set
941 inpat=TRUE when parsing (non let) expressions inside stmts and quals
943 expLno : oexpLno DCOLON ctype { $$ = mkrestr($1,$3); }
946 oexpLno : oexpLno qop oexp %prec MINUS { $$ = mkinfixap($2,$1,$3); }
949 dexpLno : MINUS kexp { $$ = mknegate($2); }
953 expL : oexpL DCOLON ctype { $$ = mkrestr($1,$3); }
956 oexpL : oexpL qop oexp %prec MINUS { $$ = mkinfixap($2,$1,$3); }
961 let/if/lambda/case have higher precedence than infix operators.
968 kexpL : letdecls IN exp { $$ = mklet($1,$3); }
972 { hsincindent(); /* push new context for FN = NULL; */
973 FN = NULL; /* not actually concerned about indenting */
974 $<ulong>$ = hsplineno; /* remember current line number */
979 RARROW exp /* lambda abstraction */
981 $$ = mklambda($3, $6, $<ulong>2);
985 | IF {$<ulong>$ = hsplineno;}
986 exp THEN exp ELSE exp { $$ = mkife($3,$5,$7,$<ulong>2); }
988 /* Case Expression */
989 | CASE {$<ulong>$ = hsplineno;}
990 exp OF caserest { $$ = mkcasee($3,$5,$<ulong>2); }
993 | DO {$<ulong>$ = hsplineno;}
994 dorest { $$ = mkdoe($3,$<ulong>2); }
996 /* CCALL/CASM Expression */
997 | CCALL ccallid cexps { $$ = mkccall($2,install_literal("n"),$3); }
998 | CCALL ccallid { $$ = mkccall($2,install_literal("n"),Lnil); }
999 | CCALL_GC ccallid cexps { $$ = mkccall($2,install_literal("p"),$3); }
1000 | CCALL_GC ccallid { $$ = mkccall($2,install_literal("p"),Lnil); }
1001 | CASM CLITLIT cexps { $$ = mkccall($2,install_literal("N"),$3); }
1002 | CASM CLITLIT { $$ = mkccall($2,install_literal("N"),Lnil); }
1003 | CASM_GC CLITLIT cexps { $$ = mkccall($2,install_literal("P"),$3); }
1004 | CASM_GC CLITLIT { $$ = mkccall($2,install_literal("P"),Lnil); }
1006 /* SCC Expression */
1017 fexp : fexp aexp { $$ = mkap($1,$2); }
1021 /* simple expressions */
1022 aexp : qvar { $$ = mkident($1); }
1023 | gcon { $$ = mkident($1); }
1024 | lit_constant { $$ = mklit($1); }
1025 | OPAREN exp CPAREN { $$ = mkpar($2); } /* mkpar: stop infix parsing at ()'s */
1026 | qcon OCURLY CCURLY { $$ = mkrecord($1,Lnil); }
1027 | qcon OCURLY rbinds CCURLY { $$ = mkrecord($1,$3); } /* 1 S/R conflict on OCURLY -> shift */
1028 | OBRACK list_exps CBRACK { $$ = mkllist($2); }
1029 | OPAREN exp COMMA texps CPAREN { if (ttree($4) == tuple)
1030 $$ = mktuple(mklcons($2, gtuplelist((struct Stuple *) $4)));
1032 $$ = mktuple(ldub($2, $4)); }
1034 /* only in expressions ... */
1035 | aexp OCURLY rbinds CCURLY { $$ = mkrupdate($1,$3); }
1036 | OBRACK exp VBAR quals CBRACK { $$ = mkcomprh($2,$4); }
1037 | OBRACK exp COMMA exp DOTDOT exp CBRACK {$$= mkeenum($2,mkjust($4),mkjust($6)); }
1038 | OBRACK exp COMMA exp DOTDOT CBRACK { $$ = mkeenum($2,mkjust($4),mknothing()); }
1039 | OBRACK exp DOTDOT exp CBRACK { $$ = mkeenum($2,mknothing(),mkjust($4)); }
1040 | OBRACK exp DOTDOT CBRACK { $$ = mkeenum($2,mknothing(),mknothing()); }
1041 | OPAREN oexp qop CPAREN { $$ = mklsection($2,$3); }
1042 | OPAREN qop1 oexp CPAREN { $$ = mkrsection($2,$3); }
1044 /* only in patterns ... */
1045 /* these add 2 S/R conflict with with aexp . OCURLY rbinds CCURLY */
1046 | qvar AT aexp { checkinpat(); $$ = mkas($1,$3); }
1047 | LAZY aexp { checkinpat(); $$ = mklazyp($2); }
1048 | WILDCARD { checkinpat(); $$ = mkwildp(); }
1051 /* ccall arguments */
1052 cexps : cexps aexp { $$ = lapp($1,$2); }
1053 | aexp { $$ = lsing($1); }
1056 caserest: ocurly alts ccurly { $$ = $2; }
1057 | vocurly alts vccurly { $$ = $2; }
1059 dorest : ocurly stmts ccurly { checkdostmts($2); $$ = $2; }
1060 | vocurly stmts vccurly { checkdostmts($2); $$ = $2; }
1063 rbinds : rbind { $$ = lsing($1); }
1064 | rbinds COMMA rbind { $$ = lapp($1,$3); }
1067 rbind : qvar { $$ = mkrbind($1,mknothing()); }
1068 | qvar EQUAL exp { $$ = mkrbind($1,mkjust($3)); }
1071 texps : exp { $$ = mkpar($1); } /* mkpar: so we don't flatten last element in tuple */
1073 { if (ttree($3) == tuple)
1074 $$ = mktuple(mklcons($1, gtuplelist((struct Stuple *) $3)));
1075 else if (ttree($3) == par)
1076 $$ = mktuple(ldub($1, gpare((struct Spar *) $3)));
1078 hsperror("hsparser:texps: panic");
1080 /* right recursion? WDP */
1085 exp { $$ = lsing($1); }
1086 | exp COMMA list_exps { $$ = mklcons($1, $3); }
1087 /* right recursion? (WDP)
1089 It has to be this way, though, otherwise you
1090 may do the wrong thing to distinguish between...
1092 [ e1 , e2 .. ] -- an enumeration ...
1093 [ e1 , e2 , e3 ] -- a list
1095 (In fact, if you change the grammar and throw yacc/bison
1096 at it, it *will* do the wrong thing [WDP 94/06])
1100 letdecls: LET ocurly decls ccurly { $$ = $3 }
1101 | LET vocurly decls vccurly { $$ = $3 }
1104 quals : qual { $$ = lsing($1); }
1105 | quals COMMA qual { $$ = lapp($1,$3); }
1108 qual : letdecls { $$ = mkseqlet($1); }
1110 | {inpat=TRUE;} expLno {inpat=FALSE;}leftexp
1112 expORpat(LEGIT_EXPR,$2);
1115 expORpat(LEGIT_PATT,$2);
1121 alts : alt { $$ = $1; }
1122 | alts SEMI alt { $$ = lconc($1,$3); }
1125 alt : pat { PREVPATT = $1; } altrest { $$ = lsing($3); PREVPATT = NULL; }
1126 | /* empty */ { $$ = Lnil; }
1129 altrest : gdpat maybe_where { $$ = createpat(mkpguards($1), $2); }
1130 | RARROW exp maybe_where { $$ = createpat(mkpnoguards($2),$3); }
1133 gdpat : gd RARROW exp { $$ = lsing(mkpgdexp($1,$3)); }
1134 | gd RARROW exp gdpat { $$ = mklcons(mkpgdexp($1,$3),$4); }
1137 stmts : stmt { $$ = $1; }
1138 | stmts SEMI stmt { $$ = lconc($1,$3); }
1141 stmt : /* empty */ { $$ = Lnil; }
1142 | letdecls { $$ = lsing(mkseqlet($1)); }
1143 | expL { $$ = lsing($1); }
1144 | {inpat=TRUE;} expLno {inpat=FALSE;} leftexp
1146 expORpat(LEGIT_EXPR,$2);
1147 $$ = lsing(mkdoexp($2,endlineno));
1149 expORpat(LEGIT_PATT,$2);
1150 $$ = lsing(mkdobind($2,$4,endlineno));
1155 leftexp : LARROW exp { $$ = $2; }
1156 | /* empty */ { $$ = NULL; }
1159 /**********************************************************************
1165 **********************************************************************/
1167 pat : pat qconop bpat { $$ = mkinfixap($2,$1,$3); }
1173 | qcon OCURLY rpats CCURLY { $$ = mkrecord($1,$3); }
1174 | MINUS INTEGER { $$ = mknegate(mklit(mkinteger($2))); }
1175 | MINUS FLOAT { $$ = mknegate(mklit(mkfloatr($2))); }
1178 conpat : gcon { $$ = mkident($1); }
1179 | conpat apat { $$ = mkap($1,$2); }
1182 apat : gcon { $$ = mkident($1); }
1183 | qcon OCURLY rpats CCURLY { $$ = mkrecord($1,$3); }
1187 apatc : qvar { $$ = mkident($1); }
1188 | qvar AT apat { $$ = mkas($1,$3); }
1189 | lit_constant { $$ = mklit($1); }
1190 | WILDCARD { $$ = mkwildp(); }
1191 | OPAREN pat CPAREN { $$ = mkpar($2); }
1192 | OPAREN pat COMMA pats CPAREN { $$ = mktuple(mklcons($2,$4)); }
1193 | OBRACK pats CBRACK { $$ = mkllist($2); }
1194 | LAZY apat { $$ = mklazyp($2); }
1198 INTEGER { $$ = mkinteger($1); }
1199 | FLOAT { $$ = mkfloatr($1); }
1200 | CHAR { $$ = mkcharr($1); }
1201 | STRING { $$ = mkstring($1); }
1202 | CHARPRIM { $$ = mkcharprim($1); }
1203 | STRINGPRIM { $$ = mkstringprim($1); }
1204 | INTPRIM { $$ = mkintprim($1); }
1205 | FLOATPRIM { $$ = mkfloatprim($1); }
1206 | DOUBLEPRIM { $$ = mkdoubleprim($1); }
1207 | CLITLIT /* yurble yurble */ { $$ = mkclitlit($1); }
1210 lampats : apat lampats { $$ = mklcons($1,$2); }
1211 | apat { $$ = lsing($1); }
1212 /* right recursion? (WDP) */
1215 pats : pat COMMA pats { $$ = mklcons($1, $3); }
1216 | pat { $$ = lsing($1); }
1217 /* right recursion? (WDP) */
1220 rpats : rpat { $$ = lsing($1); }
1221 | rpats COMMA rpat { $$ = lapp($1,$3); }
1224 rpat : qvar { $$ = mkrbind($1,mknothing()); }
1225 | qvar EQUAL pat { $$ = mkrbind($1,mkjust($3)); }
1229 patk : patk qconop bpat { $$ = mkinfixap($2,$1,$3); }
1235 | qconk OCURLY rpats CCURLY { $$ = mkrecord($1,$3); }
1236 | minuskey INTEGER { $$ = mknegate(mklit(mkinteger($2))); }
1237 | minuskey FLOAT { $$ = mknegate(mklit(mkfloatr($2))); }
1240 conpatk : gconk { $$ = mkident($1); }
1241 | conpatk apat { $$ = mkap($1,$2); }
1244 apatck : qvark { $$ = mkident($1); }
1245 | qvark AT apat { $$ = mkas($1,$3); }
1246 | lit_constant { $$ = mklit($1); setstartlineno(); }
1247 | WILDCARD { $$ = mkwildp(); setstartlineno(); }
1248 | oparenkey pat CPAREN { $$ = mkpar($2); }
1249 | oparenkey pat COMMA pats CPAREN { $$ = mktuple(mklcons($2,$4)); }
1250 | obrackkey pats CBRACK { $$ = mkllist($2); }
1251 | lazykey apat { $$ = mklazyp($2); }
1256 | OBRACK CBRACK { $$ = creategid(-1); }
1257 | OPAREN CPAREN { $$ = creategid(0); }
1258 | OPAREN commas CPAREN { $$ = creategid($2); }
1262 | obrackkey CBRACK { $$ = creategid(-1); }
1263 | oparenkey CPAREN { $$ = creategid(0); }
1264 | oparenkey commas CPAREN { $$ = creategid($2); }
1267 /**********************************************************************
1270 * Keywords which record the line start *
1273 **********************************************************************/
1275 importkey: IMPORT { setstartlineno(); }
1278 datakey : DATA { setstartlineno();
1281 printf("%u\n",startlineno);
1283 fprintf(stderr,"%u\tdata\n",startlineno);
1288 typekey : TYPE { setstartlineno();
1291 printf("%u\n",startlineno);
1293 fprintf(stderr,"%u\ttype\n",startlineno);
1298 newtypekey : NEWTYPE { setstartlineno();
1301 printf("%u\n",startlineno);
1303 fprintf(stderr,"%u\tnewtype\n",startlineno);
1308 instkey : INSTANCE { setstartlineno();
1311 printf("%u\n",startlineno);
1314 fprintf(stderr,"%u\tinstance\n",startlineno);
1319 defaultkey: DEFAULT { setstartlineno(); }
1322 classkey: CLASS { setstartlineno();
1325 printf("%u\n",startlineno);
1327 fprintf(stderr,"%u\tclass\n",startlineno);
1332 modulekey: MODULE { setstartlineno();
1335 printf("%u\n",startlineno);
1337 fprintf(stderr,"%u\tmodule\n",startlineno);
1342 oparenkey: OPAREN { setstartlineno(); }
1345 obrackkey: OBRACK { setstartlineno(); }
1348 lazykey : LAZY { setstartlineno(); }
1351 minuskey: MINUS { setstartlineno(); }
1355 /**********************************************************************
1358 * Basic qualified/unqualified ids/ops *
1361 **********************************************************************/
1364 | OPAREN qvarsym CPAREN { $$ = $2; }
1367 | OPAREN qconsym CPAREN { $$ = $2; }
1370 | BQUOTE qvarid BQUOTE { $$ = $2; }
1373 | BQUOTE qconid BQUOTE { $$ = $2; }
1379 /* Non "-" op, used in right sections */
1384 /* Non "-" varop, used in right sections */
1386 | varsym_nominus { $$ = mknoqual($1); }
1387 | BQUOTE qvarid BQUOTE { $$ = $2; }
1392 | OPAREN varsym CPAREN { $$ = $2; }
1394 con : tycon /* using tycon removes conflicts */
1395 | OPAREN CONSYM CPAREN { $$ = $2; }
1398 | BQUOTE varid BQUOTE { $$ = $2; }
1401 | BQUOTE CONID BQUOTE { $$ = $2; }
1407 qvark : qvarid { setstartlineno(); $$ = $1; }
1408 | oparenkey qvarsym CPAREN { $$ = $2; }
1410 qconk : qconid { setstartlineno(); $$ = $1; }
1411 | oparenkey qconsym CPAREN { $$ = $2; }
1413 vark : varid { setstartlineno(); $$ = $1; }
1414 | oparenkey varsym CPAREN { $$ = $2; }
1418 | varid { $$ = mknoqual($1); }
1421 | varsym { $$ = mknoqual($1); }
1424 | tycon { $$ = mknoqual($1); } /* using tycon removes conflicts */
1427 | CONSYM { $$ = mknoqual($1); }
1430 | tycon { $$ = mknoqual($1); } /* using tycon removes conflicts */
1433 | tycon { $$ = mknoqual($1); } /* using tycon removes conflicts */
1436 varsym : varsym_nominus
1437 | MINUS { $$ = install_literal("-"); }
1440 /* AS HIDING QUALIFIED are valid varids */
1442 | AS { $$ = install_literal("as"); }
1443 | HIDING { $$ = install_literal("hiding"); }
1444 | QUALIFIED { $$ = install_literal("qualified"); }
1447 /* DARROW BANG are valid varsyms */
1448 varsym_nominus : VARSYM
1449 | DARROW { $$ = install_literal("=>"); }
1450 | BANG { $$ = install_literal("!"); }
1457 tyvar : varid { $$ = mknamedtvar(mknoqual($1)); }
1464 tyvar_list: tyvar { $$ = lsing($1); }
1465 | tyvar_list COMMA tyvar { $$ = lapp($1,$3); }
1468 /**********************************************************************
1471 * Stuff to do with layout *
1474 **********************************************************************/
1476 ocurly : layout OCURLY { hsincindent(); }
1478 vocurly : layout { hssetindent(); }
1481 layout : { hsindentoff(); }
1487 FN = NULL; SAMEFN = 0; PREVPATT = NULL;
1492 vccurly : { expect_ccurly = 1; } vccurly1 { expect_ccurly = 0; }
1498 FN = NULL; SAMEFN = 0; PREVPATT = NULL;
1504 FN = NULL; SAMEFN = 0; PREVPATT = NULL;
1511 /**********************************************************************
1513 * Error Processing and Reporting *
1515 * (This stuff is here in case we want to use Yacc macros and such.) *
1517 **********************************************************************/
1523 hsperror("pattern syntax used in expression");
1527 /* The parser calls "hsperror" when it sees a
1528 `report this and die' error. It sets the stage
1529 and calls "yyerror".
1531 There should be no direct calls in the parser to
1532 "yyerror", except for the one from "hsperror". Thus,
1533 the only other calls will be from the error productions
1534 introduced by yacc/bison/whatever.
1536 We need to be able to recognise the from-error-production
1537 case, because we sometimes want to say, "Oh, never mind",
1538 because the layout rule kicks into action and may save
1542 static BOOLEAN error_and_I_mean_it = FALSE;
1548 error_and_I_mean_it = TRUE;
1552 extern char *yytext;
1559 /* We want to be able to distinguish 'error'-raised yyerrors
1560 from yyerrors explicitly coded by the parser hacker.
1562 if (expect_ccurly && ! error_and_I_mean_it ) {
1566 fprintf(stderr, "\"%s\", line %d, column %d: %s on input: ",
1567 input_filename, hsplineno, hspcolno + 1, s);
1569 if (yyleng == 1 && *yytext == '\0')
1570 fprintf(stderr, "<EOF>");
1574 format_string(stderr, (unsigned char *) yytext, yyleng);
1577 fputc('\n', stderr);
1579 /* a common problem */
1580 if (strcmp(yytext, "#") == 0)
1581 fprintf(stderr, "\t(Perhaps you forgot a `-cpp' or `-fglasgow-exts' flag?)\n");
1588 format_string(fp, s, len)
1595 case '\0': fputs("\\NUL", fp); break;
1596 case '\007': fputs("\\a", fp); break;
1597 case '\010': fputs("\\b", fp); break;
1598 case '\011': fputs("\\t", fp); break;
1599 case '\012': fputs("\\n", fp); break;
1600 case '\013': fputs("\\v", fp); break;
1601 case '\014': fputs("\\f", fp); break;
1602 case '\015': fputs("\\r", fp); break;
1603 case '\033': fputs("\\ESC", fp); break;
1604 case '\034': fputs("\\FS", fp); break;
1605 case '\035': fputs("\\GS", fp); break;
1606 case '\036': fputs("\\RS", fp); break;
1607 case '\037': fputs("\\US", fp); break;
1608 case '\177': fputs("\\DEL", fp); break;
1613 fprintf(fp, "\\^%c", *s + '@');