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
128 %token COMMA SEMI OBRACK CBRACK
129 %token WILDCARD BQUOTE OPAREN CPAREN
132 /**********************************************************************
135 * Reserved Operators *
138 **********************************************************************/
140 %token DOTDOT DCOLON EQUAL LAMBDA
141 %token VBAR RARROW LARROW
142 %token AT LAZY DARROW
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 * Special symbols/identifiers which need to be recognised *
171 **********************************************************************/
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 impspec deriving
242 %type <uliteral> lit_constant
244 %type <utree> exp oexp dexp kexp fexp aexp rbind texps
245 expL oexpL kexpL expLno oexpLno dexpLno kexpLno
246 vallhs funlhs qual gd leftexp
247 pat bpat apat apatc conpat rpat
248 patk bpatk apatck conpatk
251 %type <uid> MINUS DARROW AS LAZY
252 VARID CONID VARSYM CONSYM
253 var con varop conop op
254 vark varid varsym varsym_nominus
257 %type <uqid> QVARID QCONID QVARSYM QCONSYM
258 qvarid qconid qvarsym qconsym
259 qvar qcon qvarop qconop qop
260 qvark qconk qtycon qtycls
261 gcon gconk gtycon itycon qop1 qvarop1
264 %type <ubinding> topdecl topdecls letdecls
265 typed datad newtd classd instd defaultd
266 decl decls valdef instdef instdefs
267 maybe_where cbody rinst type_and_maybe_id
269 %type <upbinding> valrhs1 altrest
271 %type <uttype> simple ctype type atype btype
272 gtyconapp ntyconapp ntycon gtyconvars
273 bbtype batype btyconapp
274 class restrict_inst general_inst tyvar
276 %type <uconstr> constr field
278 %type <ustring> FLOAT INTEGER INTPRIM
279 FLOATPRIM DOUBLEPRIM CLITLIT
281 %type <uhstring> STRING STRINGPRIM CHAR CHARPRIM
283 %type <uentid> export import
287 /**********************************************************************
290 * Start Symbol for the Parser *
293 **********************************************************************/
298 module : modulekey modid maybeexports
300 modulelineno = startlineno;
301 the_module_name = $2;
307 the_module_name = install_literal("Main");
308 module_exports = mknothing();
313 body : ocurly { setstartlineno(); } interface_pragma orestm
314 | vocurly interface_pragma vrestm
317 interface_pragma : /* empty */
318 | INTERFACE_UPRAGMA INTEGER END_UPRAGMA SEMI
320 source_version = atoi($2);
324 orestm : maybeimpdecls maybefixes topdecls ccurly
326 root = mkhmodule(the_module_name,$1,module_exports,
327 $2,$3,source_version,modulelineno);
331 root = mkhmodule(the_module_name,$1,module_exports,
332 Lnil,mknullbind(),source_version,modulelineno);
335 vrestm : maybeimpdecls maybefixes topdecls vccurly
337 root = mkhmodule(the_module_name,$1,module_exports,
338 $2,$3,source_version,modulelineno);
342 root = mkhmodule(the_module_name,$1,module_exports,
343 Lnil,mknullbind(),source_version,modulelineno);
346 maybeexports : /* empty */ { $$ = mknothing(); }
347 | OPAREN export_list CPAREN { $$ = mkjust($2); }
348 | OPAREN export_list COMMA CPAREN { $$ = mkjust($2); }
352 export { $$ = lsing($1); }
353 | export_list COMMA export { $$ = lapp($1, $3); }
356 export : qvar { $$ = mkentid($1); }
357 | gtycon { $$ = mkenttype($1); }
358 | gtycon OPAREN DOTDOT CPAREN { $$ = mkenttypeall($1); }
359 | gtycon OPAREN CPAREN { $$ = mkenttypenamed($1,Lnil); }
360 | gtycon OPAREN enames CPAREN { $$ = mkenttypenamed($1,$3); }
361 | MODULE modid { $$ = mkentmod($2); }
364 enames : ename { $$ = lsing($1); }
365 | enames COMMA ename { $$ = lapp($1,$3); }
372 maybeimpdecls : /* empty */ { $$ = Lnil; }
373 | impdecls SEMI { $$ = $1; }
376 impdecls: impdecl { $$ = $1; }
377 | impdecls SEMI impdecl { $$ = lconc($1,$3); }
381 impdecl : importkey modid impspec
382 { $$ = lsing(mkimport($2,0,mknothing(),$3,startlineno)); }
383 | importkey QUALIFIED modid impspec
384 { $$ = lsing(mkimport($3,1,mknothing(),$4,startlineno)); }
385 | importkey QUALIFIED modid AS modid impspec
386 { $$ = lsing(mkimport($3,1,mkjust($5),$6,startlineno)); }
389 impspec : /* empty */ { $$ = mknothing(); }
390 | OPAREN CPAREN { $$ = mkjust(mkleft(Lnil)); }
391 | OPAREN import_list CPAREN { $$ = mkjust(mkleft($2)); }
392 | OPAREN import_list COMMA CPAREN { $$ = mkjust(mkleft($2)); }
393 | HIDING OPAREN import_list CPAREN { $$ = mkjust(mkright($3)); }
394 | HIDING OPAREN import_list COMMA CPAREN { $$ = mkjust(mkright($3)); }
398 import { $$ = lsing($1); }
399 | import_list COMMA import { $$ = lapp($1, $3); }
402 import : var { $$ = mkentid(mknoqual($1)); }
403 | itycon { $$ = mkenttype($1); }
404 | itycon OPAREN DOTDOT CPAREN { $$ = mkenttypeall($1); }
405 | itycon OPAREN CPAREN { $$ = mkenttypenamed($1,Lnil);}
406 | itycon OPAREN inames CPAREN { $$ = mkenttypenamed($1,$3); }
409 itycon : tycon { $$ = mknoqual($1); }
410 | OBRACK CBRACK { $$ = creategid(-1); }
411 | OPAREN CPAREN { $$ = creategid(0); }
412 | OPAREN commas CPAREN { $$ = creategid($2); }
415 inames : iname { $$ = lsing($1); }
416 | inames COMMA iname { $$ = lapp($1,$3); }
418 iname : var { $$ = mknoqual($1); }
419 | con { $$ = mknoqual($1); }
422 /**********************************************************************
425 * Fixes and Decls etc *
428 **********************************************************************/
430 maybefixes: /* empty */ { $$ = Lnil; }
431 | fixes SEMI { $$ = $1; }
434 fixes : fix { $$ = $1; }
435 | fixes SEMI fix { $$ = lconc($1,$3); }
438 fix : INFIXL INTEGER { Precedence = checkfixity($2); Fixity = INFIXL; }
440 | INFIXR INTEGER { Precedence = checkfixity($2); Fixity = INFIXR; }
442 | INFIX INTEGER { Precedence = checkfixity($2); Fixity = INFIX; }
444 | INFIXL { Fixity = INFIXL; Precedence = 9; }
446 | INFIXR { Fixity = INFIXR; Precedence = 9; }
448 | INFIX { Fixity = INFIX; Precedence = 9; }
452 ops : op { $$ = lsing(mkfixop(mknoqual($1),infixint(Fixity),Precedence)); }
453 | ops COMMA op { $$ = lapp($1,mkfixop(mknoqual($3),infixint(Fixity),Precedence)); }
457 | topdecls SEMI topdecl
476 topdecl : typed { $$ = $1; }
479 | classd { $$ = $1; }
481 | defaultd { $$ = $1; }
485 typed : typekey simple EQUAL type { $$ = mknbind($2,$4,startlineno); }
489 datad : datakey simple EQUAL constrs deriving
490 { $$ = mktbind(Lnil,$2,$4,$5,startlineno); }
491 | datakey context DARROW simple EQUAL constrs deriving
492 { $$ = mktbind($2,$4,$6,$7,startlineno); }
495 newtd : newtypekey simple EQUAL constr1 deriving
496 { $$ = mkntbind(Lnil,$2,$4,$5,startlineno); }
497 | newtypekey context DARROW simple EQUAL constr1 deriving
498 { $$ = mkntbind($2,$4,$6,$7,startlineno); }
501 deriving: /* empty */ { $$ = mknothing(); }
502 | DERIVING dtyclses { $$ = mkjust($2); }
505 classd : classkey context DARROW class cbody
506 { $$ = mkcbind($2,$4,$5,startlineno); }
507 | classkey class cbody
508 { $$ = mkcbind(Lnil,$2,$3,startlineno); }
511 cbody : /* empty */ { $$ = mknullbind(); }
512 | WHERE ocurly decls ccurly { checkorder($3); $$ = $3; }
513 | WHERE vocurly decls vccurly { checkorder($3); $$ = $3; }
516 instd : instkey context DARROW gtycon restrict_inst rinst
517 { $$ = mkibind($2,$4,$5,$6,startlineno); }
518 | instkey gtycon general_inst rinst
519 { $$ = mkibind(Lnil,$2,$3,$4,startlineno); }
522 rinst : /* empty */ { $$ = mknullbind(); }
523 | WHERE ocurly instdefs ccurly { $$ = $3; }
524 | WHERE vocurly instdefs vccurly { $$ = $3; }
527 restrict_inst : gtycon { $$ = mktname($1); }
528 | OPAREN gtyconvars CPAREN { $$ = $2; }
529 | OPAREN tyvar COMMA tyvar_list CPAREN { $$ = mkttuple(mklcons($2,$4)); }
530 | OBRACK tyvar CBRACK { $$ = mktllist($2); }
531 | OPAREN tyvar RARROW tyvar CPAREN { $$ = mktfun($2,$4); }
534 general_inst : gtycon { $$ = mktname($1); }
535 | OPAREN gtyconapp CPAREN { $$ = $2; }
536 | OPAREN type COMMA types CPAREN { $$ = mkttuple(mklcons($2,$4)); }
537 | OBRACK type CBRACK { $$ = mktllist($2); }
538 | OPAREN btype RARROW type CPAREN { $$ = mktfun($2,$4); }
541 defaultd: defaultkey OPAREN types CPAREN { $$ = mkdbind($3,startlineno); }
542 | defaultkey OPAREN CPAREN { $$ = mkdbind(Lnil,startlineno); }
559 Note: if there is an iclasop_pragma here, then we must be
560 doing a class-op in an interface -- unless the user is up
561 to real mischief (ugly, but likely to work).
564 decl : qvarsk DCOLON ctype
565 { $$ = mksbind($1,$3,startlineno);
566 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
569 /* User-specified pragmas come in as "signatures"...
570 They are similar in that they can appear anywhere in the module,
571 and have to be "joined up" with their related entity.
573 Have left out the case specialising to an overloaded type.
574 Let's get real, OK? (WDP)
576 | SPECIALISE_UPRAGMA qvark DCOLON types_and_maybe_ids END_UPRAGMA
578 $$ = mkvspec_uprag($2, $4, startlineno);
579 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
582 | SPECIALISE_UPRAGMA INSTANCE gtycon general_inst END_UPRAGMA
584 $$ = mkispec_uprag($3, $4, startlineno);
585 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
588 | SPECIALISE_UPRAGMA DATA gtycon atypes END_UPRAGMA
590 $$ = mkdspec_uprag($3, $4, startlineno);
591 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
594 | INLINE_UPRAGMA qvark END_UPRAGMA
596 $$ = mkinline_uprag($2, startlineno);
597 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
600 | MAGIC_UNFOLDING_UPRAGMA qvark vark END_UPRAGMA
602 $$ = mkmagicuf_uprag($2, $3, startlineno);
603 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
606 | DEFOREST_UPRAGMA qvark END_UPRAGMA
608 $$ = mkdeforest_uprag($2, startlineno);
609 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
612 /* end of user-specified pragmas */
615 | /* empty */ { $$ = mknullbind(); PREVPATT = NULL; FN = NULL; SAMEFN = 0; }
618 qvarsk : qvark COMMA qvars_list { $$ = mklcons($1,$3); }
619 | qvark { $$ = lsing($1); }
622 qvars_list: qvar { $$ = lsing($1); }
623 | qvars_list COMMA qvar { $$ = lapp($1,$3); }
626 types_and_maybe_ids :
627 type_and_maybe_id { $$ = lsing($1); }
628 | types_and_maybe_ids COMMA type_and_maybe_id { $$ = lapp($1,$3); }
632 type { $$ = mkvspec_ty_and_id($1,mknothing()); }
633 | type EQUAL qvark { $$ = mkvspec_ty_and_id($1,mkjust($3)); }
636 /**********************************************************************
642 **********************************************************************/
644 /* "DCOLON context => type" vs "DCOLON type" is a problem,
645 because you can't distinguish between
647 foo :: (Baz a, Baz a)
648 bar :: (Baz a, Baz a) => [a] -> [a] -> [a]
650 with one token of lookahead. The HACK is to have "DCOLON ttype"
651 [tuple type] in the first case, then check that it has the right
652 form C a, or (C1 a, C2 b, ... Cn z) and convert it into a
656 /* 1 S/R conflict at DARROW -> shift */
657 ctype : type DARROW type { $$ = mkcontext(type2context($1),$3); }
661 /* 1 S/R conflict at RARROW -> shift */
662 type : btype { $$ = $1; }
663 | btype RARROW type { $$ = mktfun($1,$3); }
666 /* btype is split so we can parse gtyconapp without S/R conflicts */
667 btype : gtyconapp { $$ = $1; }
668 | ntyconapp { $$ = $1; }
671 ntyconapp: ntycon { $$ = $1; }
672 | ntyconapp atype { $$ = mktapp($1,$2); }
675 gtyconapp: gtycon { $$ = mktname($1); }
676 | gtyconapp atype { $$ = mktapp($1,$2); }
680 atype : gtycon { $$ = mktname($1); }
681 | ntycon { $$ = $1; }
684 ntycon : tyvar { $$ = $1; }
685 | OPAREN type COMMA types CPAREN { $$ = mkttuple(mklcons($2,$4)); }
686 | OBRACK type CBRACK { $$ = mktllist($2); }
687 | OPAREN type CPAREN { $$ = $2; }
691 | OPAREN RARROW CPAREN { $$ = creategid(-2); }
692 | OBRACK CBRACK { $$ = creategid(-1); }
693 | OPAREN CPAREN { $$ = creategid(0); }
694 | OPAREN commas CPAREN { $$ = creategid($2); }
697 atypes : atype { $$ = lsing($1); }
698 | atypes atype { $$ = lapp($1,$2); }
701 types : type { $$ = lsing($1); }
702 | types COMMA type { $$ = lapp($1,$3); }
705 commas : COMMA { $$ = 1; }
706 | commas COMMA { $$ = $1 + 1; }
709 /**********************************************************************
712 * Declaration stuff *
715 **********************************************************************/
717 simple : gtycon { $$ = mktname($1); }
718 | gtyconvars { $$ = $1; }
721 gtyconvars: gtycon tyvar { $$ = mktapp(mktname($1),$2); }
722 | gtyconvars tyvar { $$ = mktapp($1,$2); }
725 context : OPAREN context_list CPAREN { $$ = $2; }
726 | class { $$ = lsing($1); }
729 context_list: class { $$ = lsing($1); }
730 | context_list COMMA class { $$ = lapp($1,$3); }
733 class : gtycon tyvar { $$ = mktapp(mktname($1),$2); }
736 constrs : constr { $$ = lsing($1); }
737 | constrs VBAR constr { $$ = lapp($1,$3); }
740 constr : btyconapp { qid tyc; list tys;
741 splittyconapp($1, &tyc, &tys);
742 $$ = mkconstrpre(tyc,tys,hsplineno); }
743 | OPAREN qconsym CPAREN { $$ = mkconstrpre($2,Lnil,hsplineno); }
744 | OPAREN qconsym CPAREN batypes { $$ = mkconstrpre($2,$4,hsplineno); }
745 | btyconapp qconop bbtype { checknobangs($1);
746 $$ = mkconstrinf($1,$2,$3,hsplineno); }
747 | ntyconapp qconop bbtype { $$ = mkconstrinf($1,$2,$3,hsplineno); }
748 | BANG atype qconop bbtype { $$ = mkconstrinf(mktbang($2),$3,$4,hsplineno); }
750 /* 1 S/R conflict on OCURLY -> shift */
751 | gtycon OCURLY fields CCURLY { $$ = mkconstrrec($1,$3,hsplineno); }
754 btyconapp: gtycon { $$ = mktname($1); }
755 | btyconapp batype { $$ = mktapp($1,$2); }
758 bbtype : btype { $$ = $1; }
759 | BANG atype { $$ = mktbang($2); }
762 batype : atype { $$ = $1; }
763 | BANG atype { $$ = mktbang($2); }
766 batypes : batype { $$ = lsing($1); }
767 | batypes batype { $$ = lapp($1,$2); }
771 fields : field { $$ = lsing($1); }
772 | fields COMMA field { $$ = lapp($1,$3); }
775 field : qvars_list DCOLON type { $$ = mkfield($1,$3); }
776 | qvars_list DCOLON BANG atype { $$ = mkfield($1,mktbang($4)); }
779 constr1 : gtycon atype { $$ = lsing(mkconstrnew($1,$2,hsplineno)); }
783 dtyclses: OPAREN dtycls_list CPAREN { $$ = $2; }
784 | OPAREN CPAREN { $$ = Lnil; }
785 | qtycls { $$ = lsing($1); }
788 dtycls_list: qtycls { $$ = lsing($1); }
789 | dtycls_list COMMA qtycls { $$ = lapp($1,$3); }
792 instdefs : /* empty */ { $$ = mknullbind(); }
793 | instdef { $$ = $1; }
794 | instdefs SEMI instdef
806 /* instdef: same as valdef, except certain user-pragmas may appear */
808 SPECIALISE_UPRAGMA qvark DCOLON types_and_maybe_ids END_UPRAGMA
810 $$ = mkvspec_uprag($2, $4, startlineno);
811 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
814 | INLINE_UPRAGMA qvark END_UPRAGMA
816 $$ = mkinline_uprag($2, startlineno);
817 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
820 | MAGIC_UNFOLDING_UPRAGMA qvark vark END_UPRAGMA
822 $$ = mkmagicuf_uprag($2, $3, startlineno);
823 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
832 tree fn = function($1);
835 if(ttree(fn) == ident)
837 qid fun_id = gident((struct Sident *) fn);
842 else if (ttree(fn) == infixap)
844 qid fun_id = ginffun((struct Sinfixap *) fn);
851 printf("%u\n",startlineno);
853 fprintf(stderr,"%u\tvaldef\n",startlineno);
858 if ( lhs_is_patt($1) )
860 $$ = mkpbind($3, startlineno);
865 $$ = mkfbind($3,startlineno);
871 vallhs : patk { $$ = $1; }
872 | patk qvarop pat { $$ = mkinfixap($2,$1,$3); }
873 | funlhs { $$ = $1; }
876 funlhs : qvark apat { $$ = mkap(mkident($1),$2); }
877 | funlhs apat { $$ = mkap($1,$2); }
881 valrhs : valrhs1 maybe_where { $$ = lsing(createpat($1, $2)); }
884 valrhs1 : gdrhs { $$ = mkpguards($1); }
885 | EQUAL exp { $$ = mkpnoguards($2); }
888 gdrhs : gd EQUAL exp { $$ = lsing(mkpgdexp($1,$3)); }
889 | gd EQUAL exp gdrhs { $$ = mklcons(mkpgdexp($1,$3),$4); }
893 WHERE ocurly decls ccurly { $$ = $3; }
894 | WHERE vocurly decls vccurly { $$ = $3; }
895 | /* empty */ { $$ = mknullbind(); }
898 gd : VBAR oexp { $$ = $2; }
902 /**********************************************************************
908 **********************************************************************/
910 exp : oexp DCOLON ctype { $$ = mkrestr($1,$3); }
915 Operators must be left-associative at the same precedence for
916 precedence parsing to work.
918 /* 8 S/R conflicts on qop -> shift */
919 oexp : oexp qop oexp %prec MINUS { $$ = mkinfixap($2,$1,$3); }
924 This comes here because of the funny precedence rules concerning
927 dexp : MINUS kexp { $$ = mknegate($2); }
932 We need to factor out a leading let expression so we can set
933 inpat=TRUE when parsing (non let) expressions inside stmts and quals
935 expLno : oexpLno DCOLON ctype { $$ = mkrestr($1,$3); }
938 oexpLno : oexpLno qop oexp %prec MINUS { $$ = mkinfixap($2,$1,$3); }
941 dexpLno : MINUS kexp { $$ = mknegate($2); }
945 expL : oexpL DCOLON ctype { $$ = mkrestr($1,$3); }
948 oexpL : oexpL qop oexp %prec MINUS { $$ = mkinfixap($2,$1,$3); }
953 let/if/lambda/case have higher precedence than infix operators.
960 kexpL : letdecls IN exp { $$ = mklet($1,$3); }
964 { hsincindent(); /* push new context for FN = NULL; */
965 FN = NULL; /* not actually concerned about indenting */
966 $<ulong>$ = hsplineno; /* remember current line number */
971 RARROW exp /* lambda abstraction */
973 $$ = mklambda($3, $6, $<ulong>2);
977 | IF {$<ulong>$ = hsplineno;}
978 exp THEN exp ELSE exp { $$ = mkife($3,$5,$7,$<ulong>2); }
980 /* Case Expression */
981 | CASE {$<ulong>$ = hsplineno;}
982 exp OF caserest { $$ = mkcasee($3,$5,$<ulong>2); }
985 | DO {$<ulong>$ = hsplineno;}
986 dorest { $$ = mkdoe($3,$<ulong>2); }
988 /* CCALL/CASM Expression */
989 | CCALL ccallid cexps { $$ = mkccall($2,install_literal("n"),$3); }
990 | CCALL ccallid { $$ = mkccall($2,install_literal("n"),Lnil); }
991 | CCALL_GC ccallid cexps { $$ = mkccall($2,install_literal("p"),$3); }
992 | CCALL_GC ccallid { $$ = mkccall($2,install_literal("p"),Lnil); }
993 | CASM CLITLIT cexps { $$ = mkccall($2,install_literal("N"),$3); }
994 | CASM CLITLIT { $$ = mkccall($2,install_literal("N"),Lnil); }
995 | CASM_GC CLITLIT cexps { $$ = mkccall($2,install_literal("P"),$3); }
996 | CASM_GC CLITLIT { $$ = mkccall($2,install_literal("P"),Lnil); }
1009 fexp : fexp aexp { $$ = mkap($1,$2); }
1013 /* simple expressions */
1014 aexp : qvar { $$ = mkident($1); }
1015 | gcon { $$ = mkident($1); }
1016 | lit_constant { $$ = mklit($1); }
1017 | OPAREN exp CPAREN { $$ = mkpar($2); } /* mkpar: stop infix parsing at ()'s */
1018 | qcon OCURLY CCURLY { $$ = mkrecord($1,Lnil); }
1019 | qcon OCURLY rbinds CCURLY { $$ = mkrecord($1,$3); } /* 1 S/R conflict on OCURLY -> shift */
1020 | OBRACK list_exps CBRACK { $$ = mkllist($2); }
1021 | OPAREN exp COMMA texps CPAREN { if (ttree($4) == tuple)
1022 $$ = mktuple(mklcons($2, gtuplelist((struct Stuple *) $4)));
1024 $$ = mktuple(ldub($2, $4)); }
1026 /* only in expressions ... */
1027 | aexp OCURLY rbinds CCURLY { $$ = mkrupdate($1,$3); }
1028 | OBRACK exp VBAR quals CBRACK { $$ = mkcomprh($2,$4); }
1029 | OBRACK exp COMMA exp DOTDOT exp CBRACK {$$= mkeenum($2,mkjust($4),mkjust($6)); }
1030 | OBRACK exp COMMA exp DOTDOT CBRACK { $$ = mkeenum($2,mkjust($4),mknothing()); }
1031 | OBRACK exp DOTDOT exp CBRACK { $$ = mkeenum($2,mknothing(),mkjust($4)); }
1032 | OBRACK exp DOTDOT CBRACK { $$ = mkeenum($2,mknothing(),mknothing()); }
1033 | OPAREN oexp qop CPAREN { $$ = mklsection($2,$3); }
1034 | OPAREN qop1 oexp CPAREN { $$ = mkrsection($2,$3); }
1036 /* only in patterns ... */
1037 /* these add 2 S/R conflict with with aexp . OCURLY rbinds CCURLY */
1038 | qvar AT aexp { checkinpat(); $$ = mkas($1,$3); }
1039 | LAZY aexp { checkinpat(); $$ = mklazyp($2); }
1040 | WILDCARD { checkinpat(); $$ = mkwildp(); }
1043 /* ccall arguments */
1044 cexps : cexps aexp { $$ = lapp($1,$2); }
1045 | aexp { $$ = lsing($1); }
1048 caserest: ocurly alts ccurly { $$ = $2; }
1049 | vocurly alts vccurly { $$ = $2; }
1051 dorest : ocurly stmts ccurly { checkdostmts($2); $$ = $2; }
1052 | vocurly stmts vccurly { checkdostmts($2); $$ = $2; }
1055 rbinds : rbind { $$ = lsing($1); }
1056 | rbinds COMMA rbind { $$ = lapp($1,$3); }
1059 rbind : qvar { $$ = mkrbind($1,mknothing()); }
1060 | qvar EQUAL exp { $$ = mkrbind($1,mkjust($3)); }
1063 texps : exp { $$ = mkpar($1); } /* mkpar: so we don't flatten last element in tuple */
1065 { if (ttree($3) == tuple)
1066 $$ = mktuple(mklcons($1, gtuplelist((struct Stuple *) $3)));
1067 else if (ttree($3) == par)
1068 $$ = mktuple(ldub($1, gpare((struct Spar *) $3)));
1070 hsperror("hsparser:texps: panic");
1072 /* right recursion? WDP */
1077 exp { $$ = lsing($1); }
1078 | exp COMMA list_exps { $$ = mklcons($1, $3); }
1079 /* right recursion? (WDP)
1081 It has to be this way, though, otherwise you
1082 may do the wrong thing to distinguish between...
1084 [ e1 , e2 .. ] -- an enumeration ...
1085 [ e1 , e2 , e3 ] -- a list
1087 (In fact, if you change the grammar and throw yacc/bison
1088 at it, it *will* do the wrong thing [WDP 94/06])
1092 letdecls: LET ocurly decls ccurly { $$ = $3 }
1093 | LET vocurly decls vccurly { $$ = $3 }
1096 quals : qual { $$ = lsing($1); }
1097 | quals COMMA qual { $$ = lapp($1,$3); }
1100 qual : letdecls { $$ = mkseqlet($1); }
1102 | {inpat=TRUE;} expLno {inpat=FALSE;}leftexp
1104 expORpat(LEGIT_EXPR,$2);
1107 expORpat(LEGIT_PATT,$2);
1113 alts : alt { $$ = $1; }
1114 | alts SEMI alt { $$ = lconc($1,$3); }
1117 alt : pat { PREVPATT = $1; } altrest { $$ = lsing($3); PREVPATT = NULL; }
1118 | /* empty */ { $$ = Lnil; }
1121 altrest : gdpat maybe_where { $$ = createpat(mkpguards($1), $2); }
1122 | RARROW exp maybe_where { $$ = createpat(mkpnoguards($2),$3); }
1125 gdpat : gd RARROW exp { $$ = lsing(mkpgdexp($1,$3)); }
1126 | gd RARROW exp gdpat { $$ = mklcons(mkpgdexp($1,$3),$4); }
1129 stmts : stmt { $$ = $1; }
1130 | stmts SEMI stmt { $$ = lconc($1,$3); }
1133 stmt : /* empty */ { $$ = Lnil; }
1134 | letdecls { $$ = lsing(mkseqlet($1)); }
1135 | expL { $$ = lsing($1); }
1136 | {inpat=TRUE;} expLno {inpat=FALSE;} leftexp
1138 expORpat(LEGIT_EXPR,$2);
1139 $$ = lsing(mkdoexp($2,endlineno));
1141 expORpat(LEGIT_PATT,$2);
1142 $$ = lsing(mkdobind($2,$4,endlineno));
1147 leftexp : LARROW exp { $$ = $2; }
1148 | /* empty */ { $$ = NULL; }
1151 /**********************************************************************
1157 **********************************************************************/
1159 pat : pat qconop bpat { $$ = mkinfixap($2,$1,$3); }
1165 | qcon OCURLY rpats CCURLY { $$ = mkrecord($1,$3); }
1166 | MINUS INTEGER { $$ = mknegate(mklit(mkinteger($2))); }
1167 | MINUS FLOAT { $$ = mknegate(mklit(mkfloatr($2))); }
1170 conpat : gcon { $$ = mkident($1); }
1171 | conpat apat { $$ = mkap($1,$2); }
1174 apat : gcon { $$ = mkident($1); }
1175 | qcon OCURLY rpats CCURLY { $$ = mkrecord($1,$3); }
1179 apatc : qvar { $$ = mkident($1); }
1180 | qvar AT apat { $$ = mkas($1,$3); }
1181 | lit_constant { $$ = mklit($1); }
1182 | WILDCARD { $$ = mkwildp(); }
1183 | OPAREN pat CPAREN { $$ = mkpar($2); }
1184 | OPAREN pat COMMA pats CPAREN { $$ = mktuple(mklcons($2,$4)); }
1185 | OBRACK pats CBRACK { $$ = mkllist($2); }
1186 | LAZY apat { $$ = mklazyp($2); }
1190 INTEGER { $$ = mkinteger($1); }
1191 | FLOAT { $$ = mkfloatr($1); }
1192 | CHAR { $$ = mkcharr($1); }
1193 | STRING { $$ = mkstring($1); }
1194 | CHARPRIM { $$ = mkcharprim($1); }
1195 | STRINGPRIM { $$ = mkstringprim($1); }
1196 | INTPRIM { $$ = mkintprim($1); }
1197 | FLOATPRIM { $$ = mkfloatprim($1); }
1198 | DOUBLEPRIM { $$ = mkdoubleprim($1); }
1199 | CLITLIT /* yurble yurble */ { $$ = mkclitlit($1); }
1202 lampats : apat lampats { $$ = mklcons($1,$2); }
1203 | apat { $$ = lsing($1); }
1204 /* right recursion? (WDP) */
1207 pats : pat COMMA pats { $$ = mklcons($1, $3); }
1208 | pat { $$ = lsing($1); }
1209 /* right recursion? (WDP) */
1212 rpats : rpat { $$ = lsing($1); }
1213 | rpats COMMA rpat { $$ = lapp($1,$3); }
1216 rpat : qvar { $$ = mkrbind($1,mknothing()); }
1217 | qvar EQUAL pat { $$ = mkrbind($1,mkjust($3)); }
1221 patk : patk qconop bpat { $$ = mkinfixap($2,$1,$3); }
1227 | qconk OCURLY rpats CCURLY { $$ = mkrecord($1,$3); }
1228 | minuskey INTEGER { $$ = mknegate(mklit(mkinteger($2))); }
1229 | minuskey FLOAT { $$ = mknegate(mklit(mkfloatr($2))); }
1232 conpatk : gconk { $$ = mkident($1); }
1233 | conpatk apat { $$ = mkap($1,$2); }
1236 apatck : qvark { $$ = mkident($1); }
1237 | qvark AT apat { $$ = mkas($1,$3); }
1238 | lit_constant { $$ = mklit($1); setstartlineno(); }
1239 | WILDCARD { $$ = mkwildp(); setstartlineno(); }
1240 | oparenkey pat CPAREN { $$ = mkpar($2); }
1241 | oparenkey pat COMMA pats CPAREN { $$ = mktuple(mklcons($2,$4)); }
1242 | obrackkey pats CBRACK { $$ = mkllist($2); }
1243 | lazykey apat { $$ = mklazyp($2); }
1248 | OBRACK CBRACK { $$ = creategid(-1); }
1249 | OPAREN CPAREN { $$ = creategid(0); }
1250 | OPAREN commas CPAREN { $$ = creategid($2); }
1254 | obrackkey CBRACK { $$ = creategid(-1); }
1255 | oparenkey CPAREN { $$ = creategid(0); }
1256 | oparenkey commas CPAREN { $$ = creategid($2); }
1259 /**********************************************************************
1262 * Keywords which record the line start *
1265 **********************************************************************/
1267 importkey: IMPORT { setstartlineno(); }
1270 datakey : DATA { setstartlineno();
1273 printf("%u\n",startlineno);
1275 fprintf(stderr,"%u\tdata\n",startlineno);
1280 typekey : TYPE { setstartlineno();
1283 printf("%u\n",startlineno);
1285 fprintf(stderr,"%u\ttype\n",startlineno);
1290 newtypekey : NEWTYPE { setstartlineno();
1293 printf("%u\n",startlineno);
1295 fprintf(stderr,"%u\tnewtype\n",startlineno);
1300 instkey : INSTANCE { setstartlineno();
1303 printf("%u\n",startlineno);
1306 fprintf(stderr,"%u\tinstance\n",startlineno);
1311 defaultkey: DEFAULT { setstartlineno(); }
1314 classkey: CLASS { setstartlineno();
1317 printf("%u\n",startlineno);
1319 fprintf(stderr,"%u\tclass\n",startlineno);
1324 modulekey: MODULE { setstartlineno();
1327 printf("%u\n",startlineno);
1329 fprintf(stderr,"%u\tmodule\n",startlineno);
1334 oparenkey: OPAREN { setstartlineno(); }
1337 obrackkey: OBRACK { setstartlineno(); }
1340 lazykey : LAZY { setstartlineno(); }
1343 minuskey: MINUS { setstartlineno(); }
1347 /**********************************************************************
1350 * Basic qualified/unqualified ids/ops *
1353 **********************************************************************/
1356 | OPAREN qvarsym CPAREN { $$ = $2; }
1359 | OPAREN qconsym CPAREN { $$ = $2; }
1362 | BQUOTE qvarid BQUOTE { $$ = $2; }
1365 | BQUOTE qconid BQUOTE { $$ = $2; }
1371 /* Non "-" op, used in right sections */
1376 /* Non "-" varop, used in right sections */
1378 | varsym_nominus { $$ = mknoqual($1); }
1379 | BQUOTE qvarid BQUOTE { $$ = $2; }
1384 | OPAREN varsym CPAREN { $$ = $2; }
1386 con : tycon /* using tycon removes conflicts */
1387 | OPAREN CONSYM CPAREN { $$ = $2; }
1390 | BQUOTE varid BQUOTE { $$ = $2; }
1393 | BQUOTE CONID BQUOTE { $$ = $2; }
1399 qvark : qvarid { setstartlineno(); $$ = $1; }
1400 | oparenkey qvarsym CPAREN { $$ = $2; }
1402 qconk : qconid { setstartlineno(); $$ = $1; }
1403 | oparenkey qconsym CPAREN { $$ = $2; }
1405 vark : varid { setstartlineno(); $$ = $1; }
1406 | oparenkey varsym CPAREN { $$ = $2; }
1410 | varid { $$ = mknoqual($1); }
1413 | varsym { $$ = mknoqual($1); }
1416 | tycon { $$ = mknoqual($1); } /* using tycon removes conflicts */
1419 | CONSYM { $$ = mknoqual($1); }
1422 | tycon { $$ = mknoqual($1); } /* using tycon removes conflicts */
1425 | tycon { $$ = mknoqual($1); } /* using tycon removes conflicts */
1428 varsym : varsym_nominus
1429 | MINUS { $$ = install_literal("-"); }
1432 /* AS HIDING QUALIFIED are valid varids */
1434 | AS { $$ = install_literal("as"); }
1435 | HIDING { $$ = install_literal("hiding"); }
1436 | QUALIFIED { $$ = install_literal("qualified"); }
1439 /* BANG are valid varsyms */
1440 varsym_nominus : VARSYM
1441 | BANG { $$ = install_literal("!"); }
1448 tyvar : varid { $$ = mknamedtvar(mknoqual($1)); }
1455 tyvar_list: tyvar { $$ = lsing($1); }
1456 | tyvar_list COMMA tyvar { $$ = lapp($1,$3); }
1459 /**********************************************************************
1462 * Stuff to do with layout *
1465 **********************************************************************/
1467 ocurly : layout OCURLY { hsincindent(); }
1469 vocurly : layout { hssetindent(); }
1472 layout : { hsindentoff(); }
1478 FN = NULL; SAMEFN = 0; PREVPATT = NULL;
1483 vccurly : { expect_ccurly = 1; } vccurly1 { expect_ccurly = 0; }
1489 FN = NULL; SAMEFN = 0; PREVPATT = NULL;
1495 FN = NULL; SAMEFN = 0; PREVPATT = NULL;
1502 /**********************************************************************
1504 * Error Processing and Reporting *
1506 * (This stuff is here in case we want to use Yacc macros and such.) *
1508 **********************************************************************/
1514 hsperror("pattern syntax used in expression");
1518 /* The parser calls "hsperror" when it sees a
1519 `report this and die' error. It sets the stage
1520 and calls "yyerror".
1522 There should be no direct calls in the parser to
1523 "yyerror", except for the one from "hsperror". Thus,
1524 the only other calls will be from the error productions
1525 introduced by yacc/bison/whatever.
1527 We need to be able to recognise the from-error-production
1528 case, because we sometimes want to say, "Oh, never mind",
1529 because the layout rule kicks into action and may save
1533 static BOOLEAN error_and_I_mean_it = FALSE;
1539 error_and_I_mean_it = TRUE;
1543 extern char *yytext;
1550 /* We want to be able to distinguish 'error'-raised yyerrors
1551 from yyerrors explicitly coded by the parser hacker.
1553 if (expect_ccurly && ! error_and_I_mean_it ) {
1557 fprintf(stderr, "\"%s\", line %d, column %d: %s on input: ",
1558 input_filename, hsplineno, hspcolno + 1, s);
1560 if (yyleng == 1 && *yytext == '\0')
1561 fprintf(stderr, "<EOF>");
1565 format_string(stderr, (unsigned char *) yytext, yyleng);
1568 fputc('\n', stderr);
1570 /* a common problem */
1571 if (strcmp(yytext, "#") == 0)
1572 fprintf(stderr, "\t(Perhaps you forgot a `-cpp' or `-fglasgow-exts' flag?)\n");
1579 format_string(fp, s, len)
1586 case '\0': fputs("\\NUL", fp); break;
1587 case '\007': fputs("\\a", fp); break;
1588 case '\010': fputs("\\b", fp); break;
1589 case '\011': fputs("\\t", fp); break;
1590 case '\012': fputs("\\n", fp); break;
1591 case '\013': fputs("\\v", fp); break;
1592 case '\014': fputs("\\f", fp); break;
1593 case '\015': fputs("\\r", fp); break;
1594 case '\033': fputs("\\ESC", fp); break;
1595 case '\034': fputs("\\FS", fp); break;
1596 case '\035': fputs("\\GS", fp); break;
1597 case '\036': fputs("\\RS", fp); break;
1598 case '\037': fputs("\\US", fp); break;
1599 case '\177': fputs("\\DEL", fp); break;
1604 fprintf(fp, "\\^%c", *s + '@');