2 #include "HsVersions.h"
7 ParsedIface(..), RdrIfaceDecl(..),
9 ExportsMap(..), LocalDefsMap(..), LocalPragmasMap(..),
10 LocalVersionsMap(..), PragmaStuff(..)
16 import HsSyn ( ClassDecl, InstDecl, TyDecl, PolyType, InPat, Fake )
17 import RdrHsSyn ( RdrNameTyDecl(..), RdrNameClassDecl(..),
18 RdrNamePolyType(..), RdrNameInstDecl(..)
20 import FiniteMap ( emptyFM, listToFM, fmToList, lookupFM, keysFM, FiniteMap )
21 import Name ( ExportFlag(..) )
22 import Util ( startsWith )
23 -----------------------------------------------------------------
25 parseIface = parseIToks . lexIface
27 type LocalVersionsMap = FiniteMap FAST_STRING Version
28 type ExportsMap = FiniteMap FAST_STRING (RdrName, ExportFlag)
29 type LocalDefsMap = FiniteMap FAST_STRING RdrIfaceDecl
30 type LocalPragmasMap = FiniteMap FAST_STRING PragmaStuff
32 type PragmaStuff = String
37 Version -- Module version number
38 (Maybe Version) -- Source version number
39 LocalVersionsMap -- Local version numbers
40 ExportsMap -- Exported names
41 [Module] -- Special instance modules
42 LocalDefsMap -- Local names defined
43 [RdrIfaceDecl] -- Local instance declarations
44 LocalPragmasMap -- Pragmas for local names
47 instance Text ParsedIface where
48 showsPrec _ (ParsedIface m v mv lcm exm ims ldm lids ldp)
49 = showString "interface "
50 . showString (_UNPK_ m)
53 . showString "\n__versions__\n"
54 . showList (fmToList lcm)
55 . showString "\n__exports__\n"
56 . showList (fmToList exm)
57 . showString "\n__instance_modules__\n"
58 . showList (map _UNPK_ ims)
59 . showString "\n__declarations__\n"
60 . showList (map _UNPK_ (keysFM ldm))
61 . showString "\n__instances__\n"
63 . showString "\n__pragmas__\n"
64 . showList (map _UNPK_ (keysFM ldp))
67 -----------------------------------------------------------------
70 = TypeSig RdrName Bool SrcLoc RdrNameTyDecl
71 | NewTypeSig RdrName RdrName Bool SrcLoc RdrNameTyDecl
72 | DataSig RdrName [RdrName] Bool SrcLoc RdrNameTyDecl
73 | ClassSig RdrName [RdrName] Bool SrcLoc RdrNameClassDecl
74 | ValSig RdrName Bool SrcLoc RdrNamePolyType
75 | InstSig RdrName RdrName Bool SrcLoc RdrNameInstDecl
76 -- True => Source Iface decl
80 -----------------------------------------------------------------
84 %tokentype { IfaceToken }
87 interface { ITinterface }
88 versions_part { ITversions }
89 exports_part { ITexports }
90 instance_modules_part { ITinstance_modules }
91 instances_part { ITinstances }
92 declarations_part { ITdeclarations }
93 pragmas_part { ITpragmas }
99 instance { ITinstance }
103 dblrarrow { ITdblrarrow }
114 semicolon { ITsemicolon }
119 Iface :: { ParsedIface }
120 Iface : interface name num
121 VersionsPart ExportsPart InstanceModulesPart
122 DeclsPart InstancesPart PragmasPart
123 { ParsedIface $2 (fromInteger $3) Nothing{-src version-}
126 $6 -- instance modules
128 $8 -- local instances
132 VersionsPart :: { LocalVersionsMap }
133 VersionsPart : versions_part NameVersionPairs
136 NameVersionPairs :: { [(FAST_STRING, Int)] }
137 NameVersionPairs : NameVersionPairs name lparen num rparen
138 { ($2, fromInteger $4) : $1 }
141 ExportsPart :: { ExportsMap }
142 ExportsPart : exports_part ExportItems
145 ExportItems :: { [(FAST_STRING, (RdrName, ExportFlag))] }
146 ExportItems : ExportItems name dot name MaybeDotDot
147 { ($4, (Qual $2 $4, $5)) : $1 }
150 MaybeDotDot :: { ExportFlag }
151 MaybeDotDot : dotdot { ExportAll }
154 InstanceModulesPart :: { [Module] }
155 InstanceModulesPart : instance_modules_part ModList
158 ModList :: { [Module] }
159 ModList : ModList name { $2 : $1 }
162 DeclsPart :: { LocalDefsMap }
163 DeclsPart : declarations_part
166 InstancesPart :: { [RdrIfaceDecl] }
167 InstancesPart : instances_part
170 PragmasPart :: { LocalPragmasMap }
171 PragmasPart : pragmas_part
174 -----------------------------------------------------------------
175 happyError :: Int -> [IfaceToken] -> a
176 happyError i _ = error ("Parse error in line " ++ show i ++ "\n")
178 -----------------------------------------------------------------
180 = ITinterface -- keywords
193 | ITbar -- magic symbols
208 | ITnum Integer -- numbers and names
211 -----------------------------------------------------------------
212 lexIface :: String -> [IfaceToken]
218 -- whitespace and comments
219 ' ' : cs -> lexIface cs
220 '\t' : cs -> lexIface cs
221 '\n' : cs -> lexIface cs
222 '-' : '-' : cs -> lex_comment cs
223 '{' : '-' : cs -> lex_nested_comment 1{-one seen-} cs
225 '(' : '.' : '.' : ')' : cs -> ITdotdot : lexIface cs
226 '(' : cs -> ITlparen : lexIface cs
227 ')' : cs -> ITrparen : lexIface cs
228 '[' : cs -> ITlbrack : lexIface cs
229 ']' : cs -> ITrbrack : lexIface cs
230 '{' : cs -> ITlbrace : lexIface cs
231 '}' : cs -> ITrbrace : lexIface cs
232 '-' : '>' : cs -> ITrarrow : lexIface cs
233 '.' : cs -> ITdot : lexIface cs
234 '|' : cs -> ITbar : lexIface cs
235 ':' : ':' : cs -> ITcolons : lexIface cs
236 '=' : '>' : cs -> ITdblrarrow : lexIface cs
237 '=' : cs -> ITequal : lexIface cs
238 ',' : cs -> ITcomma : lexIface cs
239 ';' : cs -> ITsemicolon : lexIface cs
241 '_' : cs -> lex_word str
242 c : cs | isDigit c -> lex_num str
243 | isAlpha c -> lex_word str
245 other -> error ("lexing:"++other)
248 = case (span ((/=) '\n') str) of { (junk, rest) ->
251 lex_nested_comment lvl [] = error "EOF in nested comment in interface"
252 lex_nested_comment lvl str
254 '{' : '-' : xs -> lex_nested_comment (lvl+1) xs
255 '-' : '}' : xs -> if lvl == 1
257 else lex_nested_comment (lvl-1) xs
258 _ : xs -> lex_nested_comment lvl xs
261 = case (span isDigit str) of { (num, rest) ->
262 ITnum (read num) : lexIface rest }
265 = case (span is_word_sym str) of { (word, rest) ->
266 case (lookupFM keywordsFM word) of {
267 Nothing -> ITname (_PK_ word) : lexIface rest ;
268 Just xx -> xx : lexIface rest
271 is_word_sym '_' = True
272 is_word_sym c = isAlphanum c
274 keywordsFM :: FiniteMap String IfaceToken
275 keywordsFM = listToFM [
276 ("interface", ITinterface)
278 ,("__versions__", ITversions)
279 ,("__exports__", ITexports)
280 ,("__instance_modules__", ITinstance_modules)
281 ,("__instances__", ITinstances)
282 ,("__declarations__", ITdeclarations)
283 ,("__pragmas__", ITpragmas)
288 ,("instance", ITinstance)