72d4472b57f0e3406377be324ac9083f2aa0ea84
[ghc-hetmet.git] / ghc / compiler / parser / hsparser.y
1 /**************************************************************************
2 *   File:               hsparser.y                                        *
3 *                                                                         *
4 *                       Author:                 Maria M. Gutierrez        *
5 *                       Modified by:            Kevin Hammond             *
6 *                       Last date revised:      December 13 1991. KH.     *
7 *                       Modification:           Haskell 1.1 Syntax.       *
8 *                                                                         *
9 *                                                                         *
10 *   Description:  This file contains the LALR(1) grammar for Haskell.     *
11 *                                                                         *
12 *   Entry Point:  module                                                  *
13 *                                                                         *
14 *   Problems:     None known.                                             *
15 *                                                                         *
16 *                                                                         *
17 *                 LALR(1) Syntax for Haskell 1.2                          *
18 *                                                                         *
19 **************************************************************************/
20
21
22 %{
23 #ifdef HSP_DEBUG
24 # define YYDEBUG 1
25 #endif
26
27 #include <stdio.h>
28 #include <ctype.h>
29 #include <string.h>
30 #include "hspincl.h"
31 #include "constants.h"
32 #include "utils.h"
33
34 /**********************************************************************
35 *                                                                     *
36 *                                                                     *
37 *     Imported Variables and Functions                                *
38 *                                                                     *
39 *                                                                     *
40 **********************************************************************/
41
42 static BOOLEAN expect_ccurly = FALSE; /* Used to signal that a CCURLY could be inserted here */
43 extern BOOLEAN etags;
44
45 extern char *input_filename;
46 static char *the_module_name;
47 static maybe module_exports;
48
49 extern list Lnil;
50 extern list reverse_list();
51 extern tree root;
52
53 /* For FN, PREVPATT and SAMEFN macros */
54 extern qid      fns[];
55 extern BOOLEAN  samefn[];
56 extern tree     prevpatt[];
57 extern short    icontexts;
58
59 /* Line Numbers */
60 extern int hsplineno, hspcolno;
61 extern int modulelineno;
62 extern int startlineno;
63 extern int endlineno;
64
65 /**********************************************************************
66 *                                                                     *
67 *                                                                     *
68 *      Fixity and Precedence Declarations                             *
69 *                                                                     *
70 *                                                                     *
71 **********************************************************************/
72
73 static int Fixity = 0, Precedence = 0;
74
75 char *ineg PROTO((char *));
76
77 long    source_version = 0;
78
79 BOOLEAN inpat;
80 %}
81
82 %union {
83         tree utree;
84         list ulist;
85         ttype uttype;
86         constr uconstr;
87         binding ubinding;
88         pbinding upbinding;
89         entidt uentid;
90         id uid;
91         qid uqid;
92         literal uliteral;
93         maybe umaybe;
94         either ueither;
95         long ulong;
96         float ufloat;
97         char *ustring;
98         hstring uhstring;
99 }
100
101
102 /**********************************************************************
103 *                                                                     *
104 *                                                                     *
105 *     These are lexemes.                                              *
106 *                                                                     *
107 *                                                                     *
108 **********************************************************************/
109
110
111 %token  VARID           CONID           QVARID          QCONID
112         VARSYM          CONSYM          QVARSYM         QCONSYM
113
114 %token  INTEGER         FLOAT           CHAR            STRING
115         CHARPRIM        STRINGPRIM      INTPRIM         FLOATPRIM
116         DOUBLEPRIM      CLITLIT
117
118
119
120 /**********************************************************************
121 *                                                                     *
122 *                                                                     *
123 *      Special Symbols                                                *
124 *                                                                     *
125 *                                                                     *
126 **********************************************************************/
127
128 %token  OCURLY          CCURLY          VCCURLY 
129 %token  COMMA           SEMI            OBRACK          CBRACK
130 %token  WILDCARD        BQUOTE          OPAREN          CPAREN
131
132
133 /**********************************************************************
134 *                                                                     *
135 *                                                                     *
136 *     Reserved Operators                                              *
137 *                                                                     *
138 *                                                                     *
139 **********************************************************************/
140
141 %token  DOTDOT          DCOLON          EQUAL           LAMBDA          
142 %token  VBAR            RARROW          LARROW
143 %token  AT              LAZY            DARROW
144
145
146 /**********************************************************************
147 *                                                                     *
148 *                                                                     *
149 *     Reserved Identifiers                                            *
150 *                                                                     *
151 *                                                                     *
152 **********************************************************************/
153
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
161
162 %token  SCC
163 %token  CCALL           CCALL_GC        CASM            CASM_GC
164
165
166 /**********************************************************************
167 *                                                                     *
168 *                                                                     *
169 *     Special symbols/identifiers which need to be recognised         *
170 *                                                                     *
171 *                                                                     *
172 **********************************************************************/
173
174 %token  MINUS           BANG            PLUS
175 %token  AS              HIDING          QUALIFIED
176
177
178 /**********************************************************************
179 *                                                                     *
180 *                                                                     *
181 *     Special Symbols for the Lexer                                   *
182 *                                                                     *
183 *                                                                     *
184 **********************************************************************/
185
186 %token  INTERFACE_UPRAGMA SPECIALISE_UPRAGMA
187 %token  INLINE_UPRAGMA MAGIC_UNFOLDING_UPRAGMA
188 %token  END_UPRAGMA 
189 %token  SOURCE_UPRAGMA
190
191 /**********************************************************************
192 *                                                                     *
193 *                                                                     *
194 *     Precedences of the various tokens                               *
195 *                                                                     *
196 *                                                                     *
197 **********************************************************************/
198
199
200 %left   CASE    LET     IN
201         IF      ELSE    LAMBDA
202         SCC     CASM    CCALL   CASM_GC CCALL_GC
203
204 %left   VARSYM  CONSYM  QVARSYM QCONSYM
205         MINUS   BQUOTE  BANG    DARROW  PLUS
206
207 %left   DCOLON
208
209 %left   SEMI    COMMA
210
211 %left   OCURLY  OBRACK  OPAREN
212
213 %left   EQUAL
214
215 %right  RARROW
216
217 /**********************************************************************
218 *                                                                     *
219 *                                                                     *
220 *      Type Declarations                                              *
221 *                                                                     *
222 *                                                                     *
223 **********************************************************************/
224
225
226 %type <ulist>   caserest alts alt quals
227                 dorest stmts stmt
228                 rbinds rbinds1 rpats rpats1 list_exps list_rest
229                 qvarsk qvars_list
230                 constrs constr1 fields 
231                 types atypes batypes
232                 types_and_maybe_ids
233                 pats context context_list /* tyvar_list */
234                 export_list enames
235                 import_list inames
236                 impdecls maybeimpdecls impdecl
237                 maybefixes fixes fix ops
238                 dtyclses dtycls_list
239                 gdrhs gdpat valrhs
240                 lampats cexps gd
241
242 %type <umaybe>  maybeexports impspec deriving
243
244 %type <uliteral> lit_constant
245
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
251
252
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
257                 tycon modid ccallid
258
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 
264                 ename iname 
265
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
270
271 %type <upbinding> valrhs1 altrest
272
273 %type <uttype>    simple ctype sigtype sigarrowtype type atype bigatype btype
274                   gtyconvars 
275                   bbtype batype bxtype wierd_atype
276                   class tyvar contype
277
278 %type <uconstr>   constr constr_after_context field
279
280 %type <ustring>   FLOAT INTEGER INTPRIM
281                   FLOATPRIM DOUBLEPRIM CLITLIT
282
283 %type <uhstring>  STRING STRINGPRIM CHAR CHARPRIM
284
285 %type <uentid>    export import
286
287 %type <ulong>     commas importkey
288
289 /**********************************************************************
290 *                                                                     *
291 *                                                                     *
292 *      Start Symbol for the Parser                                    *
293 *                                                                     *
294 *                                                                     *
295 **********************************************************************/
296
297 %start module
298
299 %%
300 module  :  modulekey modid maybeexports
301                 {
302                   modulelineno = startlineno;
303                   the_module_name = $2;
304                   module_exports = $3;
305                 }
306            WHERE body
307         |       { 
308                   modulelineno = 0;
309                   the_module_name = install_literal("Main");
310                   module_exports = mknothing();
311                 }
312            body
313         ;
314
315 body    :  ocurly { setstartlineno(); } interface_pragma orestm
316         |  vocurly interface_pragma vrestm
317         ;
318
319 interface_pragma : /* empty */
320         | INTERFACE_UPRAGMA INTEGER END_UPRAGMA SEMI
321                {
322                  source_version = atoi($2);
323                }
324         ;
325
326 orestm  :  maybeimpdecls maybefixes topdecls ccurly
327                {
328                  root = mkhmodule(the_module_name,$1,module_exports,
329                                   $2,$3,source_version,modulelineno);
330                }
331         |  impdecls ccurly
332                {
333                  root = mkhmodule(the_module_name,$1,module_exports,
334                                   Lnil,mknullbind(),source_version,modulelineno);
335                }
336
337 vrestm  :  maybeimpdecls maybefixes topdecls vccurly
338                {
339                  root = mkhmodule(the_module_name,$1,module_exports,
340                                   $2,$3,source_version,modulelineno);
341                }
342         |  impdecls vccurly
343                {
344                  root = mkhmodule(the_module_name,$1,module_exports,
345                                   Lnil,mknullbind(),source_version,modulelineno);
346                }
347
348 maybeexports :  /* empty */                     { $$ = mknothing(); }
349         |  OPAREN export_list CPAREN            { $$ = mkjust($2); }
350         |  OPAREN export_list COMMA CPAREN      { $$ = mkjust($2); }
351         ;
352
353 export_list:
354            export                               { $$ = lsing($1); }
355         |  export_list COMMA export             { $$ = lapp($1, $3); }
356         ;
357
358 export  :  qvar                                 { $$ = mkentid($1); }
359         |  gtycon                               { $$ = mkenttype($1); }
360         |  gtycon OPAREN DOTDOT CPAREN          { $$ = mkenttypeall($1); }
361         |  gtycon OPAREN CPAREN                 { $$ = mkenttypenamed($1,Lnil); }
362         |  gtycon OPAREN enames CPAREN          { $$ = mkenttypenamed($1,$3); }
363         |  MODULE modid                         { $$ = mkentmod($2); }
364         ;
365
366 enames  :  ename                                { $$ = lsing($1); }
367         |  enames COMMA ename                   { $$ = lapp($1,$3); }
368         ;
369 ename   :  qvar
370         |  qcon
371         ;
372
373
374 maybeimpdecls : /* empty */                     { $$ = Lnil; }
375         |  impdecls SEMI                        { $$ = $1; }
376         ;
377
378 impdecls:  impdecl                              { $$ = $1; }
379         |  impdecls SEMI impdecl                { $$ = lconc($1,$3); }
380         ;
381
382
383 impdecl :  importkey modid impspec
384                 { $$ = lsing(mkimport($2,0,mknothing(),$3,$1,startlineno)); }
385         |  importkey QUALIFIED modid impspec
386                 { $$ = lsing(mkimport($3,1,mknothing(),$4,$1,startlineno)); }
387         |  importkey QUALIFIED modid AS modid impspec
388                 { $$ = lsing(mkimport($3,1,mkjust($5),$6,$1,startlineno)); }
389         ;
390
391 impspec :  /* empty */                            { $$ = mknothing(); }
392         |  OPAREN CPAREN                          { $$ = mkjust(mkleft(Lnil)); }
393         |  OPAREN import_list CPAREN              { $$ = mkjust(mkleft($2));   }
394         |  OPAREN import_list COMMA CPAREN        { $$ = mkjust(mkleft($2));   }
395         |  HIDING OPAREN import_list CPAREN       { $$ = mkjust(mkright($3));  }
396         |  HIDING OPAREN import_list COMMA CPAREN { $$ = mkjust(mkright($3));  }
397         ;
398
399 import_list:
400            import                               { $$ = lsing($1); }
401         |  import_list COMMA import             { $$ = lapp($1, $3); }
402         ;
403
404 import  :  var                                  { $$ = mkentid(mknoqual($1)); }
405         |  itycon                               { $$ = mkenttype($1); }
406         |  itycon OPAREN DOTDOT CPAREN          { $$ = mkenttypeall($1); }
407         |  itycon OPAREN CPAREN                 { $$ = mkenttypenamed($1,Lnil);}
408         |  itycon OPAREN inames CPAREN          { $$ = mkenttypenamed($1,$3); }
409         ;
410
411 itycon  :  tycon                                { $$ = mknoqual($1); }
412         |  OBRACK CBRACK                        { $$ = creategid(-1); }         
413         |  OPAREN CPAREN                        { $$ = creategid(0); }         
414         |  OPAREN commas CPAREN                 { $$ = creategid($2); }
415         ;
416
417 inames  :  iname                                { $$ = lsing($1); }
418         |  inames COMMA iname                   { $$ = lapp($1,$3); }
419         ;
420 iname   :  var                                  { $$ = mknoqual($1); }
421         |  con                                  { $$ = mknoqual($1); }
422         ;
423
424 /**********************************************************************
425 *                                                                     *
426 *                                                                     *
427 *     Fixes and Decls etc                                             *
428 *                                                                     *
429 *                                                                     *
430 **********************************************************************/
431
432 maybefixes:  /* empty */                { $$ = Lnil; }
433         |  fixes SEMI                   { $$ = $1; }
434         ;
435
436 fixes   :  fix                          { $$ = $1; }
437         |  fixes SEMI fix               { $$ = lconc($1,$3); }
438         ;
439
440 fix     :  INFIXL INTEGER       { Precedence = checkfixity($2); Fixity = INFIXL; }
441            ops                  { $$ = $4; }
442         |  INFIXR INTEGER       { Precedence = checkfixity($2); Fixity = INFIXR; }
443            ops                  { $$ = $4; }
444         |  INFIX  INTEGER       { Precedence = checkfixity($2); Fixity = INFIX; }
445            ops                  { $$ = $4; }
446         |  INFIXL               { Fixity = INFIXL; Precedence = 9; }
447            ops                  { $$ = $3; }
448         |  INFIXR               { Fixity = INFIXR; Precedence = 9; }
449            ops                  { $$ = $3; }
450         |  INFIX                { Fixity = INFIX; Precedence = 9; }
451            ops                  { $$ = $3; }
452         ;
453
454 ops     :  op            { $$ = lsing(mkfixop(mknoqual($1),infixint(Fixity),Precedence)); }
455         |  ops COMMA op  { $$ = lapp($1,mkfixop(mknoqual($3),infixint(Fixity),Precedence)); }
456         ;
457
458 topdecls:  topdecl
459         |  topdecls SEMI topdecl
460                 {
461                   if($1 != NULL)
462                     if($3 != NULL)
463                       if(SAMEFN)
464                         {
465                           extendfn($1,$3);
466                           $$ = $1;
467                         }
468                       else
469                         $$ = mkabind($1,$3);
470                     else
471                       $$ = $1;
472                   else
473                     $$ = $3;
474                   SAMEFN = 0;
475                 }
476         ;
477
478 topdecl :  typed                                { $$ = $1; FN = NULL; SAMEFN = 0; }
479         |  datad                                { $$ = $1; FN = NULL; SAMEFN = 0; }
480         |  newtd                                { $$ = $1; FN = NULL; SAMEFN = 0; }
481         |  classd                               { $$ = $1; FN = NULL; SAMEFN = 0; }
482         |  instd                                { $$ = $1; FN = NULL; SAMEFN = 0; }
483         |  defaultd                             { $$ = $1; FN = NULL; SAMEFN = 0; }
484         |  decl                                 { $$ = $1; }
485         ;
486
487 typed   :  typekey simple EQUAL type            { $$ = mknbind($2,$4,startlineno); }
488         ;
489
490
491 datad   :  datakey simple EQUAL constrs deriving
492                 { $$ = mktbind(Lnil,$2,$4,$5,startlineno); }
493         |  datakey context DARROW simple EQUAL constrs deriving
494                 { $$ = mktbind($2,$4,$6,$7,startlineno); }
495         ;
496
497 newtd   :  newtypekey simple EQUAL constr1 deriving
498                 { $$ = mkntbind(Lnil,$2,$4,$5,startlineno); }
499         |  newtypekey context DARROW simple EQUAL constr1 deriving
500                 { $$ = mkntbind($2,$4,$6,$7,startlineno); }
501         ;
502
503 deriving: /* empty */                           { $$ = mknothing(); }
504         | DERIVING dtyclses                     { $$ = mkjust($2); }
505         ;
506
507 classd  :  classkey context DARROW class cbody
508                 { $$ = mkcbind($2,$4,$5,startlineno); }
509         |  classkey class cbody                 
510                 { $$ = mkcbind(Lnil,$2,$3,startlineno); }
511         ;
512
513 cbody   :  /* empty */                          { $$ = mknullbind(); }
514         |  WHERE ocurly decls ccurly            { checkorder($3); $$ = $3; }
515         |  WHERE vocurly decls vccurly          { checkorder($3); $$ = $3; }
516         ;
517
518 instd   :  instkey context DARROW gtycon atype rinst
519                 { $$ = mkibind($2,$4,$5,$6,startlineno); }
520         |  instkey gtycon atype rinst
521                 { $$ = mkibind(Lnil,$2,$3,$4,startlineno); }
522         ;
523
524 rinst   :  /* empty */                                          { $$ = mknullbind(); }
525         |  WHERE ocurly  instdefs ccurly                        { $$ = $3; }
526         |  WHERE vocurly instdefs vccurly                       { $$ = $3; }
527         ;
528
529 /*      I now allow a general type in instance declarations, relying
530         on the type checker to reject instance decls which are ill-formed.
531         Some (non-standard) extensions of Haskell may allow more general
532         types than the Report syntax permits, and in any case not all things
533         can be checked in the syntax (eg repeated type variables).
534                 SLPJ Jan 97
535
536 restrict_inst : gtycon                          { $$ = mktname($1); }
537         |  OPAREN gtyconvars CPAREN             { $$ = $2; }
538         |  OPAREN tyvar COMMA tyvar_list CPAREN { $$ = mkttuple(mklcons($2,$4)); }
539         |  OBRACK tyvar CBRACK                  { $$ = mktllist($2); }
540         |  OPAREN tyvar RARROW tyvar CPAREN     { $$ = mktfun($2,$4); }
541         ;
542
543 general_inst : gtycon                           { $$ = mktname($1); }
544         |  OPAREN gtyconapp1 CPAREN             { $$ = $2; }
545         |  OPAREN type COMMA types CPAREN       { $$ = mkttuple(mklcons($2,$4)); }
546         |  OBRACK type CBRACK                   { $$ = mktllist($2); }
547         |  OPAREN btype RARROW type CPAREN      { $$ = mktfun($2,$4); }
548         ;
549 */
550
551 defaultd:  defaultkey OPAREN types CPAREN       { $$ = mkdbind($3,startlineno); }
552         |  defaultkey OPAREN CPAREN             { $$ = mkdbind(Lnil,startlineno); }
553         ;
554
555 decls   : decl
556         | decls SEMI decl
557                 {
558                   if(SAMEFN)
559                     {
560                       extendfn($1,$3);
561                       $$ = $1;
562                     }
563                   else
564                     $$ = mkabind($1,$3);
565                 }
566         ;
567
568 /*
569     Note: if there is an iclasop_pragma here, then we must be
570     doing a class-op in an interface -- unless the user is up
571     to real mischief (ugly, but likely to work).
572 */
573
574 decl    : qvarsk DCOLON sigtype
575                 { $$ = mksbind($1,$3,startlineno);
576                   PREVPATT = NULL; FN = NULL; SAMEFN = 0;
577                 }
578
579         /* User-specified pragmas come in as "signatures"...
580            They are similar in that they can appear anywhere in the module,
581            and have to be "joined up" with their related entity.
582
583            Have left out the case specialising to an overloaded type.
584            Let's get real, OK?  (WDP)
585         */
586         |  SPECIALISE_UPRAGMA qvark DCOLON types_and_maybe_ids END_UPRAGMA
587                 {
588                   $$ = mkvspec_uprag($2, $4, startlineno);
589                   PREVPATT = NULL; FN = NULL; SAMEFN = 0;
590                 }
591
592         |  SPECIALISE_UPRAGMA INSTANCE gtycon atype END_UPRAGMA
593                 {
594                   $$ = mkispec_uprag($3, $4, startlineno);
595                   PREVPATT = NULL; FN = NULL; SAMEFN = 0;
596                 }
597
598         |  SPECIALISE_UPRAGMA DATA gtycon atypes END_UPRAGMA
599                 {
600                   $$ = mkdspec_uprag($3, $4, startlineno);
601                   PREVPATT = NULL; FN = NULL; SAMEFN = 0;
602                 }
603
604         |  INLINE_UPRAGMA qvark END_UPRAGMA
605                 {
606                   $$ = mkinline_uprag($2, startlineno);
607                   PREVPATT = NULL; FN = NULL; SAMEFN = 0;
608                 }
609
610         |  MAGIC_UNFOLDING_UPRAGMA qvark vark END_UPRAGMA
611                 {
612                   $$ = mkmagicuf_uprag($2, $3, startlineno);
613                   PREVPATT = NULL; FN = NULL; SAMEFN = 0;
614                 }
615
616         /* end of user-specified pragmas */
617
618         |  valdef
619         |  /* empty */ { $$ = mknullbind(); PREVPATT = NULL; FN = NULL; SAMEFN = 0; }
620         ;
621
622 qvarsk  :  qvark COMMA qvars_list               { $$ = mklcons($1,$3); }
623         |  qvark                                { $$ = lsing($1); }
624         ;
625
626 qvars_list: qvar                                { $$ = lsing($1); }
627         |   qvars_list COMMA qvar               { $$ = lapp($1,$3); }
628         ;
629
630 types_and_maybe_ids :
631            type_and_maybe_id                            { $$ = lsing($1); }
632         |  types_and_maybe_ids COMMA type_and_maybe_id  { $$ = lapp($1,$3); }
633         ;
634
635 type_and_maybe_id :
636            type                                 { $$ = mkvspec_ty_and_id($1,mknothing()); }
637         |  type EQUAL qvark                     { $$ = mkvspec_ty_and_id($1,mkjust($3)); }
638
639
640 /**********************************************************************
641 *                                                                     *
642 *                                                                     *
643 *     Types etc                                                       *
644 *                                                                     *
645 *                                                                     *
646 **********************************************************************/
647
648 /*  "DCOLON context => type" vs "DCOLON type" is a problem,
649     because you can't distinguish between
650
651         foo :: (Baz a, Baz a)
652         bar :: (Baz a, Baz a) => [a] -> [a] -> [a]
653
654     with one token of lookahead.  The HACK is to have "DCOLON ttype"
655     [tuple type] in the first case, then check that it has the right
656     form C a, or (C1 a, C2 b, ... Cn z) and convert it into a
657     context.  Blaach!
658 */
659
660 /* A sigtype is a rank 2 type; it can have for-alls as function args:
661         f :: All a => (All b => ...) -> Int
662 */
663 sigtype : type DARROW sigarrowtype              { $$ = mkcontext(type2context($1),$3); }
664         | sigarrowtype 
665         ;
666
667 sigarrowtype : bigatype RARROW sigarrowtype     { $$ = mktfun($1,$3); }
668              | btype RARROW sigarrowtype        { $$ = mktfun($1,$3); }
669              | btype
670              ;
671
672 /* A "big" atype can be a forall-type in brackets.  */
673 bigatype: OPAREN type DARROW type CPAREN        { $$ = mkcontext(type2context($2),$4); }
674         ;
675
676         /* 1 S/R conflict at DARROW -> shift */
677 ctype   : type DARROW type                      { $$ = mkcontext(type2context($1),$3); }
678         | type
679         ;
680
681         /* 1 S/R conflict at RARROW -> shift */
682 type    :  btype RARROW type                    { $$ = mktfun($1,$3); }
683         |  btype                                { $$ = $1; }
684         ;
685
686 btype   :  btype atype                          { $$ = mktapp($1,$2); }
687         |  atype                                { $$ = $1; }
688         ;
689
690 atype   :  gtycon                               { $$ = mktname($1); }
691         |  tyvar                                { $$ = $1; }
692         |  OPAREN type COMMA types CPAREN       { $$ = mkttuple(mklcons($2,$4)); }
693         |  OBRACK type CBRACK                   { $$ = mktllist($2); }
694         |  OPAREN type CPAREN                   { $$ = $2; }
695         ;
696
697 gtycon  :  qtycon
698         |  OPAREN RARROW CPAREN                 { $$ = creategid(-2); }
699         |  OBRACK CBRACK                        { $$ = creategid(-1); }         
700         |  OPAREN CPAREN                        { $$ = creategid(0); }         
701         |  OPAREN commas CPAREN                 { $$ = creategid($2); }
702         ;
703
704 atypes  :  atype                                { $$ = lsing($1); }
705         |  atypes atype                         { $$ = lapp($1,$2); }
706         ;
707
708 types   :  type                                 { $$ = lsing($1); }
709         |  types COMMA type                     { $$ = lapp($1,$3); }
710         ;
711
712 commas  : COMMA                                 { $$ = 1; }
713         | commas COMMA                          { $$ = $1 + 1; }
714         ;
715
716 /**********************************************************************
717 *                                                                     *
718 *                                                                     *
719 *     Declaration stuff                                               *
720 *                                                                     *
721 *                                                                     *
722 **********************************************************************/
723
724 simple  :  gtycon                               { $$ = mktname($1); }
725         |  gtyconvars                           { $$ = $1; }
726         ;
727
728 gtyconvars: gtycon tyvar                        { $$ = mktapp(mktname($1),$2); }
729         |  gtyconvars tyvar                     { $$ = mktapp($1,$2); }
730         ;
731
732 context :  OPAREN context_list CPAREN           { $$ = $2; }
733         |  class                                { $$ = lsing($1); }
734         ;
735
736 context_list:  class                            { $$ = lsing($1); }
737         |  context_list COMMA class             { $$ = lapp($1,$3); }
738         ;
739
740 class   :  gtycon tyvar                         { $$ = mktapp(mktname($1),$2); }
741         ;
742
743 constrs :  constr                               { $$ = lsing($1); }
744         |  constrs VBAR constr                  { $$ = lapp($1,$3); }
745         ;
746
747 constr  :  constr_after_context
748         |  type DARROW constr_after_context     { $$ = mkconstrcxt ( type2context($1), $3 ); }
749         ;
750
751 constr_after_context :
752
753         /* We have to parse the constructor application as a *type*, else we get
754            into terrible ambiguity problems.  Consider the difference between
755
756                 data T = S Int Int Int `R` Int
757            and
758                 data T = S Int Int Int
759         
760            It isn't till we get to the operator that we discover that the "S" is
761            part of a type in the first, but part of a constructor application in the
762            second.
763         */
764
765 /* Con !Int (Tree a) */
766            contype                              { qid tyc; list tys;
767                                                   splittyconapp($1, &tyc, &tys);
768                                                   $$ = mkconstrpre(tyc,tys,hsplineno); }
769
770 /* !Int `Con` Tree a */
771         |  bbtype qconop bbtype                 { $$ = mkconstrinf($1,$2,$3,hsplineno); }
772
773 /* (::) (Tree a) Int */
774         |  OPAREN qconsym CPAREN batypes        { $$ = mkconstrpre($2,$4,hsplineno); }
775
776 /* Con { op1 :: Int } */
777         |  gtycon OCURLY fields CCURLY          { $$ = mkconstrrec($1,$3,hsplineno); }
778                 /* 1 S/R conflict on OCURLY -> shift */
779         ;
780
781
782 /* contype has to reduce to a btype unless there are !'s, so that
783    we don't get reduce/reduce conflicts with the second production of constr.
784    But as soon as we see a ! we must switch to using bxtype. */
785
786 contype : btype                                 { $$ = $1 }
787         | bxtype                                { $$ = $1 }
788         ;
789
790 /* S !Int Bool; at least one ! */
791 bxtype  : btype wierd_atype                     { $$ = mktapp($1, $2); }
792         | bxtype batype                         { $$ = mktapp($1, $2); }
793         ;
794
795 bbtype  :  btype                                { $$ = $1; }
796         |  wierd_atype                          { $$ = $1; }
797         ;
798
799 batype  :  atype                                { $$ = $1; }
800         |  wierd_atype                          { $$ = $1; }
801         ;
802
803 /* A wierd atype is one that isn't a regular atype;
804    it starts with a "!", or with a forall. */
805 wierd_atype : BANG bigatype                     { $$ = mktbang( $2 ) }
806             | BANG atype                        { $$ = mktbang( $2 ) }
807             | bigatype 
808             ;
809
810 batypes :                                       { $$ = Lnil; }
811         |  batypes batype                       { $$ = lapp($1,$2); }
812         ;
813
814
815 fields  : field                                 { $$ = lsing($1); }
816         | fields COMMA field                    { $$ = lapp($1,$3); }
817         ;
818
819 field   :  qvars_list DCOLON ctype              { $$ = mkfield($1,$3); }
820         |  qvars_list DCOLON BANG atype         { $$ = mkfield($1,mktbang($4)); }
821         |  qvars_list DCOLON BANG bigatype      { $$ = mkfield($1,mktbang($4)); }
822         ; 
823
824 constr1 :  gtycon atype                         { $$ = lsing(mkconstrnew($1,$2,hsplineno)); }
825         ;
826
827
828 dtyclses:  OPAREN dtycls_list CPAREN            { $$ = $2; }
829         |  OPAREN CPAREN                        { $$ = Lnil; }
830         |  qtycls                               { $$ = lsing($1); }
831         ;
832
833 dtycls_list:  qtycls                            { $$ = lsing($1); }
834         |  dtycls_list COMMA qtycls             { $$ = lapp($1,$3); }
835         ;
836
837 instdefs : /* empty */                          { $$ = mknullbind(); }
838          | instdef                              { $$ = $1; }
839          | instdefs SEMI instdef
840                 {
841                   if(SAMEFN)
842                     {
843                       extendfn($1,$3);
844                       $$ = $1;
845                     }
846                   else
847                     $$ = mkabind($1,$3);
848                 }
849         ;
850
851 /* instdef: same as valdef, except certain user-pragmas may appear */
852 instdef :
853            SPECIALISE_UPRAGMA qvark DCOLON types_and_maybe_ids END_UPRAGMA
854                 {
855                   $$ = mkvspec_uprag($2, $4, startlineno);
856                   PREVPATT = NULL; FN = NULL; SAMEFN = 0;
857                 }
858
859         |  INLINE_UPRAGMA qvark END_UPRAGMA
860                 {
861                   $$ = mkinline_uprag($2, startlineno);
862                   PREVPATT = NULL; FN = NULL; SAMEFN = 0;
863                 }
864
865         |  MAGIC_UNFOLDING_UPRAGMA qvark vark END_UPRAGMA
866                 {
867                   $$ = mkmagicuf_uprag($2, $3, startlineno);
868                   PREVPATT = NULL; FN = NULL; SAMEFN = 0;
869                 }
870
871         |  valdef
872         ;
873
874
875 valdef  :  vallhs
876                 {
877                   tree fn = function($1);
878                   PREVPATT = $1;
879
880                   if(ttree(fn) == ident)
881                     {
882                       qid fun_id = gident((struct Sident *) fn);
883                       checksamefn(fun_id);
884                       FN = fun_id;
885                     }
886
887                   else if (ttree(fn) == infixap)
888                     {
889                       qid fun_id = ginffun((struct Sinfixap *) fn); 
890                       checksamefn(fun_id);
891                       FN = fun_id;
892                     }
893
894                   else if(etags)
895 #if 1/*etags*/
896                     printf("%u\n",startlineno);
897 #else
898                     fprintf(stderr,"%u\tvaldef\n",startlineno);
899 #endif
900                 }
901            valrhs
902                 {
903                   if ( lhs_is_patt($1) )
904                     {
905                       $$ = mkpbind($3, startlineno);
906                       FN = NULL;
907                       SAMEFN = 0;
908                     }
909                   else
910                     $$ = mkfbind($3,startlineno);
911
912                   PREVPATT = NULL;
913                 }
914         ;
915
916 vallhs  : patk                                  { $$ = $1; }
917         | patk qvarop pat                       { $$ = mkinfixap($2,$1,$3); }
918         | funlhs                                { $$ = $1; }
919         ;
920
921 funlhs  :  qvark apat                           { $$ = mkap(mkident($1),$2); }
922         |  funlhs apat                          { $$ = mkap($1,$2); }
923         ;
924
925
926 valrhs  :  valrhs1 maybe_where                  { $$ = lsing(createpat($1, $2)); }
927         ;
928
929 valrhs1 :  gdrhs                                { $$ = mkpguards($1); }
930         |  EQUAL exp                            { $$ = mkpnoguards($2); }
931         ;
932
933 gdrhs   :  gd EQUAL exp                         { $$ = lsing(mkpgdexp($1,$3)); }
934         |  gd EQUAL exp gdrhs                   { $$ = mklcons(mkpgdexp($1,$3),$4); }
935         ;
936
937 maybe_where:
938            WHERE ocurly decls ccurly            { $$ = $3; }
939         |  WHERE vocurly decls vccurly          { $$ = $3; }
940            /* A where containing no decls is OK */
941         |  WHERE SEMI                           { $$ = mknullbind(); }
942         |  /* empty */                          { $$ = mknullbind(); }
943         ;
944
945 gd      :  VBAR quals                           { $$ = $2; }
946         ;
947
948
949 /**********************************************************************
950 *                                                                     *
951 *                                                                     *
952 *     Expressions                                                     *
953 *                                                                     *
954 *                                                                     *
955 **********************************************************************/
956
957 exp     :  oexp DCOLON ctype                    { $$ = mkrestr($1,$3); }
958         |  oexp
959         ;
960
961 /*
962   Operators must be left-associative at the same precedence for
963   precedence parsing to work.
964 */
965         /* 8 S/R conflicts on qop -> shift */
966 oexp    :  oexp qop oexp %prec MINUS            { $$ = mkinfixap($2,$1,$3); }
967         |  dexp
968         ;
969
970 /*
971   This comes here because of the funny precedence rules concerning
972   prefix minus.
973 */
974 dexp    :  MINUS kexp                           { $$ = mknegate($2); }
975         |  kexp
976         ;
977
978 /*
979   We need to factor out a leading let expression so we can set
980   inpat=TRUE when parsing (non let) expressions inside stmts and quals
981 */
982 expLno  :  oexpLno DCOLON ctype                 { $$ = mkrestr($1,$3); }
983         |  oexpLno
984         ;
985 oexpLno :  oexpLno qop oexp %prec MINUS         { $$ = mkinfixap($2,$1,$3); }
986         |  dexpLno
987         ;
988 dexpLno :  MINUS kexp                           { $$ = mknegate($2); }
989         |  kexpLno
990         ;
991
992 expL    :  oexpL DCOLON ctype                   { $$ = mkrestr($1,$3); }
993         |  oexpL
994         ;
995 oexpL   :  oexpL qop oexp %prec MINUS           { $$ = mkinfixap($2,$1,$3); }
996         |  kexpL
997         ;
998
999 /*
1000   let/if/lambda/case have higher precedence than infix operators.
1001 */
1002
1003 kexp    :  kexpL
1004         |  kexpLno
1005         ;
1006
1007 /* kexpL = a let expression */
1008 kexpL   :  letdecls IN exp                      { $$ = mklet($1,$3); }
1009         ;
1010
1011 /* kexpLno = any other expression more tightly binding than operator application */
1012 kexpLno :  LAMBDA
1013                 { hsincindent();        /* push new context for FN = NULL;        */
1014                   FN = NULL;            /* not actually concerned about indenting */
1015                   $<ulong>$ = hsplineno; /* remember current line number           */
1016                 }
1017            lampats
1018                 { hsendindent();
1019                 }
1020            RARROW exp                   /* lambda abstraction */
1021                 {
1022                   $$ = mklambda($3, $6, $<ulong>2);
1023                 }
1024
1025         /* If Expression */
1026         |  IF {$<ulong>$ = hsplineno;}
1027            exp THEN exp ELSE exp                { $$ = mkife($3,$5,$7,$<ulong>2); }
1028
1029         /* Case Expression */
1030         |  CASE {$<ulong>$ = hsplineno;}
1031            exp OF caserest                      { $$ = mkcasee($3,$5,$<ulong>2); }
1032
1033         /* Do Expression */
1034         |  DO {$<ulong>$ = hsplineno;}
1035            dorest                               { $$ = mkdoe($3,$<ulong>2); }
1036
1037         /* CCALL/CASM Expression */
1038         |  CCALL ccallid cexps                  { $$ = mkccall($2,install_literal("n"),$3); }
1039         |  CCALL ccallid                        { $$ = mkccall($2,install_literal("n"),Lnil); }
1040         |  CCALL_GC ccallid cexps               { $$ = mkccall($2,install_literal("p"),$3); }
1041         |  CCALL_GC ccallid                     { $$ = mkccall($2,install_literal("p"),Lnil); }
1042         |  CASM CLITLIT cexps                   { $$ = mkccall($2,install_literal("N"),$3); }
1043         |  CASM CLITLIT                         { $$ = mkccall($2,install_literal("N"),Lnil); }
1044         |  CASM_GC CLITLIT cexps                { $$ = mkccall($2,install_literal("P"),$3); }
1045         |  CASM_GC CLITLIT                      { $$ = mkccall($2,install_literal("P"),Lnil); }
1046
1047         /* SCC Expression */
1048         |  SCC STRING exp
1049                 { if (ignoreSCC) {
1050                     $$ = $3;
1051                   } else {
1052                     $$ = mkscc($2, $3);
1053                   }
1054                 }
1055         |  fexp
1056         ;
1057
1058 fexp    :  fexp aexp                            { $$ = mkap($1,$2); }
1059         |  aexp
1060         ;
1061
1062         /* simple expressions */
1063 aexp    :  qvar                                 { $$ = mkident($1); }
1064         |  gcon                                 { $$ = mkident($1); }
1065         |  lit_constant                         { $$ = mklit($1); }
1066         |  OPAREN exp CPAREN                    { $$ = mkpar($2); }         /* mkpar: stop infix parsing at ()'s */
1067         |  qcon OCURLY rbinds CCURLY            { $$ = mkrecord($1,$3); }   /* 1 S/R conflict on OCURLY -> shift */
1068         |  OBRACK list_exps CBRACK              { $$ = mkllist($2); }
1069         |  OPAREN exp COMMA texps CPAREN        { if (ttree($4) == tuple)
1070                                                      $$ = mktuple(mklcons($2, gtuplelist((struct Stuple *) $4)));
1071                                                   else
1072                                                      $$ = mktuple(ldub($2, $4)); }
1073
1074         /* only in expressions ... */
1075         |  aexp OCURLY rbinds1 CCURLY           { $$ = mkrupdate($1,$3); }
1076         |  OBRACK exp VBAR quals CBRACK         { $$ = mkcomprh($2,$4); }
1077         |  OBRACK exp COMMA exp DOTDOT exp CBRACK {$$= mkeenum($2,mkjust($4),mkjust($6)); }
1078         |  OBRACK exp COMMA exp DOTDOT CBRACK   { $$ = mkeenum($2,mkjust($4),mknothing()); }
1079         |  OBRACK exp DOTDOT exp CBRACK         { $$ = mkeenum($2,mknothing(),mkjust($4)); }
1080         |  OBRACK exp DOTDOT CBRACK             { $$ = mkeenum($2,mknothing(),mknothing()); }
1081         |  OPAREN oexp qop CPAREN               { $$ = mklsection($2,$3); }
1082         |  OPAREN qop1 oexp CPAREN              { $$ = mkrsection($2,$3); }
1083
1084         /* only in patterns ... */
1085         /* these add 2 S/R conflict with with  aexp . OCURLY rbinds CCURLY */
1086         |  qvar AT aexp                         { checkinpat(); $$ = mkas($1,$3); }
1087         |  LAZY aexp                            { checkinpat(); $$ = mklazyp($2); }
1088         |  WILDCARD                             { checkinpat(); $$ = mkwildp();   }
1089         ;
1090
1091         /* ccall arguments */
1092 cexps   :  cexps aexp                           { $$ = lapp($1,$2); }
1093         |  aexp                                 { $$ = lsing($1); }
1094         ;
1095
1096 caserest:  ocurly alts ccurly                   { $$ = $2; }
1097         |  vocurly alts vccurly                 { $$ = $2; }
1098
1099 dorest  :  ocurly stmts ccurly                  { checkdostmts($2); $$ = $2; }
1100         |  vocurly stmts vccurly                { checkdostmts($2); $$ = $2; }
1101         ;
1102
1103 rbinds  :  /* empty */                          { $$ = Lnil; }
1104         |  rbinds1
1105         ;
1106
1107 rbinds1 :  rbind                                { $$ = lsing($1); }
1108         |  rbinds1 COMMA rbind                  { $$ = lapp($1,$3); }
1109         ;
1110
1111 rbind   :  qvar                                 { $$ = mkrbind($1,mknothing()); }
1112         |  qvar EQUAL exp                       { $$ = mkrbind($1,mkjust($3)); }
1113         ;
1114
1115 texps   :  exp  { $$ = mkpar($1); }     /* mkpar: so we don't flatten last element in tuple */
1116         |  exp COMMA texps
1117                 { if (ttree($3) == tuple)
1118                     $$ = mktuple(mklcons($1, gtuplelist((struct Stuple *) $3)));
1119                   else if (ttree($3) == par)
1120                     $$ = mktuple(ldub($1, gpare((struct Spar *) $3)));
1121                   else
1122                     hsperror("hsparser:texps: panic");
1123                 }
1124         /* right recursion? WDP */
1125         ;
1126
1127 list_exps :
1128            exp                                  { $$ = lsing($1); }
1129         |  exp COMMA exp                        { $$ = mklcons( $1, lsing($3) ); }
1130         |  exp COMMA exp COMMA list_rest        { $$ = mklcons( $1, mklcons( $3, reverse_list( $5 ))); }
1131         ;
1132
1133 /* Use left recusion for list_rest, because we sometimes get programs with
1134    very long explicit lists. */
1135 list_rest :     exp                             { $$ = lsing($1); }
1136           | list_rest COMMA exp                 { $$ = mklcons( $3, $1 ); }
1137           ;
1138
1139 /* 
1140            exp                                  { $$ = lsing($1); }
1141         |  exp COMMA list_exps          { $$ = mklcons($1, $3); }
1142 */
1143         /* right recursion? (WDP)
1144
1145            It has to be this way, though, otherwise you
1146            may do the wrong thing to distinguish between...
1147
1148            [ e1 , e2 .. ]       -- an enumeration ...
1149            [ e1 , e2 , e3 ]     -- a list
1150
1151            (In fact, if you change the grammar and throw yacc/bison
1152            at it, it *will* do the wrong thing [WDP 94/06])
1153         */
1154
1155 letdecls:  LET ocurly decls ccurly              { $$ = $3 }
1156         |  LET vocurly decls vccurly            { $$ = $3 }
1157         ;
1158
1159 quals   :  qual                                 { $$ = lsing($1); }
1160         |  quals COMMA qual                     { $$ = lapp($1,$3); }
1161         ;
1162
1163 qual    :  letdecls                             { $$ = mkseqlet($1); }
1164         |  expL                                 { $$ = $1; }
1165         |  {inpat=TRUE;} expLno 
1166            {inpat=FALSE;} leftexp
1167                 { if ($4 == NULL) {
1168                       expORpat(LEGIT_EXPR,$2);
1169                       $$ = mkguard($2);
1170                   } else {
1171                       expORpat(LEGIT_PATT,$2);
1172                       $$ = mkqual($2,$4);
1173                   }
1174                 }
1175         ;
1176
1177 alts    :  alt                                  { $$ = $1; }
1178         |  alts SEMI alt                        { $$ = lconc($1,$3); }
1179         ;
1180
1181 alt     :  pat { PREVPATT = $1; } altrest       { $$ = lsing($3); PREVPATT = NULL; }
1182         |  /* empty */                          { $$ = Lnil; }
1183         ;
1184
1185 altrest :  gdpat maybe_where                    { $$ = createpat(mkpguards($1), $2); }
1186         |  RARROW exp maybe_where               { $$ = createpat(mkpnoguards($2),$3); }
1187         ;
1188
1189 gdpat   :  gd RARROW exp                        { $$ = lsing(mkpgdexp($1,$3)); }
1190         |  gd RARROW exp gdpat                  { $$ = mklcons(mkpgdexp($1,$3),$4);  }
1191         ;
1192
1193 stmts   :  stmt                                 { $$ = $1; }
1194         |  stmts SEMI stmt                      { $$ = lconc($1,$3); }
1195         ;
1196
1197 stmt    :  /* empty */                          { $$ = Lnil; }
1198         |  letdecls                             { $$ = lsing(mkseqlet($1)); }
1199         |  expL                                 { $$ = lsing(mkdoexp($1,hsplineno)); }
1200         |  {inpat=TRUE;} expLno {inpat=FALSE;} leftexp
1201                 { if ($4 == NULL) {
1202                       expORpat(LEGIT_EXPR,$2);
1203                       $$ = lsing(mkdoexp($2,endlineno));
1204                   } else {
1205                       expORpat(LEGIT_PATT,$2);
1206                       $$ = lsing(mkdobind($2,$4,endlineno));
1207                   }
1208                 }
1209         ;
1210
1211 leftexp :  LARROW exp                           { $$ = $2; }
1212         |  /* empty */                          { $$ = NULL; }
1213         ;
1214
1215 /**********************************************************************
1216 *                                                                     *
1217 *                                                                     *
1218 *     Patterns                                                        *
1219 *                                                                     *
1220 *                                                                     *
1221 **********************************************************************/
1222
1223 pat     :  qvar PLUS INTEGER                    { $$ = mkplusp($1, mkinteger($3)); }
1224         |  cpat
1225         ;
1226
1227 cpat    :  cpat qconop bpat                     { $$ = mkinfixap($2,$1,$3); }
1228         |  bpat
1229         ;
1230
1231 bpat    :  apatc
1232         |  conpat
1233         |  qcon OCURLY rpats CCURLY             { $$ = mkrecord($1,$3); }
1234         |  MINUS INTEGER                        { $$ = mknegate(mklit(mkinteger($2))); }
1235         |  MINUS FLOAT                          { $$ = mknegate(mklit(mkfloatr($2))); }
1236         ;
1237
1238 conpat  :  gcon                                 { $$ = mkident($1); }
1239         |  conpat apat                          { $$ = mkap($1,$2); }
1240         ;
1241
1242 apat    :  gcon                                 { $$ = mkident($1); }
1243         |  qcon OCURLY rpats CCURLY             { $$ = mkrecord($1,$3); }
1244         |  apatc
1245         ;
1246
1247 apatc   :  qvar                                 { $$ = mkident($1); }
1248         |  qvar AT apat                         { $$ = mkas($1,$3); }
1249         |  lit_constant                         { $$ = mklit($1); }
1250         |  WILDCARD                             { $$ = mkwildp(); }
1251         |  OPAREN pat CPAREN                    { $$ = mkpar($2); }
1252         |  OPAREN pat COMMA pats CPAREN         { $$ = mktuple(mklcons($2,$4)); }
1253         |  OBRACK pats CBRACK                   { $$ = mkllist($2); }
1254         |  LAZY apat                            { $$ = mklazyp($2); }
1255         ;
1256
1257 lit_constant:
1258            INTEGER                              { $$ = mkinteger($1); }
1259         |  FLOAT                                { $$ = mkfloatr($1); }
1260         |  CHAR                                 { $$ = mkcharr($1); }
1261         |  STRING                               { $$ = mkstring($1); }
1262         |  CHARPRIM                             { $$ = mkcharprim($1); }
1263         |  STRINGPRIM                           { $$ = mkstringprim($1); }
1264         |  INTPRIM                              { $$ = mkintprim($1); }
1265         |  FLOATPRIM                            { $$ = mkfloatprim($1); }
1266         |  DOUBLEPRIM                           { $$ = mkdoubleprim($1); }
1267         |  CLITLIT /* yurble yurble */          { $$ = mkclitlit($1); }
1268         ;
1269
1270 lampats :  apat lampats                         { $$ = mklcons($1,$2); }
1271         |  apat                                 { $$ = lsing($1); }
1272         /* right recursion? (WDP) */
1273         ;
1274
1275 pats    :  pat COMMA pats                       { $$ = mklcons($1, $3); }
1276         |  pat                                  { $$ = lsing($1); }
1277         /* right recursion? (WDP) */
1278         ;
1279
1280 rpats   : /* empty */                           { $$ = Lnil; }
1281         | rpats1
1282         ;
1283
1284 rpats1  : rpat                                  { $$ = lsing($1); }
1285         | rpats1 COMMA rpat                     { $$ = lapp($1,$3); }
1286         ;
1287
1288 rpat    :  qvar                                 { $$ = mkrbind($1,mknothing()); }
1289         |  qvar EQUAL pat                       { $$ = mkrbind($1,mkjust($3)); }
1290         ;
1291
1292
1293 patk    :  patk qconop bpat                     { $$ = mkinfixap($2,$1,$3); }
1294         |  bpatk
1295         ;
1296
1297 bpatk   :  apatck
1298         |  conpatk
1299         |  qconk OCURLY rpats CCURLY            { $$ = mkrecord($1,$3); }
1300         |  minuskey INTEGER                     { $$ = mknegate(mklit(mkinteger($2))); }
1301         |  minuskey FLOAT                       { $$ = mknegate(mklit(mkfloatr($2))); }
1302         ;
1303
1304 conpatk :  gconk                                { $$ = mkident($1); }
1305         |  conpatk apat                         { $$ = mkap($1,$2); }
1306         ;
1307
1308 apatck  :  qvark                                { $$ = mkident($1); }
1309         |  qvark AT apat                        { $$ = mkas($1,$3); }
1310         |  lit_constant                         { $$ = mklit($1); setstartlineno(); }
1311         |  WILDCARD                             { $$ = mkwildp(); setstartlineno(); }
1312         |  oparenkey pat CPAREN                 { $$ = mkpar($2); }
1313         |  oparenkey pat COMMA pats CPAREN      { $$ = mktuple(mklcons($2,$4)); }
1314         |  obrackkey pats CBRACK                { $$ = mkllist($2); }
1315         |  lazykey apat                         { $$ = mklazyp($2); }
1316         ;
1317
1318
1319 gcon    :  qcon
1320         |  OBRACK CBRACK                        { $$ = creategid(-1); }
1321         |  OPAREN CPAREN                        { $$ = creategid(0); }
1322         |  OPAREN commas CPAREN                 { $$ = creategid($2); }
1323         ;
1324
1325 gconk   :  qconk
1326         |  obrackkey CBRACK                     { $$ = creategid(-1); }
1327         |  oparenkey CPAREN                     { $$ = creategid(0); }
1328         |  oparenkey commas CPAREN              { $$ = creategid($2); }
1329         ;
1330
1331 /**********************************************************************
1332 *                                                                     *
1333 *                                                                     *
1334 *     Keywords which record the line start                            *
1335 *                                                                     *
1336 *                                                                     *
1337 **********************************************************************/
1338
1339 importkey: IMPORT                { setstartlineno(); $$ = 0; }
1340         |  IMPORT SOURCE_UPRAGMA { setstartlineno(); $$ = 1; }
1341         ;
1342
1343 datakey :   DATA        { setstartlineno();
1344                           if(etags)
1345 #if 1/*etags*/
1346                             printf("%u\n",startlineno);
1347 #else
1348                             fprintf(stderr,"%u\tdata\n",startlineno);
1349 #endif
1350                         }
1351         ;
1352
1353 typekey :   TYPE        { setstartlineno();
1354                           if(etags)
1355 #if 1/*etags*/
1356                             printf("%u\n",startlineno);
1357 #else
1358                             fprintf(stderr,"%u\ttype\n",startlineno);
1359 #endif
1360                         }
1361         ;
1362
1363 newtypekey : NEWTYPE    { setstartlineno();
1364                           if(etags)
1365 #if 1/*etags*/
1366                             printf("%u\n",startlineno);
1367 #else
1368                             fprintf(stderr,"%u\tnewtype\n",startlineno);
1369 #endif
1370                         }
1371         ;
1372
1373 instkey :   INSTANCE    { setstartlineno();
1374 #if 1/*etags*/
1375 /* OUT:                   if(etags)
1376                             printf("%u\n",startlineno);
1377 */
1378 #else
1379                             fprintf(stderr,"%u\tinstance\n",startlineno);
1380 #endif
1381                         }
1382         ;
1383
1384 defaultkey: DEFAULT     { setstartlineno(); }
1385         ;
1386
1387 classkey:   CLASS       { setstartlineno();
1388                           if(etags)
1389 #if 1/*etags*/
1390                             printf("%u\n",startlineno);
1391 #else
1392                             fprintf(stderr,"%u\tclass\n",startlineno);
1393 #endif
1394                         }
1395         ;
1396
1397 modulekey:  MODULE      { setstartlineno();
1398                           if(etags)
1399 #if 1/*etags*/
1400                             printf("%u\n",startlineno);
1401 #else
1402                             fprintf(stderr,"%u\tmodule\n",startlineno);
1403 #endif
1404                         }
1405         ;
1406
1407 oparenkey:  OPAREN      { setstartlineno(); }
1408         ;
1409
1410 obrackkey:  OBRACK      { setstartlineno(); }
1411         ;
1412
1413 lazykey :   LAZY        { setstartlineno(); }
1414         ;
1415
1416 minuskey:   MINUS       { setstartlineno(); }
1417         ;
1418
1419
1420 /**********************************************************************
1421 *                                                                     *
1422 *                                                                     *
1423 *     Basic qualified/unqualified ids/ops                             *
1424 *                                                                     *
1425 *                                                                     *
1426 **********************************************************************/
1427
1428 qvar    :  qvarid
1429         |  OPAREN qvarsym CPAREN        { $$ = $2; }
1430         ;
1431 qcon    :  qconid
1432         |  OPAREN qconsym CPAREN        { $$ = $2; }
1433         ;
1434 qvarop  :  qvarsym
1435         |  BQUOTE qvarid BQUOTE         { $$ = $2; }
1436         ;
1437 qconop  :  qconsym
1438         |  BQUOTE qconid BQUOTE         { $$ = $2; }
1439         ;
1440 qop     :  qconop
1441         |  qvarop
1442         ;
1443
1444 /* Non "-" op, used in right sections */
1445 qop1    :  qconop
1446         |  qvarop1
1447         ;
1448
1449 /* Non "-" varop, used in right sections */
1450 qvarop1 :  QVARSYM
1451         |  varsym_nominus               { $$ = mknoqual($1); }
1452         |  BQUOTE qvarid BQUOTE         { $$ = $2; }
1453         ;
1454
1455
1456 var     :  varid
1457         |  OPAREN varsym CPAREN         { $$ = $2; }
1458         ;
1459 con     :  tycon                        /* using tycon removes conflicts */
1460         |  OPAREN CONSYM CPAREN         { $$ = $2; }
1461         ;
1462 varop   :  varsym
1463         |  BQUOTE varid BQUOTE          { $$ = $2; }
1464         ;
1465 conop   :  CONSYM
1466         |  BQUOTE CONID BQUOTE          { $$ = $2; }
1467         ;
1468 op      :  conop
1469         |  varop
1470         ;
1471
1472 qvark   :  qvarid                       { setstartlineno(); $$ = $1; }
1473         |  oparenkey qvarsym CPAREN     { $$ = $2; }
1474         ;
1475 qconk   :  qconid                       { setstartlineno(); $$ = $1; }
1476         |  oparenkey qconsym CPAREN     { $$ = $2; }
1477         ;
1478 vark    :  varid                        { setstartlineno(); $$ = $1; }
1479         |  oparenkey varsym CPAREN      { $$ = $2; }
1480         ;
1481
1482 qvarid  :  QVARID
1483         |  varid                        { $$ = mknoqual($1); }
1484         ;
1485 qvarsym :  QVARSYM
1486         |  varsym                       { $$ = mknoqual($1); }
1487         ;
1488 qconid  :  QCONID
1489         |  tycon                        { $$ = mknoqual($1); } /* using tycon removes conflicts */
1490         ;
1491 qconsym :  QCONSYM
1492         |  CONSYM                       { $$ = mknoqual($1); }
1493         ;
1494 qtycon  :  QCONID
1495         |  tycon                        { $$ = mknoqual($1); } /* using tycon removes conflicts */
1496         ;
1497 qtycls  :  QCONID
1498         |  tycon                        { $$ = mknoqual($1); } /* using tycon removes conflicts */
1499         ;
1500
1501 varsym  :  varsym_nominus
1502         |  MINUS                        { $$ = install_literal("-"); }
1503         ;
1504
1505 /* PLUS, BANG are valid varsyms */
1506 varsym_nominus : VARSYM
1507         |  PLUS                         { $$ = install_literal("+"); }
1508         |  BANG                         { $$ = install_literal("!"); }  
1509         ;
1510
1511 /* AS HIDING QUALIFIED are valid varids */
1512 varid   :  VARID
1513         |  AS                           { $$ = install_literal("as"); }
1514         |  HIDING                       { $$ = install_literal("hiding"); }
1515         |  QUALIFIED                    { $$ = install_literal("qualified"); }
1516         ;
1517
1518
1519 ccallid :  VARID
1520         |  CONID
1521         ;
1522
1523 tyvar   :  varid                        { $$ = mknamedtvar(mknoqual($1)); }
1524         ;
1525 tycon   :  CONID
1526         ;
1527 modid   :  CONID
1528         ;
1529
1530 /*
1531 tyvar_list: tyvar                       { $$ = lsing($1); }
1532         |  tyvar_list COMMA tyvar       { $$ = lapp($1,$3); }
1533         ;
1534 */
1535
1536 /**********************************************************************
1537 *                                                                     *
1538 *                                                                     *
1539 *     Stuff to do with layout                                         *
1540 *                                                                     *
1541 *                                                                     *
1542 **********************************************************************/
1543
1544 ocurly  : layout OCURLY                         { hsincindent(); }
1545
1546 vocurly : layout                                { hssetindent(); }
1547         ;
1548
1549 layout  :                                       { hsindentoff(); }
1550         ;
1551
1552 ccurly  :
1553          CCURLY
1554                 {
1555                   FN = NULL; SAMEFN = 0; PREVPATT = NULL;
1556                   hsendindent();
1557                 }
1558         ;
1559
1560 vccurly :  { expect_ccurly = 1; }  vccurly1  { expect_ccurly = 0; }
1561         ;
1562
1563 vccurly1:
1564          VCCURLY
1565                 {
1566                   FN = NULL; SAMEFN = 0; PREVPATT = NULL;
1567                   hsendindent();
1568                 }
1569         | error
1570                 {
1571                   yyerrok;
1572                   FN = NULL; SAMEFN = 0; PREVPATT = NULL;
1573                   hsendindent();
1574                 }
1575         ;
1576
1577 %%
1578
1579 /**********************************************************************
1580 *                                                                     *
1581 *      Error Processing and Reporting                                 *
1582 *                                                                     *
1583 *  (This stuff is here in case we want to use Yacc macros and such.)  *
1584 *                                                                     *
1585 **********************************************************************/
1586
1587 void
1588 checkinpat()
1589 {
1590   if(!inpat)
1591     hsperror("pattern syntax used in expression");
1592 }
1593
1594
1595 /* The parser calls "hsperror" when it sees a
1596    `report this and die' error.  It sets the stage
1597    and calls "yyerror".
1598
1599    There should be no direct calls in the parser to
1600    "yyerror", except for the one from "hsperror".  Thus,
1601    the only other calls will be from the error productions
1602    introduced by yacc/bison/whatever.
1603
1604    We need to be able to recognise the from-error-production
1605    case, because we sometimes want to say, "Oh, never mind",
1606    because the layout rule kicks into action and may save
1607    the day.  [WDP]
1608 */
1609
1610 static BOOLEAN error_and_I_mean_it = FALSE;
1611
1612 void
1613 hsperror(s)
1614   char *s;
1615 {
1616     error_and_I_mean_it = TRUE;
1617     yyerror(s);
1618 }
1619
1620 extern char *yytext;
1621 extern int yyleng;
1622
1623 void
1624 yyerror(s)
1625   char *s;
1626 {
1627     /* We want to be able to distinguish 'error'-raised yyerrors
1628        from yyerrors explicitly coded by the parser hacker.
1629     */
1630     if (expect_ccurly && ! error_and_I_mean_it ) {
1631         /*NOTHING*/;
1632
1633     } else {
1634         fprintf(stderr, "%s:%d:%d: %s on input: ",
1635           input_filename, hsplineno, hspcolno + 1, s);
1636
1637         if (yyleng == 1 && *yytext == '\0')
1638             fprintf(stderr, "<EOF>");
1639
1640         else {
1641             fputc('"', stderr);
1642             format_string(stderr, (unsigned char *) yytext, yyleng);
1643             fputc('"', stderr);
1644         }
1645         fputc('\n', stderr);
1646
1647         /* a common problem */
1648         if (strcmp(yytext, "#") == 0)
1649             fprintf(stderr, "\t(Perhaps you forgot a `-cpp' or `-fglasgow-exts' flag?)\n");
1650
1651         exit(1);
1652     }
1653 }
1654
1655 void
1656 format_string(fp, s, len)
1657   FILE *fp;
1658   unsigned char *s;
1659   int len;
1660 {
1661     while (len-- > 0) {
1662         switch (*s) {
1663         case '\0':    fputs("\\NUL", fp);   break;
1664         case '\007':  fputs("\\a", fp);     break;
1665         case '\010':  fputs("\\b", fp);     break;
1666         case '\011':  fputs("\\t", fp);     break;
1667         case '\012':  fputs("\\n", fp);     break;
1668         case '\013':  fputs("\\v", fp);     break;
1669         case '\014':  fputs("\\f", fp);     break;
1670         case '\015':  fputs("\\r", fp);     break;
1671         case '\033':  fputs("\\ESC", fp);   break;
1672         case '\034':  fputs("\\FS", fp);    break;
1673         case '\035':  fputs("\\GS", fp);    break;
1674         case '\036':  fputs("\\RS", fp);    break;
1675         case '\037':  fputs("\\US", fp);    break;
1676         case '\177':  fputs("\\DEL", fp);   break;
1677         default:
1678             if (*s >= ' ')
1679                 fputc(*s, fp);
1680             else
1681                 fprintf(fp, "\\^%c", *s + '@');
1682             break;
1683         }
1684         s++;
1685     }
1686 }