[project @ 2003-06-23 10:35:15 by simonpj]
[ghc-hetmet.git] / ghc / compiler / parser / ParserCore.y
index 7e3ad74..1cd7d6a 100644 (file)
@@ -68,7 +68,7 @@ import Outputable
 
 module :: { RdrNameHsModule }
        : '%module' modid tdefs vdefgs
-               { HsModule (mkHomeModule $2) Nothing Nothing 
+               { HsModule (Just (mkHomeModule $2)) Nothing 
                           [] ($3 ++ concat $4) Nothing noSrcLoc}
 
 tdefs  :: { [RdrNameHsDecl] }
@@ -79,11 +79,15 @@ tdef        :: { RdrNameHsDecl }
        : '%data' q_tc_name tbinds '=' '{' cons1 '}'
                 { TyClD (mkTyData DataType ([], $2, $3) (DataCons $6) Nothing noSrcLoc) }
        | '%newtype' q_tc_name tbinds trep 
-               { TyClD (mkTyData NewType ([], $2, $3) ($4 $2 $3) Nothing noSrcLoc) }
+               { TyClD (mkTyData NewType ([], $2, $3) ($4 $2) Nothing noSrcLoc) }
 
-trep    :: { (RdrName -> [HsTyVarBndr RdrName] -> DataConDetails (ConDecl RdrName)) }
-        : {- empty -}   { (\ x ts -> Unknown) }
-        | '=' ty        { (\ x ts -> DataCons [ConDecl x ts [] (PrefixCon [unbangedType $2]) noSrcLoc]) }
+-- For a newtype we have to invent a fake data constructor name
+-- It doesn't matter what it is, because it won't be used
+trep    :: { (RdrName -> DataConDetails (ConDecl RdrName)) }
+        : {- empty -}   { (\ tc_name -> Unknown) }
+        | '=' ty        { (\ tc_name -> let { dc_name  = setRdrNameSpace tc_name dataName ;
+                                             con_info = PrefixCon [unbangedType $2] }
+                                       in DataCons [ConDecl dc_name [] [] con_info noSrcLoc]) }
 
 tbind  :: { HsTyVarBndr RdrName }
        :  name                    { IfaceTyVar $1 liftedTypeKind }
@@ -150,7 +154,7 @@ cons1       :: { [ConDecl RdrName] }
        | con ';' cons1 { $1:$3 }
 
 con    :: { ConDecl RdrName }
-       : q_d_name attbinds atys 
+       : q_d_patt attbinds atys 
                { ConDecl $1 $2 [] (PrefixCon (map unbangedType $3)) noSrcLoc}
 
 atys   :: { [ RdrNameHsType] }
@@ -202,8 +206,8 @@ alts1       :: { [UfAlt RdrName] }
        | alt ';' alts1 { $1:$3 }
 
 alt    :: { UfAlt RdrName }
-       : q_d_name attbinds vbinds '->' exp 
-               { {- ToDo: sort out-} (UfDataAlt $1, (map hsTyVarName $2 ++ map fst $3), $5) } 
+       : q_d_patt attbinds vbinds '->' exp 
+               { (UfDataAlt $1, (map hsTyVarName $2 ++ map fst $3), $5) } 
        | lit '->' exp
                { (UfLitAlt $1, [], $3) }
        | '%_' '->' exp
@@ -211,7 +215,7 @@ alt :: { UfAlt RdrName }
 
 lit    :: { Literal }
        : '(' INTEGER '::' aty ')'      { convIntLit $2 $4 }
-       | '(' RATIONAL '::' aty ')'     { MachDouble $2 }
+       | '(' RATIONAL '::' aty ')'     { convRatLit $2 $4 }
        | '(' CHAR '::' aty ')'         { MachChar (fromEnum $2) }
        | '(' STRING '::' aty ')'       { MachStr (mkFastString $2) }
 
@@ -229,19 +233,26 @@ modid     :: { ModuleName }
 
 qname  :: { RdrName }           -- Includes data constructors
        : name                   { $1 }
-       | mname '.' NAME         { mkIfaceOrig varName (mkFastString $1,mkFastString $3) }
-        | q_d_name               { $1 }
+       | mname '.' NAME         { mkIfaceOrig varName (mkFastString $1) (mkFastString $3) }
+        | q_d_occ                { $1 }
 
 
 -- Type constructor
 q_tc_name      :: { RdrName }
         : mname '.' cname 
-               { mkIfaceOrig tcName (mkFastString $1,mkFastString $3) }
+               { mkIfaceOrig tcName (mkFastString $1) (mkFastString $3) }
 
--- Data constructor
-q_d_name       :: { RdrName }
+-- Data constructor in a pattern or data type declaration; use the dataName, 
+-- because that's what we expect in Core case patterns
+q_d_patt :: { RdrName }
         : mname '.' cname 
-               { mkIfaceOrig dataName (mkFastString $1,mkFastString $3) }
+               { mkIfaceOrig dataName (mkFastString $1) (mkFastString $3) }
+
+-- Data constructor occurrence in an expression;
+-- use the varName because that's the worker Id
+q_d_occ :: { RdrName }
+        : mname '.' cname 
+               { mkIfaceOrig varName (mkFastString $1) (mkFastString $3) }
 
 
 {
@@ -253,13 +264,21 @@ convIntLit i (HsTyVar n)
   | n == intPrimRdrName  = MachInt  i  
   | n == wordPrimRdrName = MachWord i
 convIntLit i aty
-  = pprPanic "Unknown literal type" (ppr aty)
+  = pprPanic "Unknown integer literal type" (ppr aty $$ ppr intPrimRdrName) 
+
+convRatLit :: Rational -> RdrNameHsType -> Literal
+convRatLit r (HsTyVar n)
+  | n == floatPrimRdrName  = MachFloat  r
+  | n == doublePrimRdrName = MachDouble r
+convRatLit i aty
+  = pprPanic "Unknown rational literal type" (ppr aty $$ ppr intPrimRdrName) 
 
-wordPrimRdrName :: RdrName
-wordPrimRdrName = nameRdrName wordPrimTyConName
 
-intPrimRdrName :: RdrName
-intPrimRdrName = nameRdrName intPrimTyConName
+wordPrimRdrName, intPrimRdrName, floatPrimRdrName, doublePrimRdrName :: RdrName
+wordPrimRdrName   = nameRdrName wordPrimTyConName
+intPrimRdrName    = nameRdrName intPrimTyConName
+floatPrimRdrName  = nameRdrName floatPrimTyConName
+doublePrimRdrName = nameRdrName doublePrimTyConName
 
 happyError :: P a 
 happyError s l = failP (show l ++ ": Parse error\n") (take 100 s) l