2 module ParserCore ( parseCore ) where
12 import ParserCoreUtils
19 #include "../HsVersions.h"
27 '%module' { TKmodule }
29 '%newtype' { TKnewtype }
30 '%forall' { TKforall }
36 '%coerce' { TKcoerce }
38 '%external' { TKexternal }
56 INTEGER { TKinteger $$ }
57 RATIONAL { TKrational $$ }
58 STRING { TKstring $$ }
61 %monad { P } { thenP } { returnP }
62 %lexer { lexer } { TKEOF }
66 module :: { RdrNameHsModule }
67 : '%module' modid tdefs vdefgs
68 { HsModule $2 Nothing Nothing [] ($3 ++ concat $4) Nothing noSrcLoc}
70 tdefs :: { [RdrNameHsDecl] }
72 | tdef ';' tdefs {$1:$3}
74 tdef :: { RdrNameHsDecl }
75 : '%data' qcname tbinds '=' '{' cons1 '}'
76 { TyClD (TyData DataType [] $2 $3 (DataCons $6) Nothing [] noSrcLoc) }
77 | '%newtype' qcname tbinds trep
78 { TyClD (TyData NewType [] $2 $3 ($4 $2 $3) Nothing [] noSrcLoc) }
80 trep :: { (RdrName -> [HsTyVarBndr RdrName] -> DataConDetails (ConDecl RdrName)) }
81 : {- empty -} { (\ x ts -> Unknown) }
82 | '=' ty { (\ x ts -> DataCons [ConDecl x x ts [] (VanillaCon [unbangedType $2]) noSrcLoc]) }
84 tbind :: { HsTyVarBndr RdrName }
85 : name { IfaceTyVar $1 liftedTypeKind }
86 | '(' name '::' akind ')' { IfaceTyVar $2 $4 }
88 tbinds :: { [HsTyVarBndr RdrName] }
90 | tbind tbinds { $1:$2 }
92 vdefgs :: { [[RdrNameHsDecl]] }
94 | vdefg ';' vdefgs { ($1:$3) }
96 vdefg :: { [RdrNameHsDecl] }
97 : '%rec' '{' vdefs1 '}' { $3 }
100 vdefs1 :: { [RdrNameHsDecl] }
102 | vdef ';' vdefs1 { $1:$3 }
104 vdef :: { RdrNameHsDecl }
105 : qname '::' ty '=' exp { TyClD (CoreDecl $1 $3 $5 noSrcLoc) }
108 vbind :: { (RdrName, RdrNameHsType) }
109 : '(' name '::' ty ')' { ($2,$4) }
111 vbinds :: { [(RdrName, RdrNameHsType)] }
113 | vbind vbinds { $1:$2 }
115 bind :: { UfBinder RdrName }
116 : '@' tbind { let (IfaceTyVar v k) = $2 in UfTyBinder v k }
117 | vbind { let (v,ty) = $1 in UfValBinder v ty }
119 binds1 :: { [UfBinder RdrName] }
121 | bind binds1 { $1:$2 }
123 attbinds :: { [RdrNameHsTyVar] }
125 | '@' tbind attbinds { $2:$3 }
128 : '*' { liftedTypeKind }
129 | '#' { unliftedTypeKind }
130 | '?' { openTypeKind }
131 | '(' kind ')' { $2 }
135 | akind '->' kind { mkArrowKind $1 $3 }
137 cons1 :: { [ConDecl RdrName] }
139 | con ';' cons1 { $1:$3 }
141 con :: { ConDecl RdrName }
142 : qcname attbinds atys
143 { ConDecl $1 $1 $2 [] (VanillaCon (map unbangedType $3)) noSrcLoc}
145 atys :: { [ RdrNameHsType] }
149 aty :: { RdrNameHsType }
150 : name { HsTyVar $1 }
151 | qcname { HsTyVar $1 }
155 bty :: { RdrNameHsType }
157 | bty aty { HsAppTy $1 $2 }
159 ty :: { RdrNameHsType }
161 | bty '->' ty { HsFunTy $1 $3 }
162 | '%forall' tbinds '.' ty { HsForAllTy (Just $2) [] $4 }
164 aexp :: { UfExpr RdrName }
166 | qcname { UfVar $1 }
170 fexp :: { UfExpr RdrName }
171 : fexp aexp { UfApp $1 $2 }
172 | fexp '@' aty { UfApp $1 (UfType $3) }
175 exp :: { UfExpr RdrName }
177 | '\\' binds1 '->' exp { foldr UfLam $4 $2 }
178 | '%let' vdefg '%in' exp { UfLet (toUfBinder $2) $4 }
179 | '%case' aexp '%of' vbind
180 '{' alts1 '}' { UfCase $2 (fst $4) $6 }
181 | '%coerce' aty exp { UfNote (UfCoerce $2) $3 } -- what about the 'from' type?
184 --"SCC" -> UfNote (UfSCC "scc") $3
185 "InlineCall" -> UfNote UfInlineCall $3
186 "InlineMe" -> UfNote UfInlineMe $3
188 -- | '%external' STRING aty { External $2 $3 }
190 alts1 :: { [UfAlt RdrName] }
192 | alt ';' alts1 { $1:$3 }
194 alt :: { UfAlt RdrName }
195 : qcname attbinds vbinds '->' exp
196 { {- ToDo: sort out-} (UfDataAlt $1, (map hsTyVarName $2 ++ map fst $3), $5) }
198 { (UfLitAlt $1, [], $3) }
200 { (UfDefault, [], $3) }
203 : '(' INTEGER '::' aty ')' { MachInt $2 }
204 | '(' RATIONAL '::' aty ')' { MachDouble $2 }
205 | '(' CHAR '::' aty ')' { MachChar (fromEnum $2) }
206 | '(' STRING '::' aty ')' { MachStr (_PK_ $2) }
209 : NAME { mkUnqual varName (_PK_ $1) }
217 modid :: { ModuleName }
218 : CNAME { mkSysModuleNameFS (_PK_ $1) }
223 { mkIfaceOrig varName (_PK_ $1,_PK_ $3) }
225 qcname :: { RdrName }
227 { mkIfaceOrig dataName (_PK_ $1,_PK_ $3) }
232 toUfBinder :: [RdrNameHsDecl] -> UfBinding RdrName
235 [x] -> uncurry UfNonRec (conv x)
236 _ -> UfRec (map conv xs)
238 conv (TyClD (CoreDecl n ty rhs _)) = (UfValBinder n ty, rhs)
241 happyError s l = failP (show l ++ ": Parse error\n") (take 100 s) l