[project @ 2003-11-06 17:09:50 by simonpj]
[ghc-hetmet.git] / ghc / compiler / parser / Parser.y
index 7976b1b..68cc7ea 100644 (file)
@@ -1,6 +1,6 @@
 {-                                                             -*-haskell-*-
 -----------------------------------------------------------------------------
-$Id: Parser.y,v 1.125 2003/09/24 13:04:51 simonmar Exp $
+$Id: Parser.y,v 1.129 2003/11/06 17:09:53 simonpj Exp $
 
 Haskell grammar.
 
@@ -14,29 +14,24 @@ module Parser ( parseModule, parseStmt, parseIdentifier, parseIface ) where
 #include "HsVersions.h"
 
 import HsSyn
-import HsTypes         ( mkHsTupCon )
-
 import RdrHsSyn
-import HscTypes                ( ParsedIface(..), IsBootInterface, noDependencies )
+import HscTypes                ( ModIface, IsBootInterface, DeprecTxt )
 import Lexer
 import RdrName
-import PrelNames       ( mAIN_Name, funTyConName, listTyConName, 
-                         parrTyConName, consDataConName )
-import TysWiredIn      ( unitTyCon, unitDataCon, tupleTyCon, 
-                         tupleCon, nilDataCon )
+import TysWiredIn      ( unitTyCon, unitDataCon, tupleTyCon, tupleCon, nilDataCon,
+                         listTyCon_RDR, parrTyCon_RDR, consDataCon_RDR )
+import Type            ( funTyCon )
 import ForeignCall     ( Safety(..), CExportSpec(..), 
-                         CCallConv(..), CCallTarget(..), defaultCCallConv,
+                         CCallConv(..), CCallTarget(..), defaultCCallConv
                        )
-import OccName         ( UserFS, varName, tcName, dataName, tcClsName, tvName )
-import TyCon           ( DataConDetails(..) )
+import OccName         ( UserFS, varName, dataName, tcClsName, tvName )
 import DataCon         ( DataCon, dataConName )
-import SrcLoc          ( SrcLoc )
+import SrcLoc          ( SrcLoc, noSrcLoc )
 import Module
-import CmdLineOpts     ( opt_SccProfilingOn, opt_InPackage )
+import CmdLineOpts     ( opt_SccProfilingOn )
 import Type            ( Kind, mkArrowKind, liftedTypeKind )
-import BasicTypes      ( Boxity(..), Fixity(..), FixityDirection(..), 
-                         IPName(..), NewOrData(..), StrictnessMark(..),
-                         Activation(..), FixitySig(..) )
+import BasicTypes      ( Boxity(..), Fixity(..), FixityDirection(..), IPName(..),
+                         NewOrData(..), Activation(..) )
 import Panic
 
 import GLAEXTS
@@ -213,11 +208,10 @@ Conflicts: 29 shift/reduce, [SDM 19/9/2002]
 '[t|'           { T _ _ ITopenTypQuote  }      
 '[d|'           { T _ _ ITopenDecQuote  }      
 '|]'            { T _ _ ITcloseQuote    }
-ID_SPLICE       { T _ _ (ITidEscape $$) }     -- $x
+TH_ID_SPLICE    { T _ _ (ITidEscape $$) }     -- $x
 '$('           { T _ _ ITparenEscape   }     -- $( exp )
-REIFY_TYPE     { T _ _ ITreifyType }   
-REIFY_DECL     { T _ _ ITreifyDecl }   
-REIFY_FIXITY   { T _ _ ITreifyFixity }
+TH_VAR_QUOTE   { T _ _ ITvarQuote      }     -- 'x
+TH_TY_QUOTE    { T _ _ ITtyQuote      }      -- ''T
 
 %monad { P } { >>= } { return }
 %lexer { lexer } { T _ _ ITeof }
@@ -266,37 +260,32 @@ cvtopdecls :: { [RdrNameHsDecl] }
 -----------------------------------------------------------------------------
 -- Interfaces (.hi-boot files)
 
-iface   :: { ParsedIface }
-       : 'module' modid 'where' ifacebody
-         {         ParsedIface {
-                       pi_mod     = $2,
-                       pi_pkg     = opt_InPackage,
-                       pi_vers    = 1,                 -- Module version
-                       pi_orphan  = False,
-                       pi_exports = (1,[($2,mkIfaceExports $4)]),
-                       pi_deps    = noDependencies,
-                       pi_usages  = [],
-                       pi_fixity  = [],
-                       pi_insts   = [],
-                       pi_decls   = map (\x -> (1,x)) $4,
-                       pi_rules   = (1,[]),
-                       pi_deprecs = Nothing
-                   }
-          }
-
-ifacebody :: { [RdrNameTyClDecl] }
+iface   :: { ModIface }
+       : 'module' modid 'where' ifacebody  { mkBootIface $2 $4 }
+
+ifacebody :: { [HsDecl RdrName] }
        :  '{'            ifacedecls '}'                { $2 }
        |      vocurly    ifacedecls close              { $2 }
 
-ifacedecls :: { [RdrNameTyClDecl] }
+ifacedecls :: { [HsDecl RdrName] }
        : ifacedecl ';' ifacedecls      { $1 : $3 }
        | ';' ifacedecls                { $2 }
        | ifacedecl                     { [$1] }
        | {- empty -}                   { [] }
 
-ifacedecl :: { RdrNameTyClDecl }
-       : tycl_decl                     { $1 }
-       | srcloc var '::' sigtype       { IfaceSig $2 $4 [] $1 }
+ifacedecl :: { HsDecl RdrName }
+       : var '::' sigtype      
+                { SigD (Sig $1 $3 noSrcLoc) }
+       | 'type' syn_hdr '=' ctype      
+               { let (tc,tvs) = $2 in TyClD (TySynonym tc tvs $4 noSrcLoc) }
+       | new_or_data tycl_hdr
+               { TyClD (mkTyData $1 $2 [] Nothing noSrcLoc) }
+       | 'class' tycl_hdr fds
+               { TyClD (mkClassDecl $2 $3 [] EmptyMonoBinds noSrcLoc) }
+
+new_or_data :: { NewOrData }
+       : 'data'        { DataType }
+       | 'newtype'     { NewType }
 
 -----------------------------------------------------------------------------
 -- The Export List
@@ -393,7 +382,7 @@ topdecl :: { RdrBinding }
        : tycl_decl                     { RdrHsDecl (TyClD $1) }
        | srcloc 'instance' inst_type where
                { let (binds,sigs) = cvMonoBindsAndSigs $4
-                 in RdrHsDecl (InstD (InstDecl $3 binds sigs Nothing $1)) }
+                 in RdrHsDecl (InstD (InstDecl $3 binds sigs $1)) }
        | srcloc 'default' '(' comma_types0 ')'         { RdrHsDecl (DefD (DefaultDecl $4 $1)) }
        | 'foreign' fdecl                               { RdrHsDecl $2 }
        | '{-# DEPRECATED' deprecations '#-}'           { RdrBindings (reverse $2) }
@@ -409,18 +398,17 @@ tycl_decl :: { RdrNameTyClDecl }
                -- Instead we just say b is out of scope
                { let (tc,tvs) = $3 in TySynonym tc tvs $5 $1 }
 
-
        | srcloc 'data' tycl_hdr constrs deriving
-               { mkTyData DataType $3 (DataCons (reverse $4)) $5 $1 }
+               { mkTyData DataType $3 (reverse $4) $5 $1 }
 
        | srcloc 'newtype' tycl_hdr '=' newconstr deriving
-               { mkTyData NewType $3 (DataCons [$5]) $6 $1 }
+               { mkTyData NewType $3 [$5] $6 $1 }
 
        | srcloc 'class' tycl_hdr fds where
                { let 
                        (binds,sigs) = cvMonoBindsAndSigs $5 
                  in
-                 mkClassDecl $3 $4 sigs (Just binds) $1 }
+                 mkClassDecl $3 $4 sigs binds $1 }
 
 syn_hdr :: { (RdrName, [RdrNameHsTyVar]) }     -- We don't retain the syntax of an infix
                                                -- type synonym declaration. Oh well.
@@ -434,10 +422,8 @@ syn_hdr :: { (RdrName, [RdrNameHsTyVar]) } -- We don't retain the syntax of an i
 --     (Eq a, Ord b) => T a b
 -- Rather a lot of inlining here, else we get reduce/reduce errors
 tycl_hdr :: { (RdrNameContext, RdrName, [RdrNameHsTyVar]) }
-       : context '=>' type             {% checkTyClHdr $3      >>= \ (tc,tvs) ->
-                                          return ($1, tc, tvs) }
-       | type                          {% checkTyClHdr $1      >>= \ (tc,tvs) ->
-                                          return ([], tc, tvs) }
+       : context '=>' type             {% checkTyClHdr $1 $3 }
+       | type                          {% checkTyClHdr [] $1 }
 
 -----------------------------------------------------------------------------
 -- Nested declarations
@@ -686,7 +672,8 @@ sigtypes :: { [RdrNameHsType] }
        | sigtypes ',' sigtype          { $3 : $1 }
 
 sigtype :: { RdrNameHsType }
-       : ctype                         { mkHsForAllTy Nothing [] $1 }
+       : ctype                         { mkImplicitHsForAllTy [] $1 }
+       -- Wrap an Implicit forall if there isn't one there already
 
 sig_vars :: { [RdrName] }
         : sig_vars ',' var             { $3 : $1 }
@@ -697,8 +684,8 @@ sig_vars :: { [RdrName] }
 
 -- A ctype is a for-all type
 ctype  :: { RdrNameHsType }
-       : 'forall' tv_bndrs '.' ctype   { mkHsForAllTy (Just $2) [] $4 }
-       | context '=>' type             { mkHsForAllTy Nothing   $1 $3 }
+       : 'forall' tv_bndrs '.' ctype   { mkExplicitHsForAllTy $2 [] $4 }
+       | context '=>' type             { mkImplicitHsForAllTy   $1 $3 }
        -- A type of form (context => type) is an *implicit* HsForAllTy
        | type                          { $1 }
 
@@ -715,9 +702,9 @@ type :: { RdrNameHsType }
 
 gentype :: { RdrNameHsType }
         : btype                         { $1 }
-        | btype qtyconop gentype        { HsOpTy $1 (HsTyOp $2) $3 }
-        | btype  '`' tyvar '`' gentype  { HsOpTy $1 (HsTyOp $3) $5 }
-       | btype '->' gentype            { HsOpTy $1 HsArrow $3 }
+        | btype qtyconop gentype        { HsOpTy $1 $2 $3 }
+        | btype  '`' tyvar '`' gentype  { HsOpTy $1 $3 $5 }
+       | btype '->' gentype            { HsFunTy $1 $3 }
 
 btype :: { RdrNameHsType }
        : btype atype                   { HsAppTy $1 $2 }
@@ -726,8 +713,8 @@ btype :: { RdrNameHsType }
 atype :: { RdrNameHsType }
        : gtycon                        { HsTyVar $1 }
        | tyvar                         { HsTyVar $1 }
-       | '(' type ',' comma_types1 ')' { HsTupleTy (mkHsTupCon tcName Boxed  ($2:$4)) ($2:$4) }
-       | '(#' comma_types1 '#)'        { HsTupleTy (mkHsTupCon tcName Unboxed     $2) $2      }
+       | '(' type ',' comma_types1 ')' { HsTupleTy Boxed  ($2:$4) }
+       | '(#' comma_types1 '#)'        { HsTupleTy Unboxed $2     }
        | '[' type ']'                  { HsListTy  $2 }
        | '[:' type ':]'                { HsPArrTy  $2 }
        | '(' ctype ')'                 { HsParTy   $2 }
@@ -756,7 +743,7 @@ tv_bndrs :: { [RdrNameHsTyVar] }
 
 tv_bndr :: { RdrNameHsTyVar }
        : tyvar                         { UserTyVar $1 }
-       | '(' tyvar '::' kind ')'       { IfaceTyVar $2 $4 }
+       | '(' tyvar '::' kind ')'       { KindedTyVar $2 $4 }
 
 fds :: { [([RdrName], [RdrName])] }
        : {- empty -}                   { [] }
@@ -838,9 +825,9 @@ stype :: { RdrNameBangType }
        : ctype                         { unbangedType $1 }
        | strict_mark atype             { BangType $1 $2 }
 
-strict_mark :: { StrictnessMark }
-       : '!'                           { MarkedUserStrict }
-       | '!' '!'                       { MarkedUserUnboxed }
+strict_mark :: { HsBang }
+       : '!'                           { HsStrict }
+       | '!' '!'                       { HsUnbox }
 
 deriving :: { Maybe RdrNameContext }
        : {- empty -}                   { Nothing }
@@ -944,30 +931,16 @@ exp10 :: { RdrNameHsExpr }
 
         | '{-# CORE' STRING '#-}' exp           { HsCoreAnn $2 $4 }    -- hdaume: core annotation
 
-       | reifyexp                              { HsReify $1 }
        | fexp                                  { $1 }
 
 scc_annot :: { FastString }
        : '_scc_' STRING                        { $2 }
        | '{-# SCC' STRING '#-}'                { $2 }
 
-ccallid :: { FastString }
-       :  VARID                                { $1 }
-       |  CONID                                { $1 }
-
 fexp   :: { RdrNameHsExpr }
-       : fexp aexp                             { (HsApp $1 $2) }
+       : fexp aexp                             { HsApp $1 $2 }
        | aexp                                  { $1 }
 
-reifyexp :: { HsReify RdrName }
-       : REIFY_DECL gtycon                     { Reify ReifyDecl $2 }
-       | REIFY_DECL qvar                       { Reify ReifyDecl $2 }
-       | REIFY_TYPE qcname                     { Reify ReifyType $2 }
-       | REIFY_FIXITY qcname                   { Reify ReifyFixity $2 }
-
-aexps0         :: { [RdrNameHsExpr] }
-       : aexps                                 { reverse $1 }
-
 aexps  :: { [RdrNameHsExpr] }
        : aexps aexp                            { $2 : $1 }
        | {- empty -}                           { [] }
@@ -984,7 +957,9 @@ aexp1       :: { RdrNameHsExpr }
 -- Here was the syntax for type applications that I was planning
 -- but there are difficulties (e.g. what order for type args)
 -- so it's not enabled yet.
-       | qcname '{|' gentype '|}'          { (HsApp (HsVar $1) (HsType $3)) }
+-- But this case *is* used for the left hand side of a generic definition,
+-- which is parsed as an expression before being munged into a pattern
+       | qcname '{|' gentype '|}'      { (HsApp (HsVar $1) (HsType $3)) }
 
 aexp2  :: { RdrNameHsExpr }
        : ipvar                         { HsIPVar $1 }
@@ -1002,8 +977,12 @@ aexp2     :: { RdrNameHsExpr }
        | '_'                           { EWildPat }
        
        -- MetaHaskell Extension
-       | srcloc ID_SPLICE              { mkHsSplice (HsVar (mkUnqual varName $2)) $1 }  -- $x
+       | srcloc TH_ID_SPLICE           { mkHsSplice (HsVar (mkUnqual varName $2)) $1 }  -- $x
        | srcloc '$(' exp ')'           { mkHsSplice $3 $1 }                             -- $( exp )
+       | srcloc TH_VAR_QUOTE qvar      { HsBracket (VarBr $3) $1 }
+       | srcloc TH_VAR_QUOTE qcon      { HsBracket (VarBr $3) $1 }
+       | srcloc TH_TY_QUOTE tyvar      { HsBracket (VarBr $3) $1 }
+       | srcloc TH_TY_QUOTE gtycon     { HsBracket (VarBr $3) $1 }
        | srcloc '[|' exp '|]'          { HsBracket (ExpBr $3) $1 }                       
        | srcloc '[t|' ctype '|]'       { HsBracket (TypBr $3) $1 }                       
        | srcloc '[p|' infixexp '|]'    {% checkPattern $1 $3 >>= \p ->
@@ -1181,10 +1160,6 @@ fbind    :: { (RdrName, RdrNameHsExpr) }
 -----------------------------------------------------------------------------
 -- Implicit Parameter Bindings
 
-dbinding :: { [(IPName RdrName, RdrNameHsExpr)] }
-       : '{' dbinds '}'                { $2 }
-       | vocurly dbinds close          { $2 }
-
 dbinds         :: { [(IPName RdrName, RdrNameHsExpr)] }
        : dbinds ';' dbind              { $3 : $1 }
        | dbinds ';'                    { $1 }
@@ -1267,9 +1242,9 @@ gtycon    :: { RdrName }  -- A "general" qualified tycon
        : oqtycon                       { $1 }
        | '(' ')'                       { getRdrName unitTyCon }
        | '(' commas ')'                { getRdrName (tupleTyCon Boxed $2) }
-       | '(' '->' ')'                  { nameRdrName funTyConName }
-       | '[' ']'                       { nameRdrName listTyConName }
-       | '[:' ':]'                     { nameRdrName parrTyConName }
+       | '(' '->' ')'                  { getRdrName funTyCon }
+       | '[' ']'                       { listTyCon_RDR }
+       | '[:' ':]'                     { parrTyCon_RDR }
 
 oqtycon :: { RdrName } -- An "ordinary" qualified tycon
        : qtycon                        { $1 }
@@ -1398,8 +1373,7 @@ consym :: { RdrName }
        : CONSYM                { mkUnqual dataName $1 }
 
        -- ':' means only list cons
-       | ':'                   { nameRdrName consDataConName }
-                               -- NB: SrcName because we are reading source
+       | ':'                   { consDataCon_RDR }
 
 
 -----------------------------------------------------------------------------