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)
92 _ -> error "Should not happen, known_args out of sync?"
95 known_args :: [String]
105 "--primop-primop-info",
108 "--make-haskell-wrappers",
109 "--make-haskell-source",
113 ------------------------------------------------------------------
114 -- Code generators -----------------------------------------------
115 ------------------------------------------------------------------
117 gen_hs_source :: Info -> String
118 gen_hs_source (Info defaults entries) =
119 "-----------------------------------------------------------------------------\n"
121 ++ "-- Module : GHC.Arr\n"
123 ++ "-- Maintainer : cvs-ghc@haskell.org\n"
124 ++ "-- Stability : internal\n"
125 ++ "-- Portability : non-portable (GHC extensions)\n"
127 ++ "-- GHC\'s primitive types and operations.\n"
129 ++ "-----------------------------------------------------------------------------\n"
130 ++ "module GHC.Prim (\n"
131 ++ unlines (map (("\t" ++) . hdr) entries)
133 ++ unlines (map opt defaults) ++ "-}\n"
134 ++ unlines (map ent entries) ++ "\n\n\n"
135 where opt (OptionFalse n) = n ++ " = False"
136 opt (OptionTrue n) = n ++ " = True"
137 opt (OptionString n v) = n ++ " = { " ++ v ++ "}"
139 hdr s@(Section {}) = sec s
140 hdr (PrimOpSpec { name = n }) = wrapOp n ++ ","
141 hdr (PseudoOpSpec { name = n }) = wrapOp n ++ ","
142 hdr (PrimTypeSpec { ty = TyApp n _ }) = wrapTy n ++ ","
143 hdr (PrimTypeSpec {}) = error "Illegal type spec"
145 ent (Section {}) = ""
146 ent o@(PrimOpSpec {}) = spec o
147 ent o@(PrimTypeSpec {}) = spec o
148 ent o@(PseudoOpSpec {}) = spec o
150 sec s = "\n-- * " ++ escape (title s) ++ "\n"
151 ++ (unlines $ map ("-- " ++ ) $ lines $ unlatex $ escape $ "|" ++ desc s) ++ "\n"
153 spec o = comm ++ decl
154 where decl = case o of
155 PrimOpSpec { name = n, ty = t } -> wrapOp n ++ " :: " ++ pty t
156 PseudoOpSpec { name = n, ty = t } -> wrapOp n ++ " :: " ++ pty t
157 PrimTypeSpec { ty = t } -> "data " ++ pty t
160 comm = case (desc o) of
162 d -> "\n" ++ (unlines $ map ("-- " ++ ) $ lines $ unlatex $ escape $ "|" ++ d)
164 pty (TyF t1 t2) = pbty t1 ++ " -> " ++ pty t2
167 pbty (TyApp tc ts) = tc ++ (concat (map (' ':) (map paty ts)))
168 pbty (TyUTup ts) = "(# " ++ (concat (intersperse "," (map pty ts))) ++ " #)"
172 paty t = "(" ++ pty t ++ ")"
174 wrapOp nm | isAlpha (head nm) = nm
175 | otherwise = "(" ++ nm ++ ")"
176 wrapTy nm | isAlpha (head nm) = nm
177 | otherwise = "(" ++ nm ++ ")"
178 unlatex s = case s of
179 '\\':'t':'e':'x':'t':'t':'t':'{':cs -> markup "@" "@" cs
180 '{':'\\':'t':'t':cs -> markup "@" "@" cs
181 '{':'\\':'i':'t':cs -> markup "/" "/" cs
182 c : cs -> c : unlatex cs
184 markup s t cs = s ++ mk (dropWhile isSpace cs)
186 mk ('\n':cs) = ' ' : mk cs
187 mk ('}':cs) = t ++ unlatex cs
188 mk (c:cs) = c : mk cs
189 escape = concatMap (\c -> if c `elem` special then '\\':c:[] else c:[])
190 where special = "/'`\"@<"
192 gen_latex_doc :: Info -> String
193 gen_latex_doc (Info defaults entries)
194 = "\\primopdefaults{"
195 ++ mk_options defaults
197 ++ (concat (map mk_entry entries))
198 where mk_entry (PrimOpSpec {cons=cons,name=name,ty=ty,cat=cat,desc=desc,opts=opts}) =
200 ++ latex_encode cons ++ "}{"
201 ++ latex_encode name ++ "}{"
202 ++ latex_encode (zencode name) ++ "}{"
203 ++ latex_encode (show cat) ++ "}{"
204 ++ latex_encode (mk_source_ty ty) ++ "}{"
205 ++ latex_encode (mk_core_ty ty) ++ "}{"
209 mk_entry (Section {title=title,desc=desc}) =
211 ++ latex_encode title ++ "}{"
213 mk_entry (PrimTypeSpec {ty=ty,desc=desc,opts=opts}) =
215 ++ latex_encode (mk_source_ty ty) ++ "}{"
216 ++ latex_encode (mk_core_ty ty) ++ "}{"
220 mk_entry (PseudoOpSpec {name=name,ty=ty,desc=desc,opts=opts}) =
222 ++ latex_encode (zencode name) ++ "}{"
223 ++ latex_encode (mk_source_ty ty) ++ "}{"
224 ++ latex_encode (mk_core_ty ty) ++ "}{"
228 mk_source_ty t = pty t
229 where pty (TyF t1 t2) = pbty t1 ++ " -> " ++ pty t2
231 pbty (TyApp tc ts) = tc ++ (concat (map (' ':) (map paty ts)))
232 pbty (TyUTup ts) = "(# " ++ (concat (intersperse "," (map pty ts))) ++ " #)"
235 paty t = "(" ++ pty t ++ ")"
237 mk_core_ty t = foralls ++ (pty t)
238 where pty (TyF t1 t2) = pbty t1 ++ " -> " ++ pty t2
240 pbty (TyApp tc ts) = (zencode tc) ++ (concat (map (' ':) (map paty ts)))
241 pbty (TyUTup ts) = (zencode (utuplenm (length ts))) ++ (concat ((map (' ':) (map paty ts))))
243 paty (TyVar tv) = zencode tv
244 paty (TyApp tc []) = zencode tc
245 paty t = "(" ++ pty t ++ ")"
247 utuplenm n = "(#" ++ (replicate (n-1) ',') ++ "#)"
248 foralls = if tvars == [] then "" else "%forall " ++ (tbinds tvars)
251 tbinds ("o":tbs) = "(o::?) " ++ (tbinds tbs)
252 tbinds (tv:tbs) = tv ++ " " ++ (tbinds tbs)
253 tvars_of (TyF t1 t2) = tvars_of t1 `union` tvars_of t2
254 tvars_of (TyApp _ ts) = foldl union [] (map tvars_of ts)
255 tvars_of (TyUTup ts) = foldr union [] (map tvars_of ts)
256 tvars_of (TyVar tv) = [tv]
260 ++ mk_has_side_effects opts ++ "}{"
261 ++ mk_out_of_line opts ++ "}{"
262 ++ mk_commutable opts ++ "}{"
263 ++ mk_needs_wrapper opts ++ "}{"
264 ++ mk_can_fail opts ++ "}{"
265 ++ latex_encode (mk_strictness opts) ++ "}{"
266 ++ latex_encode (mk_usage opts)
269 mk_has_side_effects opts = mk_bool_opt opts "has_side_effects" "Has side effects." "Has no side effects."
270 mk_out_of_line opts = mk_bool_opt opts "out_of_line" "Implemented out of line." "Implemented in line."
271 mk_commutable opts = mk_bool_opt opts "commutable" "Commutable." "Not commutable."
272 mk_needs_wrapper opts = mk_bool_opt opts "needs_wrapper" "Needs wrapper." "Needs no wrapper."
273 mk_can_fail opts = mk_bool_opt opts "can_fail" "Can fail." "Cannot fail."
275 mk_bool_opt opts opt_name if_true if_false =
276 case lookup_attrib opt_name opts of
277 Just (OptionTrue _) -> if_true
278 Just (OptionFalse _) -> if_false
279 Just (OptionString _ _) -> error "String value for boolean option"
283 case lookup_attrib "strictness" opts of
284 Just (OptionString _ s) -> s -- for now
285 Just _ -> error "Boolean value for strictness"
289 case lookup_attrib "usage" opts of
290 Just (OptionString _ s) -> s -- for now
291 Just _ -> error "Boolean value for usage"
295 case maybe_tuple cs of
296 Just n -> n -- Tuples go to Z2T etc
297 Nothing -> concat (map encode_ch cs)
299 maybe_tuple "(# #)" = Just("Z1H")
300 maybe_tuple ('(' : '#' : cs) = case count_commas (0::Int) cs of
301 (n, '#' : ')' : _) -> Just ('Z' : shows (n+1) "H")
303 maybe_tuple "()" = Just("Z0T")
304 maybe_tuple ('(' : cs) = case count_commas (0::Int) cs of
305 (n, ')' : _) -> Just ('Z' : shows (n+1) "T")
307 maybe_tuple _ = Nothing
309 count_commas :: Int -> String -> (Int, String)
310 count_commas n (',' : cs) = count_commas (n+1) cs
311 count_commas n cs = (n,cs)
313 unencodedChar :: Char -> Bool -- True for chars that don't need encoding
314 unencodedChar 'Z' = False
315 unencodedChar 'z' = False
316 unencodedChar c = isAlphaNum c
318 encode_ch :: Char -> String
319 encode_ch c | unencodedChar c = [c] -- Common case first
322 encode_ch '(' = "ZL" -- Needed for things like (,), and (->)
323 encode_ch ')' = "ZR" -- For symmetry with (
343 encode_ch '\'' = "zq"
344 encode_ch '\\' = "zr"
349 encode_ch c = 'z' : shows (ord c) "U"
352 latex_encode (c:cs) | c `elem` "#$%&_^{}" = "\\" ++ c:(latex_encode cs)
353 latex_encode ('~':cs) = "\\verb!~!" ++ (latex_encode cs)
354 latex_encode ('\\':cs) = "$\\backslash$" ++ (latex_encode cs)
355 latex_encode (c:cs) = c:(latex_encode cs)
357 gen_wrappers :: Info -> String
358 gen_wrappers (Info _ entries)
359 = "{-# OPTIONS -fno-implicit-prelude #-}\n"
360 -- Dependencies on Prelude must be explicit in libraries/base, but we
361 -- don't need the Prelude here so we add -fno-implicit-prelude.
362 ++ "module GHC.PrimopWrappers where\n"
363 ++ "import qualified GHC.Prim\n"
364 ++ unlines (map f (filter (not.dodgy) (filter is_primop entries)))
366 f spec = let args = map (\n -> "a" ++ show n) [1 .. arity (ty spec)]
367 src_name = wrap (name spec)
368 in "{-# NOINLINE " ++ src_name ++ " #-}\n" ++
369 src_name ++ " " ++ unwords args
370 ++ " = (GHC.Prim." ++ name spec ++ ") " ++ unwords args
371 wrap nm | isLower (head nm) = nm
372 | otherwise = "(" ++ nm ++ ")"
376 [-- C code generator can't handle these
379 -- not interested in parallel support
380 "par#", "parGlobal#", "parLocal#", "parAt#",
381 "parAtAbs#", "parAtRel#", "parAtForNow#"
384 gen_primop_list :: Info -> String
385 gen_primop_list (Info _ entries)
387 [ " [" ++ cons first ]
389 map (\pi -> " , " ++ cons pi) rest
392 ) where (first:rest) = filter is_primop entries
394 gen_primop_tag :: Info -> String
395 gen_primop_tag (Info _ entries)
396 = unlines (max_def : zipWith f primop_entries [1 :: Int ..])
398 primop_entries = filter is_primop entries
399 f i n = "tagOf_PrimOp " ++ cons i
400 ++ " = _ILIT(" ++ show n ++ ") :: FastInt"
401 max_def = "maxPrimOpTag = " ++ show (length primop_entries) ++ " :: Int"
403 gen_data_decl :: Info -> String
404 gen_data_decl (Info _ entries)
405 = let conss = map cons (filter is_primop entries)
406 in "data PrimOp\n = " ++ head conss ++ "\n"
407 ++ unlines (map (" | "++) (tail conss))
409 gen_switch_from_attribs :: String -> String -> Info -> String
410 gen_switch_from_attribs attrib_name fn_name (Info defaults entries)
411 = let defv = lookup_attrib attrib_name defaults
412 alts = catMaybes (map mkAlt (filter is_primop entries))
414 getAltRhs (OptionFalse _) = "False"
415 getAltRhs (OptionTrue _) = "True"
416 getAltRhs (OptionString _ s) = s
419 = case lookup_attrib attrib_name (opts po) of
421 Just xx -> Just (fn_name ++ " " ++ cons po ++ " = " ++ getAltRhs xx)
425 Nothing -> error ("gen_switch_from: " ++ attrib_name)
428 ++ fn_name ++ " other = " ++ getAltRhs xx ++ "\n"
430 ------------------------------------------------------------------
431 -- Create PrimOpInfo text from PrimOpSpecs -----------------------
432 ------------------------------------------------------------------
434 gen_primop_info :: Info -> String
435 gen_primop_info (Info _ entries)
436 = unlines (map mkPOItext (filter is_primop entries))
438 mkPOItext :: Entry -> String
439 mkPOItext i = mkPOI_LHS_text i ++ mkPOI_RHS_text i
441 mkPOI_LHS_text :: Entry -> String
443 = "primOpInfo " ++ cons i ++ " = "
445 mkPOI_RHS_text :: Entry -> String
451 -> "mkCompare " ++ sl_name i ++ ppType t1
452 _ -> error "Type error in comparison op"
456 -> "mkMonadic " ++ sl_name i ++ ppType t1
457 _ -> error "Type error in monadic op"
461 -> "mkDyadic " ++ sl_name i ++ ppType t1
462 _ -> error "Type error in dyadic op"
464 -> let (argTys, resTy) = flatTys (ty i)
465 tvs = nub (tvsIn (ty i))
467 "mkGenPrimOp " ++ sl_name i ++ " "
468 ++ listify (map ppTyVar tvs) ++ " "
469 ++ listify (map ppType argTys) ++ " "
470 ++ "(" ++ ppType resTy ++ ")"
472 sl_name :: Entry -> String
473 sl_name i = "FSLIT(\"" ++ name i ++ "\") "
475 ppTyVar :: String -> String
476 ppTyVar "a" = "alphaTyVar"
477 ppTyVar "b" = "betaTyVar"
478 ppTyVar "c" = "gammaTyVar"
479 ppTyVar "s" = "deltaTyVar"
480 ppTyVar "o" = "openAlphaTyVar"
481 ppTyVar _ = error "Unknown type var"
483 ppType :: Ty -> String
484 ppType (TyApp "Bool" []) = "boolTy"
486 ppType (TyApp "Int#" []) = "intPrimTy"
487 ppType (TyApp "Int32#" []) = "int32PrimTy"
488 ppType (TyApp "Int64#" []) = "int64PrimTy"
489 ppType (TyApp "Char#" []) = "charPrimTy"
490 ppType (TyApp "Word#" []) = "wordPrimTy"
491 ppType (TyApp "Word32#" []) = "word32PrimTy"
492 ppType (TyApp "Word64#" []) = "word64PrimTy"
493 ppType (TyApp "Addr#" []) = "addrPrimTy"
494 ppType (TyApp "Float#" []) = "floatPrimTy"
495 ppType (TyApp "Double#" []) = "doublePrimTy"
496 ppType (TyApp "ByteArr#" []) = "byteArrayPrimTy"
497 ppType (TyApp "RealWorld" []) = "realWorldTy"
498 ppType (TyApp "ThreadId#" []) = "threadIdPrimTy"
499 ppType (TyApp "ForeignObj#" []) = "foreignObjPrimTy"
500 ppType (TyApp "BCO#" []) = "bcoPrimTy"
501 ppType (TyApp "()" []) = "unitTy" -- unitTy is TysWiredIn's name for ()
503 ppType (TyVar "a") = "alphaTy"
504 ppType (TyVar "b") = "betaTy"
505 ppType (TyVar "c") = "gammaTy"
506 ppType (TyVar "s") = "deltaTy"
507 ppType (TyVar "o") = "openAlphaTy"
508 ppType (TyApp "State#" [x]) = "mkStatePrimTy " ++ ppType x
509 ppType (TyApp "MutVar#" [x,y]) = "mkMutVarPrimTy " ++ ppType x
511 ppType (TyApp "MutArr#" [x,y]) = "mkMutableArrayPrimTy " ++ ppType x
514 ppType (TyApp "MutByteArr#" [x]) = "mkMutableByteArrayPrimTy "
517 ppType (TyApp "Array#" [x]) = "mkArrayPrimTy " ++ ppType x
520 ppType (TyApp "Weak#" [x]) = "mkWeakPrimTy " ++ ppType x
521 ppType (TyApp "StablePtr#" [x]) = "mkStablePtrPrimTy " ++ ppType x
522 ppType (TyApp "StableName#" [x]) = "mkStableNamePrimTy " ++ ppType x
524 ppType (TyApp "MVar#" [x,y]) = "mkMVarPrimTy " ++ ppType x
526 ppType (TyApp "TVar#" [x,y]) = "mkTVarPrimTy " ++ ppType x
528 ppType (TyUTup ts) = "(mkTupleTy Unboxed " ++ show (length ts)
530 ++ listify (map ppType ts) ++ ")"
532 ppType (TyF s d) = "(mkFunTy (" ++ ppType s ++ ") (" ++ ppType d ++ "))"
535 = error ("ppType: can't handle: " ++ show other ++ "\n")
537 listify :: [String] -> String
538 listify ss = "[" ++ concat (intersperse ", " ss) ++ "]"
540 flatTys :: Ty -> ([Ty],Ty)
541 flatTys (TyF t1 t2) = case flatTys t2 of (ts,t) -> (t1:ts,t)
542 flatTys other = ([],other)
544 tvsIn :: Ty -> [TyVar]
545 tvsIn (TyF t1 t2) = tvsIn t1 ++ tvsIn t2
546 tvsIn (TyApp _ tys) = concatMap tvsIn tys
547 tvsIn (TyVar tv) = [tv]
548 tvsIn (TyUTup tys) = concatMap tvsIn tys
551 arity = length . fst . flatTys
553 ------------------------------------------------------------------
554 -- Abstract syntax -----------------------------------------------
555 ------------------------------------------------------------------
557 -- info for all primops; the totality of the info in primops.txt(.pp)
559 = Info [Option] [Entry] -- defaults, primops
562 -- info for one primop
564 = PrimOpSpec { cons :: String, -- PrimOp name
565 name :: String, -- name in prog text
567 cat :: Category, -- category
568 desc :: String, -- description
569 opts :: [Option] } -- default overrides
570 | PseudoOpSpec { name :: String, -- name in prog text
572 desc :: String, -- description
573 opts :: [Option] } -- default overrides
574 | PrimTypeSpec { ty :: Ty, -- name in prog text
575 desc :: String, -- description
576 opts :: [Option] } -- default overrides
577 | Section { title :: String, -- section title
578 desc :: String } -- description
581 is_primop :: Entry -> Bool
582 is_primop (PrimOpSpec _ _ _ _ _ _) = True
585 -- a binding of property to value
587 = OptionFalse String -- name = False
588 | OptionTrue String -- name = True
589 | OptionString String String -- name = { ... unparsed stuff ... }
592 -- categorises primops
594 = Dyadic | Monadic | Compare | GenPrimOp
602 | TyUTup [Ty] -- unboxed tuples; just a TyCon really,
603 -- but convenient like this
610 ------------------------------------------------------------------
611 -- Sanity checking -----------------------------------------------
612 ------------------------------------------------------------------
614 {- Do some simple sanity checks:
615 * all the default field names are unique
616 * for each PrimOpSpec, all override field names are unique
617 * for each PrimOpSpec, all overriden field names
618 have a corresponding default value
619 * that primop types correspond in certain ways to the
620 Category: eg if Comparison, the type must be of the form
622 Dies with "error" if there's a problem, else returns ().
624 myseq :: () -> a -> a
627 myseqAll :: [()] -> a -> a
628 myseqAll (():ys) x = myseqAll ys x
631 sanityTop :: Info -> ()
632 sanityTop (Info defs entries)
633 = let opt_names = map get_attrib_name defs
634 primops = filter is_primop entries
636 if length opt_names /= length (nub opt_names)
637 then error ("non-unique default attribute names: " ++ show opt_names ++ "\n")
638 else myseqAll (map (sanityPrimOp opt_names) primops) ()
640 sanityPrimOp :: [String] -> Entry -> ()
641 sanityPrimOp def_names p
642 = let p_names = map get_attrib_name (opts p)
644 = length p_names == length (nub p_names)
645 && all (`elem` def_names) p_names
646 ty_ok = sane_ty (cat p) (ty p)
649 then error ("attribute names are non-unique or have no default in\n" ++
650 "info for primop " ++ cons p ++ "\n")
653 then error ("type of primop " ++ cons p ++ " doesn't make sense w.r.t" ++
654 " category " ++ show (cat p) ++ "\n")
657 sane_ty :: Category -> Ty -> Bool
658 sane_ty Compare (TyF t1 (TyF t2 td))
659 | t1 == t2 && td == TyApp "Bool" [] = True
660 sane_ty Monadic (TyF t1 td)
662 sane_ty Dyadic (TyF t1 (TyF t2 _))
663 | t1 == t2 && t2 == t2 = True
669 get_attrib_name :: Option -> String
670 get_attrib_name (OptionFalse nm) = nm
671 get_attrib_name (OptionTrue nm) = nm
672 get_attrib_name (OptionString nm _) = nm
674 lookup_attrib :: String -> [Option] -> Maybe Option
675 lookup_attrib _ [] = Nothing
676 lookup_attrib nm (a:as)
677 = if get_attrib_name a == nm then Just a else lookup_attrib nm as
679 ------------------------------------------------------------------
680 -- The parser ----------------------------------------------------
681 ------------------------------------------------------------------
684 keywords = [ "section", "primop", "pseudoop", "primtype", "with"]
686 -- Due to lack of proper lexing facilities, a hack to zap any
689 pTop = then4 (\_ ds es _ -> Info ds es)
690 pCommentAndWhitespace pDefaults (many pEntry)
691 (lit "thats_all_folks")
693 pEntry :: Parser Entry
695 = alts [pPrimOpSpec, pPrimTypeSpec, pPseudoOpSpec, pSection]
697 pSection :: Parser Entry
698 pSection = then3 (\_ n d -> Section {title = n, desc = d})
699 (lit "section") stringLiteral pDesc
701 pDefaults :: Parser [Option]
702 pDefaults = then2 sel22 (lit "defaults") (many pOption)
704 pOption :: Parser Option
707 then3 (\nm _ _ -> OptionFalse nm) pName (lit "=") (lit "False"),
708 then3 (\nm _ _ -> OptionTrue nm) pName (lit "=") (lit "True"),
709 then3 (\nm _ zz -> OptionString nm zz)
710 pName (lit "=") pStuffBetweenBraces
713 pPrimOpSpec :: Parser Entry
715 = then7 (\_ c n k t d o -> PrimOpSpec { cons = c, name = n, ty = t,
716 cat = k, desc = d, opts = o } )
717 (lit "primop") pConstructor stringLiteral
718 pCategory pType pDesc pOptions
720 pPrimTypeSpec :: Parser Entry
722 = then4 (\_ t d o -> PrimTypeSpec { ty = t, desc = d, opts = o } )
723 (lit "primtype") pType pDesc pOptions
725 pPseudoOpSpec :: Parser Entry
727 = then5 (\_ n t d o -> PseudoOpSpec { name = n, ty = t, desc = d,
729 (lit "pseudoop") stringLiteral pType pDesc pOptions
731 pOptions :: Parser [Option]
732 pOptions = optdef [] (then2 sel22 (lit "with") (many pOption))
734 pCategory :: Parser Category
737 apply (const Dyadic) (lit "Dyadic"),
738 apply (const Monadic) (lit "Monadic"),
739 apply (const Compare) (lit "Compare"),
740 apply (const GenPrimOp) (lit "GenPrimOp")
743 pDesc :: Parser String
744 pDesc = optdef "" pStuffBetweenBraces
746 pStuffBetweenBraces :: Parser String
752 return (concat ass) )
754 pInsides :: Parser String
757 stuff <- many pInsides
759 return ("{" ++ (concat stuff) ++ "}"))
761 (do c <- satisfy (/= '}')
769 pType = then2 (\t maybe_tt -> case maybe_tt of
773 (opt (then2 sel22 (lit "->") pType))
777 paT = alts [ then2 TyApp pTycon (many ppT),
779 then3 sel23 (lit "(") pType (lit ")"),
783 -- the magic bit in the middle is: T (,T)* so to speak
784 pUnboxedTupleTy :: Parser Ty
786 = then3 (\ _ ts _ -> TyUTup ts)
788 (then2 (:) pType (many (then2 sel22 (lit ",") pType)))
793 ppT = alts [apply TyVar pTyvar,
794 apply (\tc -> TyApp tc []) pTycon
797 pTyvar :: Parser String
798 pTyvar = sat (`notElem` keywords) pName
800 pTycon :: Parser String
801 pTycon = alts [pConstructor, lexeme (string "()")]
803 pName :: Parser String
804 pName = lexeme (then2 (:) lower (many isIdChar))
806 pConstructor :: Parser String
807 pConstructor = lexeme (then2 (:) upper (many isIdChar))
809 isIdChar :: Parser Char
810 isIdChar = satisfy (`elem` idChars)
813 idChars = ['a' .. 'z'] ++ ['A' .. 'Z'] ++ ['0' .. '9'] ++ "#_"
815 sat :: (a -> Bool) -> Parser a -> Parser a
822 ------------------------------------------------------------------
823 -- Helpful additions to Daan's parser stuff ----------------------
824 ------------------------------------------------------------------
826 alts :: [Parser a] -> Parser a
829 alts (p1:p2:ps) = (try p1) <|> alts (p2:ps)
831 then2 :: (a -> b -> c) -> Parser a -> Parser b -> Parser c
833 = do x1 <- p1 ; x2 <- p2 ; return (f x1 x2)
835 then3 :: (a -> b -> c -> d) -> Parser a -> Parser b -> Parser c -> Parser d
837 = do x1 <- p1 ; x2 <- p2 ; x3 <- p3 ; return (f x1 x2 x3)
839 then4 :: (a -> b -> c -> d -> e) -> Parser a -> Parser b -> Parser c -> Parser d -> Parser e
841 = do x1 <- p1 ; x2 <- p2 ; x3 <- p3 ; x4 <- p4 ; return (f x1 x2 x3 x4)
843 then5 :: (a -> b -> c -> d -> e -> f) -> Parser a -> Parser b -> Parser c -> Parser d -> Parser e -> Parser f
844 then5 f p1 p2 p3 p4 p5
845 = do x1 <- p1 ; x2 <- p2 ; x3 <- p3 ; x4 <- p4 ; x5 <- p5
846 return (f x1 x2 x3 x4 x5)
848 then6 :: (a -> b -> c -> d -> e -> f -> g) -> Parser a -> Parser b -> Parser c -> Parser d -> Parser e -> Parser f -> Parser g
849 then6 f p1 p2 p3 p4 p5 p6
850 = do x1 <- p1 ; x2 <- p2 ; x3 <- p3 ; x4 <- p4 ; x5 <- p5 ; x6 <- p6
851 return (f x1 x2 x3 x4 x5 x6)
853 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
854 then7 f p1 p2 p3 p4 p5 p6 p7
855 = do x1 <- p1 ; x2 <- p2 ; x3 <- p3 ; x4 <- p4 ; x5 <- p5 ; x6 <- p6 ; x7 <- p7
856 return (f x1 x2 x3 x4 x5 x6 x7)
858 opt :: Parser a -> Parser (Maybe a)
860 = (do x <- p; return (Just x)) <|> return Nothing
862 optdef :: a -> Parser a -> Parser a
864 = (do x <- p; return x) <|> return d
872 sel23 :: a -> b -> c -> b
875 apply :: (a -> b) -> Parser a -> Parser b
876 apply f p = liftM f p
878 -- Hacks for zapping whitespace and comments, unfortunately needed
879 -- because Daan won't let us have a lexer before the parser :-(
880 lexeme :: Parser p -> Parser p
881 lexeme p = then2 sel12 p pCommentAndWhitespace
883 lit :: String -> Parser ()
884 lit s = apply (const ()) (lexeme (string s))
886 pCommentAndWhitespace :: Parser ()
887 pCommentAndWhitespace
888 = apply (const ()) (many (alts [pLineComment,
889 apply (const ()) (satisfy isSpace)]))
893 pLineComment :: Parser ()
895 = try (then3 (\_ _ _ -> ()) (string "--") (many (satisfy (/= '\n'))) (char '\n'))
897 stringLiteral :: Parser String
898 stringLiteral = lexeme (
899 do { between (char '"')
900 (char '"' <?> "end of string")
903 <?> "literal string")
905 ------------------------------------------------------------------
907 ------------------------------------------------------------------