@@ -4,13 +4,12 @@ module Main where
5import Control.Applicative ((<$>), (<*>))
5import Control.Applicative ((<$>), (<*>))
6import Data.List (intercalate)
6import Data.List (intercalate)
7+import Data.Maybe (catMaybes)
7import Parse.Library
8import Parse.Library
8import Parse.Modules (moduleDef)
9import Parse.Modules (moduleDef)
9−import Parse.Types (typeAnnotation)
10import Text.Parsec hiding (newline,spaces)
10import Text.Parsec hiding (newline,spaces)
11import System.Environment
11import System.Environment
12import System.Exit
12import System.Exit
@@ -29,32 +28,34 @@ toModule (name, values) =
30toValue (name, tipe, desc) =
29toValue (name, tipe, desc) =
31 "{ \"name\" : " ++ show name ++
30 "{ \"name\" : " ++ show name ++
32− ",\n \"type\" : \"" ++ show tipe ++
31+ ",\n \"type\" : " ++ show tipe ++
33− "\",\n \"desc\" : " ++ show desc ++ "\n }"
32+ ",\n \"desc\" : " ++ show desc ++ "\n }"
35−docParse :: String -> Either String (String, [(String, Type, String)])
34+docParse :: String -> Either String (String, [(String, String, String)])
36docParse = setupParser $ do
35docParse = setupParser $ do
37− optional freshLine
36+ optional freshLine
38− (,) <$> option "Main" moduleName <*> types
37+ (names, exports) <- option (["Main"],[]) (moduleDef `followedBy` freshLine)
38+ info <- many (docs exports <|> try skip <|> end)
39+ return (intercalate "." names, catMaybes info)
40− skip = manyTill anyChar simpleNewline >> return []
41+ skip = manyTill anyChar simpleNewline >> return Nothing
41− end = many1 anyChar >> return []
42+ end = many1 anyChar >> return Nothing
42− types = concat <$> many (docs <|> try skip <|> end)
43+
43− getName = intercalate "." . fst
44− moduleName = do optional freshLine
45− getName <$> moduleDef `followedBy` freshLine
47−docs :: IParser [(String, Type, String)]
44+docs :: [String] -> IParser (Maybe (String, String, String))
48−docs = (tipe <$> try typeAnnotation) <|> commentTipe
45+docs exports = export <$> try annotation
50− clip str = case str of { ' ':rest -> rest ; _ -> str }
47+ tipe comment = tuple <$> name <*> tipe
51− tipe stmt = case stmt of { TypeAnnotation n t -> [(n,t,"")] ; _ -> [] }
48+ where name = do v <- lowVar <|> parens symOp
52− commentTipe = do
49+ whitespace ; hasType ; whitespace ; return v
50+ tipe = manyTill anyChar (try (freshLine >> notFollowedBy (string " ")))
51+ tuple n t = (n,t,comment)
53− cs <- map clip <$> many1 lineComment
53+ annotation = tipe =<< option "" (concatMap clip <$> many1 lineComment)
54− typ <- optionMaybe (try typeAnnotation)
54+ where clip str = case str of { ' ':rest -> rest ; _ -> str } ++ "\n"
55− return $ case typ of
55+
56− Just (TypeAnnotation n t) -> [(n, t, intercalate "\n" cs)]
56+ export info@(name,_,_) =
57− _ -> []
57+ if null exports || name `elem` exports then Just info else Nothing
60setupParser p source =
61setupParser p source =