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
189 %token SOURCE_UPRAGMA
191 /**********************************************************************
194 * Precedences of the various tokens *
197 **********************************************************************/
202 SCC CASM CCALL CASM_GC CCALL_GC
204 %left VARSYM CONSYM QVARSYM QCONSYM
205 MINUS BQUOTE BANG DARROW PLUS
211 %left OCURLY OBRACK OPAREN
217 /**********************************************************************
220 * Type Declarations *
223 **********************************************************************/
226 %type <ulist> caserest alts alt quals
228 rbinds rbinds1 rpats rpats1 list_exps list_rest
230 constrs constr1 fields
233 pats simple_context simple_context_list
236 impdecls maybeimpdecls impdecl
237 maybefixes fixes fix ops
242 %type <umaybe> maybeexports impspec deriving
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 leftexp
249 pat cpat bpat apat apatc conpat rpat
250 patk bpatk apatck conpatk
253 %type <uid> MINUS PLUS DARROW AS LAZY
254 VARID CONID VARSYM CONSYM
255 var con varop conop op
256 vark varid varsym varsym_nominus
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 itycon 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> ctype sigtype sigarrowtype type atype bigatype btype
274 bbtype batype bxtype wierd_atype
275 simple_con_app simple_con_app1 tyvar contype inst_type
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
286 %type <ulong> commas importkey get_line_no
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,$1,startlineno)); }
384 | importkey QUALIFIED modid impspec
385 { $$ = lsing(mkimport($3,1,mknothing(),$4,$1,startlineno)); }
386 | importkey QUALIFIED modid AS modid impspec
387 { $$ = lsing(mkimport($3,1,mkjust($5),$6,$1,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,startlineno)); }
454 | ops COMMA op { $$ = lapp($1,mkfixop(mknoqual($3),infixint(Fixity),Precedence,startlineno)); }
458 | topdecls SEMI topdecl
477 topdecl : typed { $$ = $1; FN = NULL; SAMEFN = 0; }
478 | datad { $$ = $1; FN = NULL; SAMEFN = 0; }
479 | newtd { $$ = $1; FN = NULL; SAMEFN = 0; }
480 | classd { $$ = $1; FN = NULL; SAMEFN = 0; }
481 | instd { $$ = $1; FN = NULL; SAMEFN = 0; }
482 | defaultd { $$ = $1; FN = NULL; SAMEFN = 0; }
486 typed : typekey simple_con_app EQUAL type { $$ = mknbind($2,$4,startlineno); }
490 datad : datakey simple_con_app EQUAL constrs deriving
491 { $$ = mktbind(Lnil,$2,$4,$5,startlineno); }
492 | datakey simple_context DARROW simple_con_app EQUAL constrs deriving
493 { $$ = mktbind($2,$4,$6,$7,startlineno); }
496 newtd : newtypekey simple_con_app EQUAL constr1 deriving
497 { $$ = mkntbind(Lnil,$2,$4,$5,startlineno); }
498 | newtypekey simple_context DARROW simple_con_app EQUAL constr1 deriving
499 { $$ = mkntbind($2,$4,$6,$7,startlineno); }
502 deriving: /* empty */ { $$ = mknothing(); }
503 | DERIVING dtyclses { $$ = mkjust($2); }
506 classd : classkey simple_context DARROW simple_con_app1 cbody
507 { $$ = mkcbind($2,$4,$5,startlineno); }
508 | classkey simple_con_app1 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 inst_type rinst { $$ = mkibind($2,$3,startlineno); }
521 inst_type : type DARROW type { is_context_format( $3, 0 ); /* Check the instance head */
522 $$ = mkcontext(type2context($1),$3); }
523 | type { is_context_format( $1, 0 ); /* Check the instance head */
528 rinst : /* empty */ { $$ = mknullbind(); }
529 | WHERE ocurly instdefs ccurly { $$ = $3; }
530 | WHERE vocurly instdefs vccurly { $$ = $3; }
533 defaultd: defaultkey OPAREN types CPAREN { $$ = mkdbind($3,startlineno); }
534 | defaultkey OPAREN CPAREN { $$ = mkdbind(Lnil,startlineno); }
551 Note: if there is an iclasop_pragma here, then we must be
552 doing a class-op in an interface -- unless the user is up
553 to real mischief (ugly, but likely to work).
556 decl : qvarsk DCOLON sigtype
557 { $$ = mksbind($1,$3,startlineno);
558 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
561 /* User-specified pragmas come in as "signatures"...
562 They are similar in that they can appear anywhere in the module,
563 and have to be "joined up" with their related entity.
565 Have left out the case specialising to an overloaded type.
566 Let's get real, OK? (WDP)
568 | SPECIALISE_UPRAGMA qvark DCOLON types_and_maybe_ids END_UPRAGMA
570 $$ = mkvspec_uprag($2, $4, startlineno);
571 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
574 | SPECIALISE_UPRAGMA INSTANCE gtycon atype END_UPRAGMA
576 $$ = mkispec_uprag($3, $4, startlineno);
577 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
580 | SPECIALISE_UPRAGMA DATA gtycon atypes END_UPRAGMA
582 $$ = mkdspec_uprag($3, $4, startlineno);
583 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
586 | INLINE_UPRAGMA qvark END_UPRAGMA
588 $$ = mkinline_uprag($2, startlineno);
589 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
592 | MAGIC_UNFOLDING_UPRAGMA qvark vark END_UPRAGMA
594 $$ = mkmagicuf_uprag($2, $3, startlineno);
595 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
598 /* end of user-specified pragmas */
601 | /* empty */ { $$ = mknullbind(); PREVPATT = NULL; FN = NULL; SAMEFN = 0; }
604 qvarsk : qvark COMMA qvars_list { $$ = mklcons($1,$3); }
605 | qvark { $$ = lsing($1); }
608 qvars_list: qvar { $$ = lsing($1); }
609 | qvars_list COMMA qvar { $$ = lapp($1,$3); }
612 types_and_maybe_ids :
613 type_and_maybe_id { $$ = lsing($1); }
614 | types_and_maybe_ids COMMA type_and_maybe_id { $$ = lapp($1,$3); }
618 type { $$ = mkvspec_ty_and_id($1,mknothing()); }
619 | type EQUAL qvark { $$ = mkvspec_ty_and_id($1,mkjust($3)); }
622 /**********************************************************************
628 **********************************************************************/
630 /* "DCOLON context => type" vs "DCOLON type" is a problem,
631 because you can't distinguish between
633 foo :: (Baz a, Baz a)
634 bar :: (Baz a, Baz a) => [a] -> [a] -> [a]
636 with one token of lookahead. The HACK is to have "DCOLON ttype"
637 [tuple type] in the first case, then check that it has the right
638 form C a, or (C1 a, C2 b, ... Cn z) and convert it into a
642 /* A sigtype is a rank 2 type; it can have for-alls as function args:
643 f :: All a => (All b => ...) -> Int
645 sigtype : type DARROW sigarrowtype { $$ = mkcontext(type2context($1),$3); }
649 sigarrowtype : bigatype RARROW sigarrowtype { $$ = mktfun($1,$3); }
650 | btype RARROW sigarrowtype { $$ = mktfun($1,$3); }
654 /* A "big" atype can be a forall-type in brackets. */
655 bigatype: OPAREN type DARROW type CPAREN { $$ = mkcontext(type2context($2),$4); }
658 /* 1 S/R conflict at DARROW -> shift */
659 ctype : type DARROW type { $$ = mkcontext(type2context($1),$3); }
663 /* 1 S/R conflict at RARROW -> shift */
664 type : btype RARROW type { $$ = mktfun($1,$3); }
668 btype : btype atype { $$ = mktapp($1,$2); }
672 atype : gtycon { $$ = mktname($1); }
674 | OPAREN type COMMA types CPAREN { $$ = mkttuple(mklcons($2,$4)); }
675 | OBRACK type CBRACK { $$ = mktllist($2); }
676 | OPAREN type CPAREN { $$ = $2; }
680 | OPAREN RARROW CPAREN { $$ = creategid(-2); }
681 | OBRACK CBRACK { $$ = creategid(-1); }
682 | OPAREN CPAREN { $$ = creategid(0); }
683 | OPAREN commas CPAREN { $$ = creategid($2); }
686 atypes : atype { $$ = lsing($1); }
687 | atypes atype { $$ = lapp($1,$2); }
690 types : type { $$ = lsing($1); }
691 | types COMMA type { $$ = lapp($1,$3); }
694 commas : COMMA { $$ = 1; }
695 | commas COMMA { $$ = $1 + 1; }
698 /**********************************************************************
701 * Declaration stuff *
704 **********************************************************************/
706 /* C a b c, where a,b,c are type variables */
707 /* C can be a class or tycon */
708 simple_con_app: gtycon { $$ = mktname($1); }
709 | simple_con_app1 { $$ = $1; }
712 simple_con_app1: gtycon tyvar { $$ = mktapp(mktname($1),$2); }
713 | simple_con_app tyvar { $$ = mktapp($1, $2); }
716 simple_context : OPAREN simple_context_list CPAREN { $$ = $2; }
717 | simple_con_app1 { $$ = lsing($1); }
720 simple_context_list: simple_con_app1 { $$ = lsing($1); }
721 | simple_context_list COMMA simple_con_app1 { $$ = lapp($1,$3); }
724 constrs : constr { $$ = lsing($1); }
725 | constrs VBAR constr { $$ = lapp($1,$3); }
728 constr : constr_after_context
729 | type DARROW constr_after_context { $$ = mkconstrcxt ( type2context($1), $3 ); }
732 constr_after_context :
734 /* We have to parse the constructor application as a *type*, else we get
735 into terrible ambiguity problems. Consider the difference between
737 data T = S Int Int Int `R` Int
739 data T = S Int Int Int
741 It isn't till we get to the operator that we discover that the "S" is
742 part of a type in the first, but part of a constructor application in the
746 /* Con !Int (Tree a) */
747 contype { qid tyc; list tys;
748 splittyconapp($1, &tyc, &tys);
749 $$ = mkconstrpre(tyc,tys,hsplineno); }
751 /* !Int `Con` Tree a */
752 | bbtype qconop bbtype { $$ = mkconstrinf($1,$2,$3,hsplineno); }
754 /* (::) (Tree a) Int */
755 | OPAREN qconsym CPAREN batypes { $$ = mkconstrpre($2,$4,hsplineno); }
757 /* Con { op1 :: Int } */
758 | gtycon OCURLY fields CCURLY { $$ = mkconstrrec($1,$3,hsplineno); }
760 /* 1 S/R conflict on OCURLY -> shift */
763 /* contype has to reduce to a btype unless there are !'s, so that
764 we don't get reduce/reduce conflicts with the second production of constr.
765 But as soon as we see a ! we must switch to using bxtype. */
767 contype : btype { $$ = $1; }
768 | bxtype { $$ = $1; }
771 /* S !Int Bool; at least one ! */
772 bxtype : btype wierd_atype { $$ = mktapp($1, $2); }
773 | bxtype batype { $$ = mktapp($1, $2); }
776 bbtype : btype { $$ = $1; }
777 | wierd_atype { $$ = $1; }
780 batype : atype { $$ = $1; }
781 | wierd_atype { $$ = $1; }
784 /* A wierd atype is one that isn't a regular atype;
785 it starts with a "!", or with a forall. */
786 wierd_atype : BANG bigatype { $$ = mktbang( $2 ); }
787 | BANG atype { $$ = mktbang( $2 ); }
791 batypes : { $$ = Lnil; }
792 | batypes batype { $$ = lapp($1,$2); }
796 fields : field { $$ = lsing($1); }
797 | fields COMMA field { $$ = lapp($1,$3); }
800 field : qvars_list DCOLON ctype { $$ = mkfield($1,$3); }
801 | qvars_list DCOLON BANG atype { $$ = mkfield($1,mktbang($4)); }
802 | qvars_list DCOLON BANG bigatype { $$ = mkfield($1,mktbang($4)); }
805 constr1 : gtycon atype { $$ = lsing(mkconstrnew($1,$2,hsplineno)); }
809 dtyclses: OPAREN dtycls_list CPAREN { $$ = $2; }
810 | OPAREN CPAREN { $$ = Lnil; }
811 | qtycls { $$ = lsing($1); }
814 dtycls_list: qtycls { $$ = lsing($1); }
815 | dtycls_list COMMA qtycls { $$ = lapp($1,$3); }
818 instdefs : /* empty */ { $$ = mknullbind(); }
819 | instdef { $$ = $1; }
820 | instdefs SEMI instdef
832 /* instdef: same as valdef, except certain user-pragmas may appear */
834 SPECIALISE_UPRAGMA qvark DCOLON types_and_maybe_ids END_UPRAGMA
836 $$ = mkvspec_uprag($2, $4, startlineno);
837 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
840 | INLINE_UPRAGMA qvark END_UPRAGMA
842 $$ = mkinline_uprag($2, startlineno);
843 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
846 | MAGIC_UNFOLDING_UPRAGMA qvark vark END_UPRAGMA
848 $$ = mkmagicuf_uprag($2, $3, startlineno);
849 PREVPATT = NULL; FN = NULL; SAMEFN = 0;
859 tree fn = function($1);
862 if(ttree(fn) == ident)
864 qid fun_id = gident((struct Sident *) fn);
869 else if (ttree(fn) == infixap)
871 qid fun_id = ginffun((struct Sinfixap *) fn);
878 printf("%u\n",startlineno);
880 fprintf(stderr,"%u\tvaldef\n",startlineno);
887 if ( lhs_is_patt($1) )
889 $$ = mkpbind($4, $3);
894 $$ = mkfbind($4, $3);
900 get_line_no : { $$ = startlineno }
903 vallhs : patk { $$ = $1; }
904 | patk qvarop pat { $$ = mkinfixap($2,$1,$3); }
905 | funlhs { $$ = $1; }
908 funlhs : qvark apat { $$ = mkap(mkident($1),$2); }
909 | funlhs apat { $$ = mkap($1,$2); }
913 valrhs : valrhs1 maybe_where { $$ = lsing(createpat($1, $2)); }
916 valrhs1 : gdrhs { $$ = mkpguards($1); }
917 | EQUAL exp { $$ = mkpnoguards($2); }
920 gdrhs : gd EQUAL exp { $$ = lsing(mkpgdexp($1,$3)); }
921 | gd EQUAL exp gdrhs { $$ = mklcons(mkpgdexp($1,$3),$4); }
925 WHERE ocurly decls ccurly { $$ = $3; }
926 | WHERE vocurly decls vccurly { $$ = $3; }
927 /* A where containing no decls is OK */
928 | WHERE SEMI { $$ = mknullbind(); }
929 | /* empty */ { $$ = mknullbind(); }
932 gd : VBAR quals { $$ = $2; }
936 /**********************************************************************
942 **********************************************************************/
944 exp : oexp DCOLON ctype { $$ = mkrestr($1,$3); }
949 Operators must be left-associative at the same precedence for
950 precedence parsing to work.
952 /* 8 S/R conflicts on qop -> shift */
953 oexp : oexp qop oexp %prec MINUS { $$ = mkinfixap($2,$1,$3); }
958 This comes here because of the funny precedence rules concerning
961 dexp : MINUS kexp { $$ = mknegate($2); }
966 We need to factor out a leading let expression so we can set
967 inpat=TRUE when parsing (non let) expressions inside stmts and quals
969 expLno : oexpLno DCOLON ctype { $$ = mkrestr($1,$3); }
972 oexpLno : oexpLno qop oexp %prec MINUS { $$ = mkinfixap($2,$1,$3); }
975 dexpLno : MINUS kexp { $$ = mknegate($2); }
979 expL : oexpL DCOLON ctype { $$ = mkrestr($1,$3); }
982 oexpL : oexpL qop oexp %prec MINUS { $$ = mkinfixap($2,$1,$3); }
987 let/if/lambda/case have higher precedence than infix operators.
994 /* kexpL = a let expression */
995 kexpL : letdecls IN exp { $$ = mklet($1,$3); }
998 /* kexpLno = any other expression more tightly binding than operator application */
1000 { hsincindent(); /* push new context for FN = NULL; */
1001 FN = NULL; /* not actually concerned about indenting */
1002 $<ulong>$ = hsplineno; /* remember current line number */
1007 RARROW exp /* lambda abstraction */
1009 $$ = mklambda($3, $6, $<ulong>2);
1013 | IF {$<ulong>$ = hsplineno;}
1014 exp THEN exp ELSE exp { $$ = mkife($3,$5,$7,$<ulong>2); }
1016 /* Case Expression */
1017 | CASE {$<ulong>$ = hsplineno;}
1018 exp OF caserest { $$ = mkcasee($3,$5,$<ulong>2); }
1021 | DO {$<ulong>$ = hsplineno;}
1022 dorest { $$ = mkdoe($3,$<ulong>2); }
1024 /* CCALL/CASM Expression */
1025 | CCALL ccallid cexps { $$ = mkccall($2,install_literal("n"),$3); }
1026 | CCALL ccallid { $$ = mkccall($2,install_literal("n"),Lnil); }
1027 | CCALL_GC ccallid cexps { $$ = mkccall($2,install_literal("p"),$3); }
1028 | CCALL_GC ccallid { $$ = mkccall($2,install_literal("p"),Lnil); }
1029 | CASM CLITLIT cexps { $$ = mkccall($2,install_literal("N"),$3); }
1030 | CASM CLITLIT { $$ = mkccall($2,install_literal("N"),Lnil); }
1031 | CASM_GC CLITLIT cexps { $$ = mkccall($2,install_literal("P"),$3); }
1032 | CASM_GC CLITLIT { $$ = mkccall($2,install_literal("P"),Lnil); }
1034 /* SCC Expression */
1039 "\"%s\":%d: _scc_ (`set [profiling] cost centre') ignored\n",
1040 input_filename, hsplineno);
1042 $$ = mkpar($3); /* Note the mkpar(). If we don't have it, then
1043 (x >> _scc_ y >> z) parses as (x >> (y >> z)),
1044 right associated. But the precedence reorganiser expects
1045 the parser to *left* associate all operators unless there
1046 are explicit parens. The _scc_ acts like an explicit paren,
1047 so if we omit it we'd better add explicit parens instead. */
1055 fexp : fexp aexp { $$ = mkap($1,$2); }
1059 /* simple expressions */
1060 aexp : qvar { $$ = mkident($1); }
1061 | gcon { $$ = mkident($1); }
1062 | lit_constant { $$ = mklit($1); }
1063 | OPAREN exp CPAREN { $$ = mkpar($2); } /* mkpar: stop infix parsing at ()'s */
1064 | qcon OCURLY rbinds CCURLY { $$ = mkrecord($1,$3); } /* 1 S/R conflict on OCURLY -> shift */
1065 | OBRACK list_exps CBRACK { $$ = mkllist($2); }
1066 | OPAREN exp COMMA texps CPAREN { if (ttree($4) == tuple)
1067 $$ = mktuple(mklcons($2, gtuplelist((struct Stuple *) $4)));
1069 $$ = mktuple(ldub($2, $4)); }
1071 /* only in expressions ... */
1072 | aexp OCURLY rbinds1 CCURLY { $$ = mkrupdate($1,$3); }
1073 | OBRACK exp VBAR quals CBRACK { $$ = mkcomprh($2,$4); }
1074 | OBRACK exp COMMA exp DOTDOT exp CBRACK {$$= mkeenum($2,mkjust($4),mkjust($6)); }
1075 | OBRACK exp COMMA exp DOTDOT CBRACK { $$ = mkeenum($2,mkjust($4),mknothing()); }
1076 | OBRACK exp DOTDOT exp CBRACK { $$ = mkeenum($2,mknothing(),mkjust($4)); }
1077 | OBRACK exp DOTDOT CBRACK { $$ = mkeenum($2,mknothing(),mknothing()); }
1078 | OPAREN oexp qop CPAREN { $$ = mklsection($2,$3); }
1079 | OPAREN qop1 oexp CPAREN { $$ = mkrsection($2,$3); }
1081 /* only in patterns ... */
1082 /* these add 2 S/R conflict with with aexp . OCURLY rbinds CCURLY */
1083 | qvar AT aexp { checkinpat(); $$ = mkas($1,$3); }
1084 | LAZY aexp { checkinpat(); $$ = mklazyp($2); }
1085 | WILDCARD { checkinpat(); $$ = mkwildp(); }
1088 /* ccall arguments */
1089 cexps : cexps aexp { $$ = lapp($1,$2); }
1090 | aexp { $$ = lsing($1); }
1093 caserest: ocurly alts ccurly { $$ = $2; }
1094 | vocurly alts vccurly { $$ = $2; }
1096 dorest : ocurly stmts ccurly { checkdostmts($2); $$ = $2; }
1097 | vocurly stmts vccurly { checkdostmts($2); $$ = $2; }
1100 rbinds : /* empty */ { $$ = Lnil; }
1104 rbinds1 : rbind { $$ = lsing($1); }
1105 | rbinds1 COMMA rbind { $$ = lapp($1,$3); }
1108 rbind : qvar { $$ = mkrbind($1,mknothing()); }
1109 | qvar EQUAL exp { $$ = mkrbind($1,mkjust($3)); }
1112 texps : exp { $$ = mkpar($1); } /* mkpar: so we don't flatten last element in tuple */
1114 { if (ttree($3) == tuple)
1115 $$ = mktuple(mklcons($1, gtuplelist((struct Stuple *) $3)));
1116 else if (ttree($3) == par)
1117 $$ = mktuple(ldub($1, gpare((struct Spar *) $3)));
1119 hsperror("hsparser:texps: panic");
1121 /* right recursion? WDP */
1125 exp { $$ = lsing($1); }
1126 | exp COMMA exp { $$ = mklcons( $1, lsing($3) ); }
1127 | exp COMMA exp COMMA list_rest { $$ = mklcons( $1, mklcons( $3, reverse_list( $5 ))); }
1130 /* Use left recusion for list_rest, because we sometimes get programs with
1131 very long explicit lists. */
1132 list_rest : exp { $$ = lsing($1); }
1133 | list_rest COMMA exp { $$ = mklcons( $3, $1 ); }
1137 exp { $$ = lsing($1); }
1138 | exp COMMA list_exps { $$ = mklcons($1, $3); }
1140 /* right recursion? (WDP)
1142 It has to be this way, though, otherwise you
1143 may do the wrong thing to distinguish between...
1145 [ e1 , e2 .. ] -- an enumeration ...
1146 [ e1 , e2 , e3 ] -- a list
1148 (In fact, if you change the grammar and throw yacc/bison
1149 at it, it *will* do the wrong thing [WDP 94/06])
1152 letdecls: LET ocurly decls ccurly { $$ = $3; }
1153 | LET vocurly decls vccurly { $$ = $3; }
1156 quals : qual { $$ = lsing($1); }
1157 | quals COMMA qual { $$ = lapp($1,$3); }
1160 qual : letdecls { $$ = mkseqlet($1); }
1162 | {inpat=TRUE;} expLno
1163 {inpat=FALSE;} leftexp
1165 expORpat(LEGIT_EXPR,$2);
1168 expORpat(LEGIT_PATT,$2);
1174 alts : alt { $$ = $1; }
1175 | alts SEMI alt { $$ = lconc($1,$3); }
1178 alt : pat { PREVPATT = $1; } altrest { $$ = lsing($3); PREVPATT = NULL; }
1179 | /* empty */ { $$ = Lnil; }
1182 altrest : gdpat maybe_where { $$ = createpat(mkpguards($1), $2); }
1183 | RARROW exp maybe_where { $$ = createpat(mkpnoguards($2),$3); }
1186 gdpat : gd RARROW exp { $$ = lsing(mkpgdexp($1,$3)); }
1187 | gd RARROW exp gdpat { $$ = mklcons(mkpgdexp($1,$3),$4); }
1190 stmts : stmt { $$ = $1; }
1191 | stmts SEMI stmt { $$ = lconc($1,$3); }
1194 stmt : /* empty */ { $$ = Lnil; }
1195 | letdecls { $$ = lsing(mkseqlet($1)); }
1196 | expL { $$ = lsing(mkdoexp($1,hsplineno)); }
1197 | {inpat=TRUE;} expLno {inpat=FALSE;} leftexp
1199 expORpat(LEGIT_EXPR,$2);
1200 $$ = lsing(mkdoexp($2,endlineno));
1202 expORpat(LEGIT_PATT,$2);
1203 $$ = lsing(mkdobind($2,$4,endlineno));
1208 leftexp : LARROW exp { $$ = $2; }
1209 | /* empty */ { $$ = NULL; }
1212 /**********************************************************************
1218 **********************************************************************/
1220 pat : qvar PLUS INTEGER { $$ = mkplusp($1, mkinteger($3)); }
1224 cpat : cpat qconop bpat { $$ = mkinfixap($2,$1,$3); }
1230 | qcon OCURLY rpats CCURLY { $$ = mkrecord($1,$3); }
1231 | MINUS INTEGER { $$ = mknegate(mklit(mkinteger($2))); }
1232 | MINUS FLOAT { $$ = mknegate(mklit(mkfloatr($2))); }
1235 conpat : gcon { $$ = mkident($1); }
1236 | conpat apat { $$ = mkap($1,$2); }
1239 apat : gcon { $$ = mkident($1); }
1240 | qcon OCURLY rpats CCURLY { $$ = mkrecord($1,$3); }
1244 apatc : qvar { $$ = mkident($1); }
1245 | qvar AT apat { $$ = mkas($1,$3); }
1246 | lit_constant { $$ = mklit($1); }
1247 | WILDCARD { $$ = mkwildp(); }
1248 | OPAREN pat CPAREN { $$ = mkpar($2); }
1249 | OPAREN pat COMMA pats CPAREN { $$ = mktuple(mklcons($2,$4)); }
1250 | OBRACK pats CBRACK { $$ = mkllist($2); }
1251 | LAZY apat { $$ = mklazyp($2); }
1255 INTEGER { $$ = mkinteger($1); }
1256 | FLOAT { $$ = mkfloatr($1); }
1257 | CHAR { $$ = mkcharr($1); }
1258 | STRING { $$ = mkstring($1); }
1259 | CHARPRIM { $$ = mkcharprim($1); }
1260 | STRINGPRIM { $$ = mkstringprim($1); }
1261 | INTPRIM { $$ = mkintprim($1); }
1262 | FLOATPRIM { $$ = mkfloatprim($1); }
1263 | DOUBLEPRIM { $$ = mkdoubleprim($1); }
1264 | CLITLIT /* yurble yurble */ { $$ = mkclitlit($1); }
1267 lampats : apat lampats { $$ = mklcons($1,$2); }
1268 | apat { $$ = lsing($1); }
1269 /* right recursion? (WDP) */
1272 pats : pat COMMA pats { $$ = mklcons($1, $3); }
1273 | pat { $$ = lsing($1); }
1274 /* right recursion? (WDP) */
1277 rpats : /* empty */ { $$ = Lnil; }
1281 rpats1 : rpat { $$ = lsing($1); }
1282 | rpats1 COMMA rpat { $$ = lapp($1,$3); }
1285 rpat : qvar { $$ = mkrbind($1,mknothing()); }
1286 | qvar EQUAL pat { $$ = mkrbind($1,mkjust($3)); }
1290 patk : patk qconop bpat { $$ = mkinfixap($2,$1,$3); }
1296 | qconk OCURLY rpats CCURLY { $$ = mkrecord($1,$3); }
1297 | minuskey INTEGER { $$ = mknegate(mklit(mkinteger($2))); }
1298 | minuskey FLOAT { $$ = mknegate(mklit(mkfloatr($2))); }
1301 conpatk : gconk { $$ = mkident($1); }
1302 | conpatk apat { $$ = mkap($1,$2); }
1305 apatck : qvark { $$ = mkident($1); }
1306 | qvark AT apat { $$ = mkas($1,$3); }
1307 | lit_constant { $$ = mklit($1); setstartlineno(); }
1308 | WILDCARD { $$ = mkwildp(); setstartlineno(); }
1309 | oparenkey pat CPAREN { $$ = mkpar($2); }
1310 | oparenkey pat COMMA pats CPAREN { $$ = mktuple(mklcons($2,$4)); }
1311 | obrackkey pats CBRACK { $$ = mkllist($2); }
1312 | lazykey apat { $$ = mklazyp($2); }
1317 | OBRACK CBRACK { $$ = creategid(-1); }
1318 | OPAREN CPAREN { $$ = creategid(0); }
1319 | OPAREN commas CPAREN { $$ = creategid($2); }
1323 | obrackkey CBRACK { $$ = creategid(-1); }
1324 | oparenkey CPAREN { $$ = creategid(0); }
1325 | oparenkey commas CPAREN { $$ = creategid($2); }
1328 /**********************************************************************
1331 * Keywords which record the line start *
1334 **********************************************************************/
1336 importkey: IMPORT { setstartlineno(); $$ = 0; }
1337 | IMPORT SOURCE_UPRAGMA { setstartlineno(); $$ = 1; }
1340 datakey : DATA { setstartlineno();
1343 printf("%u\n",startlineno);
1345 fprintf(stderr,"%u\tdata\n",startlineno);
1350 typekey : TYPE { setstartlineno();
1353 printf("%u\n",startlineno);
1355 fprintf(stderr,"%u\ttype\n",startlineno);
1360 newtypekey : NEWTYPE { setstartlineno();
1363 printf("%u\n",startlineno);
1365 fprintf(stderr,"%u\tnewtype\n",startlineno);
1370 instkey : INSTANCE { setstartlineno();
1373 printf("%u\n",startlineno);
1376 fprintf(stderr,"%u\tinstance\n",startlineno);
1381 defaultkey: DEFAULT { setstartlineno(); }
1384 classkey: CLASS { setstartlineno();
1387 printf("%u\n",startlineno);
1389 fprintf(stderr,"%u\tclass\n",startlineno);
1394 modulekey: MODULE { setstartlineno();
1397 printf("%u\n",startlineno);
1399 fprintf(stderr,"%u\tmodule\n",startlineno);
1404 oparenkey: OPAREN { setstartlineno(); }
1407 obrackkey: OBRACK { setstartlineno(); }
1410 lazykey : LAZY { setstartlineno(); }
1413 minuskey: MINUS { setstartlineno(); }
1417 /**********************************************************************
1420 * Basic qualified/unqualified ids/ops *
1423 **********************************************************************/
1426 | OPAREN qvarsym CPAREN { $$ = $2; }
1429 | OPAREN qconsym CPAREN { $$ = $2; }
1432 | BQUOTE qvarid BQUOTE { $$ = $2; }
1435 | BQUOTE qconid BQUOTE { $$ = $2; }
1441 /* Non "-" op, used in right sections */
1446 /* Non "-" varop, used in right sections */
1448 | varsym_nominus { $$ = mknoqual($1); }
1449 | BQUOTE qvarid BQUOTE { $$ = $2; }
1454 | OPAREN varsym CPAREN { $$ = $2; }
1456 con : tycon /* using tycon removes conflicts */
1457 | OPAREN CONSYM CPAREN { $$ = $2; }
1460 | BQUOTE varid BQUOTE { $$ = $2; }
1463 | BQUOTE CONID BQUOTE { $$ = $2; }
1469 qvark : qvarid { setstartlineno(); $$ = $1; }
1470 | oparenkey qvarsym CPAREN { $$ = $2; }
1472 qconk : qconid { setstartlineno(); $$ = $1; }
1473 | oparenkey qconsym CPAREN { $$ = $2; }
1475 vark : varid { setstartlineno(); $$ = $1; }
1476 | oparenkey varsym CPAREN { $$ = $2; }
1480 | varid { $$ = mknoqual($1); }
1483 | varsym { $$ = mknoqual($1); }
1486 | tycon { $$ = mknoqual($1); } /* using tycon removes conflicts */
1489 | CONSYM { $$ = mknoqual($1); }
1492 | tycon { $$ = mknoqual($1); } /* using tycon removes conflicts */
1495 | tycon { $$ = mknoqual($1); } /* using tycon removes conflicts */
1498 varsym : varsym_nominus
1499 | MINUS { $$ = install_literal("-"); }
1502 /* PLUS, BANG are valid varsyms */
1503 varsym_nominus : VARSYM
1504 | PLUS { $$ = install_literal("+"); }
1505 | BANG { $$ = install_literal("!"); }
1508 /* AS HIDING QUALIFIED are valid varids */
1510 | AS { $$ = install_literal("as"); }
1511 | HIDING { $$ = install_literal("hiding"); }
1512 | QUALIFIED { $$ = install_literal("qualified"); }
1520 tyvar : varid { $$ = mknamedtvar(mknoqual($1)); }
1528 tyvar_list: tyvar { $$ = lsing($1); }
1529 | tyvar_list COMMA tyvar { $$ = lapp($1,$3); }
1533 /**********************************************************************
1536 * Stuff to do with layout *
1539 **********************************************************************/
1541 ocurly : layout OCURLY { hsincindent(); }
1543 vocurly : layout { hssetindent(); }
1546 layout : { hsindentoff(); }
1552 FN = NULL; SAMEFN = 0; PREVPATT = NULL;
1557 vccurly : { expect_ccurly = 1; } vccurly1 { expect_ccurly = 0; }
1563 FN = NULL; SAMEFN = 0; PREVPATT = NULL;
1569 FN = NULL; SAMEFN = 0; PREVPATT = NULL;
1576 /**********************************************************************
1578 * Error Processing and Reporting *
1580 * (This stuff is here in case we want to use Yacc macros and such.) *
1582 **********************************************************************/
1588 hsperror("pattern syntax used in expression");
1592 /* The parser calls "hsperror" when it sees a
1593 `report this and die' error. It sets the stage
1594 and calls "yyerror".
1596 There should be no direct calls in the parser to
1597 "yyerror", except for the one from "hsperror". Thus,
1598 the only other calls will be from the error productions
1599 introduced by yacc/bison/whatever.
1601 We need to be able to recognise the from-error-production
1602 case, because we sometimes want to say, "Oh, never mind",
1603 because the layout rule kicks into action and may save
1607 static BOOLEAN error_and_I_mean_it = FALSE;
1613 error_and_I_mean_it = TRUE;
1617 extern char *yytext;
1624 /* We want to be able to distinguish 'error'-raised yyerrors
1625 from yyerrors explicitly coded by the parser hacker.
1627 if (expect_ccurly && ! error_and_I_mean_it ) {
1631 fprintf(stderr, "%s:%d:%d: %s on input: ",
1632 input_filename, hsplineno, hspcolno + 1, s);
1634 if (yyleng == 1 && *yytext == '\0')
1635 fprintf(stderr, "<EOF>");
1639 format_string(stderr, (unsigned char *) yytext, yyleng);
1642 fputc('\n', stderr);
1644 /* a common problem */
1645 if (strcmp(yytext, "#") == 0)
1646 fprintf(stderr, "\t(Perhaps you forgot a `-cpp' or `-fglasgow-exts' flag?)\n");
1653 format_string(fp, s, len)
1660 case '\0': fputs("\\NUL", fp); break;
1661 case '\007': fputs("\\a", fp); break;
1662 case '\010': fputs("\\b", fp); break;
1663 case '\011': fputs("\\t", fp); break;
1664 case '\012': fputs("\\n", fp); break;
1665 case '\013': fputs("\\v", fp); break;
1666 case '\014': fputs("\\f", fp); break;
1667 case '\015': fputs("\\r", fp); break;
1668 case '\033': fputs("\\ESC", fp); break;
1669 case '\034': fputs("\\FS", fp); break;
1670 case '\035': fputs("\\GS", fp); break;
1671 case '\036': fputs("\\RS", fp); break;
1672 case '\037': fputs("\\US", fp); break;
1673 case '\177': fputs("\\DEL", fp); break;
1678 fprintf(fp, "\\^%c", *s + '@');