2 ------------------------------------------------------------------
3 -- A primop-table mangling program --
4 ------------------------------------------------------------------
8 #if __GLASGOW_HASKELL__ >= 504
9 import Text.ParserCombinators.Parsec
17 import System ( getArgs )
18 import Maybe ( catMaybes )
21 main = getArgs >>= \args ->
22 if length args /= 1 || head args `notElem` known_args
23 then error ("usage: genprimopcode command < primops.txt > ...\n"
24 ++ " where command is one of\n"
25 ++ unlines (map (" "++) known_args)
29 let pres = parse pTop "" s
31 Left err -> error ("parse error at " ++ (show err))
33 -> myseq (sanityTop p_o_specs) (
37 -> putStr (gen_data_decl p_o_specs)
40 -> putStr (gen_switch_from_attribs
42 "primOpHasSideEffects" p_o_specs)
45 -> putStr (gen_switch_from_attribs
47 "primOpOutOfLine" p_o_specs)
50 -> putStr (gen_switch_from_attribs
52 "commutableOp" p_o_specs)
55 -> putStr (gen_switch_from_attribs
57 "primOpNeedsWrapper" p_o_specs)
60 -> putStr (gen_switch_from_attribs
62 "primOpCanFail" p_o_specs)
65 -> putStr (gen_switch_from_attribs
67 "primOpStrictness" p_o_specs)
70 -> putStr (gen_switch_from_attribs
72 "primOpUsg" p_o_specs)
74 "--primop-primop-info"
75 -> putStr (gen_primop_info p_o_specs)
78 -> putStr (gen_primop_tag p_o_specs)
81 -> putStr (gen_primop_list p_o_specs)
83 "--make-haskell-wrappers"
84 -> putStr (gen_wrappers p_o_specs)
86 "--make-haskell-source"
87 -> putStr (gen_hs_source p_o_specs)
90 -> putStr (gen_latex_doc p_o_specs)
93 known_args :: [String]
103 "--primop-primop-info",
106 "--make-haskell-wrappers",
107 "--make-haskell-source",
111 ------------------------------------------------------------------
112 -- Code generators -----------------------------------------------
113 ------------------------------------------------------------------
115 gen_hs_source :: Info -> String
116 gen_hs_source (Info defaults entries) =
117 "-----------------------------------------------------------------------------\n"
119 ++ "-- Module : GHC.Arr\n"
121 ++ "-- Maintainer : cvs-ghc@haskell.org\n"
122 ++ "-- Stability : internal\n"
123 ++ "-- Portability : non-portable (GHC extensions)\n"
125 ++ "-- GHC\'s primitive types and operations.\n"
127 ++ "-----------------------------------------------------------------------------\n"
128 ++ "module GHC.Prim (\n"
129 ++ unlines (map (("\t" ++) . hdr) entries)
131 ++ unlines (map opt defaults) ++ "-}\n"
132 ++ unlines (map ent entries) ++ "\n\n\n"
133 where opt (OptionFalse n) = n ++ " = False"
134 opt (OptionTrue n) = n ++ " = True"
135 opt (OptionString n v) = n ++ " = { " ++ v ++ "}"
137 hdr s@(Section {}) = sec s
138 hdr (PrimOpSpec { name = n }) = wrapOp n ++ ","
139 hdr (PseudoOpSpec { name = n }) = wrapOp n ++ ","
140 hdr (PrimTypeSpec { ty = TyApp n _ }) = wrapTy n ++ ","
142 ent s@(Section {}) = ""
143 ent o@(PrimOpSpec {}) = spec o
144 ent o@(PrimTypeSpec {}) = spec o
145 ent o@(PseudoOpSpec {}) = spec o
147 sec s = "\n-- * " ++ escape (title s) ++ "\n"
148 ++ (unlines $ map ("-- " ++ ) $ lines $ unlatex $ escape $ "|" ++ desc s) ++ "\n"
150 spec o = comm ++ decl
151 where decl = case o of
152 PrimOpSpec { name = n, ty = t } -> wrapOp n ++ " :: " ++ pty t
153 PseudoOpSpec { name = n, ty = t } -> wrapOp n ++ " :: " ++ pty t
154 PrimTypeSpec { ty = t } -> "data " ++ pty t
156 comm = case (desc o) of
158 d -> "\n" ++ (unlines $ map ("-- " ++ ) $ lines $ unlatex $ escape $ "|" ++ d)
160 pty (TyF t1 t2) = pbty t1 ++ " -> " ++ pty t2
163 pbty (TyApp tc ts) = tc ++ (concat (map (' ':) (map paty ts)))
164 pbty (TyUTup ts) = "(# " ++ (concat (intersperse "," (map pty ts))) ++ " #)"
168 paty t = "(" ++ pty t ++ ")"
170 wrapOp nm | isAlpha (head nm) = nm
171 | otherwise = "(" ++ nm ++ ")"
172 wrapTy nm | isAlpha (head nm) = nm
173 | otherwise = "(" ++ nm ++ ")"
174 unlatex s = case s of
175 '\\':'t':'e':'x':'t':'t':'t':'{':cs -> markup "@" "@" cs
176 '{':'\\':'t':'t':cs -> markup "@" "@" cs
177 '{':'\\':'i':'t':cs -> markup "/" "/" cs
178 c : cs -> c : unlatex cs
180 markup s t cs = s ++ mk (dropWhile isSpace cs)
182 mk ('\n':cs) = ' ' : mk cs
183 mk ('}':cs) = t ++ unlatex cs
184 mk (c:cs) = c : mk cs
185 escape = concatMap (\c -> if c `elem` special then '\\':c:[] else c:[])
186 where special = "/'`\"@<"
188 gen_latex_doc :: Info -> String
189 gen_latex_doc (Info defaults entries)
190 = "\\primopdefaults{"
191 ++ mk_options defaults
193 ++ (concat (map mk_entry entries))
194 where mk_entry (PrimOpSpec {cons=cons,name=name,ty=ty,cat=cat,desc=desc,opts=opts}) =
196 ++ latex_encode cons ++ "}{"
197 ++ latex_encode name ++ "}{"
198 ++ latex_encode (zencode name) ++ "}{"
199 ++ latex_encode (show cat) ++ "}{"
200 ++ latex_encode (mk_source_ty ty) ++ "}{"
201 ++ latex_encode (mk_core_ty ty) ++ "}{"
205 mk_entry (Section {title=title,desc=desc}) =
207 ++ latex_encode title ++ "}{"
209 mk_source_ty t = pty t
210 where pty (TyF t1 t2) = pbty t1 ++ " -> " ++ pty t2
212 pbty (TyApp tc ts) = tc ++ (concat (map (' ':) (map paty ts)))
213 pbty (TyUTup ts) = "(# " ++ (concat (intersperse "," (map pty ts))) ++ " #)"
216 paty t = "(" ++ pty t ++ ")"
218 mk_core_ty t = foralls ++ (pty t)
219 where pty (TyF t1 t2) = pbty t1 ++ " -> " ++ pty t2
221 pbty (TyApp tc ts) = (zencode tc) ++ (concat (map (' ':) (map paty ts)))
222 pbty (TyUTup ts) = (zencode (utuplenm (length ts))) ++ (concat ((map (' ':) (map paty ts))))
224 paty (TyVar tv) = zencode tv
225 paty (TyApp tc []) = zencode tc
226 paty t = "(" ++ pty t ++ ")"
228 utuplenm n = "(#" ++ (replicate (n-1) ',') ++ "#)"
229 foralls = if tvars == [] then "" else "%forall " ++ (tbinds tvars)
232 tbinds ("o":tbs) = "(o::?) " ++ (tbinds tbs)
233 tbinds (tv:tbs) = tv ++ " " ++ (tbinds tbs)
234 tvars_of (TyF t1 t2) = tvars_of t1 `union` tvars_of t2
235 tvars_of (TyApp tc ts) = foldl union [] (map tvars_of ts)
236 tvars_of (TyUTup ts) = foldr union [] (map tvars_of ts)
237 tvars_of (TyVar tv) = [tv]
241 ++ mk_has_side_effects opts ++ "}{"
242 ++ mk_out_of_line opts ++ "}{"
243 ++ mk_commutable opts ++ "}{"
244 ++ mk_needs_wrapper opts ++ "}{"
245 ++ mk_can_fail opts ++ "}{"
246 ++ latex_encode (mk_strictness opts) ++ "}{"
247 ++ latex_encode (mk_usage opts)
250 mk_has_side_effects opts = mk_bool_opt opts "has_side_effects" "Has side effects." "Has no side effects."
251 mk_out_of_line opts = mk_bool_opt opts "out_of_line" "Implemented out of line." "Implemented in line."
252 mk_commutable opts = mk_bool_opt opts "commutable" "Commutable." "Not commutable."
253 mk_needs_wrapper opts = mk_bool_opt opts "needs_wrapper" "Needs wrapper." "Needs no wrapper."
254 mk_can_fail opts = mk_bool_opt opts "can_fail" "Can fail." "Cannot fail."
256 mk_bool_opt opts opt_name if_true if_false =
257 case lookup_attrib opt_name opts of
258 Just (OptionTrue _) -> if_true
259 Just (OptionFalse _) -> if_false
263 case lookup_attrib "strictness" opts of
264 Just (OptionString _ s) -> s -- for now
268 case lookup_attrib "usage" opts of
269 Just (OptionString _ s) -> s -- for now
273 case maybe_tuple cs of
274 Just n -> n -- Tuples go to Z2T etc
275 Nothing -> concat (map encode_ch cs)
277 maybe_tuple "(# #)" = Just("Z1H")
278 maybe_tuple ('(' : '#' : cs) = case count_commas (0::Int) cs of
279 (n, '#' : ')' : cs) -> Just ('Z' : shows (n+1) "H")
281 maybe_tuple "()" = Just("Z0T")
282 maybe_tuple ('(' : cs) = case count_commas (0::Int) cs of
283 (n, ')' : cs) -> Just ('Z' : shows (n+1) "T")
285 maybe_tuple other = Nothing
287 count_commas :: Int -> String -> (Int, String)
288 count_commas n (',' : cs) = count_commas (n+1) cs
289 count_commas n cs = (n,cs)
291 unencodedChar :: Char -> Bool -- True for chars that don't need encoding
292 unencodedChar 'Z' = False
293 unencodedChar 'z' = False
294 unencodedChar c = isAlphaNum c
296 encode_ch :: Char -> String
297 encode_ch c | unencodedChar c = [c] -- Common case first
300 encode_ch '(' = "ZL" -- Needed for things like (,), and (->)
301 encode_ch ')' = "ZR" -- For symmetry with (
321 encode_ch '\'' = "zq"
322 encode_ch '\\' = "zr"
327 encode_ch c = 'z' : shows (ord c) "U"
330 latex_encode (c:cs) | c `elem` "#$%&_^{}" = "\\" ++ c:(latex_encode cs)
331 latex_encode ('~':cs) = "\\verb!~!" ++ (latex_encode cs)
332 latex_encode ('\\':cs) = "$\\backslash$" ++ (latex_encode cs)
333 latex_encode (c:cs) = c:(latex_encode cs)
335 gen_wrappers :: Info -> String
336 gen_wrappers (Info defaults entries)
337 = "{-# OPTIONS -fno-implicit-prelude #-}\n"
338 -- Dependencies on Prelude must be explicit in libraries/base, but we
339 -- don't need the Prelude here so we add -fno-implicit-prelude.
340 ++ "module GHC.PrimopWrappers where\n"
341 ++ "import qualified GHC.Prim\n"
342 ++ unlines (map f (filter (not.dodgy) (filter is_primop entries)))
344 f spec = let args = map (\n -> "a" ++ show n) [1 .. arity (ty spec)]
345 src_name = wrap (name spec)
346 in "{-# NOINLINE " ++ src_name ++ " #-}\n" ++
347 src_name ++ " " ++ unwords args
348 ++ " = (GHC.Prim." ++ name spec ++ ") " ++ unwords args
349 wrap nm | isLower (head nm) = nm
350 | otherwise = "(" ++ nm ++ ")"
354 [-- C code generator can't handle these
357 -- not interested in parallel support
358 "par#", "parGlobal#", "parLocal#", "parAt#",
359 "parAtAbs#", "parAtRel#", "parAtForNow#"
362 gen_primop_list :: Info -> String
363 gen_primop_list (Info defaults entries)
365 [ " [" ++ cons first ]
367 map (\pi -> " , " ++ cons pi) rest
370 ) where (first:rest) = filter is_primop entries
372 gen_primop_tag :: Info -> String
373 gen_primop_tag (Info defaults entries)
374 = unlines (max_def : zipWith f primop_entries [1..])
376 primop_entries = filter is_primop entries
377 f i n = "tagOf_PrimOp " ++ cons i
378 ++ " = _ILIT(" ++ show n ++ ") :: FastInt"
379 max_def = "maxPrimOpTag = " ++ show (length primop_entries) ++ " :: Int"
381 gen_data_decl :: Info -> String
382 gen_data_decl (Info defaults entries)
383 = let conss = map cons (filter is_primop entries)
384 in "data PrimOp\n = " ++ head conss ++ "\n"
385 ++ unlines (map (" | "++) (tail conss))
387 gen_switch_from_attribs :: String -> String -> Info -> String
388 gen_switch_from_attribs attrib_name fn_name (Info defaults entries)
389 = let defv = lookup_attrib attrib_name defaults
390 alts = catMaybes (map mkAlt (filter is_primop entries))
392 getAltRhs (OptionFalse _) = "False"
393 getAltRhs (OptionTrue _) = "True"
394 getAltRhs (OptionString _ s) = s
397 = case lookup_attrib attrib_name (opts po) of
399 Just xx -> Just (fn_name ++ " " ++ cons po ++ " = " ++ getAltRhs xx)
403 Nothing -> error ("gen_switch_from: " ++ attrib_name)
406 ++ fn_name ++ " other = " ++ getAltRhs xx ++ "\n"
408 ------------------------------------------------------------------
409 -- Create PrimOpInfo text from PrimOpSpecs -----------------------
410 ------------------------------------------------------------------
412 gen_primop_info :: Info -> String
413 gen_primop_info (Info defaults entries)
414 = unlines (map mkPOItext (filter is_primop entries))
416 mkPOItext :: Entry -> String
417 mkPOItext i = mkPOI_LHS_text i ++ mkPOI_RHS_text i
419 mkPOI_LHS_text :: Entry -> String
421 = "primOpInfo " ++ cons i ++ " = "
423 mkPOI_RHS_text :: Entry -> String
429 -> "mkCompare " ++ sl_name i ++ ppType t1
433 -> "mkMonadic " ++ sl_name i ++ ppType t1
437 -> "mkDyadic " ++ sl_name i ++ ppType t1
439 -> let (argTys, resTy) = flatTys (ty i)
440 tvs = nub (tvsIn (ty i))
442 "mkGenPrimOp " ++ sl_name i ++ " "
443 ++ listify (map ppTyVar tvs) ++ " "
444 ++ listify (map ppType argTys) ++ " "
445 ++ "(" ++ ppType resTy ++ ")"
447 sl_name :: Entry -> String
448 sl_name i = "FSLIT(\"" ++ name i ++ "\") "
450 ppTyVar :: String -> String
451 ppTyVar "a" = "alphaTyVar"
452 ppTyVar "b" = "betaTyVar"
453 ppTyVar "c" = "gammaTyVar"
454 ppTyVar "s" = "deltaTyVar"
455 ppTyVar "o" = "openAlphaTyVar"
457 ppType :: Ty -> String
458 ppType (TyApp "Bool" []) = "boolTy"
460 ppType (TyApp "Int#" []) = "intPrimTy"
461 ppType (TyApp "Int32#" []) = "int32PrimTy"
462 ppType (TyApp "Int64#" []) = "int64PrimTy"
463 ppType (TyApp "Char#" []) = "charPrimTy"
464 ppType (TyApp "Word#" []) = "wordPrimTy"
465 ppType (TyApp "Word32#" []) = "word32PrimTy"
466 ppType (TyApp "Word64#" []) = "word64PrimTy"
467 ppType (TyApp "Addr#" []) = "addrPrimTy"
468 ppType (TyApp "Float#" []) = "floatPrimTy"
469 ppType (TyApp "Double#" []) = "doublePrimTy"
470 ppType (TyApp "ByteArr#" []) = "byteArrayPrimTy"
471 ppType (TyApp "RealWorld" []) = "realWorldTy"
472 ppType (TyApp "ThreadId#" []) = "threadIdPrimTy"
473 ppType (TyApp "ForeignObj#" []) = "foreignObjPrimTy"
474 ppType (TyApp "BCO#" []) = "bcoPrimTy"
475 ppType (TyApp "()" []) = "unitTy" -- unitTy is TysWiredIn's name for ()
477 ppType (TyVar "a") = "alphaTy"
478 ppType (TyVar "b") = "betaTy"
479 ppType (TyVar "c") = "gammaTy"
480 ppType (TyVar "s") = "deltaTy"
481 ppType (TyVar "o") = "openAlphaTy"
482 ppType (TyApp "State#" [x]) = "mkStatePrimTy " ++ ppType x
483 ppType (TyApp "MutVar#" [x,y]) = "mkMutVarPrimTy " ++ ppType x
485 ppType (TyApp "MutArr#" [x,y]) = "mkMutableArrayPrimTy " ++ ppType x
488 ppType (TyApp "MutByteArr#" [x]) = "mkMutableByteArrayPrimTy "
491 ppType (TyApp "Array#" [x]) = "mkArrayPrimTy " ++ ppType x
494 ppType (TyApp "Weak#" [x]) = "mkWeakPrimTy " ++ ppType x
495 ppType (TyApp "StablePtr#" [x]) = "mkStablePtrPrimTy " ++ ppType x
496 ppType (TyApp "StableName#" [x]) = "mkStableNamePrimTy " ++ ppType x
498 ppType (TyApp "MVar#" [x,y]) = "mkMVarPrimTy " ++ ppType x
500 ppType (TyApp "TVar#" [x,y]) = "mkTVarPrimTy " ++ ppType x
502 ppType (TyUTup ts) = "(mkTupleTy Unboxed " ++ show (length ts)
504 ++ listify (map ppType ts) ++ ")"
506 ppType (TyF s d) = "(mkFunTy (" ++ ppType s ++ ") (" ++ ppType d ++ "))"
509 = error ("ppType: can't handle: " ++ show other ++ "\n")
511 listify :: [String] -> String
512 listify ss = "[" ++ concat (intersperse ", " ss) ++ "]"
514 flatTys :: Ty -> ([Ty],Ty)
515 flatTys (TyF t1 t2) = case flatTys t2 of (ts,t) -> (t1:ts,t)
516 flatTys other = ([],other)
518 tvsIn :: Ty -> [TyVar]
519 tvsIn (TyF t1 t2) = tvsIn t1 ++ tvsIn t2
520 tvsIn (TyApp tc tys) = concatMap tvsIn tys
521 tvsIn (TyVar tv) = [tv]
522 tvsIn (TyUTup tys) = concatMap tvsIn tys
525 arity = length . fst . flatTys
527 ------------------------------------------------------------------
528 -- Abstract syntax -----------------------------------------------
529 ------------------------------------------------------------------
531 -- info for all primops; the totality of the info in primops.txt(.pp)
533 = Info [Option] [Entry] -- defaults, primops
536 -- info for one primop
538 = PrimOpSpec { cons :: String, -- PrimOp name
539 name :: String, -- name in prog text
541 cat :: Category, -- category
542 desc :: String, -- description
543 opts :: [Option] } -- default overrides
544 | PseudoOpSpec { name :: String, -- name in prog text
546 desc :: String, -- description
547 opts :: [Option] } -- default overrides
548 | PrimTypeSpec { ty :: Ty, -- name in prog text
549 desc :: String, -- description
550 opts :: [Option] } -- default overrides
551 | Section { title :: String, -- section title
552 desc :: String } -- description
555 is_primop :: Entry -> Bool
556 is_primop (PrimOpSpec _ _ _ _ _ _) = True
559 -- a binding of property to value
561 = OptionFalse String -- name = False
562 | OptionTrue String -- name = True
563 | OptionString String String -- name = { ... unparsed stuff ... }
566 -- categorises primops
568 = Dyadic | Monadic | Compare | GenPrimOp
576 | TyUTup [Ty] -- unboxed tuples; just a TyCon really,
577 -- but convenient like this
584 ------------------------------------------------------------------
585 -- Sanity checking -----------------------------------------------
586 ------------------------------------------------------------------
588 {- Do some simple sanity checks:
589 * all the default field names are unique
590 * for each PrimOpSpec, all override field names are unique
591 * for each PrimOpSpec, all overriden field names
592 have a corresponding default value
593 * that primop types correspond in certain ways to the
594 Category: eg if Comparison, the type must be of the form
596 Dies with "error" if there's a problem, else returns ().
598 myseq :: () -> a -> a
601 myseqAll :: [()] -> a -> a
602 myseqAll (():ys) x = myseqAll ys x
605 sanityTop :: Info -> ()
606 sanityTop (Info defs entries)
607 = let opt_names = map get_attrib_name defs
608 primops = filter is_primop entries
610 if length opt_names /= length (nub opt_names)
611 then error ("non-unique default attribute names: " ++ show opt_names ++ "\n")
612 else myseqAll (map (sanityPrimOp opt_names) primops) ()
614 sanityPrimOp :: [String] -> Entry -> ()
615 sanityPrimOp def_names p
616 = let p_names = map get_attrib_name (opts p)
618 = length p_names == length (nub p_names)
619 && all (`elem` def_names) p_names
620 ty_ok = sane_ty (cat p) (ty p)
623 then error ("attribute names are non-unique or have no default in\n" ++
624 "info for primop " ++ cons p ++ "\n")
627 then error ("type of primop " ++ cons p ++ " doesn't make sense w.r.t" ++
628 " category " ++ show (cat p) ++ "\n")
631 sane_ty :: Category -> Ty -> Bool
632 sane_ty Compare (TyF t1 (TyF t2 td))
633 | t1 == t2 && td == TyApp "Bool" [] = True
634 sane_ty Monadic (TyF t1 td)
636 sane_ty Dyadic (TyF t1 (TyF t2 td))
637 | t1 == t2 && t2 == t2 = True
638 sane_ty GenPrimOp any_old_thing
643 get_attrib_name :: Option -> String
644 get_attrib_name (OptionFalse nm) = nm
645 get_attrib_name (OptionTrue nm) = nm
646 get_attrib_name (OptionString nm _) = nm
648 lookup_attrib :: String -> [Option] -> Maybe Option
649 lookup_attrib nm [] = Nothing
650 lookup_attrib nm (a:as)
651 = if get_attrib_name a == nm then Just a else lookup_attrib nm as
653 ------------------------------------------------------------------
654 -- The parser ----------------------------------------------------
655 ------------------------------------------------------------------
658 keywords = [ "section", "primop", "pseudoop", "primtype", "with"]
660 -- Due to lack of proper lexing facilities, a hack to zap any
663 pTop = then4 (\_ ds es _ -> Info ds es)
664 pCommentAndWhitespace pDefaults (many pEntry)
665 (lit "thats_all_folks")
667 pEntry :: Parser Entry
669 = alts [pPrimOpSpec, pPrimTypeSpec, pPseudoOpSpec, pSection]
671 pSection :: Parser Entry
672 pSection = then3 (\_ n d -> Section {title = n, desc = d})
673 (lit "section") stringLiteral pDesc
675 pDefaults :: Parser [Option]
676 pDefaults = then2 sel22 (lit "defaults") (many pOption)
678 pOption :: Parser Option
681 then3 (\nm eq ff -> OptionFalse nm) pName (lit "=") (lit "False"),
682 then3 (\nm eq tt -> OptionTrue nm) pName (lit "=") (lit "True"),
683 then3 (\nm eq zz -> OptionString nm zz)
684 pName (lit "=") pStuffBetweenBraces
687 pPrimOpSpec :: Parser Entry
689 = then7 (\_ c n k t d o -> PrimOpSpec { cons = c, name = n, ty = t,
690 cat = k, desc = d, opts = o } )
691 (lit "primop") pConstructor stringLiteral
692 pCategory pType pDesc pOptions
694 pPrimTypeSpec :: Parser Entry
696 = then4 (\_ t d o -> PrimTypeSpec { ty = t, desc = d, opts = o } )
697 (lit "primtype") pType pDesc pOptions
699 pPseudoOpSpec :: Parser Entry
701 = then5 (\_ n t d o -> PseudoOpSpec { name = n, ty = t, desc = d,
703 (lit "pseudoop") stringLiteral pType pDesc pOptions
705 pOptions :: Parser [Option]
706 pOptions = optdef [] (then2 sel22 (lit "with") (many pOption))
708 pCategory :: Parser Category
711 apply (const Dyadic) (lit "Dyadic"),
712 apply (const Monadic) (lit "Monadic"),
713 apply (const Compare) (lit "Compare"),
714 apply (const GenPrimOp) (lit "GenPrimOp")
717 pDesc :: Parser String
718 pDesc = optdef "" pStuffBetweenBraces
720 pStuffBetweenBraces :: Parser String
726 return (concat ass) )
728 pInsides :: Parser String
731 stuff <- many pInsides
733 return ("{" ++ (concat stuff) ++ "}"))
735 (do c <- satisfy (/= '}')
743 pType = then2 (\t maybe_tt -> case maybe_tt of
747 (opt (then2 sel22 (lit "->") pType))
751 paT = alts [ then2 TyApp pTycon (many ppT),
753 then3 sel23 (lit "(") pType (lit ")"),
757 -- the magic bit in the middle is: T (,T)* so to speak
758 pUnboxedTupleTy :: Parser Ty
760 = then3 (\ _ ts _ -> TyUTup ts)
762 (then2 (:) pType (many (then2 sel22 (lit ",") pType)))
767 ppT = alts [apply TyVar pTyvar,
768 apply (\tc -> TyApp tc []) pTycon
771 pTyvar :: Parser String
772 pTyvar = sat (`notElem` keywords) pName
774 pTycon :: Parser String
775 pTycon = alts [pConstructor, lexeme (string "()")]
777 pName :: Parser String
778 pName = lexeme (then2 (:) lower (many isIdChar))
780 pConstructor :: Parser String
781 pConstructor = lexeme (then2 (:) upper (many isIdChar))
783 isIdChar :: Parser Char
784 isIdChar = satisfy (`elem` idChars)
787 idChars = ['a' .. 'z'] ++ ['A' .. 'Z'] ++ ['0' .. '9'] ++ "#_"
789 sat :: (a -> Bool) -> Parser a -> Parser a
796 ------------------------------------------------------------------
797 -- Helpful additions to Daan's parser stuff ----------------------
798 ------------------------------------------------------------------
800 alts :: [Parser a] -> Parser a
802 alts (p1:p2:ps) = (try p1) <|> alts (p2:ps)
804 then2 :: (a -> b -> c) -> Parser a -> Parser b -> Parser c
806 = do x1 <- p1 ; x2 <- p2 ; return (f x1 x2)
808 then3 :: (a -> b -> c -> d) -> Parser a -> Parser b -> Parser c -> Parser d
810 = do x1 <- p1 ; x2 <- p2 ; x3 <- p3 ; return (f x1 x2 x3)
812 then4 :: (a -> b -> c -> d -> e) -> Parser a -> Parser b -> Parser c -> Parser d -> Parser e
814 = do x1 <- p1 ; x2 <- p2 ; x3 <- p3 ; x4 <- p4 ; return (f x1 x2 x3 x4)
816 then5 :: (a -> b -> c -> d -> e -> f) -> Parser a -> Parser b -> Parser c -> Parser d -> Parser e -> Parser f
817 then5 f p1 p2 p3 p4 p5
818 = do x1 <- p1 ; x2 <- p2 ; x3 <- p3 ; x4 <- p4 ; x5 <- p5
819 return (f x1 x2 x3 x4 x5)
821 then6 :: (a -> b -> c -> d -> e -> f -> g) -> Parser a -> Parser b -> Parser c -> Parser d -> Parser e -> Parser f -> Parser g
822 then6 f p1 p2 p3 p4 p5 p6
823 = do x1 <- p1 ; x2 <- p2 ; x3 <- p3 ; x4 <- p4 ; x5 <- p5 ; x6 <- p6
824 return (f x1 x2 x3 x4 x5 x6)
826 then7 :: (a -> b -> c -> d -> e -> f -> g -> h) -> Parser a -> Parser b -> Parser c -> Parser d -> Parser e -> Parser f -> Parser g -> Parser h
827 then7 f p1 p2 p3 p4 p5 p6 p7
828 = do x1 <- p1 ; x2 <- p2 ; x3 <- p3 ; x4 <- p4 ; x5 <- p5 ; x6 <- p6 ; x7 <- p7
829 return (f x1 x2 x3 x4 x5 x6 x7)
831 opt :: Parser a -> Parser (Maybe a)
833 = (do x <- p; return (Just x)) <|> return Nothing
835 optdef :: a -> Parser a -> Parser a
837 = (do x <- p; return x) <|> return d
845 sel23 :: a -> b -> c -> b
848 apply :: (a -> b) -> Parser a -> Parser b
849 apply f p = liftM f p
851 -- Hacks for zapping whitespace and comments, unfortunately needed
852 -- because Daan won't let us have a lexer before the parser :-(
853 lexeme :: Parser p -> Parser p
854 lexeme p = then2 sel12 p pCommentAndWhitespace
856 lit :: String -> Parser ()
857 lit s = apply (const ()) (lexeme (string s))
859 pCommentAndWhitespace :: Parser ()
860 pCommentAndWhitespace
861 = apply (const ()) (many (alts [pLineComment,
862 apply (const ()) (satisfy isSpace)]))
866 pLineComment :: Parser ()
868 = try (then3 (\_ _ _ -> ()) (string "--") (many (satisfy (/= '\n'))) (char '\n'))
870 stringLiteral :: Parser String
871 stringLiteral = lexeme (
872 do { between (char '"')
873 (char '"' <?> "end of string")
876 <?> "literal string")
878 ------------------------------------------------------------------
880 ------------------------------------------------------------------