[project @ 1997-06-18 23:52:36 by simonpj]
[ghc-hetmet.git] / ghc / compiler / parser / hsparser.y
index 5212226..58db2df 100644 (file)
@@ -47,6 +47,7 @@ static char *the_module_name;
 static maybe module_exports;
 
 extern list Lnil;
+extern list reverse_list();
 extern tree root;
 
 /* For FN, PREVPATT and SAMEFN macros */
@@ -170,7 +171,7 @@ BOOLEAN inpat;
 *                                                                     *
 **********************************************************************/
 
-%token MINUS           BANG
+%token MINUS           BANG            PLUS
 %token         AS              HIDING          QUALIFIED
 
 
@@ -184,7 +185,8 @@ BOOLEAN inpat;
 
 %token  INTERFACE_UPRAGMA SPECIALISE_UPRAGMA
 %token  INLINE_UPRAGMA MAGIC_UNFOLDING_UPRAGMA
-%token  DEFOREST_UPRAGMA END_UPRAGMA
+%token  DEFOREST_UPRAGMA END_UPRAGMA 
+%token  SOURCE_UPRAGMA
 
 /**********************************************************************
 *                                                                     *
@@ -200,7 +202,7 @@ BOOLEAN inpat;
        SCC     CASM    CCALL   CASM_GC CCALL_GC
 
 %left  VARSYM  CONSYM  QVARSYM QCONSYM
-       MINUS   BQUOTE  BANG    DARROW
+       MINUS   BQUOTE  BANG    DARROW  PLUS
 
 %left  DCOLON
 
@@ -223,7 +225,7 @@ BOOLEAN inpat;
 
 %type <ulist>   caserest alts alt quals
                dorest stmts stmt
-               rbinds rpats list_exps 
+               rbinds rbinds1 rpats rpats1 list_exps list_rest
                qvarsk qvars_list
                constrs constr1 fields 
                types atypes batypes
@@ -235,7 +237,7 @@ BOOLEAN inpat;
                maybefixes fixes fix ops
                dtyclses dtycls_list
                gdrhs gdpat valrhs
-               lampats cexps
+               lampats cexps gd
 
 %type <umaybe>  maybeexports impspec deriving
 
@@ -243,12 +245,12 @@ BOOLEAN inpat;
 
 %type <utree>  exp oexp dexp kexp fexp aexp rbind texps
                expL oexpL kexpL expLno oexpLno dexpLno kexpLno
-               vallhs funlhs qual gd leftexp
-               pat bpat apat apatc conpat rpat
-               patk bpatk apatck conpatk
+               vallhs funlhs qual leftexp
+               pat cpat bpat apat apatc conpat rpat
+                       patk bpatk apatck conpatk
 
 
-%type <uid>    MINUS DARROW AS LAZY
+%type <uid>    MINUS PLUS DARROW AS LAZY
                VARID CONID VARSYM CONSYM 
                var con varop conop op
                vark varid varsym varsym_nominus
@@ -268,14 +270,12 @@ BOOLEAN inpat;
 
 %type <upbinding> valrhs1 altrest
 
-%type <uttype>    simple ctype type atype btype
+%type <uttype>    simple ctype sigtype sigarrowtype type atype bigatype btype
                  gtyconvars 
-                 bbtype batype 
-                 class tyvar
-/*               gtyconapp0 gtyconapp1 ntyconapp0 ntyconapp1 btyconapp */
-/*               restrict_inst general_inst */
+                 bbtype batype bxtype wierd_atype
+                 class tyvar contype
 
-%type <uconstr>          constr field
+%type <uconstr>          constr constr_after_context field
 
 %type <ustring>   FLOAT INTEGER INTPRIM
                  FLOATPRIM DOUBLEPRIM CLITLIT
@@ -284,7 +284,7 @@ BOOLEAN inpat;
 
 %type <uentid>   export import
 
-%type <ulong>     commas
+%type <ulong>     commas importkey
 
 /**********************************************************************
 *                                                                     *
@@ -381,11 +381,11 @@ impdecls:  impdecl                                { $$ = $1; }
 
 
 impdecl        :  importkey modid impspec
-               { $$ = lsing(mkimport($2,0,mknothing(),$3,startlineno)); }
+               { $$ = lsing(mkimport($2,0,mknothing(),$3,$1,startlineno)); }
        |  importkey QUALIFIED modid impspec
-               { $$ = lsing(mkimport($3,1,mknothing(),$4,startlineno)); }
+               { $$ = lsing(mkimport($3,1,mknothing(),$4,$1,startlineno)); }
        |  importkey QUALIFIED modid AS modid impspec
-               { $$ = lsing(mkimport($3,1,mkjust($5),$6,startlineno)); }
+               { $$ = lsing(mkimport($3,1,mkjust($5),$6,$1,startlineno)); }
        ;
 
 impspec        :  /* empty */                            { $$ = mknothing(); }
@@ -475,12 +475,12 @@ topdecls:  topdecl
                }
         ;
 
-topdecl        :  typed                                { $$ = $1; }
-       |  datad                                { $$ = $1; }
-       |  newtd                                { $$ = $1; }
-       |  classd                               { $$ = $1; }
-       |  instd                                { $$ = $1; }
-       |  defaultd                             { $$ = $1; }
+topdecl        :  typed                                { $$ = $1; FN = NULL; SAMEFN = 0; }
+       |  datad                                { $$ = $1; FN = NULL; SAMEFN = 0; }
+       |  newtd                                { $$ = $1; FN = NULL; SAMEFN = 0; }
+       |  classd                               { $$ = $1; FN = NULL; SAMEFN = 0; }
+       |  instd                                { $$ = $1; FN = NULL; SAMEFN = 0; }
+       |  defaultd                             { $$ = $1; FN = NULL; SAMEFN = 0; }
        |  decl                                 { $$ = $1; }
        ;
 
@@ -571,7 +571,7 @@ decls       : decl
     to real mischief (ugly, but likely to work).
 */
 
-decl   : qvarsk DCOLON ctype
+decl   : qvarsk DCOLON sigtype
                { $$ = mksbind($1,$3,startlineno);
                  PREVPATT = NULL; FN = NULL; SAMEFN = 0;
                }
@@ -663,18 +663,34 @@ type_and_maybe_id :
     context.  Blaach!
 */
 
+/* A sigtype is a rank 2 type; it can have for-alls as function args:
+       f :: All a => (All b => ...) -> Int
+*/
+sigtype        : type DARROW sigarrowtype              { $$ = mkcontext(type2context($1),$3); }
+       | sigarrowtype 
+       ;
+
+sigarrowtype : bigatype RARROW sigarrowtype    { $$ = mktfun($1,$3); }
+            | btype RARROW sigarrowtype        { $$ = mktfun($1,$3); }
+            | btype
+            ;
+
+/* A "big" atype can be a forall-type in brackets.  */
+bigatype: OPAREN type DARROW type CPAREN       { $$ = mkcontext(type2context($2),$4); }
+       ;
+
        /* 1 S/R conflict at DARROW -> shift */
 ctype   : type DARROW type                     { $$ = mkcontext(type2context($1),$3); }
        | type
        ;
 
        /* 1 S/R conflict at RARROW -> shift */
-type   :  btype                                { $$ = $1; }
-       |  btype RARROW type                    { $$ = mktfun($1,$3); }
+type   :  btype RARROW type                    { $$ = mktfun($1,$3); }
+       |  btype                                { $$ = $1; }
        ;
 
-btype  :  atype                                { $$ = $1; }
-       |  btype atype                          { $$ = mktapp($1,$2); }
+btype  :  btype atype                          { $$ = mktapp($1,$2); }
+       |  atype                                { $$ = $1; }
        ;
 
 atype          :  gtycon                               { $$ = mktname($1); }
@@ -734,24 +750,12 @@ constrs   :  constr                               { $$ = lsing($1); }
        |  constrs VBAR constr                  { $$ = lapp($1,$3); }
        ;
 
-constr :  
-/*             This stuff looks really baroque. I've replaced it with simpler stuff.
-                       SLPJ Jan 97
-       
-          btyconapp                            { qid tyc; list tys;
-                                                 splittyconapp($1, &tyc, &tys);
-                                                 $$ = mkconstrpre(tyc,tys,hsplineno); }
-       |  btyconapp qconop bbtype              { checknobangs($1);
-                                                 $$ = mkconstrinf($1,$2,$3,hsplineno); }
-       |  ntyconapp0 qconop bbtype             { $$ = mkconstrinf($1,$2,$3,hsplineno); }
+constr :  constr_after_context
+       |  type DARROW constr_after_context     { $$ = mkconstrcxt ( type2context($1), $3 ); }
+       ;
 
-       |  BANG atype qconop bbtype             { $$ = mkconstrinf(mktbang($2),$3,$4,hsplineno); }
-       |  OPAREN qconsym CPAREN                { $$ = mkconstrpre($2,Lnil,hsplineno); }
-*/
+constr_after_context :
 
-          btype                                { qid tyc; list tys;
-                                                 splittyconapp($1, &tyc, &tys);
-                                                 $$ = mkconstrpre(tyc,tys,hsplineno); }
        /* We have to parse the constructor application as a *type*, else we get
           into terrible ambiguity problems.  Consider the difference between
 
@@ -764,26 +768,51 @@ constr    :
           second.
        */
 
-       |  OPAREN qconsym CPAREN batypes        { $$ = mkconstrpre($2,$4,hsplineno); }
+/* Con !Int (Tree a) */
+          contype                              { qid tyc; list tys;
+                                                 splittyconapp($1, &tyc, &tys);
+                                                 $$ = mkconstrpre(tyc,tys,hsplineno); }
+
+/* !Int `Con` Tree a */
        |  bbtype qconop bbtype                 { $$ = mkconstrinf($1,$2,$3,hsplineno); }
+
+/* (::) (Tree a) Int */
+       |  OPAREN qconsym CPAREN batypes        { $$ = mkconstrpre($2,$4,hsplineno); }
+
+/* Con { op1 :: Int } */
        |  gtycon OCURLY fields CCURLY          { $$ = mkconstrrec($1,$3,hsplineno); }
                /* 1 S/R conflict on OCURLY -> shift */
        ;
 
-/* 
-btyconapp: gtycon                              { $$ = mktname($1); }
-       |  btyconapp batype                     { $$ = mktapp($1,$2); }
+
+/* contype has to reduce to a btype unless there are !'s, so that
+   we don't get reduce/reduce conflicts with the second production of constr.
+   But as soon as we see a ! we must switch to using bxtype. */
+
+contype : btype                                        { $$ = $1 }
+       | bxtype                                { $$ = $1 }
+       ;
+
+/* S !Int Bool; at least one ! */
+bxtype : btype wierd_atype                     { $$ = mktapp($1, $2); }
+       | bxtype batype                         { $$ = mktapp($1, $2); }
        ;
-*/
 
 bbtype :  btype                                { $$ = $1; }
-       |  BANG atype                           { $$ = mktbang($2); }
+       |  wierd_atype                          { $$ = $1; }
        ;
 
 batype :  atype                                { $$ = $1; }
-       |  BANG atype                           { $$ = mktbang($2); }
+       |  wierd_atype                          { $$ = $1; }
        ;
 
+/* A wierd atype is one that isn't a regular atype;
+   it starts with a "!", or with a forall. */
+wierd_atype : BANG bigatype                    { $$ = mktbang( $2 ) }
+           | BANG atype                        { $$ = mktbang( $2 ) }
+           | bigatype 
+           ;
+
 batypes        :                                       { $$ = Lnil; }
        |  batypes batype                       { $$ = lapp($1,$2); }
        ;
@@ -793,8 +822,9 @@ fields      : field                                 { $$ = lsing($1); }
        | fields COMMA field                    { $$ = lapp($1,$3); }
        ;
 
-field  :  qvars_list DCOLON type               { $$ = mkfield($1,$3); }
+field  :  qvars_list DCOLON ctype              { $$ = mkfield($1,$3); }
        |  qvars_list DCOLON BANG atype         { $$ = mkfield($1,mktbang($4)); }
+       |  qvars_list DCOLON BANG bigatype      { $$ = mkfield($1,mktbang($4)); }
        ; 
 
 constr1 :  gtycon atype                                { $$ = lsing(mkconstrnew($1,$2,hsplineno)); }
@@ -913,10 +943,12 @@ gdrhs     :  gd EQUAL exp                         { $$ = lsing(mkpgdexp($1,$3)); }
 maybe_where:
           WHERE ocurly decls ccurly            { $$ = $3; }
        |  WHERE vocurly decls vccurly          { $$ = $3; }
+           /* A where containing no decls is OK */
+       |  WHERE SEMI                           { $$ = mknullbind(); }
        |  /* empty */                          { $$ = mknullbind(); }
        ;
 
-gd     :  VBAR oexp                            { $$ = $2; }
+gd     :  VBAR quals                           { $$ = $2; }
        ;
 
 
@@ -978,9 +1010,11 @@ kexp      :  kexpL
        |  kexpLno
        ;
 
+/* kexpL = a let expression */
 kexpL  :  letdecls IN exp                      { $$ = mklet($1,$3); }
        ;
 
+/* kexpLno = any other expression more tightly binding than operator application */
 kexpLno        :  LAMBDA
                { hsincindent();        /* push new context for FN = NULL;        */
                  FN = NULL;            /* not actually concerned about indenting */
@@ -1036,7 +1070,6 @@ aexp      :  qvar                                 { $$ = mkident($1); }
        |  gcon                                 { $$ = mkident($1); }
        |  lit_constant                         { $$ = mklit($1); }
        |  OPAREN exp CPAREN                    { $$ = mkpar($2); }         /* mkpar: stop infix parsing at ()'s */
-       |  qcon OCURLY CCURLY                   { $$ = mkrecord($1,Lnil); }
        |  qcon OCURLY rbinds CCURLY            { $$ = mkrecord($1,$3); }   /* 1 S/R conflict on OCURLY -> shift */
        |  OBRACK list_exps CBRACK              { $$ = mkllist($2); }
        |  OPAREN exp COMMA texps CPAREN        { if (ttree($4) == tuple)
@@ -1045,7 +1078,7 @@ aexp      :  qvar                                 { $$ = mkident($1); }
                                                     $$ = mktuple(ldub($2, $4)); }
 
        /* only in expressions ... */
-       |  aexp OCURLY rbinds CCURLY            { $$ = mkrupdate($1,$3); }
+       |  aexp OCURLY rbinds1 CCURLY           { $$ = mkrupdate($1,$3); }
        |  OBRACK exp VBAR quals CBRACK         { $$ = mkcomprh($2,$4); }
        |  OBRACK exp COMMA exp DOTDOT exp CBRACK {$$= mkeenum($2,mkjust($4),mkjust($6)); }
        |  OBRACK exp COMMA exp DOTDOT CBRACK   { $$ = mkeenum($2,mkjust($4),mknothing()); }
@@ -1073,8 +1106,12 @@ dorest  :  ocurly stmts ccurly                   { checkdostmts($2); $$ = $2; }
        |  vocurly stmts vccurly                { checkdostmts($2); $$ = $2; }
        ;
 
-rbinds :  rbind                                { $$ = lsing($1); }
-       |  rbinds COMMA rbind                   { $$ = lapp($1,$3); }
+rbinds :  /* empty */                          { $$ = Lnil; }
+       |  rbinds1
+       ;
+
+rbinds1        :  rbind                                { $$ = lsing($1); }
+       |  rbinds1 COMMA rbind                  { $$ = lapp($1,$3); }
        ;
 
 rbind          :  qvar                                 { $$ = mkrbind($1,mknothing()); }
@@ -1093,10 +1130,22 @@ texps   :  exp  { $$ = mkpar($1); }     /* mkpar: so we don't flatten last element in t
        /* right recursion? WDP */
        ;
 
-
 list_exps :
           exp                                  { $$ = lsing($1); }
+       |  exp COMMA exp                        { $$ = mklcons( $1, lsing($3) ); }
+       |  exp COMMA exp COMMA list_rest        { $$ = mklcons( $1, mklcons( $3, reverse_list( $5 ))); }
+       ;
+
+/* Use left recusion for list_rest, because we sometimes get programs with
+   very long explicit lists. */
+list_rest :    exp                             { $$ = lsing($1); }
+         | list_rest COMMA exp                 { $$ = mklcons( $3, $1 ); }
+         ;
+
+/* 
+          exp                                  { $$ = lsing($1); }
        |  exp COMMA list_exps          { $$ = mklcons($1, $3); }
+*/
        /* right recursion? (WDP)
 
           It has to be this way, though, otherwise you
@@ -1108,7 +1157,6 @@ list_exps :
           (In fact, if you change the grammar and throw yacc/bison
           at it, it *will* do the wrong thing [WDP 94/06])
        */
-       ;
 
 letdecls:  LET ocurly decls ccurly             { $$ = $3 }
        |  LET vocurly decls vccurly            { $$ = $3 }
@@ -1120,7 +1168,8 @@ quals     :  qual                                 { $$ = lsing($1); }
 
 qual   :  letdecls                             { $$ = mkseqlet($1); }
        |  expL                                 { $$ = $1; }
-       |  {inpat=TRUE;} expLno {inpat=FALSE;}leftexp
+       |  {inpat=TRUE;} expLno 
+          {inpat=FALSE;} leftexp
                { if ($4 == NULL) {
                      expORpat(LEGIT_EXPR,$2);
                      $$ = mkguard($2);
@@ -1153,7 +1202,7 @@ stmts     :  stmt                                 { $$ = $1; }
 
 stmt   :  /* empty */                          { $$ = Lnil; }
        |  letdecls                             { $$ = lsing(mkseqlet($1)); }
-       |  expL                                 { $$ = lsing($1); }
+       |  expL                                 { $$ = lsing(mkdoexp($1,hsplineno)); }
        |  {inpat=TRUE;} expLno {inpat=FALSE;} leftexp
                { if ($4 == NULL) {
                      expORpat(LEGIT_EXPR,$2);
@@ -1177,13 +1226,17 @@ leftexp :  LARROW exp                           { $$ = $2; }
 *                                                                     *
 **********************************************************************/
 
-pat    :  pat qconop bpat                      { $$ = mkinfixap($2,$1,$3); }
+pat    :  qvar PLUS INTEGER                    { $$ = mkplusp($1, mkinteger($3)); }
+       |  cpat
+       ;
+
+cpat   :  cpat qconop bpat                     { $$ = mkinfixap($2,$1,$3); }
        |  bpat
        ;
 
 bpat   :  apatc
        |  conpat
-       |  qcon OCURLY rpats CCURLY             { $$ = mkrecord($1,$3); }
+       |  qcon OCURLY rpats CCURLY             { $$ = mkrecord($1,$3); }
        |  MINUS INTEGER                        { $$ = mknegate(mklit(mkinteger($2))); }
        |  MINUS FLOAT                          { $$ = mknegate(mklit(mkfloatr($2))); }
        ;
@@ -1230,8 +1283,12 @@ pats     :  pat COMMA pats                       { $$ = mklcons($1, $3); }
        /* right recursion? (WDP) */
        ;
 
-rpats  : rpat                                  { $$ = lsing($1); }
-       | rpats COMMA rpat                      { $$ = lapp($1,$3); }
+rpats  : /* empty */                           { $$ = Lnil; }
+       | rpats1
+       ;
+
+rpats1 : rpat                                  { $$ = lsing($1); }
+       | rpats1 COMMA rpat                     { $$ = lapp($1,$3); }
        ;
 
 rpat   :  qvar                                 { $$ = mkrbind($1,mknothing()); }
@@ -1285,7 +1342,8 @@ gconk     :  qconk
 *                                                                     *
 **********************************************************************/
 
-importkey:  IMPORT     { setstartlineno(); }
+importkey: IMPORT               { setstartlineno(); $$ = 0; }
+        |  IMPORT SOURCE_UPRAGMA { setstartlineno(); $$ = 1; }
        ;
 
 datakey        :   DATA        { setstartlineno();
@@ -1450,6 +1508,12 @@ varsym   :  varsym_nominus
        |  MINUS                        { $$ = install_literal("-"); }
        ;
 
+/* PLUS, BANG are valid varsyms */
+varsym_nominus : VARSYM
+       |  PLUS                         { $$ = install_literal("+"); }
+       |  BANG                         { $$ = install_literal("!"); }  
+       ;
+
 /* AS HIDING QUALIFIED are valid varids */
 varid   :  VARID
        |  AS                           { $$ = install_literal("as"); }
@@ -1457,10 +1521,6 @@ varid   :  VARID
        |  QUALIFIED                    { $$ = install_literal("qualified"); }
        ;
 
-/* BANG are valid varsyms */
-varsym_nominus : VARSYM
-       |  BANG                         { $$ = install_literal("!"); }  
-       ;
 
 ccallid        :  VARID
        |  CONID
@@ -1577,7 +1637,7 @@ yyerror(s)
        /*NOTHING*/;
 
     } else {
-       fprintf(stderr, "\"%s\", line %d, column %d: %s on input: ",
+       fprintf(stderr, "%s:%d:%d: %s on input: ",
          input_filename, hsplineno, hspcolno + 1, s);
 
        if (yyleng == 1 && *yytext == '\0')