on master help sign in
Fix parsing bug and ensure the creation of the docs/ directory
caf2d0aEvan Czaplicki
compiler/Docs.hs+10−5
@@ -2,6 +2,7 @@
2module Main where
2module Main where
3
3
4import System.Console.CmdArgs
4import System.Console.CmdArgs
5+import System.Directory
5import System.FilePath
6import System.FilePath
6import System.Exit
7import System.Exit
7
8
@@ -39,6 +40,7 @@ parseFile path = do
39 case iParse docs "" source of
40 case iParse docs "" source of
40 Right json -> do
41 Right json -> do
41 putStrLn $ "Documenting " ++ path
42 putStrLn $ "Documenting " ++ path
43+ createDirectoryIfMissing True "docs"
42 BS.writeFile ("docs" </> replaceExtension path ".json") (encode json)
44 BS.writeFile ("docs" </> replaceExtension path ".json") (encode json)
43 Left err -> do
45 Left err -> do
44 putStrLn $ "Parse error in " ++ path ++ " at " ++ show err
46 putStrLn $ "Parse error in " ++ path ++ " at " ++ show err
@@ -48,8 +50,8 @@ docs :: IParser Value
48docs = do
50docs = do
49 optional freshLine
51 optional freshLine
50 (names, exports) <- option (["Main"],[]) moduleDef
52 (names, exports) <- option (["Main"],[]) moduleDef
51− chompUntilFreshLine
53+ optional freshLine
52− structure <- docComment
54+ structure <- option "" docComment
53 things <- document
55 things <- document
54 return $ toJson (List.intercalate "." names) exports structure things
56 return $ toJson (List.intercalate "." names) exports structure things
55
57
@@ -80,15 +82,18 @@ chompUntilFreshLine =
80 , eof >> return True ]
82 , eof >> return True ]
81
83
82docThing :: IParser (String, Declaration t v, String)
84docThing :: IParser (String, Declaration t v, String)
83−docThing = uncommented <|> commented
85+docThing = uncommentable <|> commented <|> uncommented ""
84 where
86 where
85− uncommented = do
87+ uncommentable = do
86 ifx <- infixDecl
88 ifx <- infixDecl
87 return ("", ifx, "")
89 return ("", ifx, "")
88
90
89 commented = do
91 commented = do
90− comment <- option "" docComment
92+ comment <- docComment
91 freshLine
93 freshLine
94+ uncommented comment
95+
96+ uncommented comment = do
92 (src,def) <- withSource $ choice [ alias, datatype, Definition <$> typeAnnotation ]
97 (src,def) <- withSource $ choice [ alias, datatype, Definition <$> typeAnnotation ]
93 return (comment, def, src)
98 return (comment, def, src)
94
99