2 #include "HsVersions.h"
4 module ParseIface ( parseIface ) where
10 import HsSyn -- quite a bit of stuff
11 import RdrHsSyn -- oodles of synonyms
12 import HsPragmas ( noGenPragmas )
14 import Bag ( emptyBag, unitBag, snocBag )
15 import FiniteMap ( emptyFM, unitFM, addToFM, plusFM, bagToFM )
16 import Name ( ExportFlag(..), mkTupNameStr,
17 RdrName(..){-instance Outputable:ToDo:rm-}
19 import Outputable -- ToDo:rm
20 import PprStyle ( PprStyle(..) ) -- ToDo: rm debugging
21 import SrcLoc ( mkIfaceSrcLoc )
22 import Util ( pprPanic{-ToDo:rm-} )
24 -----------------------------------------------------------------
26 parseIface = parseIToks . lexIface
28 -----------------------------------------------------------------
32 %tokentype { IfaceToken }
33 %monad { IfM }{ thenIf }{ returnIf }
36 INTERFACE { ITinterface }
37 VERSIONS_PART { ITversions }
38 EXPORTS_PART { ITexports }
39 INSTANCE_MODULES_PART { ITinstance_modules }
40 INSTANCES_PART { ITinstances }
41 FIXITIES_PART { ITfixities }
42 DECLARATIONS_PART { ITdeclarations }
43 PRAGMAS_PART { ITpragmas }
59 INSTANCE { ITinstance }
69 INTEGER { ITinteger $$ }
72 VARSYM { ITvarsym $$ }
73 CONSYM { ITconsym $$ }
74 QVARID { ITqvarid $$ }
75 QCONID { ITqconid $$ }
76 QVARSYM { ITqvarsym $$ }
77 QCONSYM { ITqconsym $$ }
80 iface :: { ParsedIface }
81 iface : INTERFACE CONID INTEGER
82 versions_part exports_part inst_modules_part
83 fixities_part decls_part instances_part pragmas_part
84 { case $8 of { (tm, vm) ->
85 ParsedIface $2 (fromInteger $3) Nothing{-src version-}
88 $6 -- instance modules
95 --------------------------------------------------------------------------
98 versions_part :: { LocalVersionsMap }
99 versions_part : VERSIONS_PART name_version_pairs
102 name_version_pairs :: { Bag (FAST_STRING, Int) }
103 name_version_pairs : iname OPAREN INTEGER CPAREN
104 { unitBag ($1, fromInteger $3) }
105 | name_version_pairs iname OPAREN INTEGER CPAREN
106 { $1 `snocBag` ($2, fromInteger $4)
107 --------------------------------------------------------------------------
110 exports_part :: { ExportsMap }
111 exports_part : EXPORTS_PART export_items { bagToFM $2 }
113 export_items :: { Bag (FAST_STRING, (RdrName, ExportFlag)) }
114 export_items : qiname maybe_dotdot
115 { unitBag (de_qual $1, ($1, $2)) }
116 | export_items qiname maybe_dotdot
117 { $1 `snocBag` (de_qual $2, ($2, $3)) }
119 maybe_dotdot :: { ExportFlag }
120 maybe_dotdot : DOTDOT { ExportAll }
122 --------------------------------------------------------------------------
125 inst_modules_part :: { Bag Module }
126 inst_modules_part : INSTANCE_MODULES_PART mod_list { $2 }
129 mod_list :: { Bag Module }
130 mod_list : CONID { unitBag $1 }
131 | mod_list CONID { $1 `snocBag` $2
132 --------------------------------------------------------------------------
135 fixities_part :: { FixitiesMap }
136 fixities_part : FIXITIES_PART fixes { $2 }
139 fixes :: { FixitiesMap }
140 fixes : fix { case $1 of (k,v) -> unitFM k v }
141 | fixes SEMI fix { case $3 of (k,v) -> addToFM $1 k v }
143 fix :: { (FAST_STRING, RdrNameFixityDecl) }
144 fix : INFIXL INTEGER qop { (de_qual $3, InfixL $3 (fromInteger $2)) }
145 | INFIXR INTEGER qop { (de_qual $3, InfixR $3 (fromInteger $2)) }
146 | INFIX INTEGER qop { (de_qual $3, InfixN $3 (fromInteger $2))
147 --------------------------------------------------------------------------
150 decls_part :: { (LocalTyDefsMap, LocalValDefsMap) }
151 decls_part : DECLARATIONS_PART topdecls { $2 }
153 topdecls :: { (LocalTyDefsMap, LocalValDefsMap) }
154 topdecls : topdecl { $1 }
155 | topdecls SEMI topdecl { case $1 of { (ts1, vs1) ->
156 case $3 of { (ts2, vs2) ->
157 (plusFM ts1 ts2, plusFM vs1 vs2)}}
160 topdecl :: { (LocalTyDefsMap, LocalValDefsMap) }
161 topdecl : typed { ($1, emptyFM) }
165 | decl { case $1 of { (n, Sig qn ty _ loc) ->
166 (emptyFM, unitFM n (ValSig qn loc ty)) }
169 typed :: { LocalTyDefsMap }
170 typed : TYPE simple EQUAL type { mk_type $2 $4 }
172 datad :: { (LocalTyDefsMap, LocalValDefsMap) }
173 datad : DATA simple EQUAL constrs { mk_data [] $2 $4 }
174 | DATA context DARROW simple EQUAL constrs { mk_data $2 $4 $6 }
176 newtd :: { (LocalTyDefsMap, LocalValDefsMap) }
177 newtd : NEWTYPE simple EQUAL constr1 { mk_new [] $2 $4 }
178 | NEWTYPE context DARROW simple EQUAL constr1 { mk_new $2 $4 $6 }
180 classd :: { (LocalTyDefsMap, LocalValDefsMap) }
181 classd : CLASS class cbody { mk_class [] $2 $3 }
182 | CLASS context DARROW class cbody { mk_class $2 $4 $5 }
184 cbody :: { [(FAST_STRING, RdrNameSig)] }
185 cbody : WHERE OCURLY decls CCURLY { $3 }
188 decls :: { [(FAST_STRING, RdrNameSig)] }
189 decls : decl { [$1] }
190 | decls SEMI decl { $1 ++ [$3] }
192 decl :: { (FAST_STRING, RdrNameSig) }
193 decl : var DCOLON ctype { (de_qual $1, Sig $1 $3 noGenPragmas mkIfaceSrcLoc) }
195 context :: { RdrNameContext }
196 context : OPAREN context_list CPAREN { reverse $2 }
199 context_list :: { RdrNameContext{-reversed-} }
200 context_list : class { [$1] }
201 | context_list COMMA class { $3 : $1 }
203 class :: { (RdrName, RdrName) }
204 class : gtycon VARID { ($1, Unqual $2) }
206 ctype :: { RdrNamePolyType }
207 ctype : type DARROW type { HsPreForAllTy (type2context $1) $3 }
208 | type { HsPreForAllTy [] $1 }
210 type :: { RdrNameMonoType }
212 | btype RARROW type { MonoFunTy $1 $3 }
214 types :: { [RdrNameMonoType] }
215 types : type { [$1] }
216 | types COMMA type { $1 ++ [$3] }
218 btype :: { RdrNameMonoType }
219 btype : gtyconapp { case $1 of (tc, tys) -> MonoTyApp tc tys }
220 | ntyconapp { case $1 of { (ty1, tys) ->
225 MonoTyVar tv -> MonoTyApp tv tys;
226 MonoTyApp tc ts -> MonoTyApp tc (ts++tys);
227 MonoFunTy t1 t2 -> MonoTyApp (Unqual SLIT("->")) (t1:t2:tys);
228 MonoListTy ty -> MonoTyApp (Unqual SLIT("[]")) (ty:tys);
229 MonoTupleTy ts -> MonoTyApp (Unqual (mkTupNameStr (length ts)))
231 _ -> pprPanic "test:" (ppr PprDebug $1)
235 ntyconapp :: { (RdrNameMonoType, [RdrNameMonoType]) }
236 ntyconapp : ntycon { ($1, []) }
237 | ntyconapp atype { case $1 of (t1,tys) -> (t1, tys ++ [$2]) }
239 gtyconapp :: { (RdrName, [RdrNameMonoType]) }
240 gtyconapp : gtycon { ($1, []) }
241 | gtyconapp atype { case $1 of (tc,tys) -> (tc, tys ++ [$2]) }
243 atype :: { RdrNameMonoType }
244 atype : gtycon { MonoTyApp $1 [] }
247 atypes :: { [RdrNameMonoType] }
248 atypes : atype { [$1] }
249 | atypes atype { $1 ++ [$2] }
251 ntycon :: { RdrNameMonoType }
252 ntycon : VARID { MonoTyVar (Unqual $1) }
253 | OPAREN type COMMA types CPAREN { MonoTupleTy ($2 : $4) }
254 | OBRACK type CBRACK { MonoListTy $2 }
255 | OPAREN type CPAREN { $2 }
257 gtycon :: { RdrName }
258 gtycon : QCONID { $1 }
259 | CONID { Unqual $1 }
260 | OPAREN RARROW CPAREN { Unqual SLIT("->") }
261 | OBRACK CBRACK { Unqual SLIT("[]") }
262 | OPAREN CPAREN { Unqual SLIT("()") }
263 | OPAREN commas CPAREN { Unqual (mkTupNameStr $2) }
266 commas : COMMA { 2{-1 comma => arity 2-} }
267 | commas COMMA { $1 + 1 }
269 simple :: { (RdrName, [FAST_STRING]) }
270 simple : gtycon { ($1, []) }
271 | gtyconvars { case $1 of (tc,tvs) -> (tc, reverse tvs) }
273 gtyconvars :: { (RdrName, [FAST_STRING] {-reversed-}) }
274 gtyconvars : gtycon VARID { ($1, [$2]) }
275 | gtyconvars VARID { case $1 of (tc,tvs) -> (tc, $2 : tvs) }
277 constrs :: { [(RdrName, RdrNameConDecl)] }
278 constrs : constr { [$1] }
279 | constrs VBAR constr { $1 ++ [$3] }
281 constr :: { (RdrName, RdrNameConDecl) }
283 { case $1 of (con, tys) -> (con, ConDecl con tys mkIfaceSrcLoc) }
284 | OPAREN QCONSYM CPAREN { ($2, ConDecl $2 [] mkIfaceSrcLoc) }
285 | OPAREN QCONSYM CPAREN batypes { ($2, ConDecl $2 $4 mkIfaceSrcLoc) }
286 | OPAREN CONSYM CPAREN { (Unqual $2, ConDecl (Unqual $2) [] mkIfaceSrcLoc) }
287 | OPAREN CONSYM CPAREN batypes { (Unqual $2, ConDecl (Unqual $2) $4 mkIfaceSrcLoc) }
288 | gtycon OCURLY fields CCURLY
289 { ($1, RecConDecl $1 $3 mkIfaceSrcLoc) }
291 btyconapp :: { (RdrName, [RdrNameBangType]) }
292 btyconapp : gtycon { ($1, []) }
293 | btyconapp batype { case $1 of (tc,tys) -> (tc, tys ++ [$2]) }
295 bbtype :: { RdrNameBangType }
296 bbtype : btype { Unbanged $1 }
297 | BANG atype { Banged $2 }
299 batype :: { RdrNameBangType }
300 batype : atype { Unbanged $1 }
301 | BANG atype { Banged $2 }
303 batypes :: { [RdrNameBangType] }
304 batypes : batype { [$1] }
305 | batypes batype { $1 ++ [$2] }
307 fields :: { [([RdrName], RdrNameBangType)] }
308 fields : field { [$1] }
309 | fields COMMA field { $1 ++ [$3] }
311 field :: { ([RdrName], RdrNameBangType) }
312 field : var DCOLON type { ([$1], Unbanged $3) }
313 | var DCOLON BANG atype { ([$1], Banged $4) }
315 constr1 :: { (RdrName, RdrNameMonoType) }
316 constr1 : gtycon atype { ($1, $2) }
320 | OPAREN QVARSYM CPAREN { $2 }
321 | VARID { Unqual $1 }
322 | OPAREN VARSYM CPAREN { Unqual $2 }
324 op :: { FAST_STRING }
325 op : BQUOTE VARID BQUOTE { $2 }
326 | BQUOTE CONID BQUOTE { $2 }
331 qop : BQUOTE QVARID BQUOTE { $2 }
332 | BQUOTE QCONID BQUOTE { $2 }
337 iname :: { FAST_STRING }
340 | OPAREN VARSYM CPAREN { $2 }
341 | OPAREN CONSYM CPAREN { $2 }
343 qiname :: { RdrName }
344 qiname : QVARID { $1 }
346 | OPAREN QVARSYM CPAREN { $2 }
347 | OPAREN QCONSYM CPAREN { $2 }
348 | iname { Unqual $1 }
350 instances_part :: { Bag RdrIfaceInst }
351 instances_part : INSTANCES_PART instdecls { $2 }
354 instdecls :: { Bag RdrIfaceInst }
355 instdecls : instd { unitBag $1 }
356 | instdecls SEMI instd { $1 `snocBag` $3 }
358 instd :: { RdrIfaceInst }
359 instd : INSTANCE context DARROW gtycon restrict_inst { mk_inst $2 $4 $5 }
360 | INSTANCE gtycon general_inst { mk_inst [] $2 $3 }
362 restrict_inst :: { RdrNameMonoType }
363 restrict_inst : gtycon { MonoTyApp $1 [] }
364 | OPAREN gtyconvars CPAREN { case $2 of (tc,tvs) -> MonoTyApp tc (map en_mono tvs) }
365 | OPAREN VARID COMMA tyvar_list CPAREN { MonoTupleTy (map en_mono ($2:$4)) }
366 | OBRACK VARID CBRACK { MonoListTy (en_mono $2) }
367 | OPAREN VARID RARROW VARID CPAREN { MonoFunTy (en_mono $2) (en_mono $4) }
369 general_inst :: { RdrNameMonoType }
370 general_inst : gtycon { MonoTyApp $1 [] }
371 | OPAREN gtyconapp CPAREN { case $2 of (tc,tys) -> MonoTyApp tc tys }
372 | OPAREN type COMMA types CPAREN { MonoTupleTy ($2:$4) }
373 | OBRACK type CBRACK { MonoListTy $2 }
374 | OPAREN btype RARROW type CPAREN { MonoFunTy $2 $4 }
376 tyvar_list :: { [FAST_STRING] }
377 tyvar_list : VARID { [$1] }
378 | tyvar_list COMMA VARID { $1 ++ [$3]
379 --------------------------------------------------------------------------
382 pragmas_part :: { LocalPragmasMap }
383 pragmas_part : PRAGMAS_PART