[project @ 2000-10-24 08:40:09 by simonpj]
[ghc-hetmet.git] / ghc / compiler / parser / Parser.y
index 122ab9a..d82fe3f 100644 (file)
@@ -1,6 +1,6 @@
 {-
 -----------------------------------------------------------------------------
-$Id: Parser.y,v 1.36 2000/09/22 15:56:13 simonpj Exp $
+$Id: Parser.y,v 1.43 2000/10/24 08:40:10 simonpj Exp $
 
 Haskell grammar.
 
@@ -14,6 +14,7 @@ module Parser ( parse ) where
 import HsSyn
 import HsPragmas
 import HsTypes         ( mkHsTupCon )
+import HsPat            ( InPat(..) )
 
 import RdrHsSyn
 import Lex
@@ -30,6 +31,7 @@ import Panic
 
 import GlaExts
 import FastString      ( tailFS )
+import Outputable
 
 #include "HsVersions.h"
 }
@@ -158,6 +160,8 @@ Conflicts: 14 shift/reduce
 
  '{'           { ITocurly }                    -- special symbols
  '}'           { ITccurly }
+ '{|'           { ITocurlybar }
+ '|}'           { ITccurlybar }
  vccurly       { ITvccurly } -- virtual close curly (from layout)
  '['           { ITobrack }
  ']'           { ITcbrack }
@@ -328,14 +332,12 @@ topdecl :: { RdrBinding }
        | srcloc 'data' ctype '=' constrs deriving
                {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
                   returnP (RdrHsDecl (TyClD
-                     (TyData DataType cs c ts (reverse $5) (length $5) $6
-                       NoDataPragmas $1))) }
+                     (mkTyData DataType cs c ts (reverse $5) (length $5) $6 $1))) }
 
        | srcloc 'newtype' ctype '=' newconstr deriving
                {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
                   returnP (RdrHsDecl (TyClD
-                     (TyData NewType cs c ts [$5] 1 $6
-                       NoDataPragmas $1))) }
+                     (mkTyData NewType cs c ts [$5] 1 $6 $1))) }
 
        | srcloc 'class' ctype fds where
                {% checkDataHeader $3 `thenP` \(cs,c,ts) ->
@@ -343,8 +345,7 @@ topdecl :: { RdrBinding }
                        (binds,sigs) = cvMonoBindsAndSigs cvClassOpSig (groupBindings $5) 
                   in
                   returnP (RdrHsDecl (TyClD
-                     (mkClassDecl cs c ts $4 sigs binds 
-                       NoClassPragmas $1))) }
+                     (mkClassDecl cs c ts $4 sigs binds $1))) }
 
        | srcloc 'instance' inst_type where
                { let (binds,sigs) 
@@ -447,7 +448,7 @@ deprecations :: { RdrBinding }
 
 -- SUP: TEMPORARY HACK, not checking for `module Foo'
 deprecation :: { RdrBinding }
-       : srcloc exportlist STRING
+       : srcloc depreclist STRING
                { foldr RdrAndBindings RdrNullBind 
                        [ RdrHsDecl (DeprecD (Deprecation n $3 $1)) | n <- $2 ] }
 
@@ -486,7 +487,7 @@ sigtypes :: { [RdrNameHsType] }
        | sigtypes ',' sigtype          { $3 : $1 }
 
 sigtype :: { RdrNameHsType }
-       : ctype                         { mkHsForAllTy Nothing [] $1 }
+       : ctype                         { (mkHsForAllTy Nothing [] $1) }
 
 sig_vars :: { [RdrName] }
         : sig_vars ',' var             { $3 : $1 }
@@ -499,16 +500,21 @@ sig_vars :: { [RdrName] }
 ctype  :: { RdrNameHsType }
        : 'forall' tyvars '.' ctype     { mkHsForAllTy (Just $2) [] $4 }
        | context type                  { mkHsForAllTy Nothing   $1 $2 }
-               -- A type of form (context => type) is an *implicit* HsForAllTy
+       -- A type of form (context => type) is an *implicit* HsForAllTy
        | type                          { $1 }
 
 type :: { RdrNameHsType }
-       : btype '->' type               { HsFunTy $1 $3 }
+       : gentype '->' type             { HsFunTy $1 $3 }
        | ipvar '::' type               { mkHsIParamTy $1 $3 }
-       | btype                         { $1 }
+       | gentype                       { $1 }
+
+gentype :: { RdrNameHsType }
+        : btype                         { $1 }
+-- Generics
+        | atype tyconop atype           { HsOpTy $1 $2 $3 }
 
 btype :: { RdrNameHsType }
-       : btype atype                   { HsAppTy $1 $2 }
+       : btype atype                   { (HsAppTy $1 $2) }
        | atype                         { $1 }
 
 atype :: { RdrNameHsType }
@@ -517,7 +523,9 @@ atype :: { RdrNameHsType }
        | '(' type ',' types ')'        { HsTupleTy (mkHsTupCon tcName Boxed  ($2:$4)) ($2 : reverse $4) }
        | '(#' types '#)'               { HsTupleTy (mkHsTupCon tcName Unboxed     $2) (reverse $2)      }
        | '[' type ']'                  { HsListTy $2 }
-       | '(' ctype ')'                 { $2 }
+       | '(' ctype ')'                 { $2 }
+-- Generics
+        | INTEGER                       { HsNumTy $1 }
 
 -- An inst_type is what occurs in the head of an instance decl
 --     e.g.  (Foo a, Gaz b) => Wibble a b
@@ -559,6 +567,11 @@ varids0    :: { [RdrName] }
 -----------------------------------------------------------------------------
 -- Datatype declarations
 
+newconstr :: { RdrNameConDecl }
+       : srcloc conid atype    { mkConDecl $2 [] [] (VanillaCon [Unbanged $3]) $1 }
+       | srcloc conid '{' var '::' type '}'
+                               { mkConDecl $2 [] [] (RecCon [([$4], Unbanged $6)]) $1 }
+
 constrs :: { [RdrNameConDecl] }
        : constrs '|' constr            { $3 : $1 }
        | constr                        { [$1] }
@@ -577,34 +590,22 @@ context :: { RdrNameContext }
        : btype '=>'                    {% checkContext $1 }
 
 constr_stuff :: { (RdrName, RdrNameConDetails) }
-       : scontype                      { (fst $1, VanillaCon (snd $1)) }
+       : btype                         {% mkVanillaCon $1 []               }
+       | btype '!' atype satypes       {% mkVanillaCon $1 (Banged $3 : $4) }
+       | gtycon '{' fielddecls '}'     {% mkRecCon $1 $3 }
        | sbtype conop sbtype           { ($2, InfixCon $1 $3) }
-       | con '{' fielddecls '}'        { ($1, RecCon (reverse $3)) }
-
-newconstr :: { RdrNameConDecl }
-       : srcloc conid atype    { mkConDecl $2 [] [] (VanillaCon [Unbanged $3]) $1 }
-       | srcloc conid '{' var '::' type '}'
-                               { mkConDecl $2 [] [] (RecCon [([$4], Unbanged $6)]) $1 }
-
-scontype :: { (RdrName, [RdrNameBangType]) }
-       : btype                         {% splitForConApp $1 [] }
-       | scontype1                     { $1 }
-
-scontype1 :: { (RdrName, [RdrNameBangType]) }
-       : btype '!' atype               {% splitForConApp $1 [Banged $3] }
-       | scontype1 satype              { (fst $1, snd $1 ++ [$2] ) }
-        | '(' consym ')'               { ($2,[]) }
 
-satype :: { RdrNameBangType }
-       : atype                         { Unbanged $1 }
-       | '!' atype                     { Banged   $2 }
+satypes        :: { [RdrNameBangType] }
+       : atype satypes                 { Unbanged $1 : $2 }
+       | '!' atype satypes             { Banged   $2 : $3 }
+       | {- empty -}                   { [] }
 
 sbtype :: { RdrNameBangType }
        : btype                         { Unbanged $1 }
        | '!' atype                     { Banged   $2 }
 
 fielddecls :: { [([RdrName],RdrNameBangType)] }
-       : fielddecls ',' fielddecl      { $3 : $1 }
+       : fielddecl ',' fielddecls      { $1 : $3 }
        | fielddecl                     { [$1] }
 
 fielddecl :: { ([RdrName],RdrNameBangType) }
@@ -648,15 +649,16 @@ dclasses :: { [RdrName] }
 -}
 
 valdef :: { RdrBinding }
-       : infixexp srcloc opt_sig rhs           {% checkValDef $1 $3 $4 $2 }
-       | infixexp srcloc '::' sigtype          {% checkValSig $1 $4 $2 }
+       : infixexp srcloc opt_sig rhs           {% (checkValDef $1 $3 $4 $2) }
+       | infixexp srcloc '::' sigtype          {% (checkValSig $1 $4 $2) }
        | var ',' sig_vars srcloc '::' sigtype  { foldr1 RdrAndBindings 
                                                         [ RdrSig (Sig n $6 $4) | n <- $1:$3 ]
-                                               }
+                                                }
+
 
 rhs    :: { RdrNameGRHSs }
-       : '=' srcloc exp wherebinds     { GRHSs (unguardedRHS $3 $2) 
-                                                               $4 Nothing}
+       : '=' srcloc exp wherebinds     { (GRHSs (unguardedRHS $3 $2) 
+                                                               $4 Nothing)}
        | gdrhs wherebinds              { GRHSs (reverse $1) $2 Nothing }
 
 gdrhs :: { [RdrNameGRHS] }
@@ -670,13 +672,14 @@ gdrh :: { RdrNameGRHS }
 -- Expressions
 
 exp   :: { RdrNameHsExpr }
-       : infixexp '::' sigtype         { ExprWithTySig $1 $3 }
+       : infixexp '::' sigtype         { (ExprWithTySig $1 $3) }
        | infixexp 'with' dbinding      { HsWith $1 $3 }
        | infixexp                      { $1 }
 
 infixexp :: { RdrNameHsExpr }
        : exp10                         { $1 }
-       | infixexp qop exp10            { OpApp $1 $2 (panic "fixity") $3 }
+       | infixexp qop exp10            { (OpApp $1 (HsVar $2) 
+                                               (panic "fixity") $3 )}
 
 exp10 :: { RdrNameHsExpr }
        : '\\' aexp aexps opt_asig '->' srcloc exp      
@@ -706,24 +709,29 @@ ccallid :: { FAST_STRING }
        |  CONID                                { $1 }
 
 fexp   :: { RdrNameHsExpr }
-       : fexp aexp                             { HsApp $1 $2 }
+       : fexp aexp                             { (HsApp $1 $2) }
        | aexp                                  { $1 }
 
 aexps0         :: { [RdrNameHsExpr] }
-       : aexps                                 { reverse $1 }
+       : aexps                                 { (reverse $1) }
 
 aexps  :: { [RdrNameHsExpr] }
        : aexps aexp                            { $2 : $1 }
        | {- empty -}                           { [] }
 
 aexp   :: { RdrNameHsExpr }
-       : aexp '{' fbinds '}'           {% mkRecConstrOrUpdate $1 (reverse $3) }
-       | aexp1                         { $1 }
+        : var_or_con '{|' gentype '|}'          { (HsApp $1 (HsType $3)) }
+       | aexp '{' fbinds '}'                   {% (mkRecConstrOrUpdate $1 
+                                                       (reverse $3)) }
+       | aexp1                                 { $1 }
+
+var_or_con :: { RdrNameHsExpr }
+        : qvar                          { HsVar $1 }
+        | gcon                          { HsVar $1 }
 
 aexp1  :: { RdrNameHsExpr }
-       : qvar                          { HsVar $1 }
-       | ipvar                         { HsIPVar $1 }
-       | gcon                          { HsVar $1 }
+       : ipvar                         { HsIPVar $1 }
+       | var_or_con                    { $1 }
        | literal                       { HsLit $1 }
        | INTEGER                       { HsOverLit (mkHsIntegralLit $1) }
        | RATIONAL                      { HsOverLit (mkHsFractionalLit $1) }
@@ -731,8 +739,8 @@ aexp1       :: { RdrNameHsExpr }
        | '(' exp ',' texps ')'         { ExplicitTuple ($2 : reverse $4) Boxed}
        | '(#' texps '#)'               { ExplicitTuple (reverse $2)      Unboxed }
        | '[' list ']'                  { $2 }
-       | '(' infixexp qop ')'          { SectionL $2 $3  }
-       | '(' qopm infixexp ')'         { SectionR $2 $3 }
+       | '(' infixexp qop ')'          { (SectionL $2 (HsVar $3))  }
+       | '(' qopm infixexp ')'         { (SectionR $2 $3) }
        | qvar '@' aexp                 { EAsPat $1 $3 }
        | '_'                           { EWildPat }
        | '~' aexp1                     { ELazyPat $2 }
@@ -741,6 +749,7 @@ texps :: { [RdrNameHsExpr] }
        : texps ',' exp                 { $3 : $1 }
        | exp                           { [$1] }
 
+
 -----------------------------------------------------------------------------
 -- List expressions
 
@@ -792,9 +801,9 @@ alts1       :: { [RdrNameMatch] }
 
 alt    :: { RdrNameMatch }
        : infixexp opt_sig ralt wherebinds
-                                       {% checkPattern $1 `thenP` \p ->
+                                       {% (checkPattern $1 `thenP` \p ->
                                           returnP (Match [] [p] $2
-                                                    (GRHSs $3 $4 Nothing)) }
+                                                    (GRHSs $3 $4 Nothing))  )}
 
 ralt :: { [RdrNameGRHS] }
        : '->' srcloc exp               { [GRHS [ExprStmt $3 $2] $2] }
@@ -864,8 +873,17 @@ dbind      : ipvar '=' exp                 { ($1, $3) }
 -----------------------------------------------------------------------------
 -- Variables, Constructors and Operators.
 
+depreclist :: { [RdrName] }
+depreclist : deprec_var                        { [$1] }
+          | deprec_var ',' depreclist  { $1 : $2 }
+
+deprec_var :: { RdrName }
+deprec_var : var                       { $1 }
+          | tycon                      { $1 }
+
 gtycon         :: { RdrName }
        : qtycon                        { $1 }
+       | '(' qtyconop ')'              { $2 }
        | '(' ')'                       { unitTyCon_RDR }
        | '(' '->' ')'                  { funTyCon_RDR }
        | '[' ']'                       { listTyCon_RDR }
@@ -890,11 +908,7 @@ qvar       :: { RdrName }
 -- *after* we see the close paren.
 
 ipvar  :: { RdrName }
-       : IPVARID               { (mkSrcUnqual ipName (tailFS $1)) }
-
-con    :: { RdrName }
-       : conid                 { $1 }
-       | '(' consym ')'        { $2 }
+       : IPVARID               { (mkUnqual ipName (tailFS $1)) }
 
 qcon   :: { RdrName }
        : qconid                { $1 }
@@ -927,9 +941,9 @@ op  :: { RdrName }   -- used in infix decls
        : varop                 { $1 }
        | conop                 { $1 }
 
-qop    :: { RdrNameHsExpr }   -- used in sections
-       : qvarop                { HsVar $1 }
-       | qconop                { HsVar $1 }
+qop    :: { RdrName {-HsExpr-} }   -- used in sections
+       : qvarop                { $1 }
+       | qconop                { $1 }
 
 qopm   :: { RdrNameHsExpr }   -- used in sections
        : qvaropm               { HsVar $1 }
@@ -940,21 +954,21 @@ qopm      :: { RdrNameHsExpr }   -- used in sections
 
 qvarid :: { RdrName }
        : varid                 { $1 }
-       | QVARID                { mkSrcQual varName $1 }
+       | QVARID                { mkQual varName $1 }
 
 varid :: { RdrName }
        : varid_no_unsafe       { $1 }
-       | 'unsafe'              { mkSrcUnqual varName SLIT("unsafe") }
+       | 'unsafe'              { mkUnqual varName SLIT("unsafe") }
 
 varid_no_unsafe :: { RdrName }
-       : VARID                 { mkSrcUnqual varName $1 }
-       | special_id            { mkSrcUnqual varName $1 }
-       | 'forall'              { mkSrcUnqual varName SLIT("forall") }
+       : VARID                 { mkUnqual varName $1 }
+       | special_id            { mkUnqual varName $1 }
+       | 'forall'              { mkUnqual varName SLIT("forall") }
 
 tyvar  :: { RdrName }
-       : VARID                 { mkSrcUnqual tvName $1 }
-       | special_id            { mkSrcUnqual tvName $1 }
-       | 'unsafe'              { mkSrcUnqual tvName SLIT("unsafe") }
+       : VARID                 { mkUnqual tvName $1 }
+       | special_id            { mkUnqual tvName $1 }
+       | 'unsafe'              { mkUnqual tvName SLIT("unsafe") }
 
 -- These special_ids are treated as keywords in various places, 
 -- but as ordinary ids elsewhere.   A special_id collects all thsee
@@ -975,20 +989,20 @@ special_id
 
 qconid :: { RdrName }
        : conid                 { $1 }
-       | QCONID                { mkSrcQual dataName $1 }
+       | QCONID                { mkQual dataName $1 }
 
 conid  :: { RdrName }
-       : CONID                 { mkSrcUnqual dataName $1 }
+       : CONID                 { mkUnqual dataName $1 }
 
 -----------------------------------------------------------------------------
 -- ConSyms
 
 qconsym :: { RdrName }
        : consym                { $1 }
-       | QCONSYM               { mkSrcQual dataName $1 }
+       | QCONSYM               { mkQual dataName $1 }
 
 consym :: { RdrName }
-       : CONSYM                { mkSrcUnqual dataName $1 }
+       : CONSYM                { mkUnqual dataName $1 }
 
 -----------------------------------------------------------------------------
 -- VarSyms
@@ -1002,15 +1016,15 @@ qvarsym_no_minus :: { RdrName }
        | qvarsym1              { $1 }
 
 qvarsym1 :: { RdrName }
-qvarsym1 : QVARSYM             { mkSrcQual varName $1 }
+qvarsym1 : QVARSYM             { mkQual varName $1 }
 
 varsym :: { RdrName }
        : varsym_no_minus       { $1 }
-       | '-'                   { mkSrcUnqual varName SLIT("-") }
+       | '-'                   { mkUnqual varName SLIT("-") }
 
 varsym_no_minus :: { RdrName } -- varsym not including '-'
-       : VARSYM                { mkSrcUnqual varName $1 }
-       | special_sym           { mkSrcUnqual varName $1 }
+       : VARSYM                { mkUnqual varName $1 }
+       | special_sym           { mkUnqual varName $1 }
 
 
 -- See comments with special_id
@@ -1050,11 +1064,18 @@ modid   :: { ModuleName }
        : CONID                 { mkSrcModuleFS $1 }
 
 tycon  :: { RdrName }
-       : CONID                 { mkSrcUnqual tcClsName $1 }
+       : CONID                 { mkUnqual tcClsName $1 }
+
+tyconop        :: { RdrName }
+       : CONSYM                { mkUnqual tcClsName $1 }
 
 qtycon :: { RdrName }
        : tycon                 { $1 }
-       | QCONID                { mkSrcQual tcClsName $1 }
+       | QCONID                { mkQual tcClsName $1 }
+
+qtyconop :: { RdrName }
+         : tyconop             { $1 }
+         | QCONSYM             { mkQual tcClsName $1 }
 
 qtycls         :: { RdrName }
        : qtycon                { $1 }