2 % (c) The GRASP/AQUA Project, Glasgow University, 1999
4 \section[ParseUtil]{Parser Utilities}
8 parseError -- String -> Pa
10 , mkVanillaCon, mkRecCon,
12 , mkRecConstrOrUpdate -- HsExp -> [HsFieldUpdate] -> P HsExp
15 , mkExtName -- Maybe ExtName -> RdrName -> ExtName
17 , checkPrec -- String -> P String
18 , checkContext -- HsType -> P HsContext
19 , checkInstType -- HsType -> P HsType
20 , checkDataHeader -- HsQualType -> P (HsContext,HsName,[HsName])
21 , checkSimple -- HsType -> [HsName] -> P ((HsName,[HsName]))
22 , checkPattern -- HsExp -> P HsPat
23 , checkPatterns -- SrcLoc -> [HsExp] -> P [HsPat]
24 , checkDo -- [HsStmt] -> P [HsStmt]
25 , checkValDef -- (SrcLoc, HsExp, HsRhs, [HsDecl]) -> P HsDecl
26 , checkValSig -- (SrcLoc, HsExp, HsRhs, [HsDecl]) -> P HsDecl
29 #include "HsVersions.h"
32 import HsSyn -- Lots of it
34 import RdrHsSyn ( RdrBinding(..),
35 RdrNameHsType, RdrNameBangType, RdrNameContext,
36 RdrNameHsTyVar, RdrNamePat, RdrNameHsExpr, RdrNameGRHSs,
37 RdrNameHsRecordBinds, RdrNameMonoBinds, RdrNameConDetails
40 import PrelNames ( unitTyCon_RDR )
41 import ForeignCall ( CCallConv(..) )
42 import OccName ( dataName, varName, tcClsName,
43 occNameSpace, setOccNameSpace, occNameUserString )
44 import FastString ( unpackFS )
45 import UniqFM ( UniqFM, listToUFM )
48 -----------------------------------------------------------------------------
51 parseError :: String -> P a
53 getSrcLocP `thenP` \ loc ->
54 failMsgP (hcat [ppr loc, text ": ", text s])
56 cbot = panic "CCall:result_ty"
58 -----------------------------------------------------------------------------
61 -- When parsing data declarations, we sometimes inadvertently parse
62 -- a constructor application as a type (eg. in data T a b = C a b `D` E a b)
63 -- This function splits up the type application, adds any pending
64 -- arguments, and converts the type constructor back into a data constructor.
66 mkVanillaCon :: RdrNameHsType -> [RdrNameBangType] -> P (RdrName, RdrNameConDetails)
71 split (HsAppTy t u) ts = split t (unbangedType u : ts)
72 split (HsTyVar tc) ts = tyConToDataCon tc `thenP` \ data_con ->
73 returnP (data_con, VanillaCon ts)
74 split _ _ = parseError "Illegal data/newtype declaration"
76 mkRecCon :: RdrName -> [([RdrName],RdrNameBangType)] -> P (RdrName, RdrNameConDetails)
78 = tyConToDataCon con `thenP` \ data_con ->
79 returnP (data_con, RecCon fields)
81 tyConToDataCon :: RdrName -> P RdrName
83 | occNameSpace tc_occ == tcClsName
84 = returnP (setRdrNameOcc tc (setOccNameSpace tc_occ dataName))
86 = parseError (showSDoc (text "not a constructor:" <+> quotes (ppr tc)))
88 tc_occ = rdrNameOcc tc
91 ----------------------------------------------------------------------------
92 -- Various Syntactic Checks
94 checkInstType :: RdrNameHsType -> P RdrNameHsType
97 HsForAllTy tvs ctxt ty ->
98 checkDictTy ty [] `thenP` \ dict_ty ->
99 returnP (HsForAllTy tvs ctxt dict_ty)
101 ty -> checkDictTy ty [] `thenP` \ dict_ty->
102 returnP (HsForAllTy Nothing [] dict_ty)
104 checkContext :: RdrNameHsType -> P RdrNameContext
105 checkContext (HsTupleTy _ ts)
106 = mapP (\t -> checkPred t []) ts `thenP` \ps ->
108 checkContext (HsTyVar t) -- empty contexts are allowed
109 | t == unitTyCon_RDR = returnP []
111 = checkPred t [] `thenP` \p ->
114 checkPred :: RdrNameHsType -> [RdrNameHsType]
115 -> P (HsPred RdrName)
116 checkPred (HsTyVar t) args@(_:_) | not (isRdrTyVar t)
117 = returnP (HsClassP t args)
118 checkPred (HsAppTy l r) args = checkPred l (r:args)
119 checkPred (HsPredTy (HsIParam n ty)) [] = returnP (HsIParam n ty)
120 checkPred _ _ = parseError "Illegal class assertion"
122 checkDictTy :: RdrNameHsType -> [RdrNameHsType] -> P RdrNameHsType
123 checkDictTy (HsTyVar t) args@(_:_) | not (isRdrTyVar t)
124 = returnP (mkHsDictTy t args)
125 checkDictTy (HsAppTy l r) args = checkDictTy l (r:args)
126 checkDictTy _ _ = parseError "Malformed context in instance header"
128 -- Put more comments!
129 -- Checks that the lhs of a datatype declaration
130 -- is of the form Context => T a b ... z
131 checkDataHeader :: RdrNameHsType
132 -> P (RdrNameContext, RdrName, [RdrNameHsTyVar])
134 checkDataHeader (HsForAllTy Nothing cs t) =
135 checkSimple t [] `thenP` \(c,ts) ->
136 returnP (cs,c,map UserTyVar ts)
138 checkSimple t [] `thenP` \(c,ts) ->
139 returnP ([],c,map UserTyVar ts)
141 -- Checks the type part of the lhs of a datatype declaration
142 checkSimple :: RdrNameHsType -> [RdrName] -> P ((RdrName,[RdrName]))
143 checkSimple (HsAppTy l (HsTyVar a)) xs | isRdrTyVar a
144 = checkSimple l (a:xs)
145 checkSimple (HsTyVar tycon) xs | not (isRdrTyVar tycon) = returnP (tycon,xs)
147 checkSimple (HsOpTy (HsTyVar t1) tycon (HsTyVar t2)) []
148 | not (isRdrTyVar tycon) && isRdrTyVar t1 && isRdrTyVar t2
149 = returnP (tycon,[t1,t2])
151 checkSimple t _ = parseError "Illegal left hand side in data/newtype declaration"
153 ---------------------------------------------------------------------------
154 -- Checking statements in a do-expression
155 -- We parse do { e1 ; e2 ; }
156 -- as [ExprStmt e1, ExprStmt e2]
157 -- checkDo (a) checks that the last thing is an ExprStmt
158 -- (b) transforms it to a ResultStmt
160 checkDo [] = parseError "Empty 'do' construct"
161 checkDo [ExprStmt e l] = returnP [ResultStmt e l]
162 checkDo [s] = parseError "The last statment in a 'do' construct must be an expression"
163 checkDo (s:ss) = checkDo ss `thenP` \ ss' ->
166 ---------------------------------------------------------------------------
167 -- Checking Patterns.
169 -- We parse patterns as expressions and check for valid patterns below,
170 -- converting the expression into a pattern at the same time.
172 checkPattern :: SrcLoc -> RdrNameHsExpr -> P RdrNamePat
173 checkPattern loc e = setSrcLocP loc (checkPat e [])
175 checkPatterns :: SrcLoc -> [RdrNameHsExpr] -> P [RdrNamePat]
176 checkPatterns loc es = mapP (checkPattern loc) es
178 checkPat :: RdrNameHsExpr -> [RdrNamePat] -> P RdrNamePat
179 checkPat (HsVar c) args | isRdrDataCon c = returnP (ConPatIn c args)
180 checkPat (HsApp f x) args =
181 checkPat x [] `thenP` \x ->
183 checkPat e [] = case e of
184 EWildPat -> returnP WildPatIn
185 HsVar x -> returnP (VarPatIn x)
186 HsLit l -> returnP (LitPatIn l)
187 HsOverLit l -> returnP (NPatIn l)
188 ELazyPat e -> checkPat e [] `thenP` (returnP . LazyPatIn)
189 EAsPat n e -> checkPat e [] `thenP` (returnP . AsPatIn n)
190 ExprWithTySig e t -> checkPat e [] `thenP` \e ->
191 -- Pattern signatures are parsed as sigtypes,
192 -- but they aren't explicit forall points. Hence
193 -- we have to remove the implicit forall here.
195 HsForAllTy Nothing [] ty -> ty
198 returnP (SigPatIn e t')
200 OpApp (HsVar n) (HsVar plus) _ (HsOverLit lit@(HsIntegral k))
202 -> returnP (NPlusKPatIn n lit)
204 plus_RDR = mkUnqual varName SLIT("+") -- Hack
206 OpApp l op fix r -> checkPat l [] `thenP` \l ->
207 checkPat r [] `thenP` \r ->
209 HsVar c -> returnP (ConOpPatIn l c fix r)
212 HsPar e -> checkPat e [] `thenP` (returnP . ParPatIn)
213 ExplicitList es -> mapP (\e -> checkPat e []) es `thenP` \ps ->
214 returnP (ListPatIn ps)
216 ExplicitTuple es b -> mapP (\e -> checkPat e []) es `thenP` \ps ->
217 returnP (TuplePatIn ps b)
219 RecordCon c fs -> mapP checkPatField fs `thenP` \fs ->
220 returnP (RecPatIn c fs)
222 HsType ty -> returnP (TypePatIn ty)
225 checkPat _ _ = patFail
227 checkPatField :: (RdrName, RdrNameHsExpr, Bool)
228 -> P (RdrName, RdrNamePat, Bool)
229 checkPatField (n,e,b) =
230 checkPat e [] `thenP` \p ->
233 patFail = parseError "Parse error in pattern"
236 ---------------------------------------------------------------------------
237 -- Check Equation Syntax
241 -> Maybe RdrNameHsType
246 checkValDef lhs opt_sig grhss loc
247 = case isFunLhs lhs [] of
249 checkPatterns loc es `thenP` \ps ->
250 returnP (RdrValBinding (FunMonoBind f inf [Match [] ps opt_sig grhss] loc))
253 checkPattern loc lhs `thenP` \lhs ->
254 returnP (RdrValBinding (PatMonoBind lhs grhss loc))
261 checkValSig (HsVar v) ty loc = returnP (RdrSig (Sig v ty loc))
262 checkValSig other ty loc = parseError "Type signature given for an expression"
265 -- A variable binding is parsed as an RdrNameFunMonoBind.
266 -- See comments with HsBinds.MonoBinds
268 isFunLhs :: RdrNameHsExpr -> [RdrNameHsExpr] -> Maybe (RdrName, Bool, [RdrNameHsExpr])
269 isFunLhs (OpApp l (HsVar op) fix r) es | not (isRdrDataCon op)
270 = Just (op, True, (l:r:es))
272 = case isFunLhs l es of
273 Just (op', True, j : k : es') ->
274 Just (op', True, j : OpApp k (HsVar op) fix r : es')
276 isFunLhs (HsVar f) es | not (isRdrDataCon f)
278 isFunLhs (HsApp f e) es = isFunLhs f (e:es)
279 isFunLhs (HsPar e) es = isFunLhs e es
280 isFunLhs _ _ = Nothing
282 ---------------------------------------------------------------------------
283 -- Miscellaneous utilities
285 checkPrec :: Integer -> P ()
286 checkPrec i | 0 <= i && i <= 9 = returnP ()
287 | otherwise = parseError "precedence out of range"
291 -> RdrNameHsRecordBinds
294 mkRecConstrOrUpdate (HsVar c) fs | isRdrDataCon c
295 = returnP (RecordCon c fs)
296 mkRecConstrOrUpdate exp fs@(_:_)
297 = returnP (RecordUpd exp fs)
298 mkRecConstrOrUpdate _ _
299 = parseError "Empty record update"
301 -- Supplying the ext_name in a foreign decl is optional ; if it
302 -- isn't there, the Haskell name is assumed. Note that no transformation
303 -- of the Haskell name is then performed, so if you foreign export (++),
304 -- it's external name will be "++". Too bad; it's important because we don't
305 -- want z-encoding (e.g. names with z's in them shouldn't be doubled)
306 -- (This is why we use occNameUserString.)
308 mkExtName :: Maybe ExtName -> RdrName -> ExtName
309 mkExtName Nothing rdrNm = ExtName (_PK_ (occNameUserString (rdrNameOcc rdrNm)))
311 mkExtName (Just x) _ = x
313 -----------------------------------------------------------------------------
314 -- group function bindings into equation groups
316 -- we assume the bindings are coming in reverse order, so we take the srcloc
317 -- from the *last* binding in the group as the srcloc for the whole group.
319 groupBindings :: [RdrBinding] -> RdrBinding
320 groupBindings binds = group Nothing binds
321 where group :: Maybe RdrNameMonoBinds -> [RdrBinding] -> RdrBinding
322 group (Just bind) [] = RdrValBinding bind
323 group Nothing [] = RdrNullBind
325 -- don't group together FunMonoBinds if they have
326 -- no arguments. This is necessary now that variable bindings
327 -- with no arguments are now treated as FunMonoBinds rather
328 -- than pattern bindings (tests/rename/should_fail/rnfail002).
329 group (Just (FunMonoBind f inf1 mtchs ignore_srcloc))
330 (RdrValBinding (FunMonoBind f' _
331 [mtch@(Match _ (_:_) _ _)] loc)
333 | f == f' = group (Just (FunMonoBind f inf1 (mtch:mtchs) loc)) binds
335 group (Just so_far) binds
336 = RdrValBinding so_far `RdrAndBindings` group Nothing binds
337 group Nothing (bind:binds)
339 RdrValBinding b@(FunMonoBind _ _ _ _) -> group (Just b) binds
340 other -> bind `RdrAndBindings` group Nothing binds