@@ -159,40 +159,61 @@ printForeign sigMap = do
159159 SI. hPutStrLn h $ " module " ++ moduleName ++ " where"
160160 SI. hPutStrLn h " "
161161 SI. hPutStrLn h " import Foreign.C.Types"
162+ SI. hPutStrLn h " import Foreign.Marshal.Error ( throwIf )"
162163 SI. hPutStrLn h " import Foreign.Ptr"
164+ SI. hPutStrLn h $ " import " ++ moduleNameFor [" GetProcAddress" ] ++ " ( getProcAddress )"
163165 SI. hPutStrLn h $ " import " ++ moduleNameFor [" Types" ]
164166 SI. hPutStrLn h " import Numeric.Fixed"
165167 SI. hPutStrLn h " import Numeric.Half"
166168 SI. hPutStrLn h " "
169+ SI. hPutStrLn h " getCommand :: String -> IO (FunPtr a)"
170+ SI. hPutStrLn h " getCommand cmd ="
171+ SI. hPutStrLn h " throwIfNullFunPtr (\" unknown OpenGL command \" ++ cmd) $ getProcAddress cmd"
172+ SI. hPutStrLn h " where throwIfNullFunPtr :: String -> IO (FunPtr a) -> IO (FunPtr a)"
173+ SI. hPutStrLn h " throwIfNullFunPtr = throwIf (== nullFunPtr) . const"
174+ SI. hPutStrLn h " "
167175 mapM_ (SI. hPutStrLn h . uncurry makeImportDynamic) (M. assocs sigMap)
168176
177+ chunksOf :: Int -> [a ] -> [[a ]]
178+ chunksOf n = takeWhile (not . null ) . L. unfoldr (Just . splitAt n)
179+
180+ justifyRight :: Int -> a -> [a ] -> [a ]
181+ justifyRight n c xs = reverse . take (max n (length xs)) . (++ repeat c) . reverse $ xs
182+
169183printFunctions :: API -> Registry -> M. Map String String -> IO ()
170184printFunctions api registry sigMap = do
171185 let comment =
172186 [" All raw functions from the" ,
173187 " <http://www.opengl.org/registry/ OpenGL registry>." ]
188+ cmds = chunksOf 100 . M. toAscList . commands $ registry
189+ mnames = [ [ " Functions" , " F" ++ justifyRight 2 ' 0' (show i) ] |
190+ i <- [ 1 .. length cmds ] ]
174191 startModule [" Functions" ] Nothing comment $ \ moduleName h -> do
175192 SI. hPutStrLn h $ " module " ++ moduleName ++ " ("
176- SI. hPutStrLn h . separate unCommandName . M. keys . commands $ registry
193+ SI. hPutStrLn h . separate (( " module " ++ ) . moduleNameFor) $ mnames
177194 SI. hPutStrLn h " ) where"
178195 SI. hPutStrLn h " "
179- SI. hPutStrLn h " import Control.Monad.IO.Class ( MonadIO(..) )"
180- SI. hPutStrLn h " import Foreign.Marshal.Error ( throwIf )"
181- SI. hPutStrLn h " import Foreign.Ptr ( Ptr, FunPtr, nullFunPtr )"
182- SI. hPutStrLn h " import System.IO.Unsafe ( unsafePerformIO )"
196+ mapM_ (SI. hPutStrLn h . (" import " ++ ) . moduleNameFor) mnames
197+ CM. zipWithM_ (printSubFunctions api registry sigMap) mnames cmds
198+
199+ printSubFunctions :: API -> Registry -> M. Map String String ->
200+ [String ] -> [(CommandName , Command )] -> IO ()
201+ printSubFunctions api registry sigMap mname cmds = do
202+ let comment =
203+ [" Raw functions from the" ,
204+ " <http://www.opengl.org/registry/ OpenGL registry>." ]
205+ startModule mname (Just " {-# OPTIONS_HADDOCK hide #-}" ) comment $ \ moduleName h -> do
206+ SI. hPutStrLn h $ " module " ++ moduleName ++ " ("
207+ SI. hPutStrLn h . separate unCommandName . map fst $ cmds
208+ SI. hPutStrLn h " ) where"
183209 SI. hPutStrLn h " "
210+ SI. hPutStrLn h " import Control.Monad.IO.Class ( MonadIO(..) )"
211+ SI. hPutStrLn h " import Foreign.Ptr"
184212 SI. hPutStrLn h $ " import " ++ moduleNameFor [" Foreign" ]
185- SI. hPutStrLn h $ " import " ++ moduleNameFor [" GetProcAddress" ] ++ " ( getProcAddress )"
186213 SI. hPutStrLn h $ " import " ++ moduleNameFor [" Types" ]
214+ SI. hPutStrLn h " import System.IO.Unsafe ( unsafePerformIO )"
187215 SI. hPutStrLn h " "
188- SI. hPutStrLn h " getCommand :: String -> IO (FunPtr a)"
189- SI. hPutStrLn h " getCommand cmd ="
190- SI. hPutStrLn h " throwIfNullFunPtr (\" unknown OpenGL command \" ++ cmd) $ getProcAddress cmd"
191- SI. hPutStrLn h " "
192- SI. hPutStrLn h " throwIfNullFunPtr :: String -> IO (FunPtr a) -> IO (FunPtr a)"
193- SI. hPutStrLn h " throwIfNullFunPtr = throwIf (== nullFunPtr) . const"
194- SI. hPutStrLn h " "
195- mapM_ (SI. hPutStrLn h . showCommand api registry sigMap) (M. elems (commands registry))
216+ mapM_ (SI. hPutStrLn h . showCommand api registry sigMap . snd ) cmds
196217
197218type ExtensionParts = ([TypeName ], [Enum' ], [Command ])
198219type ExtensionModule = (ExtensionName , ExtensionName , ExtensionParts )
0 commit comments