2013-03-14 08:04:51 +00:00
|
|
|
|
|
|
|
module Main where
|
|
|
|
|
|
|
|
import Ast
|
|
|
|
import Control.Applicative ((<$>), (<*>))
|
2013-03-17 05:24:18 +00:00
|
|
|
import Data.List (intercalate)
|
2013-03-14 08:04:51 +00:00
|
|
|
import Parse.Library
|
|
|
|
import Parse.Modules (moduleDef)
|
|
|
|
import Parse.Types (typeAnnotation)
|
2013-03-17 05:24:18 +00:00
|
|
|
import Text.Parsec hiding (newline,spaces)
|
2013-03-14 08:04:51 +00:00
|
|
|
import System.Environment
|
|
|
|
import System.Exit
|
|
|
|
import Types.Types
|
|
|
|
|
|
|
|
|
|
|
|
main = do
|
|
|
|
srcs <- mapM readFile =<< getArgs
|
|
|
|
case mapM docParse srcs of
|
|
|
|
Left err -> putStrLn err >> exitFailure
|
|
|
|
Right ms -> putStrLn (toModules ms)
|
|
|
|
|
2013-03-21 09:29:23 +00:00
|
|
|
toModules ms = wrap (intercalate ",\n " (map toModule ms))
|
|
|
|
where wrap s = "{ \"modules\" : [\n " ++ s ++ "\n ]\n}"
|
2013-03-14 08:04:51 +00:00
|
|
|
|
|
|
|
toModule (name, values) =
|
2013-03-21 09:29:23 +00:00
|
|
|
"{ \"name\" : " ++ show name ++ ",\n " ++
|
|
|
|
"\"values\" : [\n " ++ vs ++ "\n ]\n }"
|
|
|
|
where vs = intercalate ",\n " (map toValue values)
|
2013-03-14 08:04:51 +00:00
|
|
|
|
2013-03-21 09:29:23 +00:00
|
|
|
toValue (name, tipe, desc) =
|
|
|
|
"{ \"name\" : " ++ show name ++
|
|
|
|
",\n \"type\" : \"" ++ show tipe ++
|
|
|
|
",\n \"desc\" : " ++ show desc ++ "\n }"
|
2013-03-14 08:04:51 +00:00
|
|
|
|
2013-03-21 09:29:23 +00:00
|
|
|
docParse :: String -> Either String (String, [(String, Type, String)])
|
2013-03-14 08:04:51 +00:00
|
|
|
docParse = setupParser $ do
|
|
|
|
optional freshLine
|
|
|
|
(,) <$> option "Main" moduleName <*> types
|
|
|
|
where
|
|
|
|
skip = manyTill anyChar simpleNewline >> return []
|
|
|
|
end = many1 anyChar >> return []
|
2013-03-21 09:29:23 +00:00
|
|
|
types = concat <$> many (docs <|> try skip <|> end)
|
2013-03-14 08:04:51 +00:00
|
|
|
getName = intercalate "." . fst
|
|
|
|
moduleName = do optional freshLine
|
|
|
|
getName <$> moduleDef `followedBy` freshLine
|
|
|
|
|
2013-03-21 09:29:23 +00:00
|
|
|
docs :: IParser [(String, Type, String)]
|
|
|
|
docs = (tipe <$> try typeAnnotation) <|> commentTipe
|
|
|
|
where
|
|
|
|
clip str = case str of { ' ':rest -> rest ; _ -> str }
|
|
|
|
tipe stmt = case stmt of { TypeAnnotation n t -> [(n,t,"")] ; _ -> [] }
|
|
|
|
commentTipe = do
|
|
|
|
cs <- map clip <$> many1 lineComment
|
|
|
|
typ <- optionMaybe (try typeAnnotation)
|
|
|
|
return $ case typ of
|
|
|
|
Just (TypeAnnotation n t) -> [(n, t, intercalate "\n" cs)]
|
|
|
|
_ -> []
|
|
|
|
|
|
|
|
|
2013-03-14 08:04:51 +00:00
|
|
|
setupParser p source =
|
|
|
|
case iParse p "" source of
|
|
|
|
Right result -> Right result
|
|
|
|
Left err -> Left $ "Parse error at " ++ show err
|