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;
50 extern list reverse_list();
53 /* For FN, PREVPATT and SAMEFN macros */
55 extern BOOLEAN samefn[];
56 extern tree prevpatt[];
57 extern short icontexts;
60 extern int hsplineno, hspcolno;
61 extern int modulelineno;
62 extern int startlineno;
65 /**********************************************************************
68 * Fixity and Precedence Declarations *
71 **********************************************************************/
73 static int Fixity = 0, Precedence = 0;
75 char *ineg PROTO((char *));
77 long source_version = 0;
102 /**********************************************************************
105 * These are lexemes. *
108 **********************************************************************/
111 %token VARID CONID QVARID QCONID
112 VARSYM CONSYM QVARSYM QCONSYM
114 %token INTEGER FLOAT CHAR STRING
115 CHARPRIM STRINGPRIM INTPRIM FLOATPRIM
120 /**********************************************************************
126 **********************************************************************/
128 %token OCURLY CCURLY VCCURLY
129 %token COMMA SEMI OBRACK CBRACK
130 %token WILDCARD BQUOTE OPAREN CPAREN
133 /**********************************************************************
136 * Reserved Operators *
139 **********************************************************************/
141 %token DOTDOT DCOLON EQUAL LAMBDA
142 %token VBAR RARROW LARROW
143 %token AT LAZY DARROW
146 /**********************************************************************
149 * Reserved Identifiers *
152 **********************************************************************/
154 %token CASE CLASS DATA
155 %token DEFAULT DERIVING DO
156 %token ELSE IF IMPORT
157 %token IN INFIX INFIXL
158 %token INFIXR INSTANCE LET
159 %token MODULE NEWTYPE OF
160 %token THEN TYPE WHERE
163 %token CCALL CCALL_GC CASM CASM_GC
166 /**********************************************************************
169 * Special symbols/identifiers which need to be recognised *
172 **********************************************************************/
174 %token MINUS BANG PLUS
175 %token AS HIDING QUALIFIED
178 /**********************************************************************
181 * Special Symbols for the Lexer *
184 **********************************************************************/
186 %token INTERFACE_UPRAGMA SPECIALISE_UPRAGMA
187 %token INLINE_UPRAGMA MAGIC_UNFOLDING_UPRAGMA
188 %token DEFOREST_UPRAGMA END_UPRAGMA
190 /**********************************************************************
193 * Precedences of the various tokens *
196 **********************************************************************/
201 SCC CASM CCALL CASM_GC CCALL_GC
203 %left VARSYM CONSYM QVARSYM QCONSYM
204 MINUS BQUOTE BANG DARROW PLUS
210 %left OCURLY OBRACK OPAREN
216 /**********************************************************************
219 * Type Declarations *
222 **********************************************************************/
225 %type <ulist> caserest alts alt quals
227 rbinds rbinds1 rpats rpats1 list_exps list_rest
229 constrs constr1 fields
232 pats context context_list /* tyvar_list */
235 impdecls maybeimpdecls impdecl
236 maybefixes fixes fix ops
241 %type <umaybe> maybeexports impspec deriving
243 %type <uliteral> lit_constant
245 %type <utree> exp oexp dexp kexp fexp aexp rbind texps
246 expL oexpL kexpL expLno oexpLno dexpLno kexpLno
247 vallhs funlhs qual leftexp
248 pat cpat bpat apat apatc conpat rpat
249 patk bpatk apatck conpatk
252 %type <uid> MINUS PLUS DARROW AS LAZY
253 VARID CONID VARSYM CONSYM
254 var con varop conop op
255 vark varid varsym varsym_nominus
258 %type <uqid> QVARID QCONID QVARSYM QCONSYM
259 qvarid qconid qvarsym qconsym
260 qvar qcon qvarop qconop qop
261 qvark qconk qtycon qtycls
262 gcon gconk gtycon itycon qop1 qvarop1
265 %type <ubinding> topdecl topdecls letdecls
266 typed datad newtd classd instd defaultd
267 decl decls valdef instdef instdefs
268 maybe_where cbody rinst type_and_maybe_id
270 %type <upbinding> valrhs1 altrest
272 %type <uttype> simple ctype sigtype sigarrowtype type atype bigatype btype
274 bbtype batype bxtype wierd_atype
277 %type <uconstr> constr constr_after_context field
279 %type <ustring> FLOAT INTEGER INTPRIM
280 FLOATPRIM DOUBLEPRIM CLITLIT
282 %type <uhstring> STRING STRINGPRIM CHAR CHARPRIM
284 %type <uentid> export import
288 /**********************************************************************
291 * Start Symbol for the Parser *
294 **********************************************************************/
299 module : modulekey modid maybeexports
301 modulelineno = startlineno;
302 the_module_name = $2;
308 the_module_name = install_literal("Main");
309 module_exports = mknothing();
314 body : ocurly { setstartlineno(); } interface_pragma orestm
315 | vocurly interface_pragma vrestm
318 interface_pragma : /* empty */
319 | INTERFACE_UPRAGMA INTEGER END_UPRAGMA SEMI
321 source_version = atoi($2);
325 orestm : maybeimpdecls maybefixes topdecls ccurly
327 root = mkhmodule(the_module_name,$1,module_exports,
328 $2,$3,source_version,modulelineno);
332 root = mkhmodule(the_module_name,$1,module_exports,
333 Lnil,mknullbind(),source_version,modulelineno);
336 vrestm : maybeimpdecls maybefixes topdecls vccurly
338 root = mkhmodule(the_module_name,$1,module_exports,
339 $2,$3,source_version,modulelineno);
343 root = mkhmodule(the_module_name,$1,module_exports,
344 Lnil,mknullbind(),source_version,modulelineno);
347 maybeexports : /* empty */ { $$ = mknothing(); }
348 | OPAREN export_list CPAREN { $$ = mkjust($2); }
349 | OPAREN export_list COMMA CPAREN { $$ = mkjust($2); }
353 export { $$ = lsing($1); }
354 | export_list COMMA export { $$ = lapp($1, $3); }
357 export : qvar { $$ = mkentid($1); }
358 | gtycon { $$ = mkenttype($1); }
359 | gtycon OPAREN DOTDOT CPAREN { $$ = mkenttypeall($1); }
360 | gtycon OPAREN CPAREN { $$ = mkenttypenamed($1,Lnil); }
361 | gtycon OPAREN enames CPAREN { $$ = mkenttypenamed($1,$3); }
362 | MODULE modid { $$ = mkentmod($2); }
365 enames : ename { $$ = lsing($1); }
366 | enames COMMA ename { $$ = lapp($1,$3); }
373 maybeimpdecls : /* empty */ { $$ = Lnil; }
374 | impdecls SEMI { $$ = $1; }
377 impdecls: impdecl { $$ = $1; }
378 | impdecls SEMI impdecl { $$ = lconc($1,$3); }
382 impdecl : importkey modid impspec
383 { $$ = lsing(mkimport($2,0,mknothing(),$3,startlineno)); }
384 | importkey QUALIFIED modid impspec
385 { $$ = lsing(mkimport($3,1,mknothing(),$4,startlineno)); }
386 | importkey QUALIFIED modid AS modid impspec
387 { $$ = lsing(mkimport($3,1,mkjust($5),$6,startlineno)); }
390 impspec : /* empty */ { $$ = mknothing(); }
391 | OPAREN CPAREN { $$ = mkjust(mkleft(Lnil)); }
392 | OPAREN import_list CPAREN { $$ = mkjust(mkleft($2)); }
393 | OPAREN import_list COMMA CPAREN { $$ = mkjust(mkleft($2)); }
394 | HIDING OPAREN import_list CPAREN { $$ = mkjust(mkright($3)); }
395 | HIDING OPAREN import_list COMMA CPAREN { $$ = mkjust(mkright($3)); }
399 import { $$ = lsing($1); }
400 | import_list COMMA import { $$ = lapp($1, $3); }
403 import : var { $$ = mkentid(mknoqual($1)); }
404 | itycon { $$ = mkenttype($1); }
405 | itycon OPAREN DOTDOT CPAREN { $$ = mkenttypeall($1); }
406 | itycon OPAREN CPAREN { $$ = mkenttypenamed($1,Lnil);}
407 | itycon OPAREN inames CPAREN { $$ = mkenttypenamed($1,$3); }
410 itycon : tycon { $$ = mknoqual($1); }
411 | OBRACK CBRACK { $$ = creategid(-1); }
412 | OPAREN CPAREN { $$ = creategid(0); }
413 | OPAREN commas CPAREN { $$ = creategid($2); }
416 inames : iname { $$ = lsing($1); }
417 | inames COMMA iname { $$ = lapp($1,$3); }
419 iname : var { $$ = mknoqual($1); }
420 | con { $$ = mknoqual($1); }
423 /**********************************************************************
426 * Fixes and Decls etc *
429 **********************************************************************/
431 maybefixes: /* empty */ { $$ = Lnil; }
432 | fixes SEMI { $$ = $1; }
435 fixes : fix { $$ = $1; }
436 | fixes SEMI fix { $$ = lconc($1,$3); }
439 fix : INFIXL INTEGER { Precedence = checkfixity($2); Fixity = INFIXL; }
441 | INFIXR INTEGER { Precedence = checkfixity($2); Fixity = INFIXR; }
443 | INFIX INTEGER { Precedence = checkfixity($2); Fixity = INFIX; }
445 | INFIXL { Fixity = INFIXL; Precedence = 9; }
447 | INFIXR { Fixity = INFIXR; Precedence = 9; }
449 | INFIX { Fixity = INFIX; Precedence = 9; }
453 ops : op { $$ = lsing(mkfixop(mknoqual($1),infixint(Fixity),Precedence)); }
454 | ops COMMA op { $$ = lapp($1,mkfixop(mknoqual($3),infixint(Fixity),Precedence)); }
458 | topdecls SEMI topdecl
477 topdecl : typed { $$ = $1; }
480 | classd { $$ = $1; }
482 | defaultd { $$ = $1; }
486 typed : typekey simple EQUAL type { $$ = mknbind($2,$4,startlineno); }
490 datad : datakey simple EQUAL constrs deriving
491 { $$ = mktbind(Lnil,$2,$4,$5,startlineno); }
492 | datakey context DARROW simple EQUAL constrs deriving
493 { $$ = mktbind($2,$4,$6,$7,startlineno); }
496 newtd : newtypekey simple EQUAL constr1 deriving
497 { $$ = mkntbind(Lnil,$2,$4,$5,startlineno); }
498 | newtypekey context DARROW simple EQUAL constr1 deriving
499 { $$ = mkntbind($2,$4,$6,$7,startlineno); }
502 deriving: /* empty */ { $$ = mknothing(); }
503 | DERIVING dtyclses { $$ = mkjust($2); }
506 classd : classkey context DARROW class cbody
507 { $$ = mkcbind($2,$4,$5,startlineno); }
508 | classkey class cbody
509 { $$ = mkcbind(Lnil,$2,$3,startlineno); }
512 cbody : /* empty */ { $$ = mknullbind(); }
513 | WHERE ocurly decls ccurly { checkorder($3); $$ = $3; }
514 | WHERE vocurly decls vccurly { checkorder($3); $$ = $3; }
517 instd : instkey context DARROW gtycon atype rinst
518 { $$ = mkibind($2,$4,$5,$6,startlineno); }
519 | instkey gtycon atype rinst
520 { $$ = mkibind(Lnil,$2,$3,$4,startlineno); }
523 rinst : /* empty */ { $$ = mknullbind(); }
524 | WHERE ocurly instdefs ccurly { $$ = $3; }
525 | WHERE vocurly instdefs vccurly { $$ = $3; }
528 /* I now allow a general type in instance declarations, relying
529 on the type checker to reject instance decls which are ill-formed.
530 Some (non-standard) extensions of Haskell may allow more general
531 types than the Report syntax permits, and in any case not all things
532 can be checked in the syntax (eg repeated type variables).
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 gtyconapp1 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); }
550 defaultd: defaultkey OPAREN types CPAREN { $$ = mkdbind($3,startlineno); }
551 | defaultkey OPAREN CPAREN { $$ = mkdbind(Lnil,startlineno); }
568 Note: if there is an iclasop_pragma here, then we must be
569 doing a class-op in an interface -- unless the user is up
570 to real mischief (ugly, but likely to work).
573 decl : qvarsk DCOLON sigtype
574 { $$ = mksbind($1,$3,startlineno);
575 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
578 /* User-specified pragmas come in as "signatures"...
579 They are similar in that they can appear anywhere in the module,
580 and have to be "joined up" with their related entity.
582 Have left out the case specialising to an overloaded type.
583 Let's get real, OK? (WDP)
585 | SPECIALISE_UPRAGMA qvark DCOLON types_and_maybe_ids END_UPRAGMA
587 $$ = mkvspec_uprag($2, $4, startlineno);
588 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
591 | SPECIALISE_UPRAGMA INSTANCE gtycon atype END_UPRAGMA
593 $$ = mkispec_uprag($3, $4, startlineno);
594 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
597 | SPECIALISE_UPRAGMA DATA gtycon atypes END_UPRAGMA
599 $$ = mkdspec_uprag($3, $4, startlineno);
600 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
603 | INLINE_UPRAGMA qvark END_UPRAGMA
605 $$ = mkinline_uprag($2, startlineno);
606 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
609 | MAGIC_UNFOLDING_UPRAGMA qvark vark END_UPRAGMA
611 $$ = mkmagicuf_uprag($2, $3, startlineno);
612 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
615 | DEFOREST_UPRAGMA qvark END_UPRAGMA
617 $$ = mkdeforest_uprag($2, startlineno);
618 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
621 /* end of user-specified pragmas */
624 | /* empty */ { $$ = mknullbind(); PREVPATT = NULL; FN = NULL; SAMEFN = 0; }
627 qvarsk : qvark COMMA qvars_list { $$ = mklcons($1,$3); }
628 | qvark { $$ = lsing($1); }
631 qvars_list: qvar { $$ = lsing($1); }
632 | qvars_list COMMA qvar { $$ = lapp($1,$3); }
635 types_and_maybe_ids :
636 type_and_maybe_id { $$ = lsing($1); }
637 | types_and_maybe_ids COMMA type_and_maybe_id { $$ = lapp($1,$3); }
641 type { $$ = mkvspec_ty_and_id($1,mknothing()); }
642 | type EQUAL qvark { $$ = mkvspec_ty_and_id($1,mkjust($3)); }
645 /**********************************************************************
651 **********************************************************************/
653 /* "DCOLON context => type" vs "DCOLON type" is a problem,
654 because you can't distinguish between
656 foo :: (Baz a, Baz a)
657 bar :: (Baz a, Baz a) => [a] -> [a] -> [a]
659 with one token of lookahead. The HACK is to have "DCOLON ttype"
660 [tuple type] in the first case, then check that it has the right
661 form C a, or (C1 a, C2 b, ... Cn z) and convert it into a
665 /* A sigtype is a rank 2 type; it can have for-alls as function args:
666 f :: All a => (All b => ...) -> Int
668 sigtype : type DARROW sigarrowtype { $$ = mkcontext(type2context($1),$3); }
672 sigarrowtype : bigatype RARROW sigarrowtype { $$ = mktfun($1,$3); }
673 | btype RARROW sigarrowtype { $$ = mktfun($1,$3); }
677 /* A "big" atype can be a forall-type in brackets. */
678 bigatype: OPAREN type DARROW type CPAREN { $$ = mkcontext(type2context($2),$4); }
681 /* 1 S/R conflict at DARROW -> shift */
682 ctype : type DARROW type { $$ = mkcontext(type2context($1),$3); }
686 /* 1 S/R conflict at RARROW -> shift */
687 type : btype RARROW type { $$ = mktfun($1,$3); }
691 btype : btype atype { $$ = mktapp($1,$2); }
695 atype : gtycon { $$ = mktname($1); }
697 | OPAREN type COMMA types CPAREN { $$ = mkttuple(mklcons($2,$4)); }
698 | OBRACK type CBRACK { $$ = mktllist($2); }
699 | OPAREN type CPAREN { $$ = $2; }
703 | OPAREN RARROW CPAREN { $$ = creategid(-2); }
704 | OBRACK CBRACK { $$ = creategid(-1); }
705 | OPAREN CPAREN { $$ = creategid(0); }
706 | OPAREN commas CPAREN { $$ = creategid($2); }
709 atypes : atype { $$ = lsing($1); }
710 | atypes atype { $$ = lapp($1,$2); }
713 types : type { $$ = lsing($1); }
714 | types COMMA type { $$ = lapp($1,$3); }
717 commas : COMMA { $$ = 1; }
718 | commas COMMA { $$ = $1 + 1; }
721 /**********************************************************************
724 * Declaration stuff *
727 **********************************************************************/
729 simple : gtycon { $$ = mktname($1); }
730 | gtyconvars { $$ = $1; }
733 gtyconvars: gtycon tyvar { $$ = mktapp(mktname($1),$2); }
734 | gtyconvars tyvar { $$ = mktapp($1,$2); }
737 context : OPAREN context_list CPAREN { $$ = $2; }
738 | class { $$ = lsing($1); }
741 context_list: class { $$ = lsing($1); }
742 | context_list COMMA class { $$ = lapp($1,$3); }
745 class : gtycon tyvar { $$ = mktapp(mktname($1),$2); }
748 constrs : constr { $$ = lsing($1); }
749 | constrs VBAR constr { $$ = lapp($1,$3); }
752 constr : constr_after_context
753 | type DARROW constr_after_context { $$ = mkconstrcxt ( type2context($1), $3 ); }
756 constr_after_context :
758 /* We have to parse the constructor application as a *type*, else we get
759 into terrible ambiguity problems. Consider the difference between
761 data T = S Int Int Int `R` Int
763 data T = S Int Int Int
765 It isn't till we get to the operator that we discover that the "S" is
766 part of a type in the first, but part of a constructor application in the
770 /* Con !Int (Tree a) */
771 contype { qid tyc; list tys;
772 splittyconapp($1, &tyc, &tys);
773 $$ = mkconstrpre(tyc,tys,hsplineno); }
775 /* !Int `Con` Tree a */
776 | bbtype qconop bbtype { $$ = mkconstrinf($1,$2,$3,hsplineno); }
778 /* (::) (Tree a) Int */
779 | OPAREN qconsym CPAREN batypes { $$ = mkconstrpre($2,$4,hsplineno); }
781 /* Con { op1 :: Int } */
782 | gtycon OCURLY fields CCURLY { $$ = mkconstrrec($1,$3,hsplineno); }
783 /* 1 S/R conflict on OCURLY -> shift */
787 /* contype has to reduce to a btype unless there are !'s, so that
788 we don't get reduce/reduce conflicts with the second production of constr.
789 But as soon as we see a ! we must switch to using bxtype. */
791 contype : btype { $$ = $1 }
795 /* S !Int Bool; at least one ! */
796 bxtype : btype wierd_atype { $$ = mktapp($1, $2); }
797 | bxtype batype { $$ = mktapp($1, $2); }
800 bbtype : btype { $$ = $1; }
801 | wierd_atype { $$ = $1; }
804 batype : atype { $$ = $1; }
805 | wierd_atype { $$ = $1; }
808 /* A wierd atype is one that isn't a regular atype;
809 it starts with a "!", or with a forall. */
810 wierd_atype : BANG bigatype { $$ = mktbang( $2 ) }
811 | BANG atype { $$ = mktbang( $2 ) }
815 batypes : { $$ = Lnil; }
816 | batypes batype { $$ = lapp($1,$2); }
820 fields : field { $$ = lsing($1); }
821 | fields COMMA field { $$ = lapp($1,$3); }
824 field : qvars_list DCOLON ctype { $$ = mkfield($1,$3); }
825 | qvars_list DCOLON BANG atype { $$ = mkfield($1,mktbang($4)); }
826 | qvars_list DCOLON BANG bigatype { $$ = mkfield($1,mktbang($4)); }
829 constr1 : gtycon atype { $$ = lsing(mkconstrnew($1,$2,hsplineno)); }
833 dtyclses: OPAREN dtycls_list CPAREN { $$ = $2; }
834 | OPAREN CPAREN { $$ = Lnil; }
835 | qtycls { $$ = lsing($1); }
838 dtycls_list: qtycls { $$ = lsing($1); }
839 | dtycls_list COMMA qtycls { $$ = lapp($1,$3); }
842 instdefs : /* empty */ { $$ = mknullbind(); }
843 | instdef { $$ = $1; }
844 | instdefs SEMI instdef
856 /* instdef: same as valdef, except certain user-pragmas may appear */
858 SPECIALISE_UPRAGMA qvark DCOLON types_and_maybe_ids END_UPRAGMA
860 $$ = mkvspec_uprag($2, $4, startlineno);
861 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
864 | INLINE_UPRAGMA qvark END_UPRAGMA
866 $$ = mkinline_uprag($2, startlineno);
867 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
870 | MAGIC_UNFOLDING_UPRAGMA qvark vark END_UPRAGMA
872 $$ = mkmagicuf_uprag($2, $3, startlineno);
873 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
882 tree fn = function($1);
885 if(ttree(fn) == ident)
887 qid fun_id = gident((struct Sident *) fn);
892 else if (ttree(fn) == infixap)
894 qid fun_id = ginffun((struct Sinfixap *) fn);
901 printf("%u\n",startlineno);
903 fprintf(stderr,"%u\tvaldef\n",startlineno);
908 if ( lhs_is_patt($1) )
910 $$ = mkpbind($3, startlineno);
915 $$ = mkfbind($3,startlineno);
921 vallhs : patk { $$ = $1; }
922 | patk qvarop pat { $$ = mkinfixap($2,$1,$3); }
923 | funlhs { $$ = $1; }
926 funlhs : qvark apat { $$ = mkap(mkident($1),$2); }
927 | funlhs apat { $$ = mkap($1,$2); }
931 valrhs : valrhs1 maybe_where { $$ = lsing(createpat($1, $2)); }
934 valrhs1 : gdrhs { $$ = mkpguards($1); }
935 | EQUAL exp { $$ = mkpnoguards($2); }
938 gdrhs : gd EQUAL exp { $$ = lsing(mkpgdexp($1,$3)); }
939 | gd EQUAL exp gdrhs { $$ = mklcons(mkpgdexp($1,$3),$4); }
943 WHERE ocurly decls ccurly { $$ = $3; }
944 | WHERE vocurly decls vccurly { $$ = $3; }
945 /* A where containing no decls is OK */
946 | WHERE SEMI { $$ = mknullbind(); }
947 | /* empty */ { $$ = mknullbind(); }
950 gd : VBAR quals { $$ = $2; }
954 /**********************************************************************
960 **********************************************************************/
962 exp : oexp DCOLON ctype { $$ = mkrestr($1,$3); }
967 Operators must be left-associative at the same precedence for
968 precedence parsing to work.
970 /* 8 S/R conflicts on qop -> shift */
971 oexp : oexp qop oexp %prec MINUS { $$ = mkinfixap($2,$1,$3); }
976 This comes here because of the funny precedence rules concerning
979 dexp : MINUS kexp { $$ = mknegate($2); }
984 We need to factor out a leading let expression so we can set
985 inpat=TRUE when parsing (non let) expressions inside stmts and quals
987 expLno : oexpLno DCOLON ctype { $$ = mkrestr($1,$3); }
990 oexpLno : oexpLno qop oexp %prec MINUS { $$ = mkinfixap($2,$1,$3); }
993 dexpLno : MINUS kexp { $$ = mknegate($2); }
997 expL : oexpL DCOLON ctype { $$ = mkrestr($1,$3); }
1000 oexpL : oexpL qop oexp %prec MINUS { $$ = mkinfixap($2,$1,$3); }
1005 let/if/lambda/case have higher precedence than infix operators.
1012 /* kexpL = a let expression */
1013 kexpL : letdecls IN exp { $$ = mklet($1,$3); }
1016 /* kexpLno = any other expression more tightly binding than operator application */
1018 { hsincindent(); /* push new context for FN = NULL; */
1019 FN = NULL; /* not actually concerned about indenting */
1020 $<ulong>$ = hsplineno; /* remember current line number */
1025 RARROW exp /* lambda abstraction */
1027 $$ = mklambda($3, $6, $<ulong>2);
1031 | IF {$<ulong>$ = hsplineno;}
1032 exp THEN exp ELSE exp { $$ = mkife($3,$5,$7,$<ulong>2); }
1034 /* Case Expression */
1035 | CASE {$<ulong>$ = hsplineno;}
1036 exp OF caserest { $$ = mkcasee($3,$5,$<ulong>2); }
1039 | DO {$<ulong>$ = hsplineno;}
1040 dorest { $$ = mkdoe($3,$<ulong>2); }
1042 /* CCALL/CASM Expression */
1043 | CCALL ccallid cexps { $$ = mkccall($2,install_literal("n"),$3); }
1044 | CCALL ccallid { $$ = mkccall($2,install_literal("n"),Lnil); }
1045 | CCALL_GC ccallid cexps { $$ = mkccall($2,install_literal("p"),$3); }
1046 | CCALL_GC ccallid { $$ = mkccall($2,install_literal("p"),Lnil); }
1047 | CASM CLITLIT cexps { $$ = mkccall($2,install_literal("N"),$3); }
1048 | CASM CLITLIT { $$ = mkccall($2,install_literal("N"),Lnil); }
1049 | CASM_GC CLITLIT cexps { $$ = mkccall($2,install_literal("P"),$3); }
1050 | CASM_GC CLITLIT { $$ = mkccall($2,install_literal("P"),Lnil); }
1052 /* SCC Expression */
1063 fexp : fexp aexp { $$ = mkap($1,$2); }
1067 /* simple expressions */
1068 aexp : qvar { $$ = mkident($1); }
1069 | gcon { $$ = mkident($1); }
1070 | lit_constant { $$ = mklit($1); }
1071 | OPAREN exp CPAREN { $$ = mkpar($2); } /* mkpar: stop infix parsing at ()'s */
1072 | qcon OCURLY rbinds CCURLY { $$ = mkrecord($1,$3); } /* 1 S/R conflict on OCURLY -> shift */
1073 | OBRACK list_exps CBRACK { $$ = mkllist($2); }
1074 | OPAREN exp COMMA texps CPAREN { if (ttree($4) == tuple)
1075 $$ = mktuple(mklcons($2, gtuplelist((struct Stuple *) $4)));
1077 $$ = mktuple(ldub($2, $4)); }
1079 /* only in expressions ... */
1080 | aexp OCURLY rbinds1 CCURLY { $$ = mkrupdate($1,$3); }
1081 | OBRACK exp VBAR quals CBRACK { $$ = mkcomprh($2,$4); }
1082 | OBRACK exp COMMA exp DOTDOT exp CBRACK {$$= mkeenum($2,mkjust($4),mkjust($6)); }
1083 | OBRACK exp COMMA exp DOTDOT CBRACK { $$ = mkeenum($2,mkjust($4),mknothing()); }
1084 | OBRACK exp DOTDOT exp CBRACK { $$ = mkeenum($2,mknothing(),mkjust($4)); }
1085 | OBRACK exp DOTDOT CBRACK { $$ = mkeenum($2,mknothing(),mknothing()); }
1086 | OPAREN oexp qop CPAREN { $$ = mklsection($2,$3); }
1087 | OPAREN qop1 oexp CPAREN { $$ = mkrsection($2,$3); }
1089 /* only in patterns ... */
1090 /* these add 2 S/R conflict with with aexp . OCURLY rbinds CCURLY */
1091 | qvar AT aexp { checkinpat(); $$ = mkas($1,$3); }
1092 | LAZY aexp { checkinpat(); $$ = mklazyp($2); }
1093 | WILDCARD { checkinpat(); $$ = mkwildp(); }
1096 /* ccall arguments */
1097 cexps : cexps aexp { $$ = lapp($1,$2); }
1098 | aexp { $$ = lsing($1); }
1101 caserest: ocurly alts ccurly { $$ = $2; }
1102 | vocurly alts vccurly { $$ = $2; }
1104 dorest : ocurly stmts ccurly { checkdostmts($2); $$ = $2; }
1105 | vocurly stmts vccurly { checkdostmts($2); $$ = $2; }
1108 rbinds : /* empty */ { $$ = Lnil; }
1112 rbinds1 : rbind { $$ = lsing($1); }
1113 | rbinds1 COMMA rbind { $$ = lapp($1,$3); }
1116 rbind : qvar { $$ = mkrbind($1,mknothing()); }
1117 | qvar EQUAL exp { $$ = mkrbind($1,mkjust($3)); }
1120 texps : exp { $$ = mkpar($1); } /* mkpar: so we don't flatten last element in tuple */
1122 { if (ttree($3) == tuple)
1123 $$ = mktuple(mklcons($1, gtuplelist((struct Stuple *) $3)));
1124 else if (ttree($3) == par)
1125 $$ = mktuple(ldub($1, gpare((struct Spar *) $3)));
1127 hsperror("hsparser:texps: panic");
1129 /* right recursion? WDP */
1133 exp { $$ = lsing($1); }
1134 | exp COMMA exp { $$ = mklcons( $1, lsing($3) ); }
1135 | exp COMMA exp COMMA list_rest { $$ = mklcons( $1, mklcons( $3, reverse_list( $5 ))); }
1138 /* Use left recusion for list_rest, because we sometimes get programs with
1139 very long explicit lists. */
1140 list_rest : exp { $$ = lsing($1); }
1141 | list_rest COMMA exp { $$ = mklcons( $3, $1 ); }
1145 exp { $$ = lsing($1); }
1146 | exp COMMA list_exps { $$ = mklcons($1, $3); }
1148 /* right recursion? (WDP)
1150 It has to be this way, though, otherwise you
1151 may do the wrong thing to distinguish between...
1153 [ e1 , e2 .. ] -- an enumeration ...
1154 [ e1 , e2 , e3 ] -- a list
1156 (In fact, if you change the grammar and throw yacc/bison
1157 at it, it *will* do the wrong thing [WDP 94/06])
1160 letdecls: LET ocurly decls ccurly { $$ = $3 }
1161 | LET vocurly decls vccurly { $$ = $3 }
1164 quals : qual { $$ = lsing($1); }
1165 | quals COMMA qual { $$ = lapp($1,$3); }
1168 qual : letdecls { $$ = mkseqlet($1); }
1170 | {inpat=TRUE;} expLno
1171 {inpat=FALSE;} leftexp
1173 expORpat(LEGIT_EXPR,$2);
1176 expORpat(LEGIT_PATT,$2);
1182 alts : alt { $$ = $1; }
1183 | alts SEMI alt { $$ = lconc($1,$3); }
1186 alt : pat { PREVPATT = $1; } altrest { $$ = lsing($3); PREVPATT = NULL; }
1187 | /* empty */ { $$ = Lnil; }
1190 altrest : gdpat maybe_where { $$ = createpat(mkpguards($1), $2); }
1191 | RARROW exp maybe_where { $$ = createpat(mkpnoguards($2),$3); }
1194 gdpat : gd RARROW exp { $$ = lsing(mkpgdexp($1,$3)); }
1195 | gd RARROW exp gdpat { $$ = mklcons(mkpgdexp($1,$3),$4); }
1198 stmts : stmt { $$ = $1; }
1199 | stmts SEMI stmt { $$ = lconc($1,$3); }
1202 stmt : /* empty */ { $$ = Lnil; }
1203 | letdecls { $$ = lsing(mkseqlet($1)); }
1204 | expL { $$ = lsing(mkdoexp($1,hsplineno)); }
1205 | {inpat=TRUE;} expLno {inpat=FALSE;} leftexp
1207 expORpat(LEGIT_EXPR,$2);
1208 $$ = lsing(mkdoexp($2,endlineno));
1210 expORpat(LEGIT_PATT,$2);
1211 $$ = lsing(mkdobind($2,$4,endlineno));
1216 leftexp : LARROW exp { $$ = $2; }
1217 | /* empty */ { $$ = NULL; }
1220 /**********************************************************************
1226 **********************************************************************/
1228 pat : qvar PLUS INTEGER { $$ = mkplusp($1, mkinteger($3)); }
1232 cpat : cpat qconop bpat { $$ = mkinfixap($2,$1,$3); }
1238 | qcon OCURLY rpats CCURLY { $$ = mkrecord($1,$3); }
1239 | MINUS INTEGER { $$ = mknegate(mklit(mkinteger($2))); }
1240 | MINUS FLOAT { $$ = mknegate(mklit(mkfloatr($2))); }
1243 conpat : gcon { $$ = mkident($1); }
1244 | conpat apat { $$ = mkap($1,$2); }
1247 apat : gcon { $$ = mkident($1); }
1248 | qcon OCURLY rpats CCURLY { $$ = mkrecord($1,$3); }
1252 apatc : qvar { $$ = mkident($1); }
1253 | qvar AT apat { $$ = mkas($1,$3); }
1254 | lit_constant { $$ = mklit($1); }
1255 | WILDCARD { $$ = mkwildp(); }
1256 | OPAREN pat CPAREN { $$ = mkpar($2); }
1257 | OPAREN pat COMMA pats CPAREN { $$ = mktuple(mklcons($2,$4)); }
1258 | OBRACK pats CBRACK { $$ = mkllist($2); }
1259 | LAZY apat { $$ = mklazyp($2); }
1263 INTEGER { $$ = mkinteger($1); }
1264 | FLOAT { $$ = mkfloatr($1); }
1265 | CHAR { $$ = mkcharr($1); }
1266 | STRING { $$ = mkstring($1); }
1267 | CHARPRIM { $$ = mkcharprim($1); }
1268 | STRINGPRIM { $$ = mkstringprim($1); }
1269 | INTPRIM { $$ = mkintprim($1); }
1270 | FLOATPRIM { $$ = mkfloatprim($1); }
1271 | DOUBLEPRIM { $$ = mkdoubleprim($1); }
1272 | CLITLIT /* yurble yurble */ { $$ = mkclitlit($1); }
1275 lampats : apat lampats { $$ = mklcons($1,$2); }
1276 | apat { $$ = lsing($1); }
1277 /* right recursion? (WDP) */
1280 pats : pat COMMA pats { $$ = mklcons($1, $3); }
1281 | pat { $$ = lsing($1); }
1282 /* right recursion? (WDP) */
1285 rpats : /* empty */ { $$ = Lnil; }
1289 rpats1 : rpat { $$ = lsing($1); }
1290 | rpats1 COMMA rpat { $$ = lapp($1,$3); }
1293 rpat : qvar { $$ = mkrbind($1,mknothing()); }
1294 | qvar EQUAL pat { $$ = mkrbind($1,mkjust($3)); }
1298 patk : patk qconop bpat { $$ = mkinfixap($2,$1,$3); }
1304 | qconk OCURLY rpats CCURLY { $$ = mkrecord($1,$3); }
1305 | minuskey INTEGER { $$ = mknegate(mklit(mkinteger($2))); }
1306 | minuskey FLOAT { $$ = mknegate(mklit(mkfloatr($2))); }
1309 conpatk : gconk { $$ = mkident($1); }
1310 | conpatk apat { $$ = mkap($1,$2); }
1313 apatck : qvark { $$ = mkident($1); }
1314 | qvark AT apat { $$ = mkas($1,$3); }
1315 | lit_constant { $$ = mklit($1); setstartlineno(); }
1316 | WILDCARD { $$ = mkwildp(); setstartlineno(); }
1317 | oparenkey pat CPAREN { $$ = mkpar($2); }
1318 | oparenkey pat COMMA pats CPAREN { $$ = mktuple(mklcons($2,$4)); }
1319 | obrackkey pats CBRACK { $$ = mkllist($2); }
1320 | lazykey apat { $$ = mklazyp($2); }
1325 | OBRACK CBRACK { $$ = creategid(-1); }
1326 | OPAREN CPAREN { $$ = creategid(0); }
1327 | OPAREN commas CPAREN { $$ = creategid($2); }
1331 | obrackkey CBRACK { $$ = creategid(-1); }
1332 | oparenkey CPAREN { $$ = creategid(0); }
1333 | oparenkey commas CPAREN { $$ = creategid($2); }
1336 /**********************************************************************
1339 * Keywords which record the line start *
1342 **********************************************************************/
1344 importkey: IMPORT { setstartlineno(); }
1347 datakey : DATA { setstartlineno();
1350 printf("%u\n",startlineno);
1352 fprintf(stderr,"%u\tdata\n",startlineno);
1357 typekey : TYPE { setstartlineno();
1360 printf("%u\n",startlineno);
1362 fprintf(stderr,"%u\ttype\n",startlineno);
1367 newtypekey : NEWTYPE { setstartlineno();
1370 printf("%u\n",startlineno);
1372 fprintf(stderr,"%u\tnewtype\n",startlineno);
1377 instkey : INSTANCE { setstartlineno();
1380 printf("%u\n",startlineno);
1383 fprintf(stderr,"%u\tinstance\n",startlineno);
1388 defaultkey: DEFAULT { setstartlineno(); }
1391 classkey: CLASS { setstartlineno();
1394 printf("%u\n",startlineno);
1396 fprintf(stderr,"%u\tclass\n",startlineno);
1401 modulekey: MODULE { setstartlineno();
1404 printf("%u\n",startlineno);
1406 fprintf(stderr,"%u\tmodule\n",startlineno);
1411 oparenkey: OPAREN { setstartlineno(); }
1414 obrackkey: OBRACK { setstartlineno(); }
1417 lazykey : LAZY { setstartlineno(); }
1420 minuskey: MINUS { setstartlineno(); }
1424 /**********************************************************************
1427 * Basic qualified/unqualified ids/ops *
1430 **********************************************************************/
1433 | OPAREN qvarsym CPAREN { $$ = $2; }
1436 | OPAREN qconsym CPAREN { $$ = $2; }
1439 | BQUOTE qvarid BQUOTE { $$ = $2; }
1442 | BQUOTE qconid BQUOTE { $$ = $2; }
1448 /* Non "-" op, used in right sections */
1453 /* Non "-" varop, used in right sections */
1455 | varsym_nominus { $$ = mknoqual($1); }
1456 | BQUOTE qvarid BQUOTE { $$ = $2; }
1461 | OPAREN varsym CPAREN { $$ = $2; }
1463 con : tycon /* using tycon removes conflicts */
1464 | OPAREN CONSYM CPAREN { $$ = $2; }
1467 | BQUOTE varid BQUOTE { $$ = $2; }
1470 | BQUOTE CONID BQUOTE { $$ = $2; }
1476 qvark : qvarid { setstartlineno(); $$ = $1; }
1477 | oparenkey qvarsym CPAREN { $$ = $2; }
1479 qconk : qconid { setstartlineno(); $$ = $1; }
1480 | oparenkey qconsym CPAREN { $$ = $2; }
1482 vark : varid { setstartlineno(); $$ = $1; }
1483 | oparenkey varsym CPAREN { $$ = $2; }
1487 | varid { $$ = mknoqual($1); }
1490 | varsym { $$ = mknoqual($1); }
1493 | tycon { $$ = mknoqual($1); } /* using tycon removes conflicts */
1496 | CONSYM { $$ = mknoqual($1); }
1499 | tycon { $$ = mknoqual($1); } /* using tycon removes conflicts */
1502 | tycon { $$ = mknoqual($1); } /* using tycon removes conflicts */
1505 varsym : varsym_nominus
1506 | MINUS { $$ = install_literal("-"); }
1509 /* PLUS, BANG are valid varsyms */
1510 varsym_nominus : VARSYM
1511 | PLUS { $$ = install_literal("+"); }
1512 | BANG { $$ = install_literal("!"); }
1515 /* AS HIDING QUALIFIED are valid varids */
1517 | AS { $$ = install_literal("as"); }
1518 | HIDING { $$ = install_literal("hiding"); }
1519 | QUALIFIED { $$ = install_literal("qualified"); }
1527 tyvar : varid { $$ = mknamedtvar(mknoqual($1)); }
1535 tyvar_list: tyvar { $$ = lsing($1); }
1536 | tyvar_list COMMA tyvar { $$ = lapp($1,$3); }
1540 /**********************************************************************
1543 * Stuff to do with layout *
1546 **********************************************************************/
1548 ocurly : layout OCURLY { hsincindent(); }
1550 vocurly : layout { hssetindent(); }
1553 layout : { hsindentoff(); }
1559 FN = NULL; SAMEFN = 0; PREVPATT = NULL;
1564 vccurly : { expect_ccurly = 1; } vccurly1 { expect_ccurly = 0; }
1570 FN = NULL; SAMEFN = 0; PREVPATT = NULL;
1576 FN = NULL; SAMEFN = 0; PREVPATT = NULL;
1583 /**********************************************************************
1585 * Error Processing and Reporting *
1587 * (This stuff is here in case we want to use Yacc macros and such.) *
1589 **********************************************************************/
1595 hsperror("pattern syntax used in expression");
1599 /* The parser calls "hsperror" when it sees a
1600 `report this and die' error. It sets the stage
1601 and calls "yyerror".
1603 There should be no direct calls in the parser to
1604 "yyerror", except for the one from "hsperror". Thus,
1605 the only other calls will be from the error productions
1606 introduced by yacc/bison/whatever.
1608 We need to be able to recognise the from-error-production
1609 case, because we sometimes want to say, "Oh, never mind",
1610 because the layout rule kicks into action and may save
1614 static BOOLEAN error_and_I_mean_it = FALSE;
1620 error_and_I_mean_it = TRUE;
1624 extern char *yytext;
1631 /* We want to be able to distinguish 'error'-raised yyerrors
1632 from yyerrors explicitly coded by the parser hacker.
1634 if (expect_ccurly && ! error_and_I_mean_it ) {
1638 fprintf(stderr, "%s:%d:%d: %s on input: ",
1639 input_filename, hsplineno, hspcolno + 1, s);
1641 if (yyleng == 1 && *yytext == '\0')
1642 fprintf(stderr, "<EOF>");
1646 format_string(stderr, (unsigned char *) yytext, yyleng);
1649 fputc('\n', stderr);
1651 /* a common problem */
1652 if (strcmp(yytext, "#") == 0)
1653 fprintf(stderr, "\t(Perhaps you forgot a `-cpp' or `-fglasgow-exts' flag?)\n");
1660 format_string(fp, s, len)
1667 case '\0': fputs("\\NUL", fp); break;
1668 case '\007': fputs("\\a", fp); break;
1669 case '\010': fputs("\\b", fp); break;
1670 case '\011': fputs("\\t", fp); break;
1671 case '\012': fputs("\\n", fp); break;
1672 case '\013': fputs("\\v", fp); break;
1673 case '\014': fputs("\\f", fp); break;
1674 case '\015': fputs("\\r", fp); break;
1675 case '\033': fputs("\\ESC", fp); break;
1676 case '\034': fputs("\\FS", fp); break;
1677 case '\035': fputs("\\GS", fp); break;
1678 case '\036': fputs("\\RS", fp); break;
1679 case '\037': fputs("\\US", fp); break;
1680 case '\177': fputs("\\DEL", fp); break;
1685 fprintf(fp, "\\^%c", *s + '@');