2 module ParserCore ( parseCore ) where
12 import ParserCoreUtils
22 #include "../HsVersions.h"
30 '%module' { TKmodule }
32 '%newtype' { TKnewtype }
33 '%forall' { TKforall }
39 '%coerce' { TKcoerce }
41 '%external' { TKexternal }
59 INTEGER { TKinteger $$ }
60 RATIONAL { TKrational $$ }
61 STRING { TKstring $$ }
64 %monad { P } { thenP } { returnP }
65 %lexer { lexer } { TKEOF }
69 module :: { RdrNameHsModule }
70 : '%module' modid tdefs vdefgs
71 { HsModule (mkHomeModule $2) Nothing Nothing
72 [] ($3 ++ concat $4) Nothing noSrcLoc}
74 tdefs :: { [RdrNameHsDecl] }
76 | tdef ';' tdefs {$1:$3}
78 tdef :: { RdrNameHsDecl }
79 : '%data' q_tc_name tbinds '=' '{' cons1 '}'
80 { TyClD (mkTyData DataType ([], $2, $3) (DataCons $6) Nothing noSrcLoc) }
81 | '%newtype' q_tc_name tbinds trep
82 { TyClD (mkTyData NewType ([], $2, $3) ($4 $2 $3) Nothing noSrcLoc) }
84 trep :: { (RdrName -> [HsTyVarBndr RdrName] -> DataConDetails (ConDecl RdrName)) }
85 : {- empty -} { (\ x ts -> Unknown) }
86 | '=' ty { (\ x ts -> DataCons [ConDecl x ts [] (PrefixCon [unbangedType $2]) noSrcLoc]) }
88 tbind :: { HsTyVarBndr RdrName }
89 : name { IfaceTyVar $1 liftedTypeKind }
90 | '(' name '::' akind ')' { IfaceTyVar $2 $4 }
92 tbinds :: { [HsTyVarBndr RdrName] }
94 | tbind tbinds { $1:$2 }
96 vdefgs :: { [[RdrNameHsDecl]] }
98 | vdefg ';' vdefgs { ($1:$3) }
100 vdefg :: { [RdrNameHsDecl] }
101 : '%rec' '{' vdefs1 '}' { map CoreD $3 }
102 | vdef { [CoreD $1] }
104 let_bind :: { UfBinding RdrName }
105 : '%rec' '{' vdefs1 '}' { UfRec (map convBind $3) }
106 | vdef { let (b,r) = convBind $1
109 vdefs1 :: { [RdrNameCoreDecl] }
111 | vdef ';' vdefs1 { $1:$3 }
113 vdef :: { RdrNameCoreDecl }
114 : qname '::' ty '=' exp { CoreDecl $1 $3 $5 noSrcLoc }
115 -- NB: qname includes data constructors, because
116 -- we allow data-constructor wrappers at top level
119 vbind :: { (RdrName, RdrNameHsType) }
120 : '(' name '::' ty ')' { ($2,$4) }
122 vbinds :: { [(RdrName, RdrNameHsType)] }
124 | vbind vbinds { $1:$2 }
126 bind :: { UfBinder RdrName }
127 : '@' tbind { let (IfaceTyVar v k) = $2 in UfTyBinder v k }
128 | vbind { let (v,ty) = $1 in UfValBinder v ty }
130 binds1 :: { [UfBinder RdrName] }
132 | bind binds1 { $1:$2 }
134 attbinds :: { [RdrNameHsTyVar] }
136 | '@' tbind attbinds { $2:$3 }
139 : '*' { liftedTypeKind }
140 | '#' { unliftedTypeKind }
141 | '?' { openTypeKind }
142 | '(' kind ')' { $2 }
146 | akind '->' kind { mkArrowKind $1 $3 }
148 cons1 :: { [ConDecl RdrName] }
150 | con ';' cons1 { $1:$3 }
152 con :: { ConDecl RdrName }
153 : q_d_name attbinds atys
154 { ConDecl $1 $2 [] (PrefixCon (map unbangedType $3)) noSrcLoc}
156 atys :: { [ RdrNameHsType] }
160 aty :: { RdrNameHsType }
161 : name { HsTyVar $1 }
162 | q_tc_name { HsTyVar $1 }
166 bty :: { RdrNameHsType }
168 | bty aty { HsAppTy $1 $2 }
170 ty :: { RdrNameHsType }
172 | bty '->' ty { HsFunTy $1 $3 }
173 | '%forall' tbinds '.' ty { HsForAllTy (Just $2) [] $4 }
175 aexp :: { UfExpr RdrName }
180 fexp :: { UfExpr RdrName }
181 : fexp aexp { UfApp $1 $2 }
182 | fexp '@' aty { UfApp $1 (UfType $3) }
185 exp :: { UfExpr RdrName }
187 | '\\' binds1 '->' exp { foldr UfLam $4 $2 }
188 | '%let' let_bind '%in' exp { UfLet $2 $4 }
189 | '%case' aexp '%of' vbind
190 '{' alts1 '}' { UfCase $2 (fst $4) $6 }
191 | '%coerce' aty exp { UfNote (UfCoerce $2) $3 } -- what about the 'from' type?
194 --"SCC" -> UfNote (UfSCC "scc") $3
195 "InlineCall" -> UfNote UfInlineCall $3
196 "InlineMe" -> UfNote UfInlineMe $3
198 -- | '%external' STRING aty { External $2 $3 }
200 alts1 :: { [UfAlt RdrName] }
202 | alt ';' alts1 { $1:$3 }
204 alt :: { UfAlt RdrName }
205 : q_d_name attbinds vbinds '->' exp
206 { {- ToDo: sort out-} (UfDataAlt $1, (map hsTyVarName $2 ++ map fst $3), $5) }
208 { (UfLitAlt $1, [], $3) }
210 { (UfDefault, [], $3) }
213 : '(' INTEGER '::' aty ')' { convIntLit $2 $4 }
214 | '(' RATIONAL '::' aty ')' { MachDouble $2 }
215 | '(' CHAR '::' aty ')' { MachChar (fromEnum $2) }
216 | '(' STRING '::' aty ')' { MachStr (mkFastString $2) }
219 : NAME { mkRdrUnqual (mkVarOccEncoded (mkFastString $1)) }
227 modid :: { ModuleName }
228 : CNAME { mkSysModuleNameFS (mkFastString $1) }
230 qname :: { RdrName } -- Includes data constructors
232 | mname '.' NAME { mkIfaceOrig varName (mkFastString $1) (mkFastString $3) }
237 q_tc_name :: { RdrName }
239 { mkIfaceOrig tcName (mkFastString $1) (mkFastString $3) }
242 q_d_name :: { RdrName }
244 { mkIfaceOrig dataName (mkFastString $1) (mkFastString $3) }
248 convBind :: RdrNameCoreDecl -> (UfBinder RdrName, UfExpr RdrName)
249 convBind (CoreDecl n ty rhs _) = (UfValBinder n ty, rhs)
251 convIntLit :: Integer -> RdrNameHsType -> Literal
252 convIntLit i (HsTyVar n)
253 | n == intPrimRdrName = MachInt i
254 | n == wordPrimRdrName = MachWord i
256 = pprPanic "Unknown literal type" (ppr aty $$ ppr intPrimRdrName)
258 wordPrimRdrName :: RdrName
259 wordPrimRdrName = nameRdrName wordPrimTyConName
261 intPrimRdrName :: RdrName
262 intPrimRdrName = nameRdrName intPrimTyConName
265 happyError s l = failP (show l ++ ": Parse error\n") (take 100 s) l