2 % (c) The GRASP/AQUA Project, Glasgow University, 1999
4 \section[ParseUtil]{Parser Utilities}
8 parseError -- String -> Pa
10 , splitForConApp -- RdrNameHsType -> [RdrNameBangType]
11 -- -> P (RdrName, [RdrNameBangType])
13 , mkRecConstrOrUpdate -- HsExp -> [HsFieldUpdate] -> P HsExp
16 , mkExtName -- Maybe ExtName -> RdrName -> ExtName
18 , checkPrec -- String -> P String
19 , checkContext -- HsType -> P HsContext
20 , checkInstType -- HsType -> P HsType
21 , checkDataHeader -- HsQualType -> P (HsContext,HsName,[HsName])
22 , checkSimple -- HsType -> [HsName] -> P ((HsName,[HsName]))
23 , checkPattern -- HsExp -> P HsPat
24 , checkPatterns -- [HsExp] -> P [HsPat]
25 -- , checkExpr -- HsExp -> P HsExp
26 , checkValDef -- (SrcLoc, HsExp, HsRhs, [HsDecl]) -> P HsDecl
27 , checkValSig -- (SrcLoc, HsExp, HsRhs, [HsDecl]) -> P HsDecl
30 #include "HsVersions.h"
33 import HsSyn -- Lots of it
35 import RdrHsSyn ( mkNPlusKPatIn, unitTyCon_RDR,
37 RdrNameHsType, RdrNameBangType, RdrNameContext,
38 RdrNameHsTyVar, RdrNamePat, RdrNameHsExpr, RdrNameGRHSs,
39 RdrNameHsRecordBinds, RdrNameMonoBinds
43 import OccName ( dataName, varName, tcClsName,
44 occNameSpace, setOccNameSpace, occNameUserString )
45 import FastString ( unpackFS )
46 import UniqFM ( UniqFM, listToUFM, lookupUFM )
49 -----------------------------------------------------------------------------
52 parseError :: String -> P a
54 getSrcLocP `thenP` \ loc ->
55 failMsgP (hcat [ppr loc, text ": ", text s])
57 cbot = panic "CCall:result_ty"
59 -----------------------------------------------------------------------------
62 -- When parsing data declarations, we sometimes inadvertently parse
63 -- a constructor application as a type (eg. in data T a b = C a b `D` E a b)
64 -- This function splits up the type application, adds any pending
65 -- arguments, and converts the type constructor back into a data constructor.
67 splitForConApp :: RdrNameHsType -> [RdrNameBangType]
68 -> P (RdrName, [RdrNameBangType])
70 splitForConApp t ts = split t ts
72 split (HsAppTy t u) ts = split t (Unbanged u : ts)
74 split (HsTyVar t) ts =
75 -- check that we've got a type constructor at the head
76 if occNameSpace t_occ /= tcClsName
78 (showSDoc (text "not a constructor: `" <>
80 else returnP (con, ts)
81 where t_occ = rdrNameOcc t
82 con = setRdrNameOcc t (setOccNameSpace t_occ dataName)
84 split _ _ = parseError "Illegal data/newtype declaration"
86 ----------------------------------------------------------------------------
87 -- Various Syntactic Checks
89 callConvFM :: UniqFM CallConv
90 callConvFM = listToUFM $
91 map (\ (x,y) -> (_PK_ x,y))
92 [ ("stdcall", stdCallConv),
94 -- ("pascal", pascalCallConv),
95 -- ("fastcall", fastCallConv)
98 checkCallConv :: FAST_STRING -> P CallConv
100 case lookupUFM callConvFM s of
101 Nothing -> parseError ("unknown calling convention: `"
102 ++ unpackFS s ++ "'")
103 Just conv -> returnP conv
105 checkInstType :: RdrNameHsType -> P RdrNameHsType
108 HsForAllTy tvs ctxt ty ->
109 checkDictTy ty [] `thenP` \ dict_ty ->
110 returnP (HsForAllTy tvs ctxt dict_ty)
112 ty -> checkDictTy ty [] `thenP` \ dict_ty->
113 returnP (HsForAllTy Nothing [] dict_ty)
115 checkContext :: RdrNameHsType -> P RdrNameContext
116 checkContext (HsTupleTy _ ts)
117 = mapP (\t -> checkPred t []) ts `thenP` \ps ->
119 checkContext (HsTyVar t) -- empty contexts are allowed
120 | t == unitTyCon_RDR = returnP []
122 = checkPred t [] `thenP` \p ->
125 checkPred :: RdrNameHsType -> [RdrNameHsType]
126 -> P (HsPred RdrName)
127 checkPred (HsTyVar t) args@(_:_) | not (isRdrTyVar t)
128 = returnP (HsPClass t args)
129 checkPred (HsAppTy l r) args = checkPred l (r:args)
130 checkPred (HsPredTy (HsPIParam n ty)) [] = returnP (HsPIParam n ty)
131 checkPred _ _ = parseError "Illegal class assertion"
133 checkDictTy :: RdrNameHsType -> [RdrNameHsType] -> P RdrNameHsType
134 checkDictTy (HsTyVar t) args@(_:_) | not (isRdrTyVar t)
135 = returnP (mkHsDictTy t args)
136 checkDictTy (HsAppTy l r) args = checkDictTy l (r:args)
137 checkDictTy _ _ = parseError "Illegal class assertion"
139 checkDataHeader :: RdrNameHsType
140 -> P (RdrNameContext, RdrName, [RdrNameHsTyVar])
141 checkDataHeader (HsForAllTy Nothing cs t) =
142 checkSimple t [] `thenP` \(c,ts) ->
143 returnP (cs,c,map UserTyVar ts)
145 checkSimple t [] `thenP` \(c,ts) ->
146 returnP ([],c,map UserTyVar ts)
148 checkSimple :: RdrNameHsType -> [RdrName] -> P ((RdrName,[RdrName]))
149 checkSimple (HsAppTy l (HsTyVar a)) xs | isRdrTyVar a
150 = checkSimple l (a:xs)
151 checkSimple (HsTyVar t) xs | not (isRdrTyVar t) = returnP (t,xs)
152 checkSimple t _ = trace (showSDoc (ppr t)) $ parseError "Illegal data/newtype declaration"
154 ---------------------------------------------------------------------------
155 -- Checking Patterns.
157 -- We parse patterns as expressions and check for valid patterns below,
158 -- nverting the expression into a pattern at the same time.
160 checkPattern :: RdrNameHsExpr -> P RdrNamePat
161 checkPattern e = checkPat e []
163 checkPatterns :: [RdrNameHsExpr] -> P [RdrNamePat]
164 checkPatterns es = mapP checkPattern es
166 checkPat :: RdrNameHsExpr -> [RdrNamePat] -> P RdrNamePat
167 checkPat (HsVar c) args | isRdrDataCon c = returnP (ConPatIn c args)
168 checkPat (HsApp f x) args =
169 checkPat x [] `thenP` \x ->
171 checkPat e [] = case e of
172 EWildPat -> returnP WildPatIn
173 HsVar x -> returnP (VarPatIn x)
174 HsLit l -> returnP (LitPatIn l)
175 HsOverLit l -> returnP (NPatIn l)
176 ELazyPat e -> checkPat e [] `thenP` (returnP . LazyPatIn)
177 EAsPat n e -> checkPat e [] `thenP` (returnP . AsPatIn n)
178 ExprWithTySig e t -> checkPat e [] `thenP` \e ->
179 -- Pattern signatures are parsed as sigtypes,
180 -- but they aren't explicit forall points. Hence
181 -- we have to remove the implicit forall here.
183 HsForAllTy Nothing [] ty -> ty
186 returnP (SigPatIn e t')
188 OpApp (HsVar n) (HsVar plus) _ (HsOverLit lit@(HsIntegral k _))
190 -> returnP (mkNPlusKPatIn n lit)
192 OpApp l op fix r -> checkPat l [] `thenP` \l ->
193 checkPat r [] `thenP` \r ->
195 HsVar c -> returnP (ConOpPatIn l c fix r)
198 HsPar e -> checkPat e [] `thenP` (returnP . ParPatIn)
199 ExplicitList es -> mapP (\e -> checkPat e []) es `thenP` \ps ->
200 returnP (ListPatIn ps)
202 ExplicitTuple es b -> mapP (\e -> checkPat e []) es `thenP` \ps ->
203 returnP (TuplePatIn ps b)
205 RecordCon c fs -> mapP checkPatField fs `thenP` \fs ->
206 returnP (RecPatIn c fs)
209 checkPat _ _ = patFail
211 checkPatField :: (RdrName, RdrNameHsExpr, Bool)
212 -> P (RdrName, RdrNamePat, Bool)
213 checkPatField (n,e,b) =
214 checkPat e [] `thenP` \p ->
217 patFail = parseError "Parse error in pattern"
220 ---------------------------------------------------------------------------
221 -- Check Equation Syntax
225 -> Maybe RdrNameHsType
230 checkValDef lhs opt_sig grhss loc
231 = case isFunLhs lhs [] of
233 checkPatterns es `thenP` \ps ->
234 returnP (RdrValBinding (FunMonoBind f inf [Match [] ps opt_sig grhss] loc))
237 checkPattern lhs `thenP` \lhs ->
238 returnP (RdrValBinding (PatMonoBind lhs grhss loc))
245 checkValSig (HsVar v) ty loc = returnP (RdrSig (Sig v ty loc))
246 checkValSig other ty loc = parseError "Type signature given for an expression"
249 -- A variable binding is parsed as an RdrNameFunMonoBind.
250 -- See comments with HsBinds.MonoBinds
252 isFunLhs (OpApp l (HsVar op) fix r) es | not (isRdrDataCon op)
253 = Just (op, True, (l:r:es))
254 isFunLhs (HsVar f) es | not (isRdrDataCon f)
256 isFunLhs (HsApp f e) es = isFunLhs f (e:es)
257 isFunLhs (HsPar e) es = isFunLhs e es
258 isFunLhs _ _ = Nothing
260 ---------------------------------------------------------------------------
261 -- Miscellaneous utilities
263 checkPrec :: Integer -> P ()
264 checkPrec i | 0 <= i && i <= 9 = returnP ()
265 | otherwise = parseError "precedence out of range"
269 -> RdrNameHsRecordBinds
272 mkRecConstrOrUpdate (HsVar c) fs | isRdrDataCon c
273 = returnP (RecordCon c fs)
274 mkRecConstrOrUpdate exp fs@(_:_)
275 = returnP (RecordUpd exp fs)
276 mkRecConstrOrUpdate _ _
277 = parseError "Empty record update"
279 -- Supplying the ext_name in a foreign decl is optional ; if it
280 -- isn't there, the Haskell name is assumed. Note that no transformation
281 -- of the Haskell name is then performed, so if you foreign export (++),
282 -- it's external name will be "++". Too bad; it's important because we don't
283 -- want z-encoding (e.g. names with z's in them shouldn't be doubled)
284 -- (This is why we use occNameUserString.)
285 mkExtName :: Maybe ExtName -> RdrName -> ExtName
286 mkExtName Nothing rdrNm = ExtName (_PK_ (occNameUserString (rdrNameOcc rdrNm)))
288 mkExtName (Just x) _ = x
290 -----------------------------------------------------------------------------
291 -- group function bindings into equation groups
293 -- we assume the bindings are coming in reverse order, so we take the srcloc
294 -- from the *last* binding in the group as the srcloc for the whole group.
296 groupBindings :: [RdrBinding] -> RdrBinding
297 groupBindings binds = group Nothing binds
298 where group :: Maybe RdrNameMonoBinds -> [RdrBinding] -> RdrBinding
299 group (Just bind) [] = RdrValBinding bind
300 group Nothing [] = RdrNullBind
302 -- don't group together FunMonoBinds if they have
303 -- no arguments. This is necessary now that variable bindings
304 -- with no arguments are now treated as FunMonoBinds rather
305 -- than pattern bindings (tests/rename/should_fail/rnfail002).
306 group (Just (FunMonoBind f inf1 mtchs ignore_srcloc))
307 (RdrValBinding (FunMonoBind f' _
308 [mtch@(Match _ (_:_) _ _)] loc)
310 | f == f' = group (Just (FunMonoBind f inf1 (mtch:mtchs) loc)) binds
312 group (Just so_far) binds
313 = RdrValBinding so_far `RdrAndBindings` group Nothing binds
314 group Nothing (bind:binds)
316 RdrValBinding b@(FunMonoBind _ _ _ _) -> group (Just b) binds
317 other -> bind `RdrAndBindings` group Nothing binds
319 plus_RDR = mkSrcUnqual varName SLIT("+")