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_source_ty t = pty t
214 where pty (TyF t1 t2) = pbty t1 ++ " -> " ++ pty t2
216 pbty (TyApp tc ts) = tc ++ (concat (map (' ':) (map paty ts)))
217 pbty (TyUTup ts) = "(# " ++ (concat (intersperse "," (map pty ts))) ++ " #)"
220 paty t = "(" ++ pty t ++ ")"
222 mk_core_ty t = foralls ++ (pty t)
223 where pty (TyF t1 t2) = pbty t1 ++ " -> " ++ pty t2
225 pbty (TyApp tc ts) = (zencode tc) ++ (concat (map (' ':) (map paty ts)))
226 pbty (TyUTup ts) = (zencode (utuplenm (length ts))) ++ (concat ((map (' ':) (map paty ts))))
228 paty (TyVar tv) = zencode tv
229 paty (TyApp tc []) = zencode tc
230 paty t = "(" ++ pty t ++ ")"
232 utuplenm n = "(#" ++ (replicate (n-1) ',') ++ "#)"
233 foralls = if tvars == [] then "" else "%forall " ++ (tbinds tvars)
236 tbinds ("o":tbs) = "(o::?) " ++ (tbinds tbs)
237 tbinds (tv:tbs) = tv ++ " " ++ (tbinds tbs)
238 tvars_of (TyF t1 t2) = tvars_of t1 `union` tvars_of t2
239 tvars_of (TyApp _ ts) = foldl union [] (map tvars_of ts)
240 tvars_of (TyUTup ts) = foldr union [] (map tvars_of ts)
241 tvars_of (TyVar tv) = [tv]
245 ++ mk_has_side_effects opts ++ "}{"
246 ++ mk_out_of_line opts ++ "}{"
247 ++ mk_commutable opts ++ "}{"
248 ++ mk_needs_wrapper opts ++ "}{"
249 ++ mk_can_fail opts ++ "}{"
250 ++ latex_encode (mk_strictness opts) ++ "}{"
251 ++ latex_encode (mk_usage opts)
254 mk_has_side_effects opts = mk_bool_opt opts "has_side_effects" "Has side effects." "Has no side effects."
255 mk_out_of_line opts = mk_bool_opt opts "out_of_line" "Implemented out of line." "Implemented in line."
256 mk_commutable opts = mk_bool_opt opts "commutable" "Commutable." "Not commutable."
257 mk_needs_wrapper opts = mk_bool_opt opts "needs_wrapper" "Needs wrapper." "Needs no wrapper."
258 mk_can_fail opts = mk_bool_opt opts "can_fail" "Can fail." "Cannot fail."
260 mk_bool_opt opts opt_name if_true if_false =
261 case lookup_attrib opt_name opts of
262 Just (OptionTrue _) -> if_true
263 Just (OptionFalse _) -> if_false
264 Just (OptionString _ _) -> error "String value for boolean option"
268 case lookup_attrib "strictness" opts of
269 Just (OptionString _ s) -> s -- for now
270 Just _ -> error "Boolean value for strictness"
274 case lookup_attrib "usage" opts of
275 Just (OptionString _ s) -> s -- for now
276 Just _ -> error "Boolean value for usage"
280 case maybe_tuple cs of
281 Just n -> n -- Tuples go to Z2T etc
282 Nothing -> concat (map encode_ch cs)
284 maybe_tuple "(# #)" = Just("Z1H")
285 maybe_tuple ('(' : '#' : cs) = case count_commas (0::Int) cs of
286 (n, '#' : ')' : _) -> Just ('Z' : shows (n+1) "H")
288 maybe_tuple "()" = Just("Z0T")
289 maybe_tuple ('(' : cs) = case count_commas (0::Int) cs of
290 (n, ')' : _) -> Just ('Z' : shows (n+1) "T")
292 maybe_tuple _ = Nothing
294 count_commas :: Int -> String -> (Int, String)
295 count_commas n (',' : cs) = count_commas (n+1) cs
296 count_commas n cs = (n,cs)
298 unencodedChar :: Char -> Bool -- True for chars that don't need encoding
299 unencodedChar 'Z' = False
300 unencodedChar 'z' = False
301 unencodedChar c = isAlphaNum c
303 encode_ch :: Char -> String
304 encode_ch c | unencodedChar c = [c] -- Common case first
307 encode_ch '(' = "ZL" -- Needed for things like (,), and (->)
308 encode_ch ')' = "ZR" -- For symmetry with (
328 encode_ch '\'' = "zq"
329 encode_ch '\\' = "zr"
334 encode_ch c = 'z' : shows (ord c) "U"
337 latex_encode (c:cs) | c `elem` "#$%&_^{}" = "\\" ++ c:(latex_encode cs)
338 latex_encode ('~':cs) = "\\verb!~!" ++ (latex_encode cs)
339 latex_encode ('\\':cs) = "$\\backslash$" ++ (latex_encode cs)
340 latex_encode (c:cs) = c:(latex_encode cs)
342 gen_wrappers :: Info -> String
343 gen_wrappers (Info _ entries)
344 = "{-# OPTIONS -fno-implicit-prelude #-}\n"
345 -- Dependencies on Prelude must be explicit in libraries/base, but we
346 -- don't need the Prelude here so we add -fno-implicit-prelude.
347 ++ "module GHC.PrimopWrappers where\n"
348 ++ "import qualified GHC.Prim\n"
349 ++ unlines (map f (filter (not.dodgy) (filter is_primop entries)))
351 f spec = let args = map (\n -> "a" ++ show n) [1 .. arity (ty spec)]
352 src_name = wrap (name spec)
353 in "{-# NOINLINE " ++ src_name ++ " #-}\n" ++
354 src_name ++ " " ++ unwords args
355 ++ " = (GHC.Prim." ++ name spec ++ ") " ++ unwords args
356 wrap nm | isLower (head nm) = nm
357 | otherwise = "(" ++ nm ++ ")"
361 [-- C code generator can't handle these
364 -- not interested in parallel support
365 "par#", "parGlobal#", "parLocal#", "parAt#",
366 "parAtAbs#", "parAtRel#", "parAtForNow#"
369 gen_primop_list :: Info -> String
370 gen_primop_list (Info _ entries)
372 [ " [" ++ cons first ]
374 map (\pi -> " , " ++ cons pi) rest
377 ) where (first:rest) = filter is_primop entries
379 gen_primop_tag :: Info -> String
380 gen_primop_tag (Info _ entries)
381 = unlines (max_def : zipWith f primop_entries [1 :: Int ..])
383 primop_entries = filter is_primop entries
384 f i n = "tagOf_PrimOp " ++ cons i
385 ++ " = _ILIT(" ++ show n ++ ") :: FastInt"
386 max_def = "maxPrimOpTag = " ++ show (length primop_entries) ++ " :: Int"
388 gen_data_decl :: Info -> String
389 gen_data_decl (Info _ entries)
390 = let conss = map cons (filter is_primop entries)
391 in "data PrimOp\n = " ++ head conss ++ "\n"
392 ++ unlines (map (" | "++) (tail conss))
394 gen_switch_from_attribs :: String -> String -> Info -> String
395 gen_switch_from_attribs attrib_name fn_name (Info defaults entries)
396 = let defv = lookup_attrib attrib_name defaults
397 alts = catMaybes (map mkAlt (filter is_primop entries))
399 getAltRhs (OptionFalse _) = "False"
400 getAltRhs (OptionTrue _) = "True"
401 getAltRhs (OptionString _ s) = s
404 = case lookup_attrib attrib_name (opts po) of
406 Just xx -> Just (fn_name ++ " " ++ cons po ++ " = " ++ getAltRhs xx)
410 Nothing -> error ("gen_switch_from: " ++ attrib_name)
413 ++ fn_name ++ " other = " ++ getAltRhs xx ++ "\n"
415 ------------------------------------------------------------------
416 -- Create PrimOpInfo text from PrimOpSpecs -----------------------
417 ------------------------------------------------------------------
419 gen_primop_info :: Info -> String
420 gen_primop_info (Info _ entries)
421 = unlines (map mkPOItext (filter is_primop entries))
423 mkPOItext :: Entry -> String
424 mkPOItext i = mkPOI_LHS_text i ++ mkPOI_RHS_text i
426 mkPOI_LHS_text :: Entry -> String
428 = "primOpInfo " ++ cons i ++ " = "
430 mkPOI_RHS_text :: Entry -> String
436 -> "mkCompare " ++ sl_name i ++ ppType t1
437 _ -> error "Type error in comparison op"
441 -> "mkMonadic " ++ sl_name i ++ ppType t1
442 _ -> error "Type error in monadic op"
446 -> "mkDyadic " ++ sl_name i ++ ppType t1
447 _ -> error "Type error in dyadic op"
449 -> let (argTys, resTy) = flatTys (ty i)
450 tvs = nub (tvsIn (ty i))
452 "mkGenPrimOp " ++ sl_name i ++ " "
453 ++ listify (map ppTyVar tvs) ++ " "
454 ++ listify (map ppType argTys) ++ " "
455 ++ "(" ++ ppType resTy ++ ")"
457 sl_name :: Entry -> String
458 sl_name i = "FSLIT(\"" ++ name i ++ "\") "
460 ppTyVar :: String -> String
461 ppTyVar "a" = "alphaTyVar"
462 ppTyVar "b" = "betaTyVar"
463 ppTyVar "c" = "gammaTyVar"
464 ppTyVar "s" = "deltaTyVar"
465 ppTyVar "o" = "openAlphaTyVar"
466 ppTyVar _ = error "Unknown type var"
468 ppType :: Ty -> String
469 ppType (TyApp "Bool" []) = "boolTy"
471 ppType (TyApp "Int#" []) = "intPrimTy"
472 ppType (TyApp "Int32#" []) = "int32PrimTy"
473 ppType (TyApp "Int64#" []) = "int64PrimTy"
474 ppType (TyApp "Char#" []) = "charPrimTy"
475 ppType (TyApp "Word#" []) = "wordPrimTy"
476 ppType (TyApp "Word32#" []) = "word32PrimTy"
477 ppType (TyApp "Word64#" []) = "word64PrimTy"
478 ppType (TyApp "Addr#" []) = "addrPrimTy"
479 ppType (TyApp "Float#" []) = "floatPrimTy"
480 ppType (TyApp "Double#" []) = "doublePrimTy"
481 ppType (TyApp "ByteArr#" []) = "byteArrayPrimTy"
482 ppType (TyApp "RealWorld" []) = "realWorldTy"
483 ppType (TyApp "ThreadId#" []) = "threadIdPrimTy"
484 ppType (TyApp "ForeignObj#" []) = "foreignObjPrimTy"
485 ppType (TyApp "BCO#" []) = "bcoPrimTy"
486 ppType (TyApp "()" []) = "unitTy" -- unitTy is TysWiredIn's name for ()
488 ppType (TyVar "a") = "alphaTy"
489 ppType (TyVar "b") = "betaTy"
490 ppType (TyVar "c") = "gammaTy"
491 ppType (TyVar "s") = "deltaTy"
492 ppType (TyVar "o") = "openAlphaTy"
493 ppType (TyApp "State#" [x]) = "mkStatePrimTy " ++ ppType x
494 ppType (TyApp "MutVar#" [x,y]) = "mkMutVarPrimTy " ++ ppType x
496 ppType (TyApp "MutArr#" [x,y]) = "mkMutableArrayPrimTy " ++ ppType x
499 ppType (TyApp "MutByteArr#" [x]) = "mkMutableByteArrayPrimTy "
502 ppType (TyApp "Array#" [x]) = "mkArrayPrimTy " ++ ppType x
505 ppType (TyApp "Weak#" [x]) = "mkWeakPrimTy " ++ ppType x
506 ppType (TyApp "StablePtr#" [x]) = "mkStablePtrPrimTy " ++ ppType x
507 ppType (TyApp "StableName#" [x]) = "mkStableNamePrimTy " ++ ppType x
509 ppType (TyApp "MVar#" [x,y]) = "mkMVarPrimTy " ++ ppType x
511 ppType (TyApp "TVar#" [x,y]) = "mkTVarPrimTy " ++ ppType x
513 ppType (TyUTup ts) = "(mkTupleTy Unboxed " ++ show (length ts)
515 ++ listify (map ppType ts) ++ ")"
517 ppType (TyF s d) = "(mkFunTy (" ++ ppType s ++ ") (" ++ ppType d ++ "))"
520 = error ("ppType: can't handle: " ++ show other ++ "\n")
522 listify :: [String] -> String
523 listify ss = "[" ++ concat (intersperse ", " ss) ++ "]"
525 flatTys :: Ty -> ([Ty],Ty)
526 flatTys (TyF t1 t2) = case flatTys t2 of (ts,t) -> (t1:ts,t)
527 flatTys other = ([],other)
529 tvsIn :: Ty -> [TyVar]
530 tvsIn (TyF t1 t2) = tvsIn t1 ++ tvsIn t2
531 tvsIn (TyApp _ tys) = concatMap tvsIn tys
532 tvsIn (TyVar tv) = [tv]
533 tvsIn (TyUTup tys) = concatMap tvsIn tys
536 arity = length . fst . flatTys
538 ------------------------------------------------------------------
539 -- Abstract syntax -----------------------------------------------
540 ------------------------------------------------------------------
542 -- info for all primops; the totality of the info in primops.txt(.pp)
544 = Info [Option] [Entry] -- defaults, primops
547 -- info for one primop
549 = PrimOpSpec { cons :: String, -- PrimOp name
550 name :: String, -- name in prog text
552 cat :: Category, -- category
553 desc :: String, -- description
554 opts :: [Option] } -- default overrides
555 | PseudoOpSpec { name :: String, -- name in prog text
557 desc :: String, -- description
558 opts :: [Option] } -- default overrides
559 | PrimTypeSpec { ty :: Ty, -- name in prog text
560 desc :: String, -- description
561 opts :: [Option] } -- default overrides
562 | Section { title :: String, -- section title
563 desc :: String } -- description
566 is_primop :: Entry -> Bool
567 is_primop (PrimOpSpec _ _ _ _ _ _) = True
570 -- a binding of property to value
572 = OptionFalse String -- name = False
573 | OptionTrue String -- name = True
574 | OptionString String String -- name = { ... unparsed stuff ... }
577 -- categorises primops
579 = Dyadic | Monadic | Compare | GenPrimOp
587 | TyUTup [Ty] -- unboxed tuples; just a TyCon really,
588 -- but convenient like this
595 ------------------------------------------------------------------
596 -- Sanity checking -----------------------------------------------
597 ------------------------------------------------------------------
599 {- Do some simple sanity checks:
600 * all the default field names are unique
601 * for each PrimOpSpec, all override field names are unique
602 * for each PrimOpSpec, all overriden field names
603 have a corresponding default value
604 * that primop types correspond in certain ways to the
605 Category: eg if Comparison, the type must be of the form
607 Dies with "error" if there's a problem, else returns ().
609 myseq :: () -> a -> a
612 myseqAll :: [()] -> a -> a
613 myseqAll (():ys) x = myseqAll ys x
616 sanityTop :: Info -> ()
617 sanityTop (Info defs entries)
618 = let opt_names = map get_attrib_name defs
619 primops = filter is_primop entries
621 if length opt_names /= length (nub opt_names)
622 then error ("non-unique default attribute names: " ++ show opt_names ++ "\n")
623 else myseqAll (map (sanityPrimOp opt_names) primops) ()
625 sanityPrimOp :: [String] -> Entry -> ()
626 sanityPrimOp def_names p
627 = let p_names = map get_attrib_name (opts p)
629 = length p_names == length (nub p_names)
630 && all (`elem` def_names) p_names
631 ty_ok = sane_ty (cat p) (ty p)
634 then error ("attribute names are non-unique or have no default in\n" ++
635 "info for primop " ++ cons p ++ "\n")
638 then error ("type of primop " ++ cons p ++ " doesn't make sense w.r.t" ++
639 " category " ++ show (cat p) ++ "\n")
642 sane_ty :: Category -> Ty -> Bool
643 sane_ty Compare (TyF t1 (TyF t2 td))
644 | t1 == t2 && td == TyApp "Bool" [] = True
645 sane_ty Monadic (TyF t1 td)
647 sane_ty Dyadic (TyF t1 (TyF t2 _))
648 | t1 == t2 && t2 == t2 = True
654 get_attrib_name :: Option -> String
655 get_attrib_name (OptionFalse nm) = nm
656 get_attrib_name (OptionTrue nm) = nm
657 get_attrib_name (OptionString nm _) = nm
659 lookup_attrib :: String -> [Option] -> Maybe Option
660 lookup_attrib _ [] = Nothing
661 lookup_attrib nm (a:as)
662 = if get_attrib_name a == nm then Just a else lookup_attrib nm as
664 ------------------------------------------------------------------
665 -- The parser ----------------------------------------------------
666 ------------------------------------------------------------------
669 keywords = [ "section", "primop", "pseudoop", "primtype", "with"]
671 -- Due to lack of proper lexing facilities, a hack to zap any
674 pTop = then4 (\_ ds es _ -> Info ds es)
675 pCommentAndWhitespace pDefaults (many pEntry)
676 (lit "thats_all_folks")
678 pEntry :: Parser Entry
680 = alts [pPrimOpSpec, pPrimTypeSpec, pPseudoOpSpec, pSection]
682 pSection :: Parser Entry
683 pSection = then3 (\_ n d -> Section {title = n, desc = d})
684 (lit "section") stringLiteral pDesc
686 pDefaults :: Parser [Option]
687 pDefaults = then2 sel22 (lit "defaults") (many pOption)
689 pOption :: Parser Option
692 then3 (\nm _ _ -> OptionFalse nm) pName (lit "=") (lit "False"),
693 then3 (\nm _ _ -> OptionTrue nm) pName (lit "=") (lit "True"),
694 then3 (\nm _ zz -> OptionString nm zz)
695 pName (lit "=") pStuffBetweenBraces
698 pPrimOpSpec :: Parser Entry
700 = then7 (\_ c n k t d o -> PrimOpSpec { cons = c, name = n, ty = t,
701 cat = k, desc = d, opts = o } )
702 (lit "primop") pConstructor stringLiteral
703 pCategory pType pDesc pOptions
705 pPrimTypeSpec :: Parser Entry
707 = then4 (\_ t d o -> PrimTypeSpec { ty = t, desc = d, opts = o } )
708 (lit "primtype") pType pDesc pOptions
710 pPseudoOpSpec :: Parser Entry
712 = then5 (\_ n t d o -> PseudoOpSpec { name = n, ty = t, desc = d,
714 (lit "pseudoop") stringLiteral pType pDesc pOptions
716 pOptions :: Parser [Option]
717 pOptions = optdef [] (then2 sel22 (lit "with") (many pOption))
719 pCategory :: Parser Category
722 apply (const Dyadic) (lit "Dyadic"),
723 apply (const Monadic) (lit "Monadic"),
724 apply (const Compare) (lit "Compare"),
725 apply (const GenPrimOp) (lit "GenPrimOp")
728 pDesc :: Parser String
729 pDesc = optdef "" pStuffBetweenBraces
731 pStuffBetweenBraces :: Parser String
737 return (concat ass) )
739 pInsides :: Parser String
742 stuff <- many pInsides
744 return ("{" ++ (concat stuff) ++ "}"))
746 (do c <- satisfy (/= '}')
754 pType = then2 (\t maybe_tt -> case maybe_tt of
758 (opt (then2 sel22 (lit "->") pType))
762 paT = alts [ then2 TyApp pTycon (many ppT),
764 then3 sel23 (lit "(") pType (lit ")"),
768 -- the magic bit in the middle is: T (,T)* so to speak
769 pUnboxedTupleTy :: Parser Ty
771 = then3 (\ _ ts _ -> TyUTup ts)
773 (then2 (:) pType (many (then2 sel22 (lit ",") pType)))
778 ppT = alts [apply TyVar pTyvar,
779 apply (\tc -> TyApp tc []) pTycon
782 pTyvar :: Parser String
783 pTyvar = sat (`notElem` keywords) pName
785 pTycon :: Parser String
786 pTycon = alts [pConstructor, lexeme (string "()")]
788 pName :: Parser String
789 pName = lexeme (then2 (:) lower (many isIdChar))
791 pConstructor :: Parser String
792 pConstructor = lexeme (then2 (:) upper (many isIdChar))
794 isIdChar :: Parser Char
795 isIdChar = satisfy (`elem` idChars)
798 idChars = ['a' .. 'z'] ++ ['A' .. 'Z'] ++ ['0' .. '9'] ++ "#_"
800 sat :: (a -> Bool) -> Parser a -> Parser a
807 ------------------------------------------------------------------
808 -- Helpful additions to Daan's parser stuff ----------------------
809 ------------------------------------------------------------------
811 alts :: [Parser a] -> Parser a
814 alts (p1:p2:ps) = (try p1) <|> alts (p2:ps)
816 then2 :: (a -> b -> c) -> Parser a -> Parser b -> Parser c
818 = do x1 <- p1 ; x2 <- p2 ; return (f x1 x2)
820 then3 :: (a -> b -> c -> d) -> Parser a -> Parser b -> Parser c -> Parser d
822 = do x1 <- p1 ; x2 <- p2 ; x3 <- p3 ; return (f x1 x2 x3)
824 then4 :: (a -> b -> c -> d -> e) -> Parser a -> Parser b -> Parser c -> Parser d -> Parser e
826 = do x1 <- p1 ; x2 <- p2 ; x3 <- p3 ; x4 <- p4 ; return (f x1 x2 x3 x4)
828 then5 :: (a -> b -> c -> d -> e -> f) -> Parser a -> Parser b -> Parser c -> Parser d -> Parser e -> Parser f
829 then5 f p1 p2 p3 p4 p5
830 = do x1 <- p1 ; x2 <- p2 ; x3 <- p3 ; x4 <- p4 ; x5 <- p5
831 return (f x1 x2 x3 x4 x5)
833 then6 :: (a -> b -> c -> d -> e -> f -> g) -> Parser a -> Parser b -> Parser c -> Parser d -> Parser e -> Parser f -> Parser g
834 then6 f p1 p2 p3 p4 p5 p6
835 = do x1 <- p1 ; x2 <- p2 ; x3 <- p3 ; x4 <- p4 ; x5 <- p5 ; x6 <- p6
836 return (f x1 x2 x3 x4 x5 x6)
838 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
839 then7 f p1 p2 p3 p4 p5 p6 p7
840 = do x1 <- p1 ; x2 <- p2 ; x3 <- p3 ; x4 <- p4 ; x5 <- p5 ; x6 <- p6 ; x7 <- p7
841 return (f x1 x2 x3 x4 x5 x6 x7)
843 opt :: Parser a -> Parser (Maybe a)
845 = (do x <- p; return (Just x)) <|> return Nothing
847 optdef :: a -> Parser a -> Parser a
849 = (do x <- p; return x) <|> return d
857 sel23 :: a -> b -> c -> b
860 apply :: (a -> b) -> Parser a -> Parser b
861 apply f p = liftM f p
863 -- Hacks for zapping whitespace and comments, unfortunately needed
864 -- because Daan won't let us have a lexer before the parser :-(
865 lexeme :: Parser p -> Parser p
866 lexeme p = then2 sel12 p pCommentAndWhitespace
868 lit :: String -> Parser ()
869 lit s = apply (const ()) (lexeme (string s))
871 pCommentAndWhitespace :: Parser ()
872 pCommentAndWhitespace
873 = apply (const ()) (many (alts [pLineComment,
874 apply (const ()) (satisfy isSpace)]))
878 pLineComment :: Parser ()
880 = try (then3 (\_ _ _ -> ()) (string "--") (many (satisfy (/= '\n'))) (char '\n'))
882 stringLiteral :: Parser String
883 stringLiteral = lexeme (
884 do { between (char '"')
885 (char '"' <?> "end of string")
888 <?> "literal string")
890 ------------------------------------------------------------------
892 ------------------------------------------------------------------